Skip to content

Instantly share code, notes, and snippets.

@maprangzth
Last active November 2, 2022 14:58
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maprangzth/73a1e9a38f2470ba9af9cf2f4212dfa5 to your computer and use it in GitHub Desktop.
Save maprangzth/73a1e9a38f2470ba9af9cf2f4212dfa5 to your computer and use it in GitHub Desktop.
VIP for Elasticsearch and Logstash with Keepalived

VIP for Elasticsearch and Logstash with Keepalived

Step 1: Install keepalived

# yum -y install keepalived
# chkconfig keepalived on

Step 2: Configure Keepalived

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

Step 3: Notify Script

  • 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

Step 4: Allowing VIP

# echo "net.ipv4.ip_nonlocal_bind=1" >> /etc/sysctl.conf
# sysctl -p  

ref: https://everythingshouldbevirtual.com/highly-available-elk-elasticsearch-logstash-kibana-setup/

Step 5: Start Keepalived service

# service keepalived start
@marcohald
Copy link

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment