keepalived+lvs实现nginx高可用集群

图示

keepalived+lvs实现nginx高可用集群_第1张图片

环境准备

# 关闭防火墙

#centos7关闭防火墙
systemctl stop firewalld.service
#centos7永久关闭防火墙
systemctl disable firewalld.service

关闭selinux

[root@localhost nginx]# getenforce    #查询selinux状态
[root@localhost nginx]# setenforce 0        #临时将selinux关闭
如果需要永久关闭selinux,请编辑/etc/selinux/config文件,将SELINUX=disabled。

lvs安装

192.168.10.130
192.168.10.131

# 安装ipvs

sudo yum -y install ipvsadm

#启动ipvs

sudo ipvsadm

#查看是否支持lvs:

sudo lsmod |grep ip_vs

 出现如下结果说明安装成功

keepalived+lvs实现nginx高可用集群_第2张图片

#配置虚拟ip

我们需要配置虚拟ip,这是因为我们使用的lvs调度器是DR模式,前面我们讲到过,这种模式下,对客户端的响应是真实服务器直接返回给客户端的,而真实服务器需要将响应报文中的源ip修改为虚拟ip,这里配置的虚拟ip就是起这个作用的。

配置文件:/etc/init.d/lvsrs

#!/bin/bash

ifconfig lo:0 192.168.10.129 netmask 255.255.255.255 broadcast 192.168.10.129 up
route add -host 192.168.10.129 dev lo:0

echo "0" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce

exit 0

lo:表示当前主机真实网卡的名称;
192.168.10.129:表示虚拟ip;
 

keepalived安装

192.168.10.130(主)
 

sudo yum -y install keepalived

配置位置:/etc/keepalived

配置文件:keepalived.conf

! Configuration File for keepalived
 
global_defs {
 
    notification_email {
 
        [email protected]
 
        [email protected]
 
        [email protected]
     }
 
        notification_email_from [email protected]
 
        smtp_server 192.168.10.130
 
        smtp_connect_timeout 30
 
        router_id 192.168.10.130 # 主机ip地址或者主机名字
}

vrrp_script check_nginx_alive { 
    # 脚本位置
	script "/etc/keepalived/check_nginx_alive_or_not.sh" 
	# 每隔两秒运行上一行脚本 
	interval 2
	# 如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
    #如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少
	weight 10 
} 
 
        vrrp_instance VI_1 {
        state MASTER # 备份服务器上将 MASTER 改为 BACKUP
        interface enp0s3 # 网卡
        virtual_router_id 51  # 主、备机的 virtual_router_id 必须相同
        priority 100  # 主、备机取不同的优先级,主机值较大,备份机值较小
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 1111
        }

       track_script {
 	# 追踪 nginx 脚本 
 	check_nginx_alive 
       }
 
        virtual_ipaddress {
             192.168.10.129 # VRRP H 虚拟地址
        }
 
}

检测脚本:

此脚本在nginx挂掉的话会尝试启动nginx

/etc/keepalived/check_nginx_alive_or_not.sh

#!/bin/bash 
#判断nginx服务是否启动,如果不存在,调用nginx启用命令,并停止2秒,若#启动失败,杀掉keepalived
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then 
 service  nginx start
 sleep 2
 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

192.168.10.131(备)

sudo yum -y install keepalived

配置位置:/etc/keepalived

配置文件:keepalived.conf

! Configuration File for keepalived
 
global_defs {
 
    notification_email {
 
        [email protected]
 
        [email protected]
 
        [email protected] 
 
     }
 
        notification_email_from [email protected]
 
        smtp_server 192.168.10.131 #这里保持不变
 
        smtp_connect_timeout 30
 
        router_id 192.168.10.131 #这里保持不变
}
 

vrrp_script check_nginx_alive {
    # 脚本位置
        script "/etc/keepalived/check_nginx_alive_or_not.sh"
        # 每隔两秒运行上一行脚本
        interval 2
        # 如果脚本执行结果为0,并且weight配置的值大于0,则优先级相应的增加
    #如果脚本执行结果非0,并且weight配置的值小于0,则优先级相应的减少
        weight 10
}

 
        vrrp_instance VI_1 {
        state BACKUP # 修改为从机 BACKUP
        interface enp0s3 # 修改为从机 ip
        virtual_router_id 51
        priority 90 # 优先级比主机低
        advert_int 1
        authentication {
            auth_type PASS
            auth_pass 1111
        }
  
      track_script {
 	# 追踪 nginx 脚本 
 	check_nginx_alive 
      }
 
        virtual_ipaddress {
            192.168.10.129
        }
 
}

 检测脚本:

此脚本在nginx挂掉的话会尝试启动nginx

/etc/keepalived/check_nginx_alive_or_not.sh

#!/bin/bash 
#判断nginx服务是否启动,如果不存在,调用nginx启用命令,并停止2秒,若#启动失败,杀掉keepalived
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then 
 service  nginx start
 sleep 2
 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

启动keepalived

130,131两台服务都启动

sudo service keepalived start

查看是否启动成功

ps aux | grep keepalived

出现如下结果

nginx安装

192.168.10.130
192.168.10.131

yum -y nginx

service nginx start

配置开机自启动

systemctl enable nginx.service
systemctl enable keepalived.service

测试:

1.访问:http://192.168.10.129:80可以访问到内容,访问正常说明虚拟ip配置生效

2.192.168.10.130中关闭nginx,几秒后查看nginx是否重启,启动正常,说明keepalived守护nginx生效

3.关闭主机130,如果http://192.168.10.129:80可以访问到内容,说明高可用生效

来源

Keepalived+LVS+nginx搭建nginx高可用集群

nginx 搭建高可用集群_爱上口袋的天空的博客-CSDN博客_nginx高可用集群

Keepalived配置Nginx自动重启_wangJiaLun-china的博客-CSDN博客_keepalived重启命令

你可能感兴趣的:(nginx,运维)