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

2022.02.27
2024.03.24
Kubernetes
kubectl

本ページはAmazonアフィリエイトのリンクを含みます。

はじめに

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

Podのログ確認

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

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

1kubectl logs <pod name>
1❯ kubectl logs myapp-7b757d9494-9vvwl
2/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
3/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
4/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
510-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
610-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
7/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
8/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
9/docker-entrypoint.sh: Configuration complete; ready for start up
102022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
112022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
122022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
132022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
142022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
152022/02/27 04:38:58 [notice] 1#1: start worker processes
162022/02/27 04:38:58 [notice] 1#1: start worker process 32
172022/02/27 04:38:58 [notice] 1#1: start worker process 33
182022/02/27 04:38:58 [notice] 1#1: start worker process 34
192022/02/27 04:38:58 [notice] 1#1: start worker process 35
202022/02/27 04:38:58 [notice] 1#1: start worker process 36

直近の出力のみ表示

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

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

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

1kubectl logs --since=1h <pod name>
1❯ kubectl logs --since=1h myapp-7b757d9494-9vvwl
2/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
3/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
4/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
510-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
610-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
7/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
8/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
9/docker-entrypoint.sh: Configuration complete; ready for start up
102022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
112022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
122022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
132022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
142022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
152022/02/27 04:38:58 [notice] 1#1: start worker processes
162022/02/27 04:38:58 [notice] 1#1: start worker process 32
172022/02/27 04:38:58 [notice] 1#1: start worker process 33
182022/02/27 04:38:58 [notice] 1#1: start worker process 34
192022/02/27 04:38:58 [notice] 1#1: start worker process 35
202022/02/27 04:38:58 [notice] 1#1: start worker process 36

特定のコンテナのみ表示

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

1kubectl logs <pod name> -c <container name>
1❯ kubectl logs myapp-7b757d9494-9vvwl -c web-container
2/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
3/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
4/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
510-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
610-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
7/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
8/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
9/docker-entrypoint.sh: Configuration complete; ready for start up
102022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
112022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
122022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
132022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
142022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
152022/02/27 04:38:58 [notice] 1#1: start worker processes
162022/02/27 04:38:58 [notice] 1#1: start worker process 32
172022/02/27 04:38:58 [notice] 1#1: start worker process 33
182022/02/27 04:38:58 [notice] 1#1: start worker process 34
192022/02/27 04:38:58 [notice] 1#1: start worker process 35
202022/02/27 04:38:58 [notice] 1#1: start worker process 36

継続して表示

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

1kubectl logs -f <pod name>
1❯ kubectl logs -f myapp-7b757d9494-9vvwl
2/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
3/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
4/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
510-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
610-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
7/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
8/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
9/docker-entrypoint.sh: Configuration complete; ready for start up
102022/02/27 04:38:58 [notice] 1#1: using the "epoll" event method
112022/02/27 04:38:58 [notice] 1#1: nginx/1.21.6
122022/02/27 04:38:58 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
132022/02/27 04:38:58 [notice] 1#1: OS: Linux 5.10.76-linuxkit
142022/02/27 04:38:58 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
152022/02/27 04:38:58 [notice] 1#1: start worker processes
162022/02/27 04:38:58 [notice] 1#1: start worker process 32
172022/02/27 04:38:58 [notice] 1#1: start worker process 33
182022/02/27 04:38:58 [notice] 1#1: start worker process 34
192022/02/27 04:38:58 [notice] 1#1: start worker process 35
202022/02/27 04:38:58 [notice] 1#1: start worker process 36

ラベルを指定して表示

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

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

前のPodのログを表示

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

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

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

参考

Support

\ この記事が役に立ったと思ったら、サポートお願いします! /

buy me a coffee
Share

Profile

author

Masa

都内のIT企業で働くエンジニア
自分が学んだことをブログでわかりやすく発信していきながらスキルアップを目指していきます!

buy me a coffee