設定 NGINX 負載平衡器
NGINX 將被配置為第 4 層負載平衡器(TCP),將連線轉發到您的其中一個 Rancher 節點。
在此配置中,負載平衡器位於您的節點前面。負載平衡器可以是任何能夠執行 NGINX 的主機。
備註
請勿將您的其中一個 Rancher 節點用作負載平衡器。
這些範例展示了將負載平衡器配置為將流量導向三個 Rancher 伺服器節點。如果 Rancher 安裝在 RKE Kubernetes 叢集上,則需要三個節點。如果 Rancher 安裝在 K3s Kubernetes 叢集上,則只需要兩個節點。
安裝 NGINX
首先在您要用作負載平衡器的節點上安裝 NGINX。NGINX 為所有已知的作業系統提供了套件。經過測試的版本為 1.14 和 1.15。有關安裝 NGINX 的協助,請參閱他們的安裝文件。
需要 stream 模組,使用官方 NGINX 套件時會包含此模組。請參閱您的作業系統文件,了解如何在您的作業系統上安裝和啟用 NGINX stream 模組。
建立 NGINX 配置
安裝 NGINX 後,您需要使用節點的 IP 位址更新 NGINX 配置檔案 nginx.conf。
-
將下面的程式碼範例複製並貼上到您喜愛的文字編輯器中。將其儲存為
nginx.conf。 -
從
nginx.conf中,將兩處(連接埠 80 和連接埠 443)的<IP_NODE_1>、<IP_NODE_2>和<IP_NODE_3>替換為您節點的 IP。備註有關所有配置選項,請參閱 NGINX 文件:TCP 和 UDP 負載平衡。
NGINX 配置範例 worker_processes 4;
worker_rlimit_nofile 40000;
events {
worker_connections 8192;
}
stream {
upstream rancher_servers_http {
least_conn;
server <IP_NODE_1>:80 max_fails=3 fail_timeout=5s;
server <IP_NODE_2>:80 max_fails=3 fail_timeout=5s;
server <IP_NODE_3>:80 max_fails=3 fail_timeout=5s;
}
server {
listen 80;
proxy_pass rancher_servers_http;
}
}
http {
upstream rancher_servers_https {
least_conn;
server <IP_NODE_1>:443 max_fails=3 fail_timeout=5s;
server <IP_NODE_2>:443 max_fails=3 fail_timeout=5s;
server <IP_NODE_3>:443 max_fails=3 fail_timeout=5s;
}
server {
listen 443 ssl;
ssl_certificate /path/to/tls.crt;
ssl_certificate_key /path/to/key.key;
location / {
proxy_pass https://rancher_servers_https;
proxy_set_header Host <rancher UI URL>;
proxy_ssl_server_name on;
proxy_ssl_name <rancher UI URL>
}
}
} -
將
nginx.conf儲存到負載平衡器的以下路徑:/etc/nginx/nginx.conf。 -
執行以下命令以載入 NGINX 配置的更新:
# nginx -s reload