一、VIP电脑haproxy日志配置
1、配置listen stats
vim /etc/haproxy/haproxy.cfg
listen stats
mode http
bind :stats enable
log global
stats uri /haproxy-status
stats auth haadmin:123456
2、重启haproxy
systemctl restart haproxy
3、网页通过绑定的9999端口访问
4、配置haproxy.cfg 添加listen app1
listen app1
# this is the address and port we'll listen to, the ones to aim the
# load generators at
bind :80
log global
# create a certificate and uncomment this for SSL
# bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
# Put the server's IP address and port below
server web1 192.168.157.200:80
server web2 192.168.157.201:80
5、配置日志rsyslog
vim /etc/rsyslog.conf
配置第75、76行可参考72、73行
75 # Save haproxy messages also to haproxy.log
76 local2.* /var/log/haproxy.log
6、重启haproxy,rsyslog
systemctl restart haproxy.service rsyslog
此时如果第四台电脑curl第一台电脑就会出现haproxy.log
二、Haproxy Proxies配置
1、配置haproxy.cfg的前端和后端
frontend web1
bind :88
log global
mode tcp
use_backend webserver1
backend webserver1
mode tcp
default-server inter 1000 weight 3
server app1 192.168.157.200:80 check
server app2 192.168.157.201:80 check
此时第四台电脑curl 192.168.157.10
下面将web2的http关掉假设宕机
2、创建子配置文件保存配置
创建子配置文件app1.cfg 并且将此部分内容从haproxy.cfg中删除
创建目录
mkdir /etc/haproxy/ conf.d
切到此目录下
cd /etc/haproxy/ conf.d
编辑app1.cfg
vim /etc/haproxy/conf.d/app1.cfg
listen app1
# this is the address and port we'll listen to, the ones to aim the
# load generators at
bind :80
log global
# create a certificate and uncomment this for SSL
# bind :8443 ssl crt my-cert.pem alpn h2,http/1.1
# Put the server's IP address and port below
server web1 192.168.157.200:80 check
server web2 192.168.157.201:80 check
3、配置haproxy.service并重启
cd /usr/lib/systemd/system
vim haproxy.service
[Unit]
Description=HAProxy Load Balancer
After=syslog.target network.target
[Service]
ExecStartPre=/usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d -c -q
ExecStart=/usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -f /etc/haproxy/conf.d -p /usr/local/haproxy/haproxy.pid
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
重新启动haproxy
systemctl restart haproxy
此时第四台虚拟机curl 第一台成功
三、HAProxy调度算法
1、 socat
yum install socat -y
此时第四台电脑curl第一台
2、static-rr(静态)
修改haproxy.cfg设置权重
此时第四台curl第一台
3、random(动态)
此时第四台curl第一台