prometheus operator监控k8s集群之外的haproxy组件

简介: prometheus operator监控k8s集群之外的haproxy组件

安装haproxy

在k8s的master1节点安装haproxy,k8s的master1节点的机器ip是192.168.0.6,haproxy1.8.6版本压缩包所在的百度网盘地址如下:

链接:https://pan.baidu.com/s/1glUeOwnvZrdnETr4KayNjg
提取码:i3rn

把压缩包上传到自己的k8s master1节点,手动解压,然后编译安装:


tar zxvf haproxy-1.8.6.tar.gz

cd haproxy-1.8.6

make TARGET=linux31

make install PREFIX=/usr/local/haproxy

mkdir /usr/local/haproxy/conf

cp  examples/option-http_proxy.cfg  /usr/local/haproxy/conf/haproxy.cfg


修改/usr/local/haproxy/conf/haproxy.cfg文件里的监听端口,把bind后面的值改成*:5000,这个是haproxy的监听端口,大家可以随意改,符合自己的环境即可:

cat /usr/local/haproxy/conf/haproxy.cfg


修改haproxy启动脚本文件:

在/etc/init.d/新建一个haproxy文件,haproxy文件所在百度网盘地址如下,

链接:https://pan.baidu.com/s/1zFxHftdAHob-Rtyizk10Xg
提取码:u2lz

可从百度网盘下载,然后拷贝到自己haproxy机器的/etc/init.d/目录下:

 

具体内容也可以参考如下,建议直接从百度王盼盼下载,这样不会出现乱码问题:

cat  /etc/init.d/haproxy
#!/bin/sh
# chkconfig 2345 on
# description: HAProxy is a TCP/HTTP reverse proxy which is particularly suited for high availability environments.
if [ -f /etc/init.d/functions ]; then
 . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
 . /etc/rc.d/init.d/functions
else
 exit 0
fi
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
config="/usr/local/haproxy/conf/haproxy.cfg"
exec="/usr/local/haproxy/sbin/haproxy"
PID="/var/run/haproxy.pid"
[ -f $config ] || exit 1
RETVAL=0
start() {
  daemon $exec -c -q -f $config
    if [ $? -ne 0 ]; then
        echo "Errors found in configuration file."
        return 1
    fi
  echo -n "Starting HAproxy: "
  $exec -D -f $config -p $PID
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch /var/lock/subsys/haproxy
  return $RETVAL
}
stop() {
 echo -n "Shutting down HAproxy: "
 killproc haproxy -USR1
 RETVAL=$?
 echo
 [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/haproxy
 [ $RETVAL -eq 0 ] && rm -f $PID
 return $RETVAL
}
restart() {
 $exec -c -q -f $config
   if [ $? -ne 0 ]; then
       echo "Errors found in configuration file, check it with 'haproxy check'."
       return 1
   fi
 stop
 start
}
rhstatus() {
 status haproxy
}
# See how we were called.
case "$1" in
 start)
        start
        ;;
 stop)
        stop
        ;;
 restart)
        restart
        ;;
 status)
        rhstatus
        ;;
 *)
        echo $"Usage: haproxy {start|stop|restart|status}"
        RETVAL=1
        esac
        exit $RETVAL


启动haproxy:

    chmod  +x  /etc/init.d/haproxy
    service  haproxy start

    查看haproxy是否启动成功:

      ss -antulp | grep :5000

      显示如下,说明启动成功:

        tcp    LISTEN     0      128       *:5000                  *:*                   u

        安装haproxy_exporter

        haproxy_exporter需要的压缩包是:


          haproxy_exporter-0.9.0.linux-386.tar.gz

          压缩包所在的百度网盘地址如下:

            链接:https://pan.baidu.com/s/1zFxHftdAHob-Rtyizk10Xg
            提取码:u2lz

            可从百度网盘把压缩包下载到电脑,然后上传到k8s的master1节点进行解压安装:

            tar  zxvf  haproxy_exporter-0.9.0.linux-386.tar.gz

            cd   haproxy_exporter-0.9.0.linux-386

            cp haproxy_exporter  /usr/bin

             

            启动haproxy_exporter:

            haproxy_exporter --haproxy.scrape-uri="http://192.168.0.6:5000/baz?stats;csv" --web.listen-address="192.168.0.6:9100" &

             

            注意:

            192.168.0.6是部署haproxy和haproxy_exporter所在机器的ip,5000端口是haproxy组件端口,9100是haproxy_exporter端口

             

            验证haproxy_exporter是否启动成功:

            ss -antulp | grep :9100


            显示如下,说明启动成功:

              tcp    LISTEN     0     128    192.168.0.6:9100                  *:*                   users:(("haproxy_exporte",pid=51714,fd=3))


              通过haproxy_exporter采集haproxy组件监控指标:

              curl  192.168.0.6:9100/metrics

              可看到如下监控指标数据:

              # HELP go_gc_duration_seconds A summary of the GC invocationdurations.
              # TYPE go_gc_duration_seconds summary
              go_gc_duration_seconds{quantile="0"} 0.00011131
              go_gc_duration_seconds{quantile="0.25"} 0.000411065
              go_gc_duration_seconds{quantile="0.5"} 0.000803894
              go_gc_duration_seconds{quantile="0.75"} 0.002416707
              go_gc_duration_seconds{quantile="1"} 0.031029562
              go_gc_duration_seconds_sum 0.104676686
              go_gc_duration_seconds_count 19
              # HELP go_goroutines Number of goroutines that currently exist.
              # TYPE go_goroutines gauge
              go_goroutines 9
              # HELP go_memstats_alloc_bytes Number of bytes allocated and stillin use.
              # TYPE go_memstats_alloc_bytes gauge
              go_memstats_alloc_bytes 3.384024e+06
              # HELP go_memstats_alloc_bytes_total Total number of bytesallocated, even if freed.
              # TYPE go_memstats_alloc_bytes_total counter
              go_memstats_alloc_bytes_total 5.452464e+07
              # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by theprofiling bucket hash table.
              # TYPE go_memstats_buck_hash_sys_bytes gauge
              go_memstats_buck_hash_sys_bytes 725494
              # HELP go_memstats_frees_total Total number of frees.
              # TYPE go_memstats_frees_total counter
              go_memstats_frees_total 81339
              # HELP go_memstats_gc_sys_bytes Number of bytes used for garbagecollection system metadata.
              # TYPE go_memstats_gc_sys_bytes gauge
              go_memstats_gc_sys_bytes 2.8383232e+07
              # HELP go_memstats_heap_alloc_bytes Number of heap bytes allocatedand still in use.
              # TYPE go_memstats_heap_alloc_bytes gauge
              go_memstats_heap_alloc_bytes 3.384024e+06
              # HELP go_memstats_heap_idle_bytes Number of heap bytes waiting tobe used.
              # TYPE go_memstats_heap_idle_bytes gauge
              go_memstats_heap_idle_bytes 1.39264e+06
              ---
              ---
              ---
              ---
              ---
              ---

              配置prometheus operator的servicemonitor实现对haproxy的监控

              在k8s的master1节点编写servicemonitor,所需要的yaml文件在百度网盘地址如下:

                链接:https://pan.baidu.com/s/1zFxHftdAHob-Rtyizk10Xg
                提取码:u2lz

                建议直接从百度网盘下载资源清单文件prometheus-servicemonitor-haproxy.yaml,然后传到k8s的master1节点,这样不会出现乱码问题,

                也可参考下面内容:

                cat prometheus-servicemonitor-haproxy.yaml

                kind: Service
                apiVersion: v1
                metadata:
                 name: haproxy
                 labels:
                   app: haproxy
                spec:
                 type: ClusterIP
                 ports:
                 - name: haproxy
                   port: 9100
                   targetPort: 9100
                ---
                kind: Endpoints
                apiVersion: v1
                metadata:
                 name: haproxy
                 labels:
                   app: haproxy
                subsets:
                 - addresses:
                     - ip: 192.168.0.6
                   ports:
                     - name: haproxy
                       port: 9100
                ---
                apiVersion: monitoring.coreos.com/v1
                kind: ServiceMonitor
                metadata:
                  name: haproxy
                  namespace: monitoring
                  labels:
                    team: frontend
                spec:
                  namespaceSelector:
                    any: true
                    matchNames:
                    - default
                  selector:
                    matchLabels:
                      app: haproxy
                  endpoints:
                  - port: haproxy
                targetPort: 9100

                通过kubectl apply更新yaml:

                  kubectl apply  -f prometheus-servicemonitor-haproxy.yaml

                   

                  在prometheus的web界面查看是否监控到了haproxy数据,显示如下,说明配置正常:



                  相关实践学习
                  深入解析Docker容器化技术
                  Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化,容器是完全使用沙箱机制,相互之间不会有任何接口。Docker是世界领先的软件容器平台。开发人员利用Docker可以消除协作编码时“在我的机器上可正常工作”的问题。运维人员利用Docker可以在隔离容器中并行运行和管理应用,获得更好的计算密度。企业利用Docker可以构建敏捷的软件交付管道,以更快的速度、更高的安全性和可靠的信誉为Linux和Windows Server应用发布新功能。 在本套课程中,我们将全面的讲解Docker技术栈,从环境安装到容器、镜像操作以及生产环境如何部署开发的微服务应用。本课程由黑马程序员提供。     相关的阿里云产品:容器服务 ACK 容器服务 Kubernetes 版(简称 ACK)提供高性能可伸缩的容器应用管理能力,支持企业级容器化应用的全生命周期管理。整合阿里云虚拟化、存储、网络和安全能力,打造云端最佳容器化应用运行环境。 了解产品详情: https://www.aliyun.com/product/kubernetes
                  相关文章
                  |
                  6月前
                  |
                  人工智能 算法 调度
                  阿里云ACK托管集群Pro版共享GPU调度操作指南
                  本文介绍在阿里云ACK托管集群Pro版中,如何通过共享GPU调度实现显存与算力的精细化分配,涵盖前提条件、使用限制、节点池配置及任务部署全流程,提升GPU资源利用率,适用于AI训练与推理场景。
                  552 1
                  |
                  6月前
                  |
                  弹性计算 监控 调度
                  ACK One 注册集群云端节点池升级:IDC 集群一键接入云端 GPU 算力,接入效率提升 80%
                  ACK One注册集群节点池实现“一键接入”,免去手动编写脚本与GPU驱动安装,支持自动扩缩容与多场景调度,大幅提升K8s集群管理效率。
                  367 89
                  |
                  11月前
                  |
                  资源调度 Kubernetes 调度
                  从单集群到多集群的快速无损转型:ACK One 多集群应用分发
                  ACK One 的多集群应用分发,可以最小成本地结合您已有的单集群 CD 系统,无需对原先应用资源 YAML 进行修改,即可快速构建成多集群的 CD 系统,并同时获得强大的多集群资源调度和分发的能力。
                  722 9
                  |
                  11月前
                  |
                  资源调度 Kubernetes 调度
                  从单集群到多集群的快速无损转型:ACK One 多集群应用分发
                  本文介绍如何利用阿里云的分布式云容器平台ACK One的多集群应用分发功能,结合云效CD能力,快速将单集群CD系统升级为多集群CD系统。通过增加分发策略(PropagationPolicy)和差异化策略(OverridePolicy),并修改单集群kubeconfig为舰队kubeconfig,可实现无损改造。该方案具备多地域多集群智能资源调度、重调度及故障迁移等能力,帮助用户提升业务效率与可靠性。
                  |
                  存储 Kubernetes 监控
                  K8s集群实战:使用kubeadm和kuboard部署Kubernetes集群
                  总之,使用kubeadm和kuboard部署K8s集群就像回归童年一样,简单又有趣。不要忘记,技术是为人服务的,用K8s集群操控云端资源,我们不过是想在复杂的世界找寻简单。尽管部署过程可能遇到困难,但朝着简化复杂的目标,我们就能找到意义和乐趣。希望你也能利用这些工具,找到你的乐趣,满足你的需求。
                  1094 33
                  |
                  10月前
                  |
                  Prometheus 监控 Cloud Native
                  云原生监控实战:Prometheus+Grafana快速搭建指南
                  云原生监控实战:Prometheus+Grafana快速搭建指南
                  |
                  10月前
                  |
                  存储 Prometheus 监控
                  OSS监控体系搭建:Prometheus+Grafana实时监控流量、错误码、存储量(开源方案替代云监控自定义视图)
                  本方案基于Prometheus构建OSS监控系统,涵盖架构设计、指标采集、可视化、告警及性能优化,助力企业实现高可用、低成本的自建监控体系。
                  938 1
                  |
                  11月前
                  |
                  Prometheus 监控 Cloud Native
                  除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
                  除了Prometheus,还有哪些工具可以监控Docker Swarm集群的资源使用情况?
                  838 79
                  |
                  10月前
                  |
                  存储 监控 Cloud Native
                  云原生监控实战:Prometheus+Grafana打造RDS多维度预警体系
                  本方案构建了基于Prometheus与Thanos的云原生RDS监控体系,涵盖数据采集、存储、可视化与告警全流程。支持10万+QPS采集、90%存储压缩,具备<30秒告警延迟能力。通过自定义指标与智能预警策略,显著提升故障发现效率,实现分钟级响应。
                  682 5
                  |
                  10月前
                  |
                  Prometheus 监控 Cloud Native

                  推荐镜像

                  更多