pod에 문제가 생겨 지금 즉시 로그를 확인해야할 것같다.
어떻게 바로 확인할 수 있을까?
개념 이해를 위해 nginx 이미지의 pod를 띄워보자.
kubectl run po --image=nginx
kubectl logs po
>>>
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2025/07/14 01:36:22 [notice] 1#1: using the "epoll" event method
2025/07/14 01:36:22 [notice] 1#1: nginx/1.29.0
2025/07/14 01:36:22 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14+deb12u1)
2025/07/14 01:36:22 [notice] 1#1: OS: Linux 6.10.14-linuxkit
2025/07/14 01:36:22 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2025/07/14 01:36:22 [notice] 1#1: start worker processes
2025/07/14 01:36:22 [notice] 1#1: start worker process 29
2025/07/14 01:36:22 [notice] 1#1: start worker process 30
2025/07/14 01:36:22 [notice] 1#1: start worker process 31
2025/07/14 01:36:22 [notice] 1#1: start worker process 32
2025/07/14 01:36:22 [notice] 1#1: start worker process 33
2025/07/14 01:36:22 [notice] 1#1: start worker process 34
2025/07/14 01:36:22 [notice] 1#1: start worker process 35
2025/07/14 01:36:22 [notice] 1#1: start worker process 36
2025/07/14 01:36:22 [notice] 1#1: start worker process 37
2025/07/14 01:36:22 [notice] 1#1: start worker process 38
2025/07/14 01:36:22 [notice] 1#1: start worker process 39
2025/07/14 01:36:22 [notice] 1#1: start worker process 40
파드로 들어가서 curl 명령어를 사용하고 pod에 로그가 찍히는지 보도록 하자.
### pod 내부로 이동
k exec po -it -- /bin/bash
### 명령어 수행
root@po:/# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
### pod에서 나오기 (node로 이동)
root@po:/# exit
exit
### 로그 출력
k logs po
>>
....
...
..
..
127.0.0.1 - - [14/Jul/2025:01:38:54 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-" ### 여기에 curl 명령어에 대한 로그가 나온다.
root@po:/# cd /var/log/
root@po:/var/log# cd nginx/
root@po:/var/log/nginx# ls -l
total 0
lrwxrwxrwx 1 root root 11 Jul 1 03:10 access.log -> /dev/stdout
lrwxrwxrwx 1 root root 11 Jul 1 03:10 error.log -> /dev/stderr
pod 내부에서 nginx 로그를 보면 access.log, error.log가 /dev/stdout. /dev/stderr에
심볼릭 링크가 걸려있다. 왜 심볼릭 링크가 걸려있냐면,
# forward request and error logs to docker log collector
&& ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log \
이렇게 nginx의 도커파일에 심볼릭 링크가 걸리도록 되어있기 때문이다.
Nginx 도커파일
/dev/stdout가 어디로 링크 되어있는지를 보자.
lrwxrwxrwx 1 root root 15 Jul 14 01:36 /dev/stdout -> /proc/self/fd/1
여기서 proc는 프로세스를 의미하고, self란 pid 1번을 말한다.
원래 리눅스의 pid 1번은 기본적으로 systemd이지만, 도커 컨테이너에서는 다르다.
root@po:/var/log/nginx# apt udpate
root@po:/var/log/nginx# apt install procps -y
root@po:/var/log/nginx# ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 01:36 ? 00:00:00 nginx: master process nginx -g daemon off;
nginx 29 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 30 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 31 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 32 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 33 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 34 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 35 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 36 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 37 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 38 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 39 1 0 01:36 ? 00:00:00 nginx: worker process
nginx 40 1 0 01:36 ? 00:00:00 nginx: worker process
root 48 0 0 01:40 pts/0 00:00:00 /bin/bash
root 243 48 0 01:47 pts/0 00:00:00 ps -ef
즉, 1번 pid의 프로세스를 말하고 fd/1이라는 것은 stdout을 의미한다.
stdin은 0, stdout은 1 stderr는 2이다.
그렇다면 /proc/self/fd/1을 한 번 더 보자
root@po:/var/log/nginx# ls -al /proc/1/fd/1
l-wx------ 1 root root 64 Jul 14 01:36 /proc/1/fd/1 -> 'pipe:[286870]'
컨테이너 로그를 수집하거나 systemd에서 journald 로그를 수집하거나 등등
root@po:/var/log/nginx# echo test log > test.log
root@po:/var/log/nginx# ls
access.log error.log test.log
root@po:/var/log/nginx# cat test.log
test log
root@po:/var/log/nginx# exit
exit
k logs po
>>>
...
...
..
..
2025/07/14 01:36:22 [notice] 1#1: start worker process 40
127.0.0.1 - - [14/Jul/2025:01:38:54 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
test 로그를 만들고 파드의 로그를 찍어보면 아무 것도 나오지 않는다. 왜냐?
표준 출력, 표준 에러로 심볼릭 링크가 되어있지 않기 때문이다. <- 이게 매우매우 중요하다.
k exec po -it -- /bin/bash
>>>
root@po:/# echo Mytest! >> /proc/1/fd/1
root@po:/# exit
exit
k logs po
>>>
2025/07/14 01:36:22 [notice] 1#1: start worker process 37
2025/07/14 01:36:22 [notice] 1#1: start worker process 38
2025/07/14 01:36:22 [notice] 1#1: start worker process 39
2025/07/14 01:36:22 [notice] 1#1: start worker process 40
127.0.0.1 - - [14/Jul/2025:01:38:54 +0000] "GET / HTTP/1.1" 200 615 "-" "curl/7.88.1" "-"
Mytest!
오 마이갓 Mytest! 로그가 /proc/1/fd/1 표준 출력으로 들어가서 파드 로그에 찍혔다.