本次配置的环境如下
Linux: centos7.0
virtual ip: 192.168.30.50
master: 192.168.30.61
backup: 192.168.30.62
1、下载keepalived
cd /usr/local/src
wget http://keepalived.org/software/keepalived-1.2.19.tar.gz
2、解压安装
tar -zxvf /usr/local/keepalived-1.2.19.tar.gz
cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived
make && make install
如果没有安装openssl-devel的话会报错
!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files."
使用yum -y install openssl-devel安装openssl-devel 解决这个问题
cd /usr/local/keepalived
cp sbin/keepalived /usr/sbin/
cp etc/keepalived/keepalived.conf /etc/keepalived/
cp etc/sysconfig/keepalived /etc/sysconfig/
cp etc/rc.d/init.d/keepalived /etc/init.d/
3、编写监控脚本
mkdir /usr/local/keepalived/scripts/ -p
touch /usr/local/keepalived/scripts/monitor_nginx.sh
vi /usr/local/keepalived/scripts/monitor_nginx.sh
脚本内容如下
# 监控nginx进程,若nginx主进程不存在则启动nginx
# 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
增加脚本的执行权限
chmod +x /usr/local/keepalived/scripts/monitor_nginx.sh
4、配置master
vi /etc/keepalived/keepalived.conf,master的配置文件如下
global_defs
{
notification_email
{
example@foxmail.com
}
notification_email_from example@foxmail.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
# 监控脚本
script "/usr/local/keepalived/scripts/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface enp0s3 #网卡名称,根据实际情况配置
virtual_router_id 51
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.50
}
track_script {
Monitor_Nginx
}
}
5、配置backup
vi /etc/keepalived/keepalived.conf
global_defs
{
notification_email
{
example@foxmail.com
}
notification_email_from example@foxmail.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script Monitor_Nginx {
script "/usr/local/keepalived/scripts/monitor_nginx.sh"
interval 2
weight 2
}
vrrp_instance VI_1 {
state BACKUP
interface enp0s3
virtual_router_id 51
priority 100
advert_int 1
track_interface {
enp0s3
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.30.50
}
track_script {
Monitor_Nginx
}
}
至此,keepalived已配置完成,在主备两机器上使用service keepalived start启动keepalived
- 使用ip a命令查看keepalived是否起作用
enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 08:00:27:c2:6c:02 brd ff:ff:ff:ff:ff:ff
inet 192.168.30.61/24 brd 192.168.30.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet ***192.168.30.50***/32 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fe80::a00:27ff:fec2:6c02/64 scope link
valid_lft forever preferred_lft forever
- 查看系统日志
tail -n 100 /var/log/messages |grep Keepalived
Jan 29 00:02:33 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Transition to MASTER STATE
Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Entering MASTER STATE
Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) setting protocol VIPs.
Jan 29 00:02:34 localhost Keepalived_vrrp[3693]: VRRP_Instance(VI_1) Sending gratuitous ARPs on enp0s3 for 192.168.30.50
测试的时候可以在nginx的默认index页面加入ip信息方便观察
nginx的默认页面所在目录是
$NGINX_HOME/html/index.html