Nginx高可用-Nginx+Keepalived
# Nginx 高可用(Nginx+Keepalived )
安装keepalived
yum install -y keepalived
1修改配置文件
配置文件
/etc/keepalived/keepalived.conf
配置文件说明:
# global_defs 模块 global_defs { notification_email { jast_zsh@foxmail.com } notification_email_from sns-lvs@gmail.com smtp_server smtp.hysec.com smtp_connection_timeout 30 router_id nginx_master # 设置 master的id,在一个网络下应该是唯一的(我们也可以设置与hostname相同这样可以保证) } # vrrp_script 模块 vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行 interval 2 #(检测脚本执行的间隔,单位是秒) weight 2 } # vrrp_instance 模块 vrrp_instance VI_1 { state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) virtual_router_id 66 # 虚拟路由编号,主从要一直 priority 100 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { 10.8.10.99 10.8.10.98 # 定义虚拟ip(VIP),可多设,每行一个,注意这里设置的ip要与所在网段相同,否则无法生效 } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37vrrp_instance.interface 取值:服务器使用的网卡,
ens192
[root@localhost src]# ifconfig ens192: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.8.10.23 netmask 255.255.255.0 broadcast 10.8.10.255 inet6 fe80::a3c0:ef25:a705:2ae1 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:45:83:22 txqueuelen 1000 (Ethernet) RX packets 135400 bytes 93376258 (89.0 MiB) RX errors 0 dropped 158 overruns 0 frame 0 TX packets 109485 bytes 186236718 (177.6 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 59809 bytes 92054271 (87.7 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 59809 bytes 92054271 (87.7 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18- MASTER 配置
global_defs { notification_email { jast_zsh@foxmail.com } notification_email_from sns-lvs@gmail.com smtp_server smtp.hysec.com smtp_connection_timeout 30 router_id nginx_master # 设置 master的id,在一个网络应该是唯一的 } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" #最后手动执行下此脚本,以确保此脚本能够正常执行 interval 2 #(检测脚本执行的间隔,单位是秒) weight 2 } vrrp_instance VI_1 { state MASTER # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) virtual_router_id 66 # 虚拟路由编号,主从要一直 priority 100 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { 10.8.10.99 # 定义虚拟ip(VIP),可多设,每行一个 } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31BACKUP配置
global_defs { notification_email { jast_zsh@foxmail.com } notification_email_from sns-lvs@gmail.com smtp_server smtp.hysec.com smtp_connection_timeout 30 router_id nginx_backup # 设置nginx backup的id,在一个网络应该是唯一的 } vrrp_script chk_http_port { script "/usr/local/src/check_nginx_pid.sh" interval 2 #(检测脚本执行的间隔) weight 2 } vrrp_instance VI_1 { state BACKUP # 指定keepalived的角色,MASTER为主,BACKUP为备 interface ens192 # 当前进行vrrp通讯的网络接口卡(当前centos的网卡) virtual_router_id 66 # 虚拟路由编号,主从要一直 priority 99 # 优先级,数值越大,获取处理请求的优先级越高 advert_int 1 # 检查间隔,默认为1s(vrrp组播周期秒数) authentication { auth_type PASS auth_pass 1111 } track_script { chk_http_port #(调用检测脚本) } virtual_ipaddress { 10.8.10.99 # 定义虚拟ip(VIP),可多设,每行一个 } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31check_nginx_pid.sh
配置#!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx #重启nginx if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then #nginx重启失败 exit 1 else exit 0 fi else exit 0 fi
1
2
3
4
5
6
7
8
9
10
11
12启动keepalived
systemctl start keepalived systemctl status keepalived
1
2设置开机自动启动
systemctl enable keepalived
1
访问系统
此时Nginx随便挂掉一个访问虚拟IP都可以访问到相应服务
访问虚拟地址即可实现nginx高可用
http://10.8.10.99/
1
上次更新: 2023/03/10, 16:49:38