前言:
尽量安装最新版本
尽量不要做多余的操作,这个东西除了问题有时候很难排查
按照下面绝对不会有pid文件问题,也会实现ip漂移
1.安装依赖
yum -y install gcc gcc-c++ gcc-g77 ncurses-devel bison libaio-devel openssl openssl-devel
2.下载
cd /usr/local/src/
wget https://www.keepalived.org/software/keepalived-2.1.0.tar.gz
3.解压
tar -zxvf keepalived-2.1.0.tar.gz
4.安装
cd keepalived-2.1.0
./configure
make && make install
5.配置服务
cp /usr/local/src/keepalived-2.1.0/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
6.编辑配置文件
vim keepalived.conf
#master
global_defs {
router_id node4 ##本服务器的名称
}
vrrp_instance VI_1 { ##定义VRRP热备实例
state MASTER ##MASTER表示主服务器
interface ens33 ##承载VIP地址的物理接口
virtual_router_id 51 ##虚拟路由器的ID号
priority 100 ##优先级,数值越大优先级越高
advert_int 1 ##通告间隔秒数(心跳频率)
authentication { ##认证信息
auth_type PASS ##认证类型
auth_pass 123456 ##密码字串
}
virtual_ipaddress {
192.168.199.210 ##指定漂移地址(VIP)
}
}
virtual_server 192.168.199.210 80 { ##指定vip地址
delay_loop 5 ##每隔5秒检测一次real server
lb_algo rr
lb_kind DR
protocol TCP
real_server 192.168.199.206 80 { ##指定web集群节点1,在此为nginx1
weight 1
notify_down /etc/keepalived/check1.sh ##real server检测失败后执行的脚本
TCP_CHECK {
connect_port 80
connect_timeout 3 ##连接超时
nb_get_retry 3 ##重试连接次数
delay_before_retry 4 ##重试间隔
}
}
real_server 192.168.199.203 80 { ##指定web集群节点2,在此为nginx2
weight 1
notify_down /etc/keepalived/check2.sh ##real server检测失败后执行的脚本
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
上面check.sh如下
check1.sh
#和check2.sh 就ip不一样
#!/bin/bash
echo -e " nginx1(192.168.199.203) is down on $(date +%F-%T)" >/root/203-nginx.log
5.启动、停止。重启
service keepalived start | stop | restart
6.查看日志
tail -f /var/log/messages