ingress-nginx-controller 日志持久化保存为JSON格式

背景

很多场景下需要对ingress-nginx-controller访问日志进行收集保存,一方面可以对访问数据进行统计分析,另一方面也有助于分析问题解决问题。

ingress-nginx 部署

参考
k8s 部署 ingress-nginx 并对外暴露服务

创建本地目录

mkdir /var/log/nginx/
# 运行 ingress-nginx-controller 的 UserID 是 101
chown 101:101 /var/log/nginx/

将本地目录挂载到Pod内

~]# kubectl edit ds -n ingress-nginx ingress-nginx-controller 
    spec:
      hostNetwork: true
      containers:
      - args:
        - /nginx-ingress-controller
        - --election-id=ingress-nginx-leader
        - --controller-class=k8s.io/ingress-nginx
        - --ingress-class=nginx
        - --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
        - --validating-webhook=:8443
        - --validating-webhook-certificate=/usr/local/certificates/cert
        - --validating-webhook-key=/usr/local/certificates/key
        - --log_dir=/var/log/nginx/     # If non-empty, write log files in this directory (no effect when -logtostderr=true)
        - --logtostderr=false           # log to standard error instead of files (default true)

......

        volumeMounts:
        - mountPath: /usr/local/certificates/
          name: webhook-cert
          readOnly: true
        - mountPath: /etc/localtime     # 挂载本地时区、避免日志时间不一致
          name: tz-shanghai
          readOnly: true
        - mountPath: /var/log/nginx     # 挂载本地日志目录
          name: nginx-log

......

      volumes:
      - name: webhook-cert
        secret:
          secretName: ingress-nginx-admission
      - name: tz-shanghai
        hostPath:
          path: /usr/share/zoneinfo/Asia/Shanghai
      - name: nginx-log
        hostPath:
          path: /var/log/nginx

将日志格式修改为JSON格式

~]# kubectl edit cm -n ingress-nginx ingress-nginx-controller 
kind: ConfigMap
apiVersion: v1
data:
  log-format-escape-json: "true"
  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", "protocol": "$server_protocol", "path": "$uri", "request_length":
    $request_length, "method": "$request_method", "referrer": "$http_referer", "ua":
    "$http_user_agent", "upstream_addr": "$upstream_addr", "upstream_response_time":
    $upstream_response_time, "upstream_status": $upstream_status}'

最终效果

在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CodingDemo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值