Centos7 Nginx配合 keepalived+vip高可用安装部署配置
本文最后更新于 955 天前,其中的信息可能已经有所发展或是发生改变。

Centos7 Nginx配合 keepalived+vip高可用安装部署配置

因为SRS高可用,在前一个版本还只是单机版的nginx,因此宕机后会无法使用了,因此配合keepalived+vip实现故障转移

image-20220609095201110

因此规划

192.168.100.47为主服务器,192.168.100.48是从服务器,192.168.100.49为漂移的VIP

主服务器配置(192.168.100.47):

第一步:安装keepalived

yum install -y keepalived

第二步:修改配置文件

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

[root@host147 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   # 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
   router_id keep_host147
   #vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}

vrrp_script check_web {
    script "/etc/keepalived/check_web.sh"    # 脚本存放的位置
    interval 2     # 每隔两秒运行上一行脚本
    weight -20     # 如果脚本运行成功,则升级权重-20,自动切换到备
}


vrrp_instance VI_1 {
        # 主机=MASTER;备用机=BACKUP
    state MASTER
        # 该实例绑定的网卡名称
    interface ens33
        # 保证主备节点一致
    virtual_router_id 51
        # 权重,master值 > backup值
    priority 100
        # 主备组播报发送间隔时间1秒
    advert_int 1
        # 认证权限密码,防止非法节点进入
    authentication {
        auth_type PASS
        auth_pass 1111
    }
        # 虚拟出来的ip,可以有多个(vip)
    virtual_ipaddress {
        192.168.100.149
    }
        # 调用监控脚本
    track_script {
        check_web
    }
}

第三步:编写nginx监控脚本

如果nginx服务停止,keepalived服务也停止,并切换到备主机

脚本如下:

#[root@host147 ~]# cat /etc/keepalived/check_web.sh
#!/bin/bash
num=`ps -C nginx --no-header |wc -l`
if [ $num -eq 0 ]
then
    systemctl stop keepalived
fi

第四步:启动、开机自启keepalived服务

[root@host147 ~]# systemctl start keepalived
[root@host147 ~]# systemctl enable keepalived

第五步:查看keepalived服务状态;查看虚拟IP生效情况;

[root@host147 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:9a:aa:ef brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.147/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.149/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::eec8:aaa1:fa6d:cf50/64 scope link noprefixroute
       valid_lft forever preferred_lft forever
[root@host147 ~]#
[root@host147 ~]#
[root@host147 ~]# ps -ef|grep keepalived
root       7404      1  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       7405   7404  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       7406   7404  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       7556   7199  0 09:58 pts/0    00:00:00 grep --color=auto keepalived
[root@host147 ~]#

从服务器配置(192.168.100.48):

第一步:安装keepalived

参考主服务器

第二步:修改配置文件

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

[root@host148 ~]# cat /etc/keepalived/keepalived.conf
global_defs {
   # 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
   router_id keep_host148
   #vrrp_skip_check_adv_addr
   #vrrp_strict
   #vrrp_garp_interval 0
   #vrrp_gna_interval 0
}


vrrp_script check_web {
    script "/etc/keepalived/check_web.sh"    # 脚本存放的位置
    interval 2     # 每隔两秒运行上一行脚本
    weight -20     # 如果脚本运行成功,则升级权重-20,自动切换到备
}


vrrp_instance VI_1 {
        # 主机=MASTER;备用机=BACKUP
    state BACKUP
        # 该实例绑定的网卡名称
    interface ens33
        # 保证主备节点一致
    virtual_router_id 51
        # 权重,master值 > backup值
    priority 88
        # 主备组播报发送间隔时间1秒
    advert_int 1
        # 认证权限密码,防止非法节点进入
    authentication {
        auth_type PASS
        auth_pass 1111
    }
        # 虚拟出来的ip,可以有多个(vip)
    virtual_ipaddress {
        192.168.100.149
    }
        # 调用监控脚本
    track_script {
        check_web
    }
}

**第三步:编写nginx监控脚本 **

参考主服务器

第四步:启动、开机自启keepalived服务

参考主服务器

第五步:查看keepalived服务状态

参考主服务器

测试高可用

如果主服务器nginx服务停止,则自动切换到备主机

查看主从服务器 keepalived + nginx 服务都处于开启的状态

[root@host147 ~]# ps -ef|grep "nginx\|keepalived"
root       6966      1  0 09:55 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      6968   6966  0 09:55 ?        00:00:00 nginx: worker process
root       7404      1  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       7405   7404  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       7406   7404  0 09:58 ?        00:00:00 /usr/sbin/keepalived -D
root       8177   7199  0 10:02 pts/0    00:00:00 grep --color=auto nginx\|keepalived


[root@host148 ~]# ps -ef|grep "nginx\|keepalived"
root       7024      1  0 09:55 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      7025   7024  0 09:55 ?        00:00:00 nginx: worker process
root       7422      1  0 10:03 ?        00:00:00 /usr/sbin/keepalived -D
root       7423   7422  0 10:03 ?        00:00:00 /usr/sbin/keepalived -D
root       7424   7422  0 10:03 ?        00:00:00 /usr/sbin/keepalived -D
root       7493   7202  0 10:03 pts/0    00:00:00 grep --color=auto nginx\|keepalived

查看虚拟IP在主服务器,因此访问虚拟IP则先到主服务器,然后停止主服务器 nginx 服务,并查看nginx + keepalived服务状态

[root@host147 ~]# systemctl stop nginx
[root@host147 ~]#
[root@host147 ~]#
[root@host147 ~]# ps -ef|grep "nginx\|keepalived"
root       8532   7199  0 10:05 pts/0    00:00:00 grep --color=auto nginx\|keepalived

查看是否已经自动切换到从服务器

[root@host148 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:d7:9f:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.148/24 brd 192.168.100.255 scope global noprefixroute ens33
       valid_lft forever preferred_lft forever
    inet 192.168.100.149/32 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::eec8:aaa1:fa6d:cf50/64 scope link tentative noprefixroute dadfailed
       valid_lft forever preferred_lft forever
    inet6 fe80::dabf:7345:e6bd:52aa/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

继续浏览器访问虚拟IP也可以访问成功!

评论

  1. Macintosh Chrome 102.0.5005.61
    3 年前
    2022-6-11 12:22:16

    牛啊,高深莫测!!!

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇