1. 192.168.1.247 节点01
1.1 安装keepalived01
```bash
root@ubuntu-server03:~# apt-cache search keepalived
keepalived - Failover and monitoring daemon for LVS clusters
root@ubuntu-server03:~# apt-cache policy keepalived
keepalived:
已安装: 1:1.2.7-1ubuntu1
候选软件包:1:1.2.7-1ubuntu1
版本列表:
1:1.2.13-1~ubuntu14.04.1 0
100 http://mirrors.163.com/ubuntu/ trusty-backports/main amd64 Packages
*** 1:1.2.7-1ubuntu1 0
500 http://mirrors.163.com/ubuntu/ trusty/main amd64 Packages
100 /var/lib/dpkg/status
root@ubuntu-server03:~# apt-get install -y keepalived
root@ubuntu-server03:~# dpkg -l | grep keepalived
ii keepalived 1:1.2.7-1ubuntu1 amd64 Failover and monitoring daemon for LVS clusters
```
1.2 配置keepalived01
```bash
root@ubuntu-server03:~# cat /etc/keepalived/keepalived.conf
global_defs { #自由定义
notification_email {
xxxxx@mail.com
}
notification_email_from keepalived@chtopnet.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 51
}
vrrp_script chk_gitlab {
script "/etc/keepalived/check_gitlab.sh" #定义一个监测脚本
interval 2 #脚本执行频率(单位:s)
}
vrrp_script check_mysqld {
script "/etc/keepalived/check_mysql.sh" #定义一个监测脚本
interval 2
}
vrrp_instance VI_1 {
state BACKUP #两个主机都写BACKUP即可
nopreempt #非抢占模式,只需要在主设置即可
interface em1 #绑定的网卡
virtual_router_id 51 #两个主机id一致
mcast_src_ip 192.168.1.247 #vrrp实体服务器的IP
priority 100 #权重高于另一个即可
advert_int 1
authentication {
auth_type PASS #验证方式
auth_pass chtopnet
}
virtual_ipaddress {
192.168.1.245/24 #虚拟IP
}
track_script {
check_mysqld #应用监测脚本
chk_gitlab
}
}
root@ubuntu-server03:~# service keepalived restat
```
2. 192.168.1.248 节点02
2.1 安装keepalived02
安装步骤和keepalived01 一样
2.3 配置keepalived02
```bash
root@ubuntu-server04:~# cat /etc/keepalived/keepalived.conf
global_defs {
notification_email {
xxxxx@mail.com
}
notification_email_from keepalived@chtopnet.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id 51
}
vrrp_script chk_gitlab {
script "/etc/keepalived/check_gitlab.sh"
interval 2
}
vrrp_script check_mysqld {
script "/etc/keepalived/check_mysql.sh"
interval 2
}
vrrp_instance VI_1 {
state BACKUP
interface em1
virtual_router_id 51
mcast_src_ip 192.168.1.248
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass chtopnet
}
virtual_ipaddress {
192.168.1.245/24
}
track_script {
check_mysqld
chk_gitlab
}
}
root@ubuntu-server04:~# service keepalived restat
```
3. 监测脚本
check\_gitlab.sh
```bash
root@ubuntu-server03:~# cat /etc/keepalived/check_gitlab.sh
#!/bin/bash
container_state=`docker ps -a | grep gitlabnew_boxfish-gitlab | awk '{print $1}' | xargs docker inspect --format='{{.State.Status}}'`
if [ ${container_state} = "running" ]; then
exit 0
else
exit 1
fi
```
check\_gitlab.sh
```bash
root@ubuntu-server04:~# cat /etc/keepalived/check_mysql.sh
#!/bin/bash
Mysqlbin=/usr/bin/mysql
user=root
pw='boxfish123'
port=3306
host=127.0.0.1
sbm=120
#Check for $Mysqlbin
if [ ! -f $Mysqlbin ];then
echo 'Mysqlbin not found,check the variable Mysqlbin'
exit 99
fi
#Get Mysql Slave Status
IOThread=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave statusG' 2>/dev/null|grep 'Slave_IO_Running:'|awk '{print $NF}'`
SQLThread=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave statusG' 2>/dev/null|grep 'Slave_SQL_Running:'|awk '{print $NF}'`
SBM=`$Mysqlbin -h $host -P $port -u$user -p$pw -e 'show slave statusG' 2>/dev/null|grep 'Seconds_Behind_Master:'|awk '{print $NF}'`
#Check if the mysql run
if [[ -z "$IOThread" ]];then
exit 1
fi
#Check if the thread run
if [[ "$IOThread" = "No" || "$SQLThread" = "No" ]];then
exit 1
elif [[ $SBM -ge $sbm ]];then
exit 1
else
exit 0
fi
```