【Kubernetes】Podのログ確認方法まとめ

スポンサーリンク

はじめに

KubernetesのPodのログの確認の仕方をまとめてみました。

Podのログ確認

最も基本的なPodのログの確認の仕方です。

Pod名を指定してログを出力します。

kubectl logs <pod name>
❯ kubectl logs myapp-7b757d9494-9vvwl
/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: 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
2022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
2022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
2022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:38:58 [notice] 1#1: start worker processes
2022/02/27 04:38:58 [notice] 1#1: start worker process 32
2022/02/27 04:38:58 [notice] 1#1: start worker process 33
2022/02/27 04:38:58 [notice] 1#1: start worker process 34
2022/02/27 04:38:58 [notice] 1#1: start worker process 35
2022/02/27 04:38:58 [notice] 1#1: start worker process 36

直近の出力のみ表示

直近の数行だけ出力したい時は--tailオプションを利用します。

kubectl logs --tail=10 <pod name>
❯ kubectl logs --tail=10 myapp-7b757d9494-9vvwl
2022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
2022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:38:58 [notice] 1#1: start worker processes
2022/02/27 04:38:58 [notice] 1#1: start worker process 32
2022/02/27 04:38:58 [notice] 1#1: start worker process 33
2022/02/27 04:38:58 [notice] 1#1: start worker process 34
2022/02/27 04:38:58 [notice] 1#1: start worker process 35
2022/02/27 04:38:58 [notice] 1#1: start worker process 36

また、時間で指定したい場合は--sinceオプションを利用します。

kubectl logs --since=1h <pod name>
❯ kubectl logs --since=1h myapp-7b757d9494-9vvwl
/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: 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
2022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
2022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
2022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:38:58 [notice] 1#1: start worker processes
2022/02/27 04:38:58 [notice] 1#1: start worker process 32
2022/02/27 04:38:58 [notice] 1#1: start worker process 33
2022/02/27 04:38:58 [notice] 1#1: start worker process 34
2022/02/27 04:38:58 [notice] 1#1: start worker process 35
2022/02/27 04:38:58 [notice] 1#1: start worker process 36

特定のコンテナのみ表示

Pod内の特定のコンテナのみログを出力したい場合は-cオプションでコンテナ名を指定します。

kubectl logs <pod name> -c <container name>
❯ kubectl logs myapp-7b757d9494-9vvwl -c web-container
/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: 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
2022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
2022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
2022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:38:58 [notice] 1#1: start worker processes
2022/02/27 04:38:58 [notice] 1#1: start worker process 32
2022/02/27 04:38:58 [notice] 1#1: start worker process 33
2022/02/27 04:38:58 [notice] 1#1: start worker process 34
2022/02/27 04:38:58 [notice] 1#1: start worker process 35
2022/02/27 04:38:58 [notice] 1#1: start worker process 36

継続して表示

継続してログを出力する場合は-fオプションを利用します。

kubectl logs -f <pod name>
❯ kubectl logs -f myapp-7b757d9494-9vvwl
/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: 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
2022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
2022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
2022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:38:58 [notice] 1#1: start worker processes
2022/02/27 04:38:58 [notice] 1#1: start worker process 32
2022/02/27 04:38:58 [notice] 1#1: start worker process 33
2022/02/27 04:38:58 [notice] 1#1: start worker process 34
2022/02/27 04:38:58 [notice] 1#1: start worker process 35
2022/02/27 04:38:58 [notice] 1#1: start worker process 36

ラベルを指定して表示

Pod名ではなく、ラベルを指定することで同じラベルをもつPodのログをまとめて出力できます。

kubectl logs -l <label>
❯ kubectl logs -l app=myapp
2022/02/27 04:39:00 [notice] 1#1: nginx/1.21.6
2022/02/27 04:39:00 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/02/27 04:39:00 [notice] 1#1: OS: Linux 5.10.76-linuxkit
2022/02/27 04:39:00 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/02/27 04:39:00 [notice] 1#1: start worker processes
2022/02/27 04:39:00 [notice] 1#1: start worker process 32
2022/02/27 04:39:00 [notice] 1#1: start worker process 33
2022/02/27 04:39:00 [notice] 1#1: start worker process 34
2022/02/27 04:39:00 [notice] 1#1: start worker process 35
2022/02/27 04:39:00 [notice] 1#1: start worker process 36

前のPodのログを表示

Pod内の以前のコンテナのログを確認したい場合は-pオプションを利用します。

CrashLoopBackOffになった場合に活用できます。

kubectl logs -p <pod name>
❯ kubectl logs -p dummy-pod
unable to retrieve container logs for docker://e5199e7519716fddfd4cebf0d70d329eb683c888d8c08f497d50db94fe600178

参考

タイトルとURLをコピーしました