はじめに
Sternを使って複数コンテナで複数Podのログをまとめて確認してみたいと思います。
Sternとは
Starnとは、複数コンテナを持つPodや複数Podのログをまとめてtailコマンドのように確認できるツールです。
GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of https://github.com/wercker/stern
⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of - GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friend...
オリジナルのリポジトリは下記のようですが、issueにあるように、上記のリポジトリが現在はメンテされているみたいです。
GitHub - wercker/stern: ⎈ Multi pod and container log tailing for Kubernetes
⎈ Multi pod and container log tailing for Kubernetes - GitHub - wercker/stern: ⎈ Multi pod and container log tailing for Kubernetes
インストール
Macの場合、下記でインストール可能です。
brew install stern
GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of https://github.com/wercker/stern
⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of - GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friend...
簡単な使い方
基本的な使い方は下記の通りです。
stern [pod query] [flags]
[pod query]
では、正規表現でPodの指定ができます。正規表現なしで指定すると、ある程度同じ名前のPodが指定されます。
例えば下記のように指定すると、web-111
、web-222
、web-333
、web-backend
などのPodのログが同時に確認できます。
stern web
一方、正規表現で下記のように指定すると、web-backend
などと正規表現とマッチしたPodだけのログが確認できます。
stern "backend-\w"
-t
でタイプスタンプ付きで出力することなどもできます。
GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of https://github.com/wercker/stern
⎈ Multi pod and container log tailing for Kubernetes -- Friendly fork of - GitHub - stern/stern: ⎈ Multi pod and container log tailing for Kubernetes -- Friend...
実際に試してみる
実際にDeploymentを作成して、Podのログをまとめて確認してみます。
作成するDeploymentのマニフェストdeployment.yml
は下記の通りです。Podを5つ起動して、それぞれのPodには2つのコンテナが含まれています。
apiVersion: apps/v1
kind: Deployment
metadata:
name: stern-test
labels:
app: stern-test
spec:
replicas: 5
selector:
matchLabels:
app: stern-test
template:
metadata:
labels:
app: stern-test
spec:
containers:
- name: busybox-1
image: busybox
command: ["/bin/sh", "-c"]
args:
- |
while true
do
echo "busybox-1: $(hostname)"
sleep 20
done
- name: busybox-2
image: busybox
command: ["/bin/sh", "-c"]
args:
- |
while true
do
echo "busybox-2: $(hostname)"
sleep 20
done
Deploymentを作成します。
kubectl apply -f deployment.yml
5つのPodが起動しました。それぞれのPod名はstern-test-xxxxxxxx-yyyyy
となっています。
❯ kubectl get pod | grep stern-test
stern-test-b9486b67d-fwmkt 2/2 Running 0 25s
stern-test-b9486b67d-mhc6z 2/2 Running 0 25s
stern-test-b9486b67d-s7hs9 2/2 Running 0 25s
stern-test-b9486b67d-vffpz 2/2 Running 0 25s
stern-test-b9486b67d-xp9ch 2/2 Running 0 25s
stern stern-test
でログを確認するとPodごとに色分けされたログをまとめて確認できています。また、Podのどのコンテナからのログかもわかるようになっています。