关注分享主机优惠活动
国内外VPS云服务器

Keepalived如何实现Nginx的高可用性

Keepalived实现Nginx高可用Keepalived安装可以参考Mysql+Keepalived实现双主热备。

keepalived . conf global _ defs { router _ idlvs _ level 1 #主服务器名称} vrrp _ script Check _ run { script "/usr/local/src/Check _ nginx . sh " interval 5 #每5秒执行一次脚本} vrrp _ instance VI _ 1 { state master #主服务器接口eth0 # The地址携带到物理接口virtual_router_id 51 #的虚拟路由器ID号上,每个热广播组保持相同的优先级100 #。值越高,优先级越高。advert_int 1 #的校验间隔,默认为1s认证{#认证信息,Auth_type PASS #认证类型auth_pass 1111 #密码字符串} virtual _ IP address { 192 . 168 . 0 . 200 # VIP地址(内网地址)} track_script {check_run }}

keepalived . conf global _ defs { router _ idlvs _ level 2 #备份服务器名称} vrrp _ script Check _ run { script "/usr/local/src/Check _ nginx . sh " interval 5 #每5秒执行一次脚本} vrrp _ instance VI _ 1 { state Backup #备份服务器接口eth0 #将VIP地址携带到物理接口virtual_router_ID 51 #虚拟路由器ID号,每个热播组保持相同的优先级50 #。值越高,优先级越高。advertisement _ int1 #的校验间隔,默认为1s认证{#认证信息,Auth_type PASS #认证类型auth_pass 1111 #密码字符串} virtual _ IP address { 192 . 168 . 0 . 200 # VIP地址(与主服务器设置相同)} track_script {check_run }}

Nginx检测脚本check_nginx.sh#!/bin/bash A = ` ps-c nginx --no-header | wc-l `#判断nginx是否宕机,如果宕机,尝试重启if[$ A -eq 0];然后/usr/local/nginx/sbin/nginx #等一会儿再检查nginx。如果启动不成功,停止keepalived,使其启动备用机sleep 5 if[` PS -C nginx --NO -Header | WC -l ` -EQ0];然后kill all keepalived Fifi chmod+x/etc/keepalived/nginx _ check . sh

Keepalived+Nginx高可用性集群实验环境

准备2个设备。

1设备192 . 168 . 217 . 11 nginx+保持有效

2设备nginx+保持激活

虚拟ip 192.168.217.3

1、2设备安装nginx保持有效

(nginx安装在这里的设备中)

在此基础上,我们直接用yum安装keepalived。

更新网络yum源

[root @ localhost ~]# wget -O/etc/yum . repos . d/centos -base . repo http://mirrors . aliyun . com/repo/centos -7 . repo[root @ localhost ~]# wget -O/etc/yum . repos . d/epel -7 . repo http://mirrors.aliyun.com/repo/epel-7.repo--2022-06-11 17:56:31--http://mirrors . aliyun . com/repo/epel -7 . repo已安装keepalived

[root @ localhost ~]# yum -y installkeepalived . x86 _ 64 Start nginx

[root @ localhost ~]# CD/usr/src/nginx -1 . 12 . 2/[root @ localhost nginx -1 . 12 . 2]# kill all -9 nginx[root @ localhost nginx -1 . 12 . 2]# nginx modify keepalived

[root @ localhost nginx -1 . 12 . 2]# vim/etc/keepalived/keepalived . conf vrrp _ instance VI _ 1 {状态备份#主调度器的初始角色(本实验中主从备份)接口ens33 #修改网卡名称virtual_router_id 52 #主id和从id不应重复优先级90 #选举主调度器优先级(本实验中主备100的数据越大从90, 优先级越高)advertise _ int 1 authentication { auth _ typepassauth _ pass 111 } Virtual _ ip地址{192.168.217.3 #虚拟ip(本实验需要设置为网段的主从)}}查看IP

[root @ localhost ~]# IP a2:ens 33:& lt;广播、多播、UP、LOWER _ UP & gtmtu 1500 qdisc pfifo_fast state UP组默认qlen 1000 Link/ether 00:0c:29:C2:15:cf brd ff:ff:ff:ff:ff:ff inet 192 . 168 . 217 . 11/24 brd 192 . 168 . 217 . 255作用域全局NoPrefixRoute ens 33 Valid _ lft Forever Preferred _ lft Forever inet 192 . 168 . 217 . 3/32

[root @ localhost ~]# systemctlstartekeepalived . service[root @ localhost ~]# systemctlrestartkeepalived . service关闭防火墙内核。

[root @ localhost ~]# systemctlstopfirewall d[root @ localhost ~]# set enforce 0身份验证

[root @ localhost ~]# curl 192 . 168 . 217 . 11 ' nginx 1 '[root @ localhost ~]# curl 192 . 168 . 217 . 12 ' nginx 2 '[root @ localhost ~]# curl 192 . 168 . 217

准备2个设备,双主保持激活

1设备192 . 168 . 217 . 11 nginx+保持有效

2设备nginx+保持激活

虚拟ip 192.168.217.3

虚拟ip 192.168.217.6

基于上述实验

1设备

[root @ localhost ~]# vim/etc/keepalived/keepalived . conf vrrp _ instance VI _ 1 { #修改模块名称状态MASTER #主调度器的初始作用(本实验中主备备份)接口ens33 #修改网卡名称virtual_router_id 51 #主id和从id不重复优先级100 #选举主调度器优先级(本实验中主备100的数据越大从90, 优先级越高)advertise _ int 1 authentication { Auth _ typepassauth _ pass 111 } Virtual _ IP地址{192.168.217.3 #虚拟ip }} vrrp_instance VI_2 {状态备份接口ens33 virtual_router_id 53优先级90 advert _ int 1 authentication { Auth _ type PAS Auth _ pass 1111 } Virtual _ IP地址{192.168.217.6} I}设备2

[root @ localhost ~]# vim/etc/keepalived/keepalived . conf vrrp _ instance VI _ 1 {状态备份接口ens33 virtual_router_id 51优先级90 advert_int 1认证{ auth _ type PASS auth _ PASS 1111 } virtual _ IP address { 192 . 168 . 217 . 3 } } vrrp _ instance VI _ 2 {状态主接口ens33 virtual_router_id 53优先级100 advert_int 1认证{ auth _ type PASS

[root @ localhost ~]# systemctlstartekeepalived . service[root @ localhost ~]# systemctlrestatekeepalived . service查看ip

1优雅的装备Ip很正常。

[root @ localhost ~]# IP a2:ens 33:& lt;广播、多播、UP、LOWER _ UP & gtmtu 1500 qdisc pfifo_fast state UP组默认qlen 1000 Link/ether 00:0c:29:C2:15:cf brd ff:ff:ff:ff:ff:ff inet 192 . 168 . 217 . 11/24 brd 192 . 168 . 217 . 255作用域全局NoPrefixRoute ens 33 Valid _ lft Forever Preferred _ lft Forever inet 192 . 168 . 217 . 3/32

[root @ localhost ~]# IP a2:ens 33:& lt;广播、多播、UP、LOWER _ UP & gtmtu 1500 qdisc pfifo_fast state UP组默认qlen 1000 LINK/ether 00:0c:29:49:B3:a1 brd ff:ff:ff:ff:ff inet 192 . 168 . 217 . 12/24 brd 192 . 168 . 217 . 255 scope全局NOPREFIXROUTE ens 33 Valid _ lft Forever Preferred _ lft Forever inet 192 . 168 . 217 . 6/3 scope

[root @ localhost ~]# curl 192 . 168 . 217 . 11 URL:(7)连接192.168.217.11:80失败;拒绝连接[root @ localhost ~]# curl 192 . 168 . 217 . 12 ' nginx 2 '[root @ localhost ~]# curl 192 . 168 . 217 . 3 curl:(7)连接192.168.217.3: 80失败连接超时[root @ localhost ~]# curl 192 . 168 . 217 . 6 curl:(7)连接192.168.217.6: 80失败;超时连接是我的亲身经历。希望能给大家一个参考,支持主机频道zhujipindao.com。

未经允许不得转载:主机频道 » Keepalived如何实现Nginx的高可用性

评论 抢沙发

评论前必须登录!