Skip to content

Commit

Permalink
add keepalived dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
ss75710541 committed May 19, 2017
1 parent 04615b0 commit 1acf8ee
Show file tree
Hide file tree
Showing 26 changed files with 511 additions and 0 deletions.
11 changes: 11 additions & 0 deletions dockerfiles/keepalived-base/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM demoregistry.dataman-inc.com/library/centos7-base:latest
MAINTAINER jyliu <[email protected]>

# /usr/sbin/keepalived -f /etc/keepalived/keepalived.conf --dont-fork --log-console


RUN rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7
RUN yum -y update;yum clean all

RUN yum -y install keepalived && yum clean all

1 change: 1 addition & 0 deletions dockerfiles/keepalived-base/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docker build $opts -t shurenyun/centos7-keepalived-1.2.13 /data/tools/keepalived/keepalived-base
13 changes: 13 additions & 0 deletions dockerfiles/keepalived-base/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash
set -x

if [ "x$CONFIG_SERVER" != "x" ];then
export DM_READ_URI=`curl $CONFIG_SERVER/config/$(hostname)/keepalived/filelist.json`
fi

if [ "x$DM_READ_URI" != "x" ];then
/DM_DOCKER_URI.py
fi

/init_network.sh && \
/usr/sbin/keepalived --dont-fork --log-console
23 changes: 23 additions & 0 deletions dockerfiles/keepalived-base/init_network.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#LOCAL_IP=`hostname|awk -F. '{print $2}'|sed 's/-/./g'`
ETH=${ETH:-"eth0"}
BITMASK=${BITMASK:-"24"}
export LOCAL_IP=`ip a show $ETH|awk '/inet.*brd.*'$ETH'/{print $2}'|awk -F "/" '{print $1}'`

if ! ip addr show gretap1 &>/dev/null ;then
if [ -z $NODE1 ] || [ -z $NODE2 ] || [ -z $LOCAL_IP ];then
echo "LOCAL_IP or NODE1 or NODE2 is empty"
elif [ "$LOCAL_IP" == "$NODE1" ];then
export REMOTE_NODE=$NODE2
ip link add gretap1 type gretap local $NODE1 remote $NODE2
ip link set dev gretap1 up
ip addr add dev gretap1 $GRETAP_IP1/$BITMASK
elif [ "$LOCAL_IP" == "$NODE2" ];then
export REMOTE_NODE=$NODE1
ip link add gretap1 type gretap local $NODE2 remote $NODE1
ip link set dev gretap1 up
ip addr add dev gretap1 $GRETAP_IP2/$BITMASK
else
echo "ERROR: Local_ip and node IP does not match." && exit 1
fi
fi
13 changes: 13 additions & 0 deletions dockerfiles/keepalived-check/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM shurenyun/centos7-keepalived-1.2.13
MAINTAINER jyliu <[email protected]>

#RUN yum install -y mysql &&\
# yum clean all
#ADD keepalived-scripts.tar.gz /etc/keepalived/
COPY /etc/ /etc/
COPY entrypoint.sh /

RUN chmod -R 755 /etc/keepalived/scripts /etc/keepalived/build_config.sh /entrypoint.sh
ENV TERM dumb

ENTRYPOINT ["/entrypoint.sh"]
2 changes: 2 additions & 0 deletions dockerfiles/keepalived-check/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
opts='--no-cache'
docker build $opts -t shurenyun/centos7-keepalived-1.2.13-check .
41 changes: 41 additions & 0 deletions dockerfiles/keepalived-check/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/bin/bash
#set -ex

if [ "x$CONFIG_SERVER" != "x" ];then
export DM_READ_URI=`curl $CONFIG_SERVER/config/$(hostname)/keepalived/filelist.json`
fi

if [ "x$DM_READ_URI" != "x" ];then
/DM_DOCKER_URI.py
fi

ETH=${ETH:-"eth0"}
BITMASK=${BITMASK:-"24"}
export LOCAL_IP=`ip a show $ETH|awk '/inet.*brd.*'$ETH'/{print $2}'|awk -F "/" '{print $1}'`


if [ -z $NODE1 ] || [ -z $NODE2 ] || [ -z $LOCAL_IP ];then
echo "LOCAL_IP or NODE1 or NODE2 is empty"
elif [ "$LOCAL_IP" == "$NODE1" ];then
export REMOTE_IP=$NODE2
GRETAP_IP=$GRETAP_IP1
elif [ "$LOCAL_IP" == "$NODE2" ];then
export REMOTE_IP=$NODE1
GRETAP_IP=$GRETAP_IP2
else
echo "ERROR: Local_ip and node IP does not match." && exit 1
fi

if ! ip addr show gretap1 &>/dev/null ;then
ip link add gretap1 type gretap local $LOCAL_IP remote $REMOTE_IP
ip link set dev gretap1 up
ip addr add dev gretap1 $GRETAP_IP/$BITMASK
fi



#trap "pkill keepalived ; exit 1" SIGHUP SIGINT SIGTERM EXIT

#echo '' > /var/log/keepalived-status.log
/etc/keepalived/build_config.sh && \
/usr/sbin/keepalived --dont-fork --log-console
34 changes: 34 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/build_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/sh
set -eu
BASE_DIR=$(cd `dirname $0` && pwd)
cd $BASE_DIR

if [ -f "../config.cfg" ];then
. ../config.cfg
elif [ -f "./config.cfg" ];then
. ./config.cfg
fi

VIRTUAL_ROUTER_ID=`echo $KEEPALIVED_VIP|awk -F. '{print $4}'`
PRIORITY=`echo $LOCAL_IP|awk -F. '{print $4}'`

replace_var(){
files=$@
echo $files | xargs sed -i 's/--ENNAME--/'"$ETH"'/g'
echo $files | xargs sed -i 's/--PRIORITY--/'"$PRIORITY"'/g'
echo $files | xargs sed -i 's/--VIRTUAL_ROUTER_ID--/'"$VIRTUAL_ROUTER_ID"'/g'
echo $files | xargs sed -i 's/--LOCAL_IP--/'"$LOCAL_IP"'/g'
echo $files | xargs sed -i 's/--OTHER_IP--/'"$REMOTE_IP"'/g'
echo $files | xargs sed -i 's/--KEEPALIVED_VIP--/'"$KEEPALIVED_VIP"'/g'
echo $files | xargs sed -i 's/--BITMASK--/'"$BITMASK"'/g'
}

pre_conf(){
rm -f $1
cp $1.temp $1
files="$1"
replace_var $files
}

cp keepalived-${CHECK_SERVICE}.conf keepalived.conf.temp
pre_conf keepalived.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
vrrp_script chk_haproxy_service {
script "/etc/keepalived/scripts/check_haproxy.sh"
interval 2
weight 300
}

vrrp_script chk_vip {
script "/etc/keepalived/scripts/check_vip.sh"
interval 2
weight 300
}

vrrp_instance VI_1 {
interface --ENNAME--
state BACKUP
priority --PRIORITY--
virtual_router_id --VIRTUAL_ROUTER_ID--
advert_int 1
nopreempt
unicast_src_ip --LOCAL_IP--
unicast_peer {
--OTHER_IP--
}
authentication {
auth_type PASS
auth_pass dmhaproxy
}

notify_master /etc/keepalived/scripts/to_master.sh
notify_backup /etc/keepalived/scripts/to_backup.sh

virtual_ipaddress {
--KEEPALIVED_VIP--/--BITMASK-- dev --ENNAME-- label --ENNAME--:0
}
track_script {
chk_haproxy_service
chk_vip
}
}
46 changes: 46 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/keepalived-mysql.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
global_defs {
router_id HA_mysql_254
}

vrrp_script chk_mysql {
script "/etc/keepalived/scripts/check_mysql.sh"
interval 2
}

vrrp_script chk_mysql_sync {
script "/etc/keepalived/scripts/check_mysql_sync.sh"
interval 5
}


vrrp_sync_group mysql {
group {
VI_mysql
}
}

vrrp_instance VI_mysql {
state BACKUP
interface gretap1
virtual_router_id 254
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass dm254
}
track_script {
chk_mysql
chk_mysql_sync
}

notify_master /etc/keepalived/scripts/mysql_to_master.sh
notify_backup /etc/keepalived/scripts/mysql_to_backup.sh
# notify_stop /etc/keepalived/scripts/mysql_stop.sh

virtual_ipaddress {
10.3.10.254/24 dev eth0 label eth0:0
}
}

26 changes: 26 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/keepalived-nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
vrrp_script chk_nginx_service {
script "/etc/keepalived/scripts/check_nginx.sh"
interval 2
}

vrrp_instance VI_1 {
interface eth0
state BACKUP
priority 101
virtual_router_id 76
advert_int 1
unicast_src_ip 10.3.10.47
unicast_peer {
10.3.10.48
}
authentication {
auth_type PASS
auth_pass dmnginx
}
virtual_ipaddress {
10.3.10.76
}
track_script {
chk_nginx_service
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
count=0
for (( k=0; k<2; k++ ))
do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" --user dataman:dataman "http://127.0.0.1:9000/;csv" -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
else
count=0
break
fi
done
if [ "$count" != "0" ]; then
pkill keepalived
exit 1
else
exit 0
fi
40 changes: 40 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/scripts/check_mysql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

MYSQL=/usr/bin/mysql
MYSQL_HOST=${MYSQL_HOST:-"127.0.0.1"}
MYSQL_USER=${MYSQL_USER:-"zabbix_check"}
MYSQL_PASSWORD=${MYSQL_PASSWORD:-"zabbix1234"}
CHECK_TIME=5
MYSQL_BIN="$MYSQL -h$MYSQL_HOST -u$MYSQL_USER -p$MYSQL_PASSWORD"
#mysql is working MYSQL_OK is 1 , mysql down MYSQL_OK is 0

MYSQL_OK=1

function check_mysql_helth (){
$MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p$MYSQL_PASSWORD -e "show status;" >/dev/null 2>&1
if [ $? = 0 ] ;then
MYSQL_OK=1
else
MYSQL_OK=0
fi
return $MYSQL_OK
}

while [ $CHECK_TIME -ne 0 ];do
let "CHECK_TIME -= 1"
check_mysql_helth
if [ $MYSQL_OK = 1 ] ; then
CHECK_TIME=0
/etc/keepalived/scripts/check_mysql_sync.sh
exit 0
else
if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ];then
echo "ERROR: mysql is error."
pkill keepalived
exit 1
fi
fi

sleep 1
done

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
MYSQL=/usr/bin/mysql
MYSQL_HOST=127.0.0.1
MYSQL_BIN="$MYSQL -h$MYSQL_HOST -u$MYSQL_ADMIN_USER -p$MYSQL_ADMIN_PASSWORD"

timefile="/var/log/mysql-to-backup-time"
if [ -f "$timefile" ]; then

num=`cat $timefile`
now_num=`date +%s`
let value=$now_num-$num

if [ $value -gt 60 ];then
sql="show slave status \G"
slave_status_run_num=`$MYSQL_BIN -e "$sql" | grep -E 'Slave_IO_Running:|Slave_SQL_Running:'|grep -i "Yes"|wc -l`
tail -1 /var/log/keepalived-status.log|grep 'backup'
if [ $? -eq 0 ] && [ $slave_status_run_num -ne 2 ] ;then
echo "ERROR: slave sync error." && pkill keepalived
exit 1
fi
exit 0
fi
else
date +%s > $timefile
fi
19 changes: 19 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/scripts/check_nginx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
count=0
for (( k=0; k<2; k++ ))
do
check_code=$( curl --connect-timeout 3 -sL -w "%{http_code}\\n" http://127.0.0.1/ -o /dev/null )
if [ "$check_code" != "200" ]; then
count=count+1
continue
else
count=0
break
fi
done
if [ "$count" != "0" ]; then
pkill keepalived
exit 1
else
exit 0
fi
10 changes: 10 additions & 0 deletions dockerfiles/keepalived-check/etc/keepalived/scripts/check_vip.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
vip=`awk '/dev.*label*/{print $1}' /etc/keepalived/keepalived.conf|awk -F/ '{print $1}'`

tail -1 /var/log/keepalived-status.log | grep master || exit 0

ip a | grep $vip >/dev/null 2>&1
if [ $? -ne 0 ];then
pkill keepalived
exit $status
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
/etc/keepalived/scripts/to_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
/etc/keepalived/scripts/to_master.sh
Loading

0 comments on commit 1acf8ee

Please sign in to comment.