#!/bin/bashHOSTNAME=$(HOSTNAME)ETHNAME=ens34ID="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.200MASTER=192.168.204.135SLAVE=192.168.204.136RIP1=192.168.204.137RIP2=192.168.204.139
#!/bin/bash#install keepalived
#/bin/bash#config webyum install -y httpdrm -rf /etc/httpd/conf.d/welcome.conf ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP uproute add -host $VIP dev lo:0echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignoreecho 2 > /proc/sys/net/ipv4/conf/lo/arp_announceecho 1 > /proc/sys/net/ipv4/conf/all/arp_ignoreecho 2 > /proc/sys/net/ipv4/conf/all/arp_announcesystemctl restart httpd
! Configuration File for keepalivedglobal_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.bakmv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bakcp keepalived.conf /etc/keepalived/keepalived.confif [[ "$(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.confelif [[ "$(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.confelse echo "error"fised -i "s/ETHNAME/$ETHNAME/g" /etc/keepalived/keepalived.confsed -i "s/ID/$ID/g" /etc/keepalived/keepalived.confsed -i "s/VIP/$VIP/g" /etc/keepalived/keepalived.confsed -i "s/RIP1/$RIP1/g" /etc/keepalived/keepalived.confsed -i "s/RIP2/$RIP2/g" /etc/keepalived/keepalived.confsystemctl start keepalived