一、環境說明
????????系統版本:CentOS 7.9
????????內核版本:3.10.0-1160.119.1
????????Nginx版本:1.26.3
????????第三方檢測模塊及版本:nginx_upstream_check_module(v0.4.0,兼容nginx 1.20+)
二、nginx安裝部署
????????2.1?下載檢測模塊
????????目前程序包的模塊已經升級,升級后支持nginx 1.20以上的版本,源碼包下載地址如下:
https://github.com/yaoweibin/nginx_upstream_check_module/archive/refs/tags/v0.4.0.tar.gz
????????2.2 安裝相關依賴包
????????在執行部署之前,需要安裝相關依賴包,如nginx所需的gcc、gcc-c++、openssl、zlib、pcre等,以及nginx_upstream_check_module執行補丁命令時所需的patch組件。
????????由于現在CentOS 7官方源已經無法使用,因此需要使用國內的其他yum源,或者使用CentOS 7.9鏡像來配置本地的yum源,以安裝相關依賴包。這里就不做過多說明,不熟悉的小伙伴,可以自行聯網查詢相關配置方法。
2.3 執行安裝
????????2.3.1 上傳相關包
????????將nginx以及nginx_upstream_check_module的源碼包上傳到Linux服務器的/usr/local/src目錄下,并解壓。
[root@vm-localsystem src]# ls
nginx-1.26.3.tar.gz nginx_upstream_check_module-0.4.0.tar.gz
[root@vm-localsystem src]# tar -xzf nginx-1.26.3.tar.gz
[root@vm-localsystem src]# tar -xzf nginx_upstream_check_module-0.4.0.tar.gz
[root@vm-localsystem src]# ls
nginx-1.26.3 nginx-1.26.3.tar.gz nginx_upstream_check_module-0.4.0 nginx_upstream_check_module-0.4.0.tar.gz
????????2.3.2 執行patch操作
[root@vm-localsystem src]# cd nginx-1.26.3/
[root@vm-localsystem nginx-1.26.3]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@vm-localsystem nginx-1.26.3]# patch -p1 < ../nginx_upstream_check_module-0.4.0/check_1.20.1+.patch
patching file src/http/modules/ngx_http_upstream_hash_module.c
patching file src/http/modules/ngx_http_upstream_ip_hash_module.c
patching file src/http/modules/ngx_http_upstream_least_conn_module.c
patching file src/http/ngx_http_upstream_round_robin.c
patching file src/http/ngx_http_upstream_round_robin.h
????????2.3.3 創建用戶和組
這里使用普通用戶和組來安裝和運行nginx,用戶和組(如:app)需要提前創建。
[root@vm-localsystem ~]# groupadd app
[root@vm-localsystem ~]# useradd -d /data -g app app
[root@vm-localsystem ~]# chown -R app:app /data
????????2.3.4 編譯和安裝nginx
????????編譯和安裝的時候還是需要使用root用戶的,但是需要在編譯參數里面增加--group=app --user=app這樣的參數,另外還需要增加引入nginx_upstream_check_module-0.4.0模塊的參數--add-module=../nginx_upstream_check_module-0.4.0。
????????編譯安裝成功后,記得將nginx安裝目錄的用戶和組設置為app:app。
[root@vm-localsystem ~]# cd /usr/local/src/nginx-1.26.3/
[root@vm-localsystem nginx-1.26.3]# ./configure --prefix=/data/nginx --with-threads --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-0.4.0 --group=app --user=app[root@vm-localsystem nginx-1.26.3]# make[root@vm-localsystem nginx-1.26.3]# make install[root@vm-localsystem nginx-1.26.3]# chown -R app:app /data/nginx/
????????2.3.5 nginx配置修改
????????這里需要注意的是,由于nginx使用使用普通用戶運行,因此不能使用默認的80監聽端口了,我這里修改為了8081端口。其他配置可根據需要自行修改。
????????2.3.6 啟動nginx服務
????????nginx配置校驗通過后,即可啟動nginx服務,可使用-V檢查nginx的編譯參數。
[app@vm-localsystem conf]$ ../sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[app@vm-localsystem conf]$ ../sbin/nginx -V
nginx version: nginx/1.26.3
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/data/nginx --with-threads --with-http_ssl_module --with-http_stub_status_module --add-module=../nginx_upstream_check_module-0.4.0 --group=app --user=app
[app@vm-localsystem conf]$ ../sbin/nginx
[app@vm-localsystem conf]$ ps -ef| grep nginx| grep -v grep
app 4801 1 0 11:12 ? 00:00:00 nginx: master process ../sbin/nginx
app 4802 4801 2 11:12 ? 00:00:00 nginx: worker process
app 4803 4801 2 11:12 ? 00:00:00 nginx: worker process
三、主動檢測功能測試
3.1 啟動兩個Tomcat實例
????????在一臺Linux服務器上,啟動2個Tomcat實例,部署的應用都為haweb(Oracle官方免費提供的一個測試小應用),為了做區分,對頁面做了點改造,以示區別
????????
????????tomcat1
????????
????????tomcat2
????????
? ? ? ? 在nginx中對這兩個真實的頁面訪問地址進行反向代理和負載均衡配置,部分配置如下:
http {include mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;sendfile on;tcp_nopush on;#keepalive_timeout 0;keepalive_timeout 65;gzip on;upstream haweb {server 192.168.223.96:18170; server 192.168.223.96:18171;#check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;check interval=3000 rise=2 fall=5 timeout=1000 type=http; check_http_send "GET /haweb HTTP/1.0\r\n\r\n";check_http_expect_alive http_2xx http_3xx;}server {listen 8081;server_name 192.168.223.199;charset utf-8;#access_log logs/host.access.log main;location / {root html;index index.html index.htm;}location /haweb {proxy_pass http://haweb;}location /status {check_status;access_log off;allow 192.168.223.3;deny all;}
備注:
? ? ? ? 1)upstream{}配置,以及兩個location{}配置,及/haweb /status;
? ? ? ? 2)upstream{}塊配置中,啟用主動健康檢查,檢查方式為http,使用http 1.0協議,發送/haweb的get請求。期望返回的正常的http代碼為2XX、3XX開頭的返回碼。
????????配置校驗沒問題,然后熱加載,即可通過nginx的IP和端口,來訪問后端頁面,默認訪問策略為輪詢,所以,刷新頁面會在testPage1和testPage2之間來回切換。
[app@vm-localsystem conf]$ ../sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[app@vm-localsystem conf]$ ../sbin/nginx -s reload
[app@vm-localsystem conf]$
????????
????????
? ? ? ? 可以根據配置status模塊,來查看探測的結果
????????
????????把tomcat2的服務進程殺掉后,探測頁面及展示出探測失敗的告警
????????
四、參考
https://www.cnblogs.com/linyouyi/p/11502282.html
https://www.cnblogs.com/LiuChang-blog/p/12494567.html
https://www.cnblogs.com/Xinenhui/p/15944725.html
https://www.cnblogs.com/dance-walter/p/12212607.html
https://github.com/yaoweibin/nginx_upstream_check_module