LVS + Keepalived + Nginx基于DR模式构建高可用方案
- 2019 年 10 月 3 日
- 筆記
???????????????????????????????????????????????????????????????????????????????????????????LVS???????????Keepalived?????????LVS?DR????Nginx???
1?????
?????????????
?? | ?? |
---|---|
Centos?? | Linux release 7.3.1611 (Core) |
Nginx | 1.16.0 |
LVS | ipvsadm-1.27-7.el7.x86_64 |
Keepalived | keepalived.x86_64 0:1.3.5-8.el7_6 |
??????????
?? | ?? |
---|---|
192.168.208.154 | lvs master |
192.168.208.155 | lvs slave |
192.168.208.150 | nginx1 |
192.168.208.151 | nginx2 |
???????????VIP????192.168.208.100?VIP?????IP?????????????IP???
2?????
????????????????
?????
?????DR?????????????VIP??LVS??????????????????????Nginx????Real Server????????Nginx?????????????????????????Nginx??????????IP???lo?????VIP????
3?????
?1?????lvs master???slave????lvs?keepalived?
yum install ipvsadm yum install keepalived
?2???nginx1?nginx2?????nginx?
# ??nginx?yum? rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm # ?? yum install nginx
????nginx???????????????????????????????nginx??????
vi /usr/share/nginx/html/index.html
??nginx1??150???nginx2??151?????
?????nginx1??????
?????nginx2??????
?3???lvs master???lvs slave????keepalived???
????lvs master???
???????
vi /etc/keepalived/keepalived.conf
????
! Configuration File for keepalived global_defs { # ??????????????????? # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 # router_id?keepalived???????????keepalived?????? router_id LVS_DEVEL # vrrp_skip_check_adv_addr # vrrp_strict # vrrp_garp_interval 0 # vrrp_gna_interval 0 } vrrp_instance VI_1 { # MASTER????????????BACKUP state MASTER # ???????????????????? interface ens33 # ?????MASTER?BACKUP?????????? virtual_router_id 51 # ????MASTER????????BACKUP?? priority 100 # MASTER?BACKUP???????????? advert_int 1 # lvs?????IP mcast_src_ip=192.168.208.154 authentication { auth_type PASS auth_pass 1111 } # ??IP?? virtual_ipaddress { 192.168.208.100 } } virtual_server 192.168.208.100 80 { # ???????????? delay_loop 6 # ????????????rr?????? lb_algo rr # ??DR?? lb_kind DR # ????????? nat_mask 255.255.255.0 # ??????????? persistence_timeout 50 protocol TCP # ????????? real_server 192.168.208.150 80 { # ????? weight 1 TCP_CHECK { # ???? connect_timeout 3 # ???? nb_get_retry 3 # ???? delay_before_retry 3 } } real_server 192.168.208.151 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
??????????lvs slave??????
! Configuration File for keepalived global_defs { # notification_email { # acassen@firewall.loc # failover@firewall.loc # sysadmin@firewall.loc # } # notification_email_from Alexandre.Cassen@firewall.loc # smtp_server 192.168.200.1 # smtp_connect_timeout 30 router_id LVS_DEVEL_SLAVE # vrrp_skip_check_adv_addr # vrrp_strict # vrrp_garp_interval 0 # vrrp_gna_interval 0 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 51 priority 99 advert_int 1 mcast_src_ip=192.168.208.155 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.208.100 } } virtual_server 192.168.208.100 80 { delay_loop 6 lb_algo rr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.208.150 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } real_server 192.168.208.151 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
????lvs master?slave?keepalived????????????
systemctl start keepalived systemctl enable keepalived
???lvs master????IP?????
ip a
????
????VIP?master????ens33????????
?lvs master???????????
ipvsadm -Ln
????
?????????????
?4????lvs master?slave????????
systemctl stop firewalld systemctl disable firewalld
?5???nginx????????IP?
?????????????IP????????????????lvs-rs.sh???????
#!/bin/bash echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -w net.ipv4.ip_forward=1 ifconfig lo:0 192.168.208.100 broadcast 192.168.208.100 netmask 255.255.255.255 up route add -host 192.168.208.100 dev lo:0
??????IP??????lo?????VIP??????
4???
????Chrome?IE????????http://192.168.208.100??????
????????????
?????lvs master??????????lvs slave???IP?????
ip a
????
??VIP????lvs slave?????
ipvsadm -Ln
????
??lvs slave?????????????
???
????????????
??????