今天给伙伴们分享一下Haproxy 配置实战,希望看了有所收获。
我是公众号「想吃西红柿」「云原生运维实战派」作者,对云原生运维感兴趣,也保持时刻学习,后续会分享工作中用到的运维技术,在运维的路上得到支持和共同进步!
如果伙伴们看了文档觉得有用,欢迎大家关注我的公众号,获取相关文档。爱运维,爱生活。
一、Haproxy 实现四层负载
1、Haproxy L4 配置
[root@haproxy-lb1 ~]# /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
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:1314
stats enable
stats hide-version
stats uri /haproxystats
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
#option redispatch
mode http
bind *:80
default_backend httpservers
backend httpservers
balance roundrobin
server http1 192.168.152.10:80 check maxconn 2000
server http2 192.168.152.20:80 check maxconn 2000
server http3 192.168.152.30:80 check maxconn 2000
listen mysql
bind *:3306
mode tcp
balance roundrobin
server mysql1 192.168.152.40:3306 weight 1 check inter 1s rise 2 fall 2
server mysql2 192.168.152.50:3306 weight 1 check inter 1s rise 2 fall 2
二、Haproxy + Keepalived 高可用负载均衡
1、拓扑结构
2、环境配置(集群中所有主机)
- 配置 :IP, hostname, hosts, firewalld, SELinux, ntp
[root@haproxy-lb1 ~]# cat /etc/hosts
192.168.152.150 haproxy-lb1.edenluo.com active
192.168.152.151 haproxy-lb2.edenluo.com backup
192.168.152.156 web1.edenluo.com
192.168.152.157 web2.edenluo.com
3、RS 配置
- 配置好网站服务器,测试所有RS
4、配置 Haproxy(主/备)
[root@haproxy-lb1 ~]# yum -y install haproxy
[root@haproxy-lb1 ~]# cp -rf /etc/haproxy/haproxy.cfg{,.bak}
[root@haproxy-lb1 ~]# sed -i -r '/^[ ]*#/d;/^$/d' /etc/haproxy/haproxy.cfg
[root@haproxy-lb1 ~]# vim /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
defaults
mode http
log global
option dontlognull
retries 3
maxconn 3000
contimeout 50000
clitimeout 50000
srvtimeout 50000
listen stats
bind *:1314
stats enable
stats hide-version
stats uri /haproxystats
stats realm Haproxy\ stats
stats auth admin:admin
stats admin if TRUE
frontend web
option httplog
option http-server-close
option forwardfor except 127.0.0.0/8
#option redispatch
mode http
bind *:80
default_backend httpservers
backend httpservers
balance roundrobin
server http1 192.168.152.156:80 check maxconn 2000
server http2 192.168.152.157:80 check maxconn 2000
[root@haproxy-lb1 ~]# systemctl enable haproxy.service --now
5、测试调度器(主/备)
[root@haproxy-lb1 ~]# curl 192.168.152.150
web1
[root@haproxy-lb1 ~]# curl 192.168.152.150
web2
[root@haproxy-lb1 ~]# curl 192.168.152.150
web1
[root@haproxy-lb1 ~]# curl 192.168.152.150
web2
[root@haproxy-lb2 ~]# curl 192.168.152.151
web1
[root@haproxy-lb2 ~]# curl 192.168.152.151
web2
[root@haproxy-lb2 ~]# curl 192.168.152.151
web1
[root@haproxy-lb2 ~]# curl 192.168.152.151
web2
6、Keepalived 实现 HAproxy HA
注:主/备调度器均能够实现正常调度
1、主/备调度器安装软件
[root@haproxy-lb1 ~]# yum -y install keepalived
2、Keepalived 配置
1、Master
[root@haproxy-lb1 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File