系统版本 Centos7
IP:10.10.11.79 Master
IP:10.10.11.81 Slave
虚拟ip:10.10.11.77
客户端发起一个请求 ,请求没有到Nginx的实际IP上,而是请求的虚拟IP(会和实际IP通过配置文件进行绑定)
如果有一台Nginx服务器挂了,Keepalived会自动在备Nginx服务器上选一台当主服务器
1. 主从两台机器上分别启动nginx容器:
两台nginx容器要用相同的端口号:
docker run -d --privileged=true --name keepalivedNginx -p 82:80 nanlist/nginx1.23.1:v1.0
2.在两台宿主机器上分别安装 keepalived (注意:keepalived安装在实体机上,不是安装到Docker容器中)
1.联网下载到/usr/local目录下并解压有可能会提示连接不成功,加上它提示的命令再下载就好了
如果下载不下来用浏览器下载下来再传到服务上面也可以
两台服务器分别执行下:
主:
[root@Master html]# cd /usr/local
[root@Master local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Master local]# tar -zxvf keepalived-1.4.2.tar.gz
从:
[root@Slave html]# cd /usr/local
[root@Slave local]# wget https://www.keepalived.org/software/keepalived-1.4.2.tar.gz
[root@Slave local]# tar -zxvf keepalived-1.4.2.tar.gz
2.安装相关依赖,有不用下载了
主:
[root@Master local]# yum install -y gcc openssl-devel popt-devel
从:
[root@Slave local]# yum install -y gcc openssl-devel popt-devel
3.编译安装
主:
[root@Master local]# cd keepalived-1.4.2
[root@Master keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Master keepalived-1.4.2]# make
[root@Master keepalived-1.4.2]# make install
从:
[root@Slave local]# cd keepalived-1.4.2
[root@Slave keepalived-1.4.2]# ./configure --prefix=/usr/local/keepalived
[root@Slave keepalived-1.4.2]# make
[root@Slave keepalived-1.4.2]# make install
4.相关配置,按命令执行
主:
[root@Master keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Master keepalived-1.4.2]# mkdir /etc/keepalived
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Master keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
从:
[root@Slave keepalived-1.4.2]# pwd
/usr/local/keepalived-1.4.2
You have new mail in /var/spool/mail/root
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Slave keepalived-1.4.2]# mkdir /etc/keepalived
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived-1.4.2/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Slave keepalived-1.4.2]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@Slave keepalived-1.4.2]#
安装之后,在 /etc/keepalived目录下有个 keepalived.conf 配置文件:
主:
vim /etc/keepalived/keepalived.conf
编辑内容:
global_defs {
router_id LVS_DEVEL
}
# 检查nginx状态的脚本,健康监测脚本
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh" # 脚本路径
interval 2 # 脚本执行间隔时间
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) 用ifconfig查看你具体的网卡
virtual_router_id 51 # 虚拟路由编号,主从要一至
priority 150 # 优先级,数值越大,获取处理请求的优先级越高 master要大于slave
advert_int 1
unicast_src_ip 10.10.11.79 # 本机ip
track_script {
chk_nginx
}
nopreempt
authentication {
auth_type PASS # 指定认证方式。PASS简单密码认证(推荐),AH:IPSEC认证(不推荐)
auth_pass 1111 # 指定认证所使用的密码。最多8位
}
unicast_peer { # 另外一台的服务器ip,如果是多台就配多个ip
10.10.11.81
}
virtual_ipaddress { # 指定VIP地址
10.10.11.77
}
}
从:
编辑:
vim /etc/keepalived/keepalived.conf
配置内容:
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state Slave
interface ens192
virtual_router_id 51
priority 90
advert_int 1
unicast_src_ip 10.10.11.81
unicast_peer {
10.10.11.79
}
nopreempt
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
10.10.11.77
}
}
健康监测脚本
主从机都需配置检测nginx是否在运行,不在允许就直接启动nginx的脚本,和keepalived放在一起
脚本名称 nginx_check.sh
如果 nginx 停止运行,尝试启动,如果无法启动则杀死本机的 keepalived 进程,keepalived将虚拟 ip 绑定到 BACKUP 机器上。内容如下:
#!/bin/bash
#version 0.0.1
#当nginx进程不存在时,会自动重启nginx服务;
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
docker restart keepalivedNginx #重启nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
systemctl stop keepalived
fi
fi
编辑完以后放入到/etc/keepalived目录下:
只要配置好了,以后直接启动keepalived就好了,keepalived运行之后就会检测nginx是否在运行,不在运行就通过脚本去启动
自动重启不了解决方案
查看脚本是否有运行的权限
如果你是root登陆的话(不是的话,切换到root用户,对*.sh 赋可执行的权限)
chmod 777*.sh
或者
chmod +x *.sh
主从两台服务器这一步授权一定要执行不然脚本会失效:
[root@Master keepalived]# chmod +x nginx_check.sh
[root@Master keepalived]# ll
total 12
-rw-r--r-- 1 root root 572 Oct 12 03:55 keepalived.conf
-rw-r--r-- 1 root root 3550 Oct 12 03:48 keepalived.conf.bak
-rwxr-xr-x 1 root root 205 Oct 12 04:04 nginx_check.sh
[root@Master keepalived]#
脚本上传以后重启keepalived。
keepalived常用命令
#启动
service keepalived start
#停止
service keepalived stop
#查看状态
service keepalived status
验证
1:如果验证主从是否可以自动切换实现热备效果可以把其中一台机器的keepalived服务stop掉,然后刷新虚拟ip。
2:如果验证健康脚本是否生效,可以把nginx容器stop掉,过两秒再次查看一下nginx容器是否启动。