haproxy-wi/app/scripts/install_keepalived.sh

76 lines
1.8 KiB
Bash
Raw Normal View History

2018-04-27 13:34:11 +00:00
#!/bin/bash
CONF=/etc/keepalived/keepalived.conf
2018-04-28 07:34:45 +00:00
if [ -f $CONF ];then
2018-05-06 19:58:52 +00:00
echo -e 'error: Keepalived already installed. You can edit config <a href="/app/keepalivedconfig.py" title="Edit Keepalived config">here</a><br /><br />'
2018-04-28 07:34:45 +00:00
exit 1
fi
2018-05-06 19:58:52 +00:00
sudo yum install keepalived -y > /dev/null
2018-04-27 13:34:11 +00:00
if [ $? -eq 1 ]
then
2018-05-06 19:58:52 +00:00
echo "error: Can't install keepalived <br /><br />"
exit 1
2018-04-27 13:34:11 +00:00
fi
2018-05-06 19:58:52 +00:00
sudo echo "" > $CONF
2018-04-27 13:34:11 +00:00
2018-05-06 19:58:52 +00:00
sudo bash -c cat << EOF > $CONF
2018-04-27 13:34:11 +00:00
global_defs {
router_id LVS_DEVEL
}
#health-check for keepalive
vrrp_script chk_haproxy { # Requires keepalived-1.1.13
script "pidof haproxy"
interval 2 # check every 2 seconds
weight 3 # addA<64>3 points of prio if OK
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 100
priority 102
#check if we are still running
track_script {
chk_haproxy
}
advert_int 1
authentication {
auth_type PASS
auth_pass VerySecretPass
}
virtual_ipaddress {
0.0.0.0
}
}
EOF
if [ $? -eq 1 ]
then
2018-05-06 19:58:52 +00:00
echo "error: Can't read keepalived config <br /><br />"
2018-04-27 13:34:11 +00:00
exit 1
fi
2018-05-06 19:58:52 +00:00
sudo sed -i "s/MASTER/$1/g" $CONF
sudo sed -i "s/eth0/$2/g" $CONF
sudo sed -i "s/0.0.0.0/$3/g" $CONF
2018-04-27 13:34:11 +00:00
if [[ $1 == "BACKUP" ]];then
2018-05-06 19:58:52 +00:00
sudo sed -i "s/102/103/g" $CONF
2018-04-27 13:34:11 +00:00
fi
2018-05-06 19:58:52 +00:00
sudo systemctl enable keepalived
sudo systemctl restart keepalived
sudo echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sudo sysctl -p
sudo firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
sudo firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --out-interface enp0s8 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
sudo firewall-cmd --reload
2018-04-27 13:34:11 +00:00
if [ $? -eq 1 ]
then
2018-05-06 19:58:52 +00:00
echo "error: Can't start keepalived <br /><br />"
2018-04-27 13:34:11 +00:00
exit 1
2018-05-06 19:58:52 +00:00
fi
echo "success"