LVS+Nginx+Keepalived高可用集群
准备 四台服务器 192.168.1.105 192.168.1.104 192.168.1.120 VIP 192.168.1.103 LVS Master 192.168.1.102 LVS Backup
基础环境 102 和 103 节点安装 Keepalived,104 和 105 节点安装 Nginx 和 Keepalived,参考:/archives/nginxkeepalived实现双机主备和双主热备
配置 104 和 105 节点 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /etc/sysconfig/network-scripts cp ifcfg-lo ifcfg-lo:1vim ifcfg-lo:1 DEVICE=lo:1 IPADDR=192.168.1.120 NETMASK=255.255.255.255 NETWORK=127.0.0.0 BROADCAST=127.255.255.255 ONBOOT=yes NAME=loopback service network restart
1 2 3 4 5 6 7 8 9 10 11 12 vim /etc/sysctl.conf net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.default.arp_ignore = 1 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.default.arp_announce = 2 net.ipv4.conf.lo.arp_announce = 2 sysctl -p route add -host 192.168.1.120 dev lo:1 echo "route add -host 192.168.1.120 dev lo:1" >> /etc/rc.local
配置 103 节点 安装 ipvsadm 并配置 ip
1 2 3 4 5 6 7 8 9 cd /etc/sysconfig/network-scripts/cp ifcfg-ens33 ifcfg-ens33:1vim ifcfg-ens33:1 BOOTPROTO=static DEVICE=ens33:1 ONBOOT=yes IPADDR=192.168.1.120 NETMASK=255.255.255.0
修改 Keepalived 配置文件后重启 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 global_defs { router_id LVS_102 } vrrp_instance VI_1 { state MASTER interface ens33:1 virtual_router_id 66 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.120 } } virtual_server 192.168.1.120 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 5 protocol TCP real_server 192.168.1.104 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 2 nb_get_retry 2 delay_before_retry 3 } } real_server 192.168.1.105 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 2 nb_get_retry 2 delay_before_retry 3 } } }
配置 102 节点 安装 ipvsadm
修改 Keepalived 配置文件后重启 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 global_defs { router_id LVS_102 } vrrp_instance VI_1 { state BACKUP interface ens33:1 virtual_router_id 66 priority 50 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.120 } } virtual_server 192.168.1.120 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 5 protocol TCP real_server 192.168.1.104 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 2 nb_get_retry 2 delay_before_retry 3 } } real_server 192.168.1.105 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 2 nb_get_retry 2 delay_before_retry 3 } } }