kubernetesingress更改⽇志格式
 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":"$
  注意: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_len gth","request_method":"$request_method"}'
  部署了上述⽂件后,查看⼀下ingress-nginx的配置⽂件中发⽣了什么变化
#先将nginx配置⽂件拷贝出来⽐较容易查看
kubectl  cp -n ingress-nginx  nginx-ingress-controller-8f68db9b5-2flsq:/etc/f /f
#到⽂件变化的配置⽂件
  ⽬前ingress-nginx配置⽇志⽂件格式为json输出已经完成了。⼀般默认中server指令域中如果不配置单独的⽇志输出,会使⽤http中默认的⽇志配置。如果需要为每⼀个域名独⽴配置⽂件,见如下。
2.根据域名配置访问⽇志输出
  这⾥使⽤⼀个测试⽹站的ingress的yaml⽂件来作为演⽰。
cat gcc-21ldj-frontend-ingress.yaml
nginx 配置文件---
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/log;
rewrite /gcc/21ldj/(.*) /$1 break;
spec:
tls:
- hosts:
- gcc-h5-test01.mbgadev
secretName: mbgadev-20210908
rules:
- host:  gcc-h5-test01.mbgadev
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/f /f #到⽂件变化的配置⽂件
  注:if=$loggable的含义
#参数 if,设置是否记录⽇志,当参数值的条件成⽴,即不为 0 或空时,才记录⽇志。
下⾯是ingress-nginx中的配置⽂件
map $status $loggable {
~^[23]  0;
default 1;
}
access_log logs/access.log combined if=$loggable;
具体可参考:
3.参考

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。