Haproxy高级功能(基于socat管理haproxy)

目录

  • 1、安装socat
  • 2、修改haproxy配置文件并重启haproxy
  • 3、实现主机动态下上线

1、安装socat

yum install -y socat

2、修改haproxy配置文件并重启haproxy

#修改配置文件
vim /etc/haproxy/haproxy.cfg
global
    #turn on stats unix socket
    stats socket /var/lib/haproxy/stats mode 600 level admin

systemctl restart haproxy

3、实现主机动态下上线

/etc/haproxy/haproxy.cfg配置

[root@haproxy026 ~]# cat /etc/haproxy/haproxy.cfg
global
    log         127.0.0.1 local2

    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     4000
    user        haproxy
    group       haproxy
    daemon

    stats socket /var/lib/haproxy/stats mode 600 level admin

defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    maxconn                 3000

listen stats
    mode http
    bind *:8404
    stats enable
    stats uri /stats
    stats refresh 5s
    stats realm Welcome login
    stats admin if LOCALHOST
    stats auth admin:Qwe@1245PaaS


frontend sftp
    bind *:3333
    mode tcp
    default_backend sftp-server

backend sftp-server
    mode tcp
    balance roundrobin

    server sftpserver1 192.168.192.28:2222 check inter 2000 rise 2 fall 3 weight 1 
    server sftpserver2 192.168.192.38:2222 check inter 2000 rise 2 fall 3 weight 1 backup

# 获取backend值是sftp-server的服务信息
[root@haproxy026 ~]# echo "show stat" | socat stdio /var/lib/haproxy/stats | grep sftp-server | cut -d',' -f1,2,3,18
sftp-server,sftpserver1,0,UP
sftp-server,sftpserver2,0,UP
sftp-server,BACKEND,0,UP
[root@haproxy026 ~]# 


#动态下线主机
echo "disable server sftp-server/sftpserver1" | socat stdio /var/lib/haproxy/stats 

#动态上线主机
echo "enable server sftp-server/sftpserver1" | socat stdio /var/lib/haproxy/stats

你可能感兴趣的:(linux,socat,haproxy)