First, the host installation
1, extract
tar -zxvf keepalived-2.0.18.tar.gz
prefix: keepalived installation location
2、解压后进入到解压出来的目录,看到会有configure
,那么就可以做配置了
3、使用configure
命令配置安装目录与核心配置文件所在位置:
./configure --prefix=/usr/local/keepalived --sysconf=/etc
sysconf: Location keepalived core configuration file is located, fixed position, then changed to a different location keepalived not start, / var / log / messages will be error
A warning message may appear in the configuration process, as follows:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
Installation libnl/libnl-3
dependence
yum -y install libnl libnl-devel
Re- configure
look, this time OK.
4, installation keepalived
make && make install
5, into /etc/keepalived
, the directory configuration file to the core keepalived
6, the command vim keepalived.conf
to open the configuration file (the host)
global_defs {
Route # id: keepalived currently installed host node identifier to ensure globally unique
router_id keep_108
}
vrrp_instance VI_1 {
MASTER state # indicates master or backup machine BACKUP
state MASTER
# NIC bind that instance by ip addr command to view, each machine is not the same
interface ens33
# Ensure consistent standby node can be
virtual_router_id 51
# Weights, master weight is generally higher than backup, if there is more that election, whose high weight, whoever elected
priority 100
# Primary synchronization check interval between standby, in seconds
advert_int 2
# Permissions password authentication to prevent illegal node enters
authentication {
auth_type PASS
auth_pass 1111
}
# Out of the virtual ip, there can be multiple (vip)
virtual_ipaddress {
192.168.1.161
}
}
7, start keepalived
/usr/local/keepalived/sbin/keepalived
8, view the process
ps -ef | grep keepalived
9, see the vip, in the card ens33, one more 192.168.1.161, this is the virtual ip
ip addr
10, the keepalived registered as a system service, easy to lose command
Keepalived into the extracted directory
cd keepalived/etc/
cp init.d/keepalived /etc/init.d/
cp sysconfig/keepalived /etc/sysconfig/
systemctl daemon-reload
So you can use the following command to the
Second, the installation preparation
1 to 5 with the host
6, the command vim keepalived.conf
to open the configuration file (backup unit)
global_defs {
router_id keep_101
}
vrrp_instance VI_1 {
# Standby machine set BACKUP
state BACKUP
interface ens33
virtual_router_id 51
# Weight less than MASTER
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 注意:主备两台的vip都是一样的,绑定到同一个vip
192.168.1.161
}
}
7~10同主机
这样我请求192.168.1.161时,时请求的主机,当主机的keepalived挂掉后,我请求192.168.1.161时,请求自动到达备机,当主机恢复正常后请求又会到达主机,从而实现双击主备高可用。
三、keepalived实现nginx双机主备高可用
上边可知,主备机的切换是基于keepalived是否挂掉实现的,那么如何实现当主机nginx挂掉,自动切换到备机nginx呢?那就需要让keepalived去检测本机的nginx是否正常,如果发现nginx进程不在了,尝试重启nginx,如果重启失败,那么杀掉keepalived的进程,从而让请求到达备机。下边实现keepalived绑定nginx
1. 增加Nginx重启检测脚本
vim /etc/keepalived/check_nginx_alive_or_not.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
# 判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
# 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
增加运行权限
chmod +x /etc/keepalived/check_nginx_alive_or_not.sh
2. 在/etc/keepalived/keepalived.conf中添加下边两段
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
}
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
最终是这样的:
3. 重启Keepalived使得配置文件生效
systemctl restart keepalived
备机同上。