Nginx+Keepalived+VIP漂移实现HA高可用技术之详细教程
https://www.cnblogs.com/zcc666/p/13141626.html 这个是nginx安装教程地址
https://www.cnblogs.com/zcc666/p/13138260.html 这个是Keepalived的安装教程地址
1.首先把nginx、Keepalived安装好了,我们开始进行今天的技术分析话题
2.准备2台linux负载服务器,并且2台都要安装nginx、Keepalived程序。
准备一个虚拟IP,我这里用192.168.1.20
第一台linux的地址:192.168.2.2 (主 MASTER)
第二台linux的地址:192.168.2.3 (从 BACKUP)
3.nginx里面的配置写好自己要负载的ip地址和端口号。
4.开始对第一台linux的地址:192.168.2.2 (主 MASTER)的Keepalived的配置进行修改。
默认的配置路径在 /etc/Keepalived/Keepalived.conf
global_defs { router_id LVS_DEVEL script_user root enable_script_security } vrrp_script chk_nginx { script "/etc/keepalived/scripts/nginx_check.sh" interval 2 timeout 2 fall 3 weight -20 } vrrp_instance nginx { state MASTER interface eth0 virtual_router_id 100 priority 100 #nopreempt # no seize,must add advert_int 1 authentication { #all node must same auth_type PASS auth_pass 1111 } unicast_src_ip 192.168.2.2 unicast_peer { 192.168.2.3 } virtual_ipaddress { 192.168.1.20 } track_script { chk_nginx } }
在这个地方新增一个文件夹
mkdir /etc/keepalived/scripts/
这个下面存放一个脚本 nginx_check.sh,代码如下
#!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then systemctl start nginx sleep 2 if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then systemctl stop keepalived.service fi fi
之后,对该scripts下脚本赋给权限
chmod a+x /etc/keepalived/scripts/*.sh
此时就可以启动keepalived、nginx
然后看看 ip add 这里的VIP就漂移到主负载地址下面了。
++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++
5.开始对第一台linux的地址:192.168.2.3 (主 BACKUP)的Keepalived的配置进行修改。
默认的配置路径在 /etc/Keepalived/Keepalived.conf
global_defs {
router_id LVS_DEVEL
script_user root
enable_script_security
}
vrrp_script chk_nginx
{
script "/etc/keepalived/scripts/nginx_check.sh"
interval 2
timeout 2
fall 3
weight -20
}
vrrp_instance nginx {
state BACKUP
interface eth0
virtual_router_id 100
priority 90
#nopreempt # no seize,must add
advert_int 1
authentication { #all node must same
auth_type PASS
auth_pass 1111
}
unicast_src_ip 192.168.2.3
unicast_peer {
192.168.2.2
}
virtual_ipaddress {
192.168.1.20
}
track_script {
chk_nginx
}
}
在这个地方新增一个文件夹
mkdir /etc/keepalived/scripts/
这个下面存放一个脚本 nginx_check.sh,代码如下
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
systemctl start nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
systemctl stop keepalived.service
fi
fi
之后,对该scripts下脚本赋给权限
chmod a+x /etc/keepalived/scripts/*.sh
此时就可以启动keepalived、nginx
然后看看 ip add 这里的VIP不在此服务器上。
最后:
可以关闭主服务器的keepalived、nginx 然后看看从服务器的vip就漂移过来了。