- Author: Komsan Kamsamur <maprangzth@hotmail.com>
- Date: 2018-01-17
- Latest updated: 2018-01-17
# yum -y install keepalived
# chkconfig keepalived on
cat <<'EOF' > /etc/keepalived/keepalived.conf
vrrp_script chk_5142 {
script "</dev/tcp/127.0.0.1/5142"
interval 30 # check every "n" seconds
fall 2 # require 2 failures for KO
rise 2 # require 4 successes for OK
}
vrrp_script check_9200 {
script "</dev/tcp/127.0.0.1/9200"
interval 30 # check every "n" seconds
fall 2 # require 2 failures for KO
rise 2 # require 4 successes for OK
}
vrrp_instance VIP_LS-ES {
state MASTER
interface eth0
dont_track_primary
virtual_router_id 1
priority 153
advert_int 5
mcast_src_ip 192.168.30.1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.30.59/24 dev eth0
}
track_script {
chk_5142
chk_9200
}
notify /etc/keepalived/notify_state
notify_master "/etc/keepalived/vrrp_script MASTER"
notify_backup "/etc/keepalived/vrrp_script BACKUP"
notify_fault "/etc/keepalived/vrrp_script FAULT"
}
EOF
- vrrp_script: if STATE = FAULT, keepalived will stop
cat <<'EOF' > /etc/keepalived/vrrp_script
#!/bin/bash
STATE=$1
NOW=$(date +"%Y-%m-%d %T")
KEEPALIVED="/etc/keepalived"
case $STATE in
"MASTER") touch $KEEPALIVED/MASTER
echo "$NOW Becoming MASTER" >> $KEEPALIVED/COUNTER
exit 0
;;
"BACKUP") rm $KEEPALIVED/MASTER
echo "$NOW Becoming BACKUP" >> $KEEPALIVED/COUNTER
exit 0
;;
"FAULT") rm $KEEPALIVED/MASTER
echo "$NOW Becoming FAULT" >> $KEEPALIVED/COUNTER
/etc/init.d/keepalived stop || killall -0 keepalived
exit 0
;;
*) echo "unknown state"
echo "$NOW Becoming UNKOWN" >> $KEEPALIVED/COUNTER
exit 1
;;
esac
EOF
- notify script when keepalived start
cat <<'EOF' > /etc/keepalived/notify_state
#!/bin/bash
echo $1 $2 is in $3 state > /var/run/keepalived.$1.$2.state
EOF
- script get instance status
cat <<'EOF' > /etc/keepalived/getVIP_state
#!/bin/bash
cat /var/run/keepalived.*.*.state
EOF
# ln -s /etc/keepalived/getVIP_state /usr/local/bin/getVIP_state
# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf
# sysctl -p
ref: https://everythingshouldbevirtual.com/highly-available-elk-elasticsearch-logstash-kibana-setup/
# service keepalived start
@maprangzth can you explain why you stop keepalived when it is in a failed state ?
For me it makes more sense to keep it running, so that keepalived can switch back when the elasticsearch service is running again.