haproxy日志配置、Proxies配置以及调度算法

一、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第一台 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值