微信公众号搜索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/haproxy
service  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 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数据,显示如下,说明配置正常:

往期精彩文章

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????

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐