安装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数据,显示如下,说明配置正常: