监控nginx负载均衡与后端节点的模块:ngx_http_upstream_check_module

默认情况下没有安装。

需要编译安装添加这个模块。由于lb01服务器已经安装了nginx,所以找另一台服务器编译,然后把编译后的命令发送到lb01服务器替换掉原来的nginx命令即可(替换前要备份nginx命令)

(具体方法见:https://www.logmm.org/2023/07/day39-%e7%bc%96%e8%af%91%e5%ae%89tengine/)

健康状态检查

流程:

1、在upstream中添加检查哪个URL地址、Host与指定状态码
2、在server中设置访问检查结果的uri location

配置如下:

upstream cluster1 {
  #simple round-robin
  server 192.168.10.7:80;
  server 192.168.10.8:80;

  #检查间隔 ms,成功2次,存活 失败5次认为挂了  超时时间 ms 检查类型
  check interval=3000 rise=2 fall=5 timeout=1000 type=http;#curl 命令 访问  curl -I
  check_http_send "HEAD / HTTP/1.0\r\n\r\n";#请求方法 URI(uri最好反应业务是否正常,找开发写个页面)
  check_http_expect_alive http_2xx http_3xx; #认为是成功的状态码 2xx 3xx
}

server {
  listen 80;
  location /1 {
    proxy_pass http://cluster1;
  }
  location /2 {
    proxy_pass http://cluster2;
  }
  location /status {
    check_status;
    access_log off;
    #allow 白名单;
    #deny all;
  }
}

指令说明:

指令说明
check指定检查频率(interval,毫秒),失败几次(fall),成功几次(rise),超时时间(timeout,毫秒),检查方式(type)
check_http_send通过http方式发出请求报文,请求报文起始行、请求方法、请求的URI、请求协议(默认是使用ip方式)
check_http_expect_alive收到指定的状态码,就认为是存活的
check_status开启负载均衡状态检查功能,web页面,location使用,如何加强安全

注意:

如果后端web有多个虚拟主机(端口相同的情况下)
upstream check进行访问的时候默认使用的ip进行访问
在发出http请求的时候指定域名:
check_http_send "HEAD / HTTP/1.0\r\nHost:域名\r\n";

完整的配置如下:

在负载均衡服务器(lb01)上配置

[root@lb01 ~]#vim /etc/nginx/conf.d/check.conf
upstream lb_pools {
  server 192.168.10.7:80 weight=1 max_fails=3 fail_timeout=30s;
  server 192.168.10.8:80 weight=1 max_fails=3 fail_timeout=30s;

  check interval=3000 rise=2 fall=5 timeout=1000 type=http;
  check_http_send "HEAD / HTTP/1.0 \r\n\r\n";
  check_http_expect_alive http_2xx http_3xx;
}

server {
 listen 80;
 server_name lb.com;
 error_log /var/log/nginx/lb-eror.log notice;
 access_log /var/log/nginx/lb-access.log main;

 location / {
    #把请求转发到后端的web节点
    proxy_pass http://lb_pools;
    proxy_set_header Host $http_host;
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-Ip $remote_addr;
 }
 location /status {
    check_status;
    access_log off;
    #allow 192.168.10.1;
    #allow 192.168.10.0/24;
   # deny all;
 }
}

重启nginx

[root@lb01 ~]#systemctl restart nginx

浏览器打开:192.168.10.5/status

如下:

generation: generation是Nginx reload的次数,服务器有2台

把web01的nginx服务停止后,则:

· 对于网站有多个虚拟主机的情况,如何使用一个域名(比如,check.test.com)检查所有的upstream?

1、把所有负载均衡的upstream放在1个文件中,对每个upstream配置checkXXX,如果端口一样的情况,要配置Host:域名
2、设置域名和location规则

第一步:所有upstream放在1个.conf文件中

[root@lb01 ~]#vim /etc/nginx/conf.d/check.upstream.conf
upstream lb_pools{
  server 192.168.10.7:80 weight=2;
  server 192.168.10.8:80 weight=1;
  check interval=1000 rise=2 fall=5 timeout=1000 type=http;
  check_http_send "HEAD / HTTP/1.0\r\n\r\n";
  check_http_expect_alive http_2xx http_3xx;
}
upstream phpmyadmin_pools {
  server 192.168.10.7:81;
  server 192.168.10.8:81;
  check interval=1000 rise=2 fall=5 timeout=1000 type=http;
  check_http_send "HEAD / HTTP/1.0\r\n\r\n";
  check_http_expect_alive http_2xx http_3xx;
}

server {
  listen 80;
  server_name check.test.com;
  error_log /var/log/nginx/check-error.log notice;
  
  location /lb_status {
    check_status;
    access_log off;
    allow 192.168.10.1;
    allow 192.168.10.0/24;
    deny all;
  }
}

第二步:其他站点的子配置文件

[root@lb01 ~]#vim /etc/nginx/conf.d/lb.conf
server {
 listen 80;
 server_name lb.com;
 error_log /var/log/nginx/lb-error.log notice;
 access_log /var/log/nginx/lb-access.log main;
 
 location / {
    #把请求转发到后端的web节点
    proxy_pass http://lb_pools;
    proxy_set_header Host $http_host;  
    proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header  X-Real-Ip $remote_addr;
 }  
}
[root@lb01 ~]#vim /etc/nginx/conf.d/phpmyadmin.conf
server {
  listen 81;
  server_name phpmyadmin.com;
  error_log /var/log/nginx/phpmyadmin-error.log notice;
  access_log /var/log/nginx/phpmyadmin-access.log main;
  location / {
    proxy_pass http://phpmyadmin_pools;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-Ip $remote_addr;
  }
}

重启nginx,后浏览器打开:192.168.10.5/lb_status

结果如下:

Windows本地hosts解析(C:\Windows\System32\drivers\etc\hosts)

192.168.10.5 check.test.com

浏览器打开: check.test.com/lb_status

结果如下:

由于location中配置了uri(lb_status),所以访问的时候要加上uri,否则报错如下:

总的思路:把upstream{}与server{}独立分开成2个文件

注意:

博客站点:listen 80; server_name lb.com;监听的是80端口
状态检查:listen 80; server_name check.test.com;也是监听80端口

因此,使用ip(192.168.10.5)访问的时候,会报错,访问不了wordpress站点:

要想访问,要么修改端口,要么,Windows本地hosts解析博客站点的域名lb.com,但是安装WordPress的时候是使用ip,所以本地解析域名,同样访问会有问题。

因此,只能修改端口。修改check.upstream.conf文件,把端口改成:82,其他内容不变

[root@lb01 ~]#vim /etc/nginx/conf.d/check.upstream.conf
listen 82;

重启nginx。

访问wordpress站点:192.168.10.5

状态检查:192.168.10.5:82/lb_status

访问phpmyadmin:192.168.10.5:81

结果如下图:

WordPress:

状态检查:

phpmyadmin:

至此,多虚拟主机的负载均衡状态检查部署完毕