記錄真實IP地址
有時候我們需要在容器中獲取客戶端真實的IP等信息,而經過NginxIngressController轉發后,這些信息不一定拿得到,所以我們需要對NginxIngressController進行配置。
$ kubectl -n ingress-nginx edit configmap ingress-nginx-controller
# 在 data 字段添加下面三行
data:
compute-full-forwarded-for: "true"
forwarded-for-header: X-Forwarded-For
use-forwarded-headers: "true"
# 重啟 ingress-nginx-controller 容器
$ kubectl -n ingress-nginx delete pod -l app.kubernetes.io/component=controller
pod "ingress-nginx-controller-6c979c5b47-hrb4k" deleted
請注意:如果在
ingress-nginx-controller
高可用上的負載均衡器沒有傳遞X-Forwarded-For
的話,同樣是獲取不到真實IP地址的。
如果 ingress-nginx-controller
是高可用的話,那么會出現多個節點有pod,必定是有一個負載均衡器。那么就獲取不到真實IP地址,使用 nginx
做七層代理的話,需要在 location
加上以下幾行參數
map $http_x_forwarded_for $full_x_forwarded_for {
default "$http_x_forwarded_for, $realip_remote_addr";
'' "$realip_remote_addr";
}
# Allow websocket connections
proxy_set_header Upgrade $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $full_x_forwarded_for;
# Pass the original X-Forwarded-For
proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
完整的nginx示例:
下面的日志是通過 ingress
設置的域名訪問,客戶端收集的日志
20.0.135.128 - - [24/Sep/2021:07:04:29 +0000] "GET /test/demo/ HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36" "192.168.32.1, 192.168.32.137"
該行的第一個段是指
上一級的訪問IP地址
。最后一段是指真實客戶端IP地址, 反向代理的后端IP地址
。
設置日志格式
修改 ingress-nginx-controller
的輸出日志格式
$ kubectl -n ingress-nginx edit configmap ingress-nginx-controller
# 在 data 字段添加下面內容
data:
log-format-upstream: '{"time": "$time_iso8601", "k8s_service_name":"$service_name",
"remote_addr": "$remote_addr", "x_forward_for": "$http_x_forwarded_for", "request_id":
"$req_id", "remote_user": "$remote_user", "bytes_sent": $bytes_sent, "request_time":
$request_time, "status": $status, "vhost": "$host", "request_proto": "$server_protocol",
"path": "$uri", "request_query": "$args", "request_length": $request_length, "duration":
$request_time,"method": "$request_method", "http_referrer": "$http_referer", "http_user_agent":
"$http_user_agent" }'
# 重啟 ingress-nginx-controller 容器
$ kubectl -n ingress-nginx delete pod -l app.kubernetes.io/component=controller
pod "ingress-nginx-controller-6c979c5b47-n6stn" deleted
優化參數
$ kubectl -n ingress-nginx edit cm ingress-nginx-controller
# 在 data 字段添加下面內容
data:
# 客戶端請求頭部的緩沖區大小,這個可以根據你的系統分頁大小來設置,一般一個請求頭的大小不會超過 1k,不過由於一般系統分頁都要大於1k,所以這里設置為分頁大小。分頁大小可以用命令getconf PAGESIZE取得。
client-header-buffer-size: 4k
# 設置保持活動的客戶端連接在服務器端保持打開狀態的時間
keep-alive: "60"
# 設置可以通過一個保持活動連接提供的最大請求數。
keep-alive-requests: "100"
# 設置每個工作進程可以打開的最大並發連接數
max-worker-connections: "65535"
# 設置每個工作進程可以打開的最大文件數
max-worker-open-files: "65535"
# 激活緩存以連接到 upstream servers。 連接參數設置保留在每個工作進程的緩存中的 upstream servers 的空閑保持連接的最大數量。 超過此數量時,將關閉最近最少使用的連接。
upstream-keepalive-connections: "10000"
# 設置可以通過一個 keepalive 連接服務的最大請求數。 發出最大請求數后,連接關閉。
upstream-keepalive-requests: "100"
# 設置超時,在此期間,與 upstream servers 的空閑保持連接將保持打開狀態。
upstream-keepalive-timeout: "60"
# 重啟 ingress-nginx-controller 容器
$ kubectl -n ingress-nginx delete pod -l app.kubernetes.io/component=controller
pod "ingress-nginx-controller-6c979c5b47-csmcj" deleted