. keepalived+haproxy
19.1 架构
主机 | IP | 软件 |
ha01 | 192.168.72.20 VIP 192.168.72.10 | haproxy, keepalived |
ha02 | 192.168.72.21 | haproxy, keepalived |
web1 | 192.168.72.22 | nginx |
19.2 规划
主机 | IP | 软件 |
web2 | 192.168.72.23 | nginx |
web3 | 192.168.72.24 | nginx |
19.3 安装
在主备上安装 haproxy 和 keepalived
dnf install haproxy keepalived -y
在三台web服务器上安装 nginx
dnf install nginx -y
echo "`hostname` - `hostname -I`" > /usr/share/nginx/html/inde.html
systemctl start nginx
19.4 配置proxy
[root@ha-proxy-master ~]# vim /etc/haproxy/haproxy.cfg
global_defs {log 127.0.0.1 local3 infomaxconn 4096uid nobodygid nobodydaemonnbproc 1
defaultslog globalmode httpmaxconn 2048retries 3option redispatchstats uri /haproxystats auth jock:123timeout connect 5000timeout client 50000timeout server 5000
frontend http-inbind 0.0.0.0:80mode httplog globaloption httplogoption httpcloseacl html url_reg -i \.html$use_backend html-server if htmldefault_backend html-server
backend html-servermode httpbalance roundrobinoption httpchk GET /index.htmlcookie SERVERID insert indirect nocacheserver http1 192.168.72.22:80 weight 1 cookie 3 check inter 2000 rise 2 fall2server http2 192.168.72.23:80 weight 1 cookie 4 check inter 2000 rise 2 fall2server http2 192.168.72.24:80 weight 1 cookie 5 check inter 2000 rise 2 fall2
将配置文件拷贝到backup服务器
[root@ha-proxy-master ~]# scp /etc/haproxy/haproxy.cfg
192.168.72.21:/etc/haproxy/
两台机器启动设置开机启动。
[root@ha-proxy-master ~]# systemctl start haproxy
[root@ha-proxy-master ~]# systemctl enable haproxy
19.5 配置keepalived
[root@ha-proxy-master ~]# vim /etc/keepalived/keepalived.conf
# 主
global_defs {router_id host-1
}
vrrp_script check_haproxy {script "killall -0 haproxy"interval 2
}
vrrp_instance HAProxy_1 {state MASTERinterface ens160virtual_router_id 51priority 100advert_int 1authentication {auth_type PASSauth_pass 1111}track_script {check_haproxy
}virtual_ipaddress {192.168.72.10/24 dev ens160}
}
global_defs {router_id host-2
}
vrrp_instance VI_1 {state BACKUPinterface ens160virtual_router_id 51priority 90advert_int 1authentication {auth_type PASSauth_pass 1111
} virtual_ipaddress {192.168.72.10/24 dev ens160
}
}
启动:
[root@ha-proxy-master ~]# systemctl start keepalived
配置完成