高可用系列文章之三 - NGINX 高可用实施方案
前文鏈接
四 NGINX 高可用實施方案
高可用的實施, 主要步驟概述如下:
系統(tǒng)軟 硬件詳細配置清單
根據(jù)制造業(yè)高可用架構設計, 以及業(yè)務需求, 部署模型建議配置如下:
- 負載均衡服務器(即 NGINX + Keepalived): 2臺, 操作系統(tǒng) Linux. 配置建議如下:
| CPU | 2 core | |
| 內(nèi)存 | 4 GB | |
| 硬盤 | 50 GB | |
| 操作系統(tǒng) | SUSE12 64位及補丁 | |
| 網(wǎng)卡 | 至少1塊網(wǎng)卡, 支持VRRP 技術 |
- 軟件運行配置環(huán)境:
| NGINX | 1.16.1 | |
| Keepalived | 2.0.10 | |
4.1 NGINX 安裝及配置
4.1.1 分區(qū)及目錄
建議至少分為以下3個區(qū):
| 主分區(qū)( /) | 默認 | nginx程序及配置文件位于該分區(qū)(/etc/nginx) |
| 日志分區(qū)(/var/log/nginx) | 10G-20G | |
| 程序目錄分區(qū)(/usr/share/nginx/html) | 10G | 可選, nginx用作web server時需要用到此目錄. |
4.1.2 程序及依賴版本
| nginx | nginx-1.16.1-1.sles12.ngx.x86_64.rpm | 1.16.1 | 396A359F26DD0100CD59545BAFFAFE85 |
4.1.3 NGINX程序規(guī)范
- nginx程序目錄: /etc/nginx
- 執(zhí)行程序路徑: /usr/sbin/nginx
- 主配置文件路徑:/etc/nginx/conf/nginx.conf
- 各個應用系統(tǒng)轉發(fā)配置文件目錄:/etc/nginx/conf.d/
- 日志目錄:/var/log/nginx
- 各個應用系統(tǒng)靜態(tài)文件目錄:/usr/share/nginx/html
4.1.4 系統(tǒng)級別配置優(yōu)化
:grey_exclamation:?注意:
需要 root 用戶執(zhí)行.
4.1.5 配置NGINX repo
:grey_exclamation: 注意:
本節(jié)命令可以根據(jù)具體情況, 在公司內(nèi)部 repo 倉庫機器上進行操作.
其他機器只需要配置內(nèi)部 repo 地址即可.
鍵入以下 zypper 命令以添加 SLES 的 zypper 存儲庫
$ sudo zypper addrepo -G -t yum -c 'http://nginx.org/packages/sles/12' nginx接下來,您必須驗證數(shù)字簽名以保持下載包的完整性和來源。使用wget命令獲取nginx簽名密鑰:
$ wget http://nginx.org/keys/nginx_signing.key示例輸出:
--2020-01-09 23:48:48-- http://nginx.org/keys/nginx_signing.key Resolving nginx.org (nginx.org)... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ... Connecting to nginx.org (nginx.org)|206.251.255.63|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 1561 (1.5K) [text/plain] Saving to: ‘nginx_signing.key’100%[==================================================>] 1,561 --.-K/s in 0s 2020-01-09 23:48:49 (117 MB/s) - ‘nginx_signing.key’ saved [1561/1561]使用rpm命令將密鑰導入rpm:
$ sudo rpm --import nginx_signing.key4.1.6 SUSE 上安裝NGINX
鍵入以下 zypper 命令:
$ sudo zypper install nginx=1.16.14.1.7 可選: 配置防火墻
:grey_exclamation: 注意:
如果機房流量入口有其他專用防火墻, 則可以關閉 nginx 服務器上的防火墻, 且不需要執(zhí)行此步驟.
首先創(chuàng)建Nginx特定服務的配置文件,使用vi命令等文本編輯器打開端口80:
$ sudo vi /etc/sysconfig/SuSEfirewall2.d/services/nginx添加以下配置:
## Name: Nginx web server ## Description: Open ports for Nginx Server# space separated list of allowed TCP ports TCP="http"(如果不需要 HTTPS 支持,則只需要允許 TCP 端口號 80 上的通信。)保存并退出 VI/VIM 文本編輯器。現(xiàn)在,只需運行以下命令打開端口80:
$ sudo yast firewall必須使用 TAB 和箭頭鍵在 YaST 中跳轉。在 YaST 中,跳轉到允許的服務,然后按 Enter 鍵:
使用 TAB 跳轉到“Allowed Services”,然后按向下箭頭鍵選擇 *Nginx web server,然后按回車鍵。必須按 *Alt-A 來將Nginx服務器添加到防火墻:
按 Alt-N 和 Alt-F 保存并完成 SLES 上的防火墻設置。返回 shell 提示符后,列出 sle 上的所有 iptables 規(guī)則:
$ sudo iptables -S示例輸出:
-A input_ext -p tcp -m limit --limit 3/min -m tcp --dport 80 --tcp-flags FIN,SYN,RST,ACK SYN -j LOG --log-prefix "SFW2-INext-ACC-TCP " --log-tcp-options --log-ip-options -A input_ext -p tcp -m tcp --dport 80 -j ACCEPT使用 sudo 命令和 grep 命令的組合來確定端口 80 是否打開:
sudo sh -c 'iptables -L -n -v | grep :80'4.1.8 啟動 NGINX Server
鍵入以下 systemctl 命令以在系統(tǒng)啟動時啟用 Nginx: (開機自啟)
$ sudo systemctl enable nginx啟動 Nginx web 服務器:
$ sudo systemctl start nginx驗證:
$ systemctl status nginx要判斷 80 端口是否監(jiān)聽, 運行以下 netstat 命令或 ss 命令:
$ sudo netstat -tulpn | grep :80 $ sudo ss -tulpn | grep :804.1.9 NGINX 基礎操作
停止:
$ sudo systemctl stop nginx啟動:
$ sudo systemctl start nginx重新啟動服務:
$ sudo systemctl restart nginx更改配置后重新加載 Ngnix:
$ sudo systemctl reload nginx:information_source: 建議:
配置更新后使用 reload 來重新加載nginx.
訪問 nginx 頁面:
假設 NGINX IP為: 192.168.0.1. 使用瀏覽器或 curl 訪問:
http://serve_IP http://your-domain http://192.168.0.1 $ curl -I 192.168.122.43示例輸出:
HTTP/1.1 200 OK Server: nginx/1.16.1 Date: Sat, 03 Feb 2020 19:18:53 GMT Content-Type: text/html Content-Length: 612 Last-Modified: Tue, 17 Oct 2019 13:30:50 GMT Connection: keep-alive ETag: "59e6060a-264" Accept-Ranges: bytes4.1.10 查找有關 SLES 上 Nginx 配置文件的信息
現(xiàn)在 Nginx 已經(jīng)啟動并運行了。接下來可以定制配置。
SLES 的服務器配置文件:
- /etc/nginx/: nginx 默認配置目錄
- /etc/nginx/nginx.conf: nginx 主配置文件
- /etc/nginx/conf.d/default.conf: 默認 virtual host 的配置
修改主配置:
$ sudo vi /etc/nginx/nginx.conf4.1.11 NGINX 日志
- /var/log/nginx/access.log: 訪問日志
- /var/log/nginx/error.log: 錯誤日志
4.1.12 nginx.conf
主配置文件詳細說明如下:
#### 全局塊 開始 ##### # 配置允許運行Nginx服務器的用戶和用戶組 #user nginx nginx; # 配置允許Nginx進程生成的worker process數(shù) worker_processes 4; #worker_cpu_affinity 0001 0010 0100 1000;# 配置Nginx服務器運行時的錯誤日志文件存放路徑和名稱 #error_log logs/error.log; error_log logs/error.log info;# 配置Nginx服務器運行時的pid文件存放路徑和名稱 pid logs/nginx.pid; #### 全局塊 結束 ######### events塊 開始 #### events {# 配置事件驅動模型use epoll;accept_mutex off;multi_accept off;worker_connections 65535; } #### events塊 結束 ######## http塊 開始 #### http {# 定義MIME-Typeinclude mime.types;default_type application/octet-stream;# 配置請求處理日志的格式log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent $request_time $upstream_response_time "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" "$host"';access_log logs/access.log main;# 配置允許使用sendfile方式傳輸sendfile on;#tcp_nopush on;# 配置連接超時時間#keepalive_timeout 0;keepalive_timeout 65;# nginx允許的客戶端請求頭部的緩沖區(qū)大小client_header_buffer_size 4k;# gzip confgzip on;gzip_min_length 1024;gzip_buffers 32 4k;gzip_http_version 1.1;gzip_comp_level 6;gzip_types text/plain application/xml image/x-icon image/svg+xml image/png text/css image/jpeg image/gif application/x-javascript application/javascript application/json;gzip_vary on;gzip_disable "MSIE [1-6]\.";# securityport_in_redirect off;server_tokens off;# proxy buffer proxy_buffers 8 4k;proxy_buffer_size 4k;proxy_temp_file_write_size 4k;proxy_temp_path proxy_temp;# proxy cache# proxy_cache_path cache/ keys_zone=cache_all:10m;#### server塊 開始 ###### 配置虛擬主機localhostserver {listen 80 reuseport;server_name localhost;#charset koi8-r;access_log logs/host.access.log main;location / {root html;index index.html index.htm;}error_page 404 /404.html;# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}#### server 塊 結束 ##### HTTPS server##server {# listen 443 ssl;# server_name localhost;# ssl_certificate cert.pem;# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;# ssl_prefer_server_ciphers on;# location / {# root html;# index index.html index.htm;# }#}# virtual hostsinclude conf.d/default.conf;} #### http塊 結束 ####:heavy_check_mark: 建議:
為了保證主配置文件的干凈. 建議通過 include conf.d/default.conf; 類似這樣的方式來引入其他virtual hosts配置.
4.1.13 日志轉儲
sudo vi /etc/logrotate.d/nginx
編輯內(nèi)容:
/var/log/nginx/*.log {dailyrotate 90 # 保留90天, 按需調(diào)整createdateext#compress # 是否啟用壓縮, 按需調(diào)整#minsize 1M#create 0644 nginx nginx # nginx日志所屬用戶和組, 按需調(diào)整# copytruncate 用于還在打開中的日志文件,把當前日志備份并截斷;是先拷貝再清空的方式,拷貝和清空之間有一個時間差,可能會丟失部分日志數(shù)據(jù)。# delaycompress 和compress 一起使用時,轉儲的日志文件到下一次轉儲時才壓縮missingokifempty # defaultnomail#noolddir # defaultsharedscripts # 運行postrotate腳本,作用是在所有日志都輪轉后統(tǒng)一執(zhí)行一次腳本。如果沒有配置這個,那么每個日志輪轉后都會執(zhí)行一次腳本postrotate # 在logrotate轉儲之后需要執(zhí)行的指令,例如重新啟動 (kill -HUP) 某個服務!必須獨立成行if [ -f /var/log/nginx/nginx.pid ]; thenkill -USR1 `cat /var/log/nginx/nginx.pid`fiendscript}強制運行一次來測試:logrotate -f -v /etc/logrotate.d/nginx(對應目錄只能 user 有w權限, 否則會報錯)
配置好即可, logrotate 會自動讀取/etc/logrotate.d的配置并自動執(zhí)行.
📓 備注:
cron.daily 會在 3:22+(5,45) 這個時間段執(zhí)行
4.2 NGINX + Keepalived配置
:information_source: 假定:
假設相關IP如下:
4.2.1 Keepalived 安裝配置
$ sudo zypper install keepalived=2.0.104.2.2 Keepalived 配置
Keepalived啟動默認讀取配置文件路徑 /etc/keepalived/keepalived.conf ,添加配置文件:
sudo vi /etc/keepalived/keepalived.confNGINX - 主 機器配置如下:
vrrp_script chk_nginx {script "/etc/keepalived/nginx_pid.sh" # 檢測nginx狀態(tài)的腳本路徑interval 2weight -20 fall 3 } vrrp_instance VI_1 {state MASTER # 從機為 BACKUPinterface eth0 # 具體的網(wǎng)卡接口看情況進行填寫virtual_router_id 51 priority 110 # 備機權值為100advert_int 1authentication {auth_type PASSauth_pass 123456}track_script {chk_nginx}virtual_ipaddress {192.168.0.100/24 brd 192.168.0.255 dev eth0 label eth0:vip # 根據(jù)實際情況進行修改} }配置說明如下:
weight -20: keepalived 配置里 priority 110 是初始權重;主的初始設置了 110,備的設置了 100;如果檢測失敗,權重 -20. 主從切換.
virtual_router_id 51 自己定義的虛擬路由的id。vrrp_instance VI_1 塊中 virtual_router_id 指令的值 51 是一個示例值;根據(jù)需要改變它,使其在您的環(huán)境中獨一無二。
添加
NGINX - 從 機器配置如下:
vrrp_script chk_nginx {script "/etc/keepalived/nginx_pid.sh" # 檢測nginx狀態(tài)的腳本路徑interval 2weight -20 } vrrp_instance VI_1 {state BACKUP # 輔機為 BACKUPinterface eth0 # 具體的網(wǎng)卡接口看情況進行填寫virtual_router_id 51priority 100 # 備機權值為100advert_int 1authentication {auth_type PASSauth_pass 123456}track_script {chk_nginx}virtual_ipaddress {192.168.0.100/24 brd 192.168.0.255 dev eth0 label eth0:vip # 根據(jù)實際情況進行修改} }4.2.3 配置檢測腳本
:grey_exclamation: 備注:
使用 root 用戶
配置檢測腳本:/etc/keepalived/nginx_pid.sh
#!/bin/bash A=`ps -C nginx --no-header|wc -l` if [ $A -eq 0 ] thensystemctl start nginxexit 1 else exit 0 fi賦予執(zhí)行權限: chmod 750 /etc/keepalived/nginx_pid.sh
4.2.4 啟動 Keepalived
鍵入以下 systemctl 命令以在系統(tǒng)啟動時啟用 keepalived: (開機自啟)
$ sudo systemctl enable keepalived啟動 Nginx web 服務器:
$ sudo systemctl start keepalived驗證:
$ systemctl status keepalived4.2.4 顯示節(jié)點狀態(tài)
要查看當前哪個節(jié)點是給定 VIP 的主節(jié)點,請在定義 VRRP 實例的接口上運行命令 ip addr show(在以下命令中,eth0節(jié)點suse12-1和上的接口suse12-2):
suse12-1 # ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000link/ether 52:54:00:33:a5:a5 brd ff:ff:ff:ff:ff:ffinet 192.168.0.1/24 brd 192.168.0.255 scope global dynamic eth0valid_lft 3071sec preferred_lft 3071secinet 192.168.0.100/32 scope global eth0valid_lft forever preferred_lft foreversuse12-2 # ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast stateUP qlen 1000link/ether 52:54:00:33:a5:87 brd ff:ff:ff:ff:ff:ffinet 192.168.0.2/24 brd 192.168.0.255 scope global eth0valid_lft forever preferred_lft forever在此輸出中,第二行 inet 表示 suse12-1 是主節(jié)點, 已為其分配了已定義的 VIP(192.168.0.100)。inet 輸出中的其他行顯示主節(jié)點的實際IP地址(192.168.0.1)和備用(或 從)節(jié)點的IP地址(192.168.0.2)。
至此, nginx + keepalived 的負載均衡層高可用已經(jīng)配置完成.
4.3 NGINX -> 應用服務層 轉發(fā)配置
:information_source: 假定:
假設應用系統(tǒng)的相關信息如下:
4.3.1 修改配置文件
以 mes 系統(tǒng)高可用配置為例(協(xié)議為 HTTP 協(xié)議), 新增 /etc/nginx/conf.d/mes.conf:
# mes accessupstream mes{# ip_hash; # 源地址會話保持, 按需開啟server 172.30.0.1:8080;server 172.30.0.2:8080; }server {listen 80;#server_name mes.example.com 192.168.0.100; # server_name為對應的訪問域名, 按需開啟#set max uploading file size to 20mclient_max_body_size 20m;#charset koi8-r;# keepalive# 設置代理的HTTP協(xié)議版本(默認是1.0版本)proxy_http_version 1.1; # 按需調(diào)整# 允許重新定義或追加字段到傳遞給代理服務器的請求頭信息(默認是close)proxy_set_header Connection "";proxy_set_header HOST $http_host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;access_log logs/mes_access.log main buffer=16k flush=5m;error_log logs/mes_error.log;location / {#root html;#index index.html index.htm;proxy_pass http://mes;#proxy_redirect off;# proxy_cache cache_all;}#error_page 404 /404.html;# redirect server error pages to the static page /50x.html##error_page 500 502 503 504 /50x.html;#location = /50x.html {# root html;#} }4.3.2 主配置文件 include
在 http 塊新增如下內(nèi)容
include conf.d/mes.conf;4.3.2 重新加載 nginx
更改配置后重新加載Ngnix使配置生效:
sudo systemctl reload nginx
4.3.3 驗證
通過 VIP 進行訪問, 查看對應應用系統(tǒng)日志, 觀察是否 2 個節(jié)點都接收到請求.
五 變更控制流程
:exclamation: 嚴格遵循 制造業(yè)本公司的變更控制規(guī)范. 進行相關的變更控制和審批流程.
一個新的應用需要上線或者變更,通常涉及以下步驟:
可以隨時實施的變更:
:exclamation: 需要在變更窗口進行的變更:
5.1 回退步驟
回退較為簡單, 步驟如下:
參考文件
| High Availability Support for NGINX |
三人行, 必有我?guī)? 知識共享, 天下為公. 本文由東風微鳴技術博客 EWhisper.cn 編寫.
總結
以上是生活随笔為你收集整理的高可用系列文章之三 - NGINX 高可用实施方案的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oci 创建nlb的terraform代
- 下一篇: 入1万