ITPub博客

首页 > 数据库 > 数据库开发技术 > prometheus operator监控k8s集群之外的haproxy组件

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

原创 数据库开发技术 作者:DevOps和k8s全栈技术 时间:2020-07-17 07:00:00 0 删除 编辑

微信公众号搜索DevOps和k8s全栈技术 ,即可关注我的公众号,也可通过扫描文章最后的二维码关注,每天都会分享技术文章供大家参考阅读~,拥抱开源,同大家共同进步~

prometheus operator连载文章

Prometheus Operator-上篇-安装和使用篇

Prometheus Operator-下篇-通过编写operator实现监控告警等

Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档

Prometheus监控Tomcat

Prometheus监控Nginx

Prometheus监控MySQL

Prometheus监控Redis

报警神器Alertmanager发送报警到多个渠道

安装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/haproxyservice  haproxy start
查看haproxy是否启动成功:
ss -antulp | grep :5000
显示如下,说明启动成功:
tcp    LISTEN     0      128       *:5000                  *:*                   users:(("haproxy",pid=61613,fd=3),("haproxy",pid=61612,fd=3),("haproxy",pid=61611,fd=3),("haproxy",pid=61610,fd=3))


安装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 summarygo_gc_duration_seconds{quantile="0"} 0.00011131go_gc_duration_seconds{quantile="0.25"} 0.000411065go_gc_duration_seconds{quantile="0.5"} 0.000803894go_gc_duration_seconds{quantile="0.75"} 0.002416707go_gc_duration_seconds{quantile="1"} 0.031029562go_gc_duration_seconds_sum 0.104676686go_gc_duration_seconds_count 19# HELP go_goroutines Number of goroutines that currently exist.# TYPE go_goroutines gaugego_goroutines 9# HELP go_memstats_alloc_bytes Number of bytes allocated and stillin use.# TYPE go_memstats_alloc_bytes gaugego_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 countergo_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 gaugego_memstats_buck_hash_sys_bytes 725494# HELP go_memstats_frees_total Total number of frees.# TYPE go_memstats_frees_total countergo_memstats_frees_total 81339# HELP go_memstats_gc_sys_bytes Number of bytes used for garbagecollection system metadata.# TYPE go_memstats_gc_sys_bytes gaugego_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 gaugego_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 gaugego_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: ServiceapiVersion: v1metadata: name: haproxy labels:   app: haproxyspec: type: ClusterIP ports: - name: haproxy   port: 9100   targetPort: 9100---kind: EndpointsapiVersion: v1metadata: name: haproxy labels:   app: haproxysubsets: - addresses:     - ip: 192.168.0.6   ports:     - name: haproxy       port: 9100---apiVersion: monitoring.coreos.com/v1kind: ServiceMonitormetadata:  name: haproxy  namespace: monitoring  labels:    team: frontendspec:  namespaceSelector:    any: true    matchNames:    - default  selector:    matchLabels:      app: haproxy  endpoints:  - port: haproxytargetPort: 9100

通过kubectl apply更新yaml:

kubectl apply  -f prometheus-servicemonitor-haproxy.yaml

 

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




往期精彩文章

kubernetes全栈技术+企业案例演示【带你快速掌握和使用k8s】

kubernetes面试题汇总

DevOps视频和资料免费领取

kubernetes技术分享-可用于企业内部培训

谈谈我的IT发展之路

kubernetes系列文章第一篇-k8s基本介绍

kubernetes系列文章第二篇-kubectl

了解pod和pod的生命周期-这一篇文章就够了

Kubernetes中部署MySQL高可用集群

Prometheus+Grafana+Alertmanager搭建全方位的监控告警系统-超详细文档

k8s1.18多master节点高可用集群安装-超详细中文官方文档

k8s中蓝绿部署、金丝雀发布、滚动更新汇总

运维常见问题汇总-tomcat篇

关于linux内核参数的调优,你需要知道

kubernetes持久化存储volume

kubernetes挂载ceph rbd和cephfs

报警神器Alertmanager发送报警到多个渠道

jenkins+kubernetes+harbor+gitlab构建企业级devops平台

kubernetes网络插件-flannel篇

kubernetes网络插件-calico篇

kubernetes认证、授权、准入控制

限制不同的用户操作k8s资源

面试真题&技术资料免费领取-覆盖面超全~

Prometheus监控MySQL

Prometheus监控Nginx

Prometheus监控Tomcat

linux面试题汇总

测试通过storageclass动态生成pv

通过编写k8s的资源清单yaml文件部署gitlab服务

helm安装和使用-通过helm部署k8s应用

Prometheus Operator-上篇-安装和使用篇

Prometheus Operator-下篇

通过kubeconfig登陆k8s的dashboard ui界面

通过token令牌登陆k8s dashboard ui界面          

技术交流群

学无止境,了解更多关于kubernetes/docker/devops/openstack/openshift/linux/IaaS/PaaS相关内容,想要获取更多资料和免费视频,可按如下方式进入技术交流群

                               扫码加群👇


微信:luckylucky421302

微信公众号

                                     长按指纹关注公众号👇


                                       

                                       点击在看少个 bug👇

原文作者:DevOps和k8s全栈技术;如有侵权,请联系管理员删除。

请登录后发表评论 登录
全部评论
云计算架构师、阿里云开发者社区专家博主、腾讯云架构师北京同盟成员、Linux基金会开源软件学园2023、2024年度优秀讲师,在中国计算机学会(CCF)任职人工智能与模式识别委员和计算机应用委员、中国科普作家协会会员,清华大学出版社出版的畅销书《Kubernetes从入门到DevOps企业应用实战》作者。

注册时间:2017-10-15

  • 博文量
    127
  • 访问量
    61687