ingress 日志落盘

mkdir /data/space/logs/ingress/ -p

# 在这里需要注意的是得跟自己nginx-ingress里的用户一样否则没有权限,因为linux 用户是判断userid的所以跟容器里的user id 一致就好了
chown -R 101.101 /data/space/logs/ingress/

# 编辑 ingress controller的deployment
kubectl -n ingress-nginx edit deploy ingress-nginx-controller的deployment
#修改一下nginx-ingress启动选项
# 设置controller日志的输出路径和方式 
# - --log_dir=/var/log/nginx/
# - --logtostderr=false

# 修改ingress configmap
kubectl -n ingress-nginx  edit configmap ingress-nginx-controller
configmap 增加access-log-path error-log-path 路径

如果要修改日志格式 把这个参数加入到data 选项下面
log-format-upstream: "[$host] [$remote_addr] [$http_x_forwarded_for]
    [$remote_user] [$time_local] [$request] [$status] [$body_bytes_sent]
    [$request_time] [$upstream_addr] [$upstream_response_time] [$connection]
    [$connection_requests] [$msec] [$uri] [$body_bytes_sent] [$http_referer]
    [$http_user_agent] [$request_length] [$http_session_id]"
apiVersion: v1
data:
  access-log-path: /var/log/nginx/ingress/access.log
  allow-snippet-annotations: "true"
  error-log-path: /var/log/nginx/ingress/error.log
  use-gzip: "true"
  worker-processes: "4"
kind: ConfigMap
metadata:
  creationTimestamp: "2022-01-21T15:39:54Z"
  labels:
    app.kubernetes.io/component: controller
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/version: 1.1.1
    helm.sh/chart: ingress-nginx-4.0.15
  name: ingress-nginx-controller
  namespace: ingress-nginx
  resourceVersion: "126997189"
  uid: dc8b13e2-e9e9-43e8-9214-b92bd29d63b2

挂载日志目录

volumeMounts:
        - mountPath: /var/log/nginx/ingress
          name: logs


volumes:
      - hostPath:
          path: /data/space/logs/ingress
          type: ""
        name: logs

重启pod 生效

ingress 日志切割脚本

#!/bin/bash
NAMESPACE=ingress-nginx
POD_NAME=$(kubectl --kubeconfig=/etc/kubernetes/admin.conf  get pods -n $NAMESPACE | grep ingress-nginx-controller | awk '{print $1}')

# Set the log file paths
ACCESS_LOG=/data/space/logs/ingress/access.log
DATETIME=$(date +%Y-%m-%d-%H-%M-%S)
ACCESS_LOG_BACKUP=/data/space/logs/ingress/access.log.$DATETIME
ERROR_LOG=/data/space/logs/ingress/error.log
ERROR_LOG_BACKUP=/data/space/logs/ingress/error.log.$DATETIME

# Backup and compress the current access and error log files
if [ -f "$ACCESS_LOG" ]; then
    mv $ACCESS_LOG $ACCESS_LOG_BACKUP
    gzip $ACCESS_LOG_BACKUP
    echo "Created backup of access log: $ACCESS_LOG_BACKUP"
fi

if [ -f "$ERROR_LOG" ]; then
    mv $ERROR_LOG $ERROR_LOG_BACKUP
    gzip "$ERROR_LOG_BACKUP"
    echo "Created backup of error log: $ERROR_LOG_BACKUP"
fi

# Reopen the log files using NGINX's reopen signal
kubectl --kubeconfig=/etc/kubernetes/admin.conf -n $NAMESPACE exec -it $POD_NAME -- /sbin/nginx -s reopen

echo "NGINX log files have been rotated."

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐