实验环境:centos7
节点1:192.168.40.140
节点2:192.168.40.141
vip地址:192.168.40.143
1、下载文件
cd /usr/local/src wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz # 官网地址 https://www.keepalived.org/download.html
2、解压文件
tar xzf keepalived-2.0.20.tar.gz cd keepalived-2.0.20
3、安装依赖
yum -y install libnl libnl-devel openssl-devel libnfnetlink-devel libnfnetlink ipvsadm libnl3 libnl3-devel lm_sensors-libs net-snmp-agent-libs net-snmp-libs openssh-server openssh-clients openssl automake iproute gcc gcc-c++ wget tree
4、初始化配置
cd keepalived-2.0.20/ ./configure --prefix=/usr/local/keepalived
5、编译安装
make && make install
6、配置
mkdir /etc/keepalived cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /etc/init.d/
节点1配置文件如下,注意两个节点mcast_src_ip 和 priority 值
nginx1
# cat keepalived.conf ! Configuration File for keepalived global_defs { router_id hf-ayd-web-balance-01 script_user root enable_script_security } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 151 mcast_src_ip 192.168.40.140 priority 100 advert_int 1 #心跳检测,10秒以下 # nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.143 } track_script { chk_nginx } }
nginx2
# cat /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id hf-ayd-web-balance-02 script_user root enable_script_security } vrrp_script chk_nginx { script "/etc/keepalived/nginx_check.sh" interval 2 weight -20 } vrrp_instance VI_1 { state BACKUP interface ens33 virtual_router_id 151 mcast_src_ip 192.168.40.141 priority 90 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.40.143 } track_script { chk_nginx } }
nginx的拉起脚本
# cat ck_ng.sh #!/bin/bash #检查nginx进程是否存在 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then #尝试启动一次nginx,停止5秒后再次检测 service nginx start sleep 5 counter=$(ps -C nginx --no-heading|wc -l) if [ "${counter}" = "0" ]; then #如果启动没成功,就杀掉keepalive触发主备切换 service keepalived stop fi fi
7、管理服务
systemctl daemon-reload #重新加载服务
systemctl start keepalived #启动 systemctl restart keepalived #重启 systemctl stop keepalived #关闭 systemctl status keepalived # 状态
常见问题一:keepalived配置nopreempt参数无效解决方法
nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高 master不能设置nopreempt 解决方案是:不设置master,全部设置成backup,这样大家都是backup,就都能添加nopreempt,即使原本成为master的LB坏掉重新修好之后也不会抢占master。 通常如果master服务死掉后backup会变成master,但是当master服务又好了的时候 master此时会抢占VIP,这样就会发生两次切换对业务繁忙的网站来说是不好的。 所以我们要在配置文件加入 nopreempt 非抢占,但是这个参数只能用于state 为backup,故我们在用HA的时候最好master和backup的state都设置成backup 让其通过priority来竞争
常见问题二:编译安装keepalived时提示以下错误
make cd . && /bin/sh /data/keepalived-2.0.19/missing automake-1.15 --foreign Makefile /data/keepalived-2.0.19/missing: line 81: automake-1.15: command not found WARNING: 'automake-1.15' is missing on your system. You should only need it if you modified 'Makefile.am' or 'configure.ac' or m4 files included by 'configure.ac'. The 'automake' program is part of the GNU Automake package: <http://www.gnu.org/software/automake> It also requires GNU Autoconf, GNU m4 and Perl in order to run: <http://www.gnu.org/software/autoconf> <http://www.gnu.org/software/m4/> <http://www.perl.org/> make: *** [Makefile.in] Error 127 解决方法如下: yum install automake -y autoreconf -ivf 再次执行make
https://blog.51cto.com/xiong51/2108353
https://www.jianshu.com/p/4e405ca6f60b
ubuntu16.04搭建keepalived-2.0.20环境
https://blog.csdn.net/Hello_World_QWP/article/details/104447076