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
                  相关文章
                  |
                  Kubernetes API 调度
                  Kubernetes 架构解析:理解其核心组件
                  【8月更文第29天】Kubernetes(简称 K8s)是一个开源的容器编排系统,用于自动化部署、扩展和管理容器化应用。它提供了一个可移植、可扩展的环境来运行分布式系统。本文将深入探讨 Kubernetes 的架构设计,包括其核心组件如何协同工作以实现这些功能。
                  1214 3
                  |
                  Prometheus Kubernetes 监控
                  Kubernetes监控:Prometheus与AlertManager结合,配置邮件告警。
                  完成这些步骤之后,您就拥有了一个可以用邮件通知你的Kubernetes监控解决方案了。当然,所有的这些配置都需要相互照应,还要对你的Kubernetes集群状况有深入的了解。希望这份指南能帮助你创建出适合自己场景的监控系统,让你在首次发现问题时就能做出响应。
                  824 22
                  |
                  存储 运维 Kubernetes
                  正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
                  飞轮科技推出了 Doris 的 Kubernetes Operator 开源项目(简称:Doris Operator),并捐赠给 Apache 基金会。该工具集成了原生 Kubernetes 资源的复杂管理能力,并融合了 Doris 组件间的分布式协同、用户集群形态的按需定制等经验,为用户提供了一个更简洁、高效、易用的容器化部署方案。
                  1042 16
                  正式开源,Doris Operator 支持高效 Kubernetes 容器化部署方案
                  |
                  Prometheus Kubernetes 监控
                  k8s部署针对外部服务器的prometheus服务
                  通过上述步骤,您不仅成功地在Kubernetes集群内部署了Prometheus,还实现了对集群外服务器的有效监控。理解并实施网络配置是关键,确保监控数据的准确无误传输。随着监控需求的增长,您还可以进一步探索Prometheus生态中的其他组件,如Alertmanager、Grafana等,以构建完整的监控与报警体系。
                  965 62
                  |
                  Prometheus Kubernetes 监控
                  k8s部署针对外部服务器的prometheus服务
                  通过上述步骤,您不仅成功地在Kubernetes集群内部署了Prometheus,还实现了对集群外服务器的有效监控。理解并实施网络配置是关键,确保监控数据的准确无误传输。随着监控需求的增长,您还可以进一步探索Prometheus生态中的其他组件,如Alertmanager、Grafana等,以构建完整的监控与报警体系。
                  565 60
                  |
                  Kubernetes 容器
                  Kubernetes附加组件Dashboard部署实战篇
                  关于如何在Kubernetes集群中部署和配置Dashboard组件的详细实战指南,涵盖了从创建证书、部署Dashboard、设置服务访问到登录认证的完整流程。
                  1786 0
                  Kubernetes附加组件Dashboard部署实战篇
                  |
                  Kubernetes 调度 容器
                  k8s descheduler 组件安装
                  k8s descheduler 组件安装
                  |
                  存储 Kubernetes API
                  在K8S中,Kubernetes的组件有哪些?
                  在K8S中,Kubernetes的组件有哪些?
                  |
                  运维 Kubernetes 监控
                  Loki+Promtail+Grafana监控K8s日志
                  综上,Loki+Promtail+Grafana 监控组合对于在 K8s 环境中优化日志管理至关重要,它不仅提供了强大且易于扩展的日志收集与汇总工具,还有可视化这些日志的能力。通过有效地使用这套工具,可以显著地提高对应用的运维监控能力和故障诊断效率。
                  2435 0
                  |
                  存储 Kubernetes API
                  在K8S中,各组件是如何实现高可用的?
                  在K8S中,各组件是如何实现高可用的?

                  推荐镜像

                  更多