1.nginx开启debug日志需要修改nginx.conf文件,官方镜像对应文件路径为/etc/nginx/nginx.conf

将error_log  /var/log/nginx/error.log notice;

修改为 error_log  /var/log/nginx/error.log debug;

只修改以上配置然后重启pod会发现日志debug模式并不生效

2.镜像默认启动采用的二进制文件为nginx,通过执行nginx -V可以查看编译二进制文件的构建参数

# nginx -V
nginx version: nginx/1.24.0
built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4) 
built with OpenSSL 3.0.7 1 Nov 2022 (running with OpenSSL 3.0.9 30 May 2023)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer -g' --with-ld-opt=-Wl,--as-needed,-O1,--sort-common

 通过查看发现,nginx二进制文件构建并没有--with-debug参数也即不支持debug,所以仅在conf文件修改日志级别不会生效。

3.实际上nginx镜像文件中存在nginx-debug二进制文件,文件位于/usr/sbin/目录

# ls -l /usr/sbin/nginx*
-rwxr-xr-x    1 root     root       1206888 Apr 11  2023 /usr/sbin/nginx
-rwxr-xr-x    1 root     root       1309296 Apr 11  2023 /usr/sbin/nginx-debug

查看编译参数会发现,该文件才支持debug

# nginx-debug -V
nginx version: nginx/1.24.0
built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r4) 
built with OpenSSL 3.0.7 1 Nov 2022 (running with OpenSSL 3.0.9 30 May 2023)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-perl_modules_path=/usr/lib/perl5/vendor_perl --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-Os -fomit-frame-pointer -g' --with-ld-opt=-Wl,--as-needed,-O1,--sort-common --with-debug

4.最终解决方案,想要使debug日志级别生效,需要修改k8s的工作负载,指定nginx-debug来启动服务。以下为Deployment的参考修改点,command命令部分指定镜像使用nginx-debug来启动服务。

spec:
  template:
    spec:
      containers:
        - command:
            - nginx-debug
            - '-g'
            - daemon off;

至此,通过可以正常输出debug日志了

Logo

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

更多推荐