1 #!/bin/bash 2 3 # client(ens33 nat-nic 172.16.0.129/24) 4 # router(ens33 host-only-nic 10.0.0.57/8, ens37 nat-nic 172.16.0.128/24) 5 # lvs-dr1(ens33:1 10.0.0.77/32, ens33 host-only-nic 10.0.0.91/24, ipvsadm+keepalived) 6 # lvs-dr2(ens33:1 10.0.0.77/32, ens33 host-only-nic 10.0.0.92/24, ipvsadm+keepalived) 7 # lvs-rs1(lo:0 10.0.0.77/32, ens33 host-only-nic 10.0.0.93/24, httpd) 8 # lvs-rs2(lo:0 10.0.0.77/32, ens33 host-only-nic 10.0.0.94/24, httpd) 9 10 ROUTERID=91 11 VROUTERID=77 12 MCASTIP=224.0.0.$VROUTERID 13 MYIP=10.0.0.$ROUTERID 14 VIPADDR=10.0.0.$VROUTERID 15 ISBACKUP=0 16 KSTATE=MASTER 17 #KSTATE=BACKUP 18 KPRIORITY=100 19 #KPRIORITY=80 20 21 setenforce 0 22 sed -i '/^SELINUX=/c SELINUX=disabled/' /etc/selinux/config 23 systemctl disable --now firewalld 24 iptables -F 25 iptables -t nat -F 26 27 28 #echo 1 > /proc/sys/net/ipv4/ip_forward 29 30 ###----keepalived 31 32 if [ $ISBACKUP -eq 1 ]; then 33 KSTATE=BACKUP 34 KPRIORITY=80 35 fi 36 37 38 yum -y install keepalived ipvsadm 39 #apt -y install keepalived ipvsadm 40 41 cp /etc/keepalived/keepalived.conf{,.bak} 42 43 cat > /etc/keepalived/keepalived.conf <<EOF 44 ! Configuration File for keepalived 45 global_defs { 46 router_id ka-lvs-$ROUTERID 47 #vrrp_skip_check_adv_addr 48 #vrrp_garp_interval 0 49 #vrrp_gna_interval 0 50 #vrrp_mcast_group4 $MCASTIP 51 } 52 vrrp_instance VI_1 { 53 state $KSTATE 54 interface ens33 55 virtual_router_id $VROUTERID 56 priority $KPRIORITY 57 authentication { 58 auth_type PASS 59 auth_pass 1111 60 } 61 virtual_ipaddress { 62 $VIPADDR/32 dev ens33 label ens33:1 63 } 64 } 65 66 virtual_server $VIPADDR 80 { 67 delay_loop 3 68 lb_algo rr 69 lb_kind DR 70 persistence_timeout 50 71 protocol TCP 72 real_server 10.0.0.93 80 { 73 weight 1 74 TCP_CHECK { 75 connect_port 80 76 connect_timeout 3 77 retry 3 78 delay_before_retry 3 79 } 80 } 81 real_server 10.0.0.94 80 { 82 weight 1 83 TCP_CHECK { 84 connect_port 80 85 connect_timeout 3 86 retry 3 87 delay_before_retry 3 88 } 89 } 90 } 91 EOF 92 93 #ipvsadm --save > /etc/sysconfig/ipvsadm 94 systemctl enable --now keepalived 95 96 echo $(date +"%F %T")