大家好,又见面了,我是你们的朋友全栈君。
Ingress Nginx默认访问日志都输出到/var/log/nginx/access.log
文件中,但是对于一般的生产环境来说,不可能把所有日志都输到一个日志文件中,一般情况都是根据域名分别输出到各个文件中。所以这里区分http指令域默认配置以及单独域名的日志的配置方式。
1.默认日志格式更改为json
修改mandatory.yaml部署文件nginx-configuration ConfigMap配置中log-format-upstream字段,具体修改如下:
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"}'
第二种:
log-format-upstream: '{"nginx_timestamp":"$time_iso8601","clientip":"$remote_addr","nginx_host":"$server_addr","host":"$http_host","request":"$request","url":"$request_uri","upstreamhost":"$upstream_addr","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","xff":"$http_x_forwarded_for","referer":"$http_referer","http_user_agent":"$http_user_agent","request_length":"$request_length","request_method":"$request_method"}'
注意:mandatory.yaml是官方ingress-nginx的默认安装yaml文件,其中也有configmap的配置,这是配置为空,留给运维人员自定义发挥。这里只是截出configmap的配置。(安装ingress这里不做演示,参考本人ingress安装博客)
kind: ConfigMap apiVersion: v1 metadata: name: nginx-configuration namespace: ingress-nginx labels: app.kubernetes.io/name: ingress-nginx app.kubernetes.io/part-of: ingress-nginx data: log-format-upstream: '{"nginx_timestamp":"$time_iso8601","clientip":"$remote_addr","nginx_host":"$server_addr","host":"$http_host","request":"$request","url":"$request_uri","upstreamhost":"$upstream_addr","status":"$status","body_bytes_sent":"$body_bytes_sent","request_time":"$request_time","upstream_response_time":"$upstream_response_time","xff":"$http_x_forwarded_for","referer":"$http_referer","http_user_agent":"$http_user_agent","request_length":"$request_length","request_method":"$request_method"}'
部署了上述文件后,查看一下ingress-nginx的配置文件中发生了什么变化
#先将nginx配置文件拷贝出来比较容易查看 kubectl cp -n ingress-nginx nginx-ingress-controller-8f68db9b5-2flsq:/etc/nginx/nginx.conf /test/nginx.conf #找到文件变化的配置文件
目前ingress-nginx配置日志文件格式为json输出已经完成了。一般默认中server指令域中如果不配置单独的日志输出,会使用http中默认的日志配置。如果需要为每一个域名独立配置文件,见如下。
2.根据域名配置访问日志输出
这里使用一个测试网站的ingress的yaml文件来作为演示。
cat gcc-21ldj-frontend-ingress.yaml --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: gcc-21ldj-frontend namespace: dev annotations: kubernetes.io/ingress.class: nginx nginx.ingress.kubernetes.io/configuration-snippet: | access_log /var/log/nginx/gcc-h5-test01.access.log upstreaminfo if=$loggable; # 这里直接引用了http指令域的upstreaminfo。 error_log /var/log/nginx/gcc-h5-test01.error.log; rewrite /gcc/21ldj/(.*) /$1 break; spec: tls: - hosts: - gcc-h5-test01.mbgadev.cn secretName: mbgadev-20210908 rules: - host: gcc-h5-test01.mbgadev.cn http: paths: - path: /gcc/21ldj/ backend: serviceName: gcc-21ldj-frontend servicePort: 80
部署该ingress文件后,查看一下nginx文件的变化。
#将最新的nginx文件拷贝一下 kubectl cp -n ingress-nginx nginx-ingress-controller-8f68db9b5-2flsq:/etc/nginx/nginx.conf /test/nginx.conf #找到文件变化的配置文件
注:if=$loggable的含义
#参数 if,设置是否记录日志,当参数值的条件成立,即不为 0 或空时,才记录日志。 下面是ingress-nginx中的配置文件 map $status $loggable { ~^[23] 0; default 1; } access_log logs/access.log combined if=$loggable; 具体可参考:Nginx日志管理
3.参考
Log format – ingress-nginx-docs-cn
Ingress配置参考:https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/configmap.md
Ingress注释参考https://github.com/kubernetes/ingress-nginx/blob/master/docs/user-guide/nginx-configuration/annotations.md
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/132848.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...