#!/bin/bash HOSTNAME=$(HOSTNAME) ETHNAME=ens34 ID="LVS$(ifconfig $ETHNAME |egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | awk -F . 'NR==1{print $4}')" VIP=192.168.204.200 MASTER=192.168.204.135 SLAVE=192.168.204.136 RIP1=192.168.204.137 RIP2=192.168.204.139
#!/bin/bash #install keepalived
#/bin/bash #config web yum install -y httpd rm -rf /etc/httpd/conf.d/welcome.conf ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP up route add -host $VIP dev lo:0 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 systemctl restart httpd
! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id ID } vrrp_instance VI_1 { state STATE interface ETHNAME virtual_router_id 51 priority PRIORITY advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { VIP } } virtual_server VIP 80 { delay_loop 6 lb_algo rr lb_kind DR persistence_timeout 50 protocol TCP real_server RIP1 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 2 } } real_server RIP2 80 { weight 1 TCP_CHECK { connect_timeout 3 nb_get_retry 3 delay_before_retry 2 } } }
rm -rf /etc/keepalived/keepalived.conf.bak mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak cp keepalived.conf /etc/keepalived/keepalived.conf if [[ "$(ifconfig $ETHNAME |egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | awk 'NR==1{print}')" == "$MASTER" ]]; then sed -i 's/STATE/MASTER/g' /etc/keepalived/keepalived.conf sed -i 's/PRIORITY/100/g' /etc/keepalived/keepalived.conf elif [[ "$(ifconfig $ETHNAME |egrep -o "[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}" | awk 'NR==1{print}')" == "$SLAVE" ]]; then sed -i 's/STATE/BACKUP/g' /etc/keepalived/keepalived.conf sed -i 's/PRIORITY/50/g' /etc/keepalived/keepalived.conf else echo "error" fi sed -i "s/ETHNAME/$ETHNAME/g" /etc/keepalived/keepalived.conf sed -i "s/ID/$ID/g" /etc/keepalived/keepalived.conf sed -i "s/VIP/$VIP/g" /etc/keepalived/keepalived.conf sed -i "s/RIP1/$RIP1/g" /etc/keepalived/keepalived.conf sed -i "s/RIP2/$RIP2/g" /etc/keepalived/keepalived.conf systemctl start keepalived