nginx-ingress的configmap配置修改方法
业务场景及诉求
用户需要使用开源的ingress-controller作为集群的负载分发入口,并希望对nginx-ingress进行定制化参数配置,可以参考如下配置方法。
【配置方法】:
1.1、常用的配置参数表如下:
修改nginx-controller日志输出格式: |
|
log-format-upstream |
'{"time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr", "x-forward-for": "$proxy_add_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" }' |
设置日志标头字段以标识客户端的原始IP地址: |
|
forwarded-for-header |
"X-Forwarded-For" |
上述是经常用的参数,详细参数说明,参考连接:https://kubernetes.github.io/ingress-nginx/user-guide/nginx-configuration/configmap/#forwarded-for-header
1.2、配置思路如下:
需要知道nginx-ingress模板安装的命名空间,并找到服务对应的默认configmap的名字和namespaces,如下所示:
命令:kubectl get deploy –all-namespaces | grep nginx-ingress-controller | awk ‘{print $2}’ 查询到服务和命名空间:
查看configmap的配置注入信息
命令:kubectl get deploy nginx-cyd-nginx-ingress-controller –ndefault –oyaml | grep configmap –C5
可以看到默认的nginx-ingress controller对应的configmap配置(controller的服务名)
1.3、Configmap参数注入
按照上图的信息在对应的命名空间下,建立同名的configmap文件,(实例需要在defalut下建立名字为:nginx-cyd-nginx-ingress-controller)
Yaml文件需要编写成如下格式
apiVersion: v1
data:
keys: values------------------------所有的注入信息在此注入
kind: ConfigMap
metadata:
name: nginx-cyd-nginx-ingress-controller
namespace: defalut
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
示例如下:
1.4、重启nginx-ingress模板服务所有的pod
1.5、验证
配置之前的nginx-controller的日志格式,及输出如下:
配置之后nginx-controller的日志格式,及输出如下:
日志格式已经修改成功,并成功打印
注:
如果controller日志中出现如下图的错误,是正常的,因为重启了controller,而已经下发过ingress的服务了,会出现短暂的后端服务找不到,后面重启会自动恢复。
常用配置如下:
apiVersion: v1
data:
allow-backend-server-header: "true"
client-header-buffer-size: "32k"
enable-underscores-in-headers: "true"
forwarded-for-header: "X-Forwarded-For"
generate-request-id: "true"
ignore-invalid-headers: "true"
large-client-header-buffers: "4 32k"
proxy-body-size: "20m"
proxy-connect-timeout: "20"
reuse-port: "true"
server-tokens: "false"
ssl-protocols: "TLSv1 TLSv1.1 TLSv1.2 SSLv3"
ssl-redirect: "false"
worker-cpu-affinity: "auto"
log-format-upstream: '{"time": "$time_iso8601", "remote_addr": "$proxy_protocol_addr", "x-forward-for": "$proxy_add_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" }'
kind: ConfigMap
metadata:
name: nginx-ingress-controller
namespace: kube-system
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
- 点赞
- 收藏
- 关注作者
评论(0)