目录
服务器名 | 主机名 | IP | VIP |
Nginx代理服务器 | proxy1 | 192.168.1.10 | 192.168.1.100 |
Nginx代理服务器 | proxy2 | 192.168.1.20 | |
web网站服务器 | web1 | 192.168.1.30 | / |
实验效果:实验keepalived实现对Nginx代理服务器的高可用,防止一台Nginx出现宕机导致网站无法访问的情况。
(1)Nginx安装
三台服务器都安装Nginx,proxy1和proxy2的Nginx负责提供Nginx代理,web1的Nginx负责提供网页服务:
安装步骤:Nginx安装、配置与概述_桂安俊@kylinOS的博客-CSDN博客
(2)、keepalived安装
- #proxy1和proxy2安装keepalived
- yum install keepalived -y
1、proxy1配置:
vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- router_id test111 #router_id后面名称自定义
- }
-
- vrrp_instance nginx_test {
- state MASTER
- interface ens33 #对应实际网卡名
- virtual_router_id 51
- priority 100 #优先级,主备竞选的时候,优先级高的竞选master,数越大,优先级越高
- advert_int 1
- authentication { #同一组keepalived认证
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress { #虚拟ip
- 192.168.1.100
- }
- }
2、proxy2配置:
vim /etc/keepalived/keepalived.conf
- ! Configuration File for keepalived
-
- global_defs {
- router_id test112 #router_id后面名称自定义,和proxy1不一样即可
- }
-
- vrrp_instance nginx_test {
- state BACKUP #proxy1为主,那这边就为备
- interface ens33 #对应实际网卡名
- virtual_router_id 51 # 虚拟路由的ID号,两个节点设置必须一样,可选IP最后一段使用,相同的VRID为一个组,他将决定多播的MAC地址
- priority 50 #备机优先级改低一点
- advert_int 1 #组播信息发送时间间隔,两个节点必须设置一样,默认为1秒
- authentication { #设置验证信息,两个节点必须一致
- auth_type PASS
- auth_pass 1111 #要和proxy1对上
- }
- virtual_ipaddress { #虚拟IP,两个节点设置必须一样。可以设置多个,一行写一个
- 192.168.1.100
- }
- }
重启proxy1和proxy2的keepalived服务
systemctl restart keepalived
此时虚拟IP在proxy1上:
测试keepalived:将proxy1关机,虚拟IP会漂到proxy2上。
keepalived的检测机制就是检查keepalived进程,当keepalived进程异常,才会进行vip漂移。如果keepalived正常,但是nginx故障,那就会带来vip不漂移导致网站无法访问的问题,这时可以自己写一个脚本去检查nginx状态,如果异常,则kill掉keepalived实现ip漂移,或者重新启动nginx。下面以keepalived重新启动nginx为例:
keepalived节点服务器配置都要改:
vim /etc/keepalived/check_nginx.sh
- #!/bin/bash
- A=`ps -C nginx --no-header | wc -l`
- if [ $A -eq 0 ];then
- /usr/local/nginx/sbin/nginx
- if [ `ps -c nginx --no-header | wc -l` -eq 0 ];then
- exit 1
- else
- exit 0
- fi
- else
- exit 0
- fi
chmod +x /etc/keepalived/check_nginx.sh #给执行权限
然后将check_nginx.sh监测脚本加入keepalived.conf配置文件中调用:
vim /etc/keepalived/keepalived.conf
keepalived节点都要加(红色字体部分),下面以proxy1的keepalived配置为例:
! Configuration File for keepalived
global_defs {
router_id test111
}vrrp_script check_nginx { #定义一个策略,策略名为"check_nginx"
script "/etc/keepalived/check_nginx.sh" #指定脚本路径
interval 1 #检测时间间隔1秒钟
weight -20 #如果发现nginx启动失败了,将当前的机器的priority优先级减20
}vrrp_instance nginx_test {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}track_script { #nginx存活状态检测脚本
check_nginx
}
}
测试:手动关闭一台服务器nginx:
/usr/local/nginx/sbin/nginx -s stop
会发现nginx被keepalived检测脚本自动启动。
【附】完整keepalived调用check_nginx.sh监测配置文件:
proxy1 keepalived.conf配置内容:
- ! Configuration File for keepalived
-
- global_defs {
- router_id test111
- }
-
- vrrp_script check_nginx {
- script "/etc/keepalived/check_nginx.sh"
- interval 1
- weight -20
- }
-
- vrrp_instance nginx_test {
- state MASTER
- interface ens33
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.100
- }
-
- track_script {
- check_nginx
- }
- }
proxy2 keepalived.conf配置内容:
- ! Configuration File for keepalived
-
- global_defs {
- router_id test112
- }
-
- vrrp_script check_nginx {
- script "/etc/keepalived/check_nginx.sh"
- interval 1
- weight -20
- }
-
- vrrp_instance nginx_test {
- state MASTER
- interface ens33
- virtual_router_id 51
- priority 50
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass 1111
- }
- virtual_ipaddress {
- 192.168.1.100
- }
-
- track_script {
- check_nginx
- }
- }