环境:centos7.5 keepalived1.3.5
下载地址:https://www.keepalived.org/software/keepalived-1.3.5.tar.gz
cd /app
tar -xvf keepalived-1.3.5.tar.gz
cd keepalived-1.3.5/
./configure --prefix=/usr/local/keepalived
yum -y install libnl libnl-devel
./configure --prefix=/usr/local/keepalived
yum install -y libnfnetlink-devel
./configure --prefix=/usr/local/keepalived
make && make install
cp ./keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
cp ./keepalived-1.3.5/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
#这样就可以执行service keepalived [start | stop | reload | restart ]命令
#修改/usr/lib/systemd/system/keepalived.service
cat /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=syslog.target network-online.target
[Service]
Type=forking
PIDFile=/var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/usr/local/keepalived/etc/sysconfig/keepalived
ExecStart=/usr/local/keepalived/sbin/keepalived -f /etc/keepalived/keepalived.conf $KEEPALIVED_OPTIONS
#ExecStart=/usr/local/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
[Install]
WantedBy=multi-user.target
测试:
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id http_test3
}
vrrp_instance VI_1 {
# MASTER表示是主节点,备份节点是BACKUP
state MASTER
# 网卡名称,这个不同的服务器,可能有所不同
interface ens192
# 路由标识,MASTER和BACKUP节点的该值要保持一致
virtual_router_id 99
# 优先级,MASTER节点的值必须大于BACKUP的值
priority 100
# MASTER与BACKUP同步的时间间隔,单位为秒
advert_int 1
# lvs对应的真实IP
#mcast_src_ip=192.168.50.154
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟IP的址
virtual_ipaddress {
192.168.50.185
}
}
virtual_server 192.168.50.185 8222 {
# 健康检查的时间,单位为秒
delay_loop 6
# 负载调度算法,这里设置为rr,即轮询算法
lb_algo rr
# 设置DR模式
lb_kind DR
# 虚拟地址的子网掩码
nat_mask 255.255.255.0
# 会话保持时间,单位为秒
persistence_timeout 50
protocol TCP
# 配置真实服务器信息
real_server 192.168.50.156 8222 {
# 节点的权值
weight 1
TCP_CHECK {
# 超时时间
connect_timeout 3
# 重试次数
nb_get_retry 3
# 重试间隔
delay_before_retry 3
}
}
real_server 192.168.50.157 8222 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
搭建过程中出现的错误:
查看/var/log/messager中的错误。可以试着取修改pid的文件路径,再重启,一定要将重启失败的keepalived进程杀掉。也有可能是因为keealived版本和centos版本,导致keepalived.conf文件中,一些keyword是无效的。
参考:https://blog.csdn.net/liyuling52011/article/details/80032317