1、1. 介绍
nginx-ingress-controller的日志包括三个部分:
- controller日志:输出到stdout,通过启动参数中的–log_dir可已配置输出到文件,重定向到文件后会自动轮转,但不会自动清理
- accesslog:输出到stdout,通过nginx-configuration中的字段可以配置输出到哪个文件。输出到文件后不会自动轮转或清理
- errorlog:输出到stderr,配置方式与accesslog类似。
2、落盘
2.1、在ingress nginx所在的节点,创建落盘日志目录,并赋予权限
mkdir /nfs/logs/nginx-ingress -p
chown -R 101.101 /nfs/logs/nginx-ingress/ # 在这里需要注意的是得跟自己nginx-ingress里的用户一样否则没有权限
2.2、将controler日志输出到文件
修改一下nginx-ingress启动选项
# 设置controller日志的输出路径和方式
- --log_dir=/var/log/nginx/
- --logtostderr=false
2.3、修改configmap设置access日志,error日志,以及logformat格式
data:
worker-processes: "4"
use-forwarded-headers: "true"
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]"
access-log-path: "/var/log/nginx/access.log"
error-log-path: "/var/log/nginx/error.log"
2.4、挂载日志
volumeMounts:
- mountPath: /var/log/nginx
name: nginx-log
- mountPath: /etc/localtime
name: localtime
volumes:
- hostPath:
path: /nfs/logs/nginx-ingress/
name: nginx-log
- hostPath:
path: /etc/localtime
name: localtime
2.5、重启一下Ingress查看一下效果
[root@k8s-m1 nginx-ingress]# ll /nfs/logs/nginx-ingress/
total 188
-rw-r--r-- 1 101 101 14229 Sep 11 17:30 access.log
-rw-r--r-- 1 101 101 539 Sep 12 14:01 error.log
lrwxrwxrwx 1 101 101 68 Sep 12 14:24 nginx-ingress-controller.ERROR -> nginx-ingress-controller.k8s-n3.www-data.log.ERROR.20210912-142434.6
lrwxrwxrwx 1 101 101 67 Sep 12 14:00 nginx-ingress-controller.INFO -> nginx-ingress-controller.k8s-n3.www-data.log.INFO.20210912-140059.6
-rw-r--r-- 1 101 101 8217 Sep 12 13:18 nginx-ingress-controller.k8s-n1.www-data.log.ERROR.20210911-135853.7
-rw-r--r-- 1 101 101 25589 Sep 12 13:18 nginx-ingress-controller.k8s-n1.www-data.log.INFO.20210911-134055.7
-rw-r--r-- 1 101 101 12091 Sep 12 13:18 nginx-ingress-controller.k8s-n1.www-data.log.WARNING.20210911-134055.7
-rw-r--r-- 1 101 101 5912 Sep 12 13:01 nginx-ingress-controller.k8s-n2.www-data.log.ERROR.20210911-135900.6
-rw-r--r-- 1 101 101 11022 Sep 12 13:01 nginx-ingress-controller.k8s-n2.www-data.log.INFO.20210911-133925.6
-rw-r--r-- 1 101 101 6190 Sep 12 13:01 nginx-ingress-controller.k8s-n2.www-data.log.WARNING.20210911-133925.6
-rw-r--r-- 1 101 101 7544 Sep 12 14:00 nginx-ingress-controller.k8s-n3.www-data.log.ERROR.20210911-135900.6
-rw-r--r-- 1 101 101 344 Sep 12 14:24 nginx-ingress-controller.k8s-n3.www-data.log.ERROR.20210912-142434.6
-rw-r--r-- 1 101 101 13627 Sep 12 14:00 nginx-ingress-controller.k8s-n3.www-data.log.INFO.20210911-134029.6
-rw-r--r-- 1 101 101 4120 Sep 12 14:24 nginx-ingress-controller.k8s-n3.www-data.log.INFO.20210912-140059.6
-rw-r--r-- 1 101 101 7822 Sep 12 14:00 nginx-ingress-controller.k8s-n3.www-data.log.WARNING.20210911-134029.6
-rw-r--r-- 1 101 101 622 Sep 12 14:24 nginx-ingress-controller.k8s-n3.www-data.log.WARNING.20210912-140059.6
-rw-r--r-- 1 101 101 7802 Sep 12 14:19 nginx-ingress-controller.k8s-n4.www-data.log.ERROR.20210911-140334.6
-rw-r--r-- 1 101 101 18859 Sep 12 14:19 nginx-ingress-controller.k8s-n4.www-data.log.INFO.20210911-133959.6
-rw-r--r-- 1 101 101 9427 Sep 12 14:19 nginx-ingress-controller.k8s-n4.www-data.log.WARNING.20210911-133959.6
lrwxrwxrwx 1 101 101 70 Sep 12 14:00 nginx-ingress-controller.WARNING -> nginx-ingress-controller.k8s-n3.www-data.log.WARNING.20210912-140059.6
3、带域名访问一下
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: lzulms-myapp
namespace: lzulms
spec:
rules:
- host: lzulmsconfig.chinaedu.net
http:
paths:
- backend:
serviceName: lzulms-myapp
servicePort: 80
path: /
cat /nfs/logs/nginx-ingress/access.log
[lzulmsconfig.chinaedu.net] [172.16.28.138] [-] [-] [11/Sep/2021:17:30:12 +0800] [GET /hostname.html HTTP/1.1] [200] [29] [0.003] [10.244.4.230:80] [0.002] [170314] [15] [1631352612.452] [/hostname.html] [29] [http://lzulmsconfig.chinaedu.net/] [Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36 Edg/93.0.961.38] [777] [-]