该架构图,利用keepalived的VIP漂移高可用,防止Nginx单点故障宕机
架构部署规划
- lb01 192.168.178.124 VIP:192.168.178.150(绑定A业务www.chaoge.com)
- lb02 192.168.178.125 VIP:192.168.178.150(绑定B业务bbs.chaoge.com)
Nginx负载均衡配置(两台机器配置一样)
无论哪一个负载均衡节点宕机,都会通过VIP的漂移,让另一个机器继续工作,用户是无感知的
#lb01(nginx.conf)
[root@lb01 keepalived]# cat -n /opt/nginx/conf/nginx.conf
1 worker_processes 1;
2 events {
3 worker_connections 1024;
4 }
5 http {
6 include mime.types;
7 default_type application/octet-stream;
8 upstream www_pools {
9 server 192.168.178.121;
10 server 192.168.178.122;
11 }
12 server {
13 listen 80;
14 server_name www.chaoge.com;
15
16 location / {
17
18 proxy_pass http://www_pools;
19 proxy_set_header Host $host;
20 }
21
22
23
24 }
25
26 }
#重启nginx
[root@lb01 keepalived]# nginx -s reload
#检查
[root@lb01 keepalived]# netstat -tunlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27706/nginx: master
#lb02(nginx.conf)
[root@lb02 keepalived]# cat -n /opt/nginx/conf/nginx.conf
1
2 worker_processes 1;
3 events {
4 worker_connections 1024;
5 }
6 http {
7 include mime.types;
8 default_type application/octet-stream;
9 upstream www_pools {
10 server 192.168.178.121;
11 server 192.168.178.122;
12 }
13 server {
14 listen 80;
15 server_name www.chaoge.com;
16 location / {
17 proxy_pass http://www_pools;
18 proxy_set_header Host $host;
19 }
20 }
21 }
#重启nginx
```csharp
[root@lb01 keepalived]# nginx -s reload
#检查
[root@lb01 keepalived]# netstat -tunlp|grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 27706/nginx: master
#lb01(keepalived.conf)
1.检查配置文件,配置好VI_1实例,实例配置如下
[root@lb01 keepalived]# cat -n /etc/keepalived/keepalived.conf
1 global_defs {
2 router_id lb01
3 }
4
5 vrrp_instance VI_1 {
6 state MASTER
7 interface ens33
8 virtual_router_id 51
9 priority 150
10 advert_int 1
11 authentication {
12 auth_type PASS
13 auth_pass 1111
14 }
15 virtual_ipaddress {
16 192.168.178.150/24 dev ens33 label ens33:3
17 }
18 }
19
2.此处的VIP就是交给Nginx负载均衡的高可用IP地址
#lb02(keepalived.conf)
[root@lb02 keepalived]# cat -n /etc/keepalived/keepalived.conf
1
2 global_defs {
3 router_id lb02
4 }
5
6 vrrp_instance VI_1 {
7 state BACKUP
8 interface ens33
9 virtual_router_id 51
10 priority 100
11 advert_int 1
12 authentication {
13 auth_type PASS
14 auth_pass 1111
15 }
16 virtual_ipaddress {
17 192.168.178.150/24 dev ens33 label ens33:3
18 }
19 }
web01站点配置
1.检查nginx.conf,有关www.chaoge.com虚拟主机配置如下
server {
listen 80;
server_name www.chaoge.com;
charset utf-8;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
2.定义好用于测试负载均衡的站点资料
[root@web01 logs]# echo “我是real server web01” > /opt/nginx/html/www/index.html
web02站点配置
1.检查nginx.conf,有关www.chaoge.com虚拟主机配置如下
server {
listen 80;
server_name www.chaoge.com;
charset utf-8;
location / {
root html/www;
index index.html index.htm;
}
access_log logs/access_www.log main;
}
2.定义好测试负载均衡的站点资料
[root@web02 nginx-1.16.0]# echo "我是real server web02" > /opt/nginx/html/www/index.html
用户访问模拟配置
在本地客户端的hosts文件中写好解析关系,用于测试站点访问
将域名解析到VIP上
浏览器访问域名www.chaoge.com
关闭Nginx负载均衡主节点
可以选择直接关机lb01负载均衡机器,或者简单的关闭keepalived服务
1.关闭服务
systemctl stop keepalived
2.直接关机来的效果更好,模拟机器宕机
poweroff
在关闭了lb01负载均衡之后,模拟宕机,我们继续用客户端访问该站点,看是否正常
检查lb02负载均衡节点
发现VIP已经成功漂移到备节点,整个实验大功告成
[root@lb02 ~]# ip a |grep "inet 192.168"
inet 192.168.178.127/24 brd 192.168.178.255 scope global noprefixroute ens33
inet 192.168.178.150/24 scope global secondary ens33:1