【kubectl status】リソースの状態を取得する

2022.12.17
2024.03.24
Kubernetes
kubectlkubectl-status

はじめに

kubectl statusを使って、Kubernetesリソースの状態を取得する方法を紹介します。

kubectl-status

kubectl statusは、Kubernetesのリソースの状態をわかりやすく表示してくれるkubectlプラグインです。

GitHub - bergerx/kubectl-status: A kubectl plugin to print a human-friendly output that focuses on the status fields of the resources in kubernetes.

GitHub - bergerx/kubectl-status: A kubectl plugin to print a human-friendly output that focuses on the status fields of the resources in kubernetes.

A kubectl plugin to print a human-friendly output that focuses on the status fields of the resources in kubernetes. - bergerx/kubectl-status

全てのリソースが対応しているわけではないですが、PodやDeployment、Serviceなどよく使いそうなものは対応しています。

下記のtemplateが対応しているリソースだと思います。

kubectl-status/pkg/plugin/templates at master · bergerx/kubectl-status

kubectl-status/pkg/plugin/templates at master · bergerx/kubectl-status

A kubectl plugin to print a human-friendly output that focuses on the status fields of the resources in kubernetes. - bergerx/kubectl-status

インストール

krewを使って、下記のコマンドからインストールできます。

1kubectl krew install status

krewについては、下記を参照してください。

【Kubernetes】Krewでkubectlのプラグインを管理する

【Kubernetes】Krewでkubectlのプラグインを管理する

はじめに kubectlのプラグインを管理できるKrewについて紹介していきます。 Krewと

使い方

基本的には下記のようにkubectl getと同じように使うことができます。

1kubectl status {resource}

例えばDeploymentの場合は下記になります。

1kubectl status deploy

ロールアウトの履歴やイベントのログなどが確認できます。

1❯ kubectl status deploy
2
3Deployment/busybox -n default, created 30m ago, gen:4 rev:4
4  Current: Deployment is available. Replicas: 1
5  Managed by busybox application
6  desired:1, existing:1, ready:1, updated:1, available:1
7  Available MinimumReplicasAvailable, Deployment has minimum availability. for 14m
8  Progressing NewReplicaSetAvailable, ReplicaSet "busybox-59c5767947" has successfully progressed. for 30m, last update was 8m ago
9  Rollouts: Use the `--include-rollout-diffs` flag to see the diff between each rollout!
10    30m ago, managed by ReplicaSet/busybox-56b5d787f4
11    9m ago, managed by ReplicaSet/busybox-6b4d4bb454
12    8m ago, managed by ReplicaSet/busybox-54cd57c4b6
13    8m ago, managed by ReplicaSet/busybox-59c5767947, has 1 replicas
14  Known/recorded manage events:
15    8m ago Updated by kubectl-client-side-apply (metadata, spec)
16    8m ago Updated by kube-controller-manager (metadata, status)
17  Events:
18    ScalingReplicaSet 30m ago from deployment-controller: Scaled up replica set busybox-56b5d787f4 to 1
19    ScalingReplicaSet 9m ago from deployment-controller: Scaled up replica set busybox-6b4d4bb454 to 1
20    ScalingReplicaSet 9m ago from deployment-controller: Scaled down replica set busybox-56b5d787f4 to 0 from 1
21    ScalingReplicaSet 8m ago from deployment-controller: Scaled up replica set busybox-54cd57c4b6 to 1
22    ScalingReplicaSet 8m ago from deployment-controller: Scaled down replica set busybox-6b4d4bb454 to 0 from 1
23    ScalingReplicaSet 8m ago from deployment-controller: Scaled up replica set busybox-59c5767947 to 1
24    ScalingReplicaSet 8m ago from deployment-controller: Scaled down replica set busybox-54cd57c4b6 to 0 from 1

Podの場合は、下記のようになります。

1kubectl status pod

Podに含まれるコンテナの状態やイベントのログなどが確認できます。

1❯ kubectl status pod
2
3Pod/busybox-59c5767947-qcp95 -n default, created 8m ago by ReplicaSet/busybox-59c5767947 Running BestEffort
4  Current: Pod is Ready
5  Managed by busybox application
6  PodScheduled -> Initialized -> ContainersReady -> Ready for 7m
7  Containers:
8    bb1 (busybox:1.34-glibc) Running for 8m and Ready
9    bb2 (busybox:1.35-musl) Running for 8m and Ready
10    bb3 (busybox:1.34-uclibc) Running for 7m and Ready
11  Known/recorded manage events:
12    8m ago Updated by kube-controller-manager (metadata, spec)
13    7m ago Updated by kubelet (status)
14  Events:
15    Scheduled 8m ago from default-scheduler: Successfully assigned default/busybox-59c5767947-qcp95 to docker-desktop
16    Pulled 8m ago from kubelet,docker-desktop: Container image "busybox:1.34-glibc" already present on machine
17    Created 8m ago from kubelet,docker-desktop: Created container bb1
18    Started 8m ago from kubelet,docker-desktop: Started container bb1
19    Pulled 8m ago from kubelet,docker-desktop: Container image "busybox:1.35-musl" already present on machine
20    Created 8m ago from kubelet,docker-desktop: Created container bb2
21    Started 8m ago from kubelet,docker-desktop: Started container bb2
22    Pulling 8m ago from kubelet,docker-desktop: Pulling image "busybox:1.34-uclibc"
23    Pulled 7m ago from kubelet,docker-desktop: Successfully pulled image "busybox:1.34-uclibc" in 4.718522272s
24    Created 7m ago from kubelet,docker-desktop: Created container bb3
25    Started 7m ago from kubelet,docker-desktop: Started container bb3

Deploymentのrevision間のdiff

個人的に自分が便利だと思ったオプションを紹介します。

--include-rollout-diffsオプションを使うことで、Deploymentのロールアウトごとの差分を表示してくれます。

1kubectl status deploy --include-rollout-diffs
1❯ kubectl status deploy --include-rollout-diffs
2
3Deployment/busybox -n default, created 39m ago, gen:4 rev:4
4  Current: Deployment is available. Replicas: 1
5  Managed by busybox application
6  desired:1, existing:1, ready:1, updated:1, available:1
7  Progressing NewReplicaSetAvailable, ReplicaSet "busybox-59c5767947" has successfully progressed. for 39m, last update was 17m ago
8  Available MinimumReplicasAvailable, Deployment has minimum availability. for 30s
9  Rollouts:
10    39m ago, managed by ReplicaSet/busybox-56b5d787f4
11    18m ago, managed by ReplicaSet/busybox-6b4d4bb454
12      Diff:
13      --- a ReplicaSet/busybox-56b5d787f4       2022-12-17 06:08:09 +0000 UTC (39m ago)
14      +++ b ReplicaSet/busybox-6b4d4bb454       2022-12-17 06:29:30 +0000 UTC (18m ago)
15      @@ -41,7 +41,7 @@
16                     "sleep",
17                     "9999"
18                   ],
19      -            "image": "busybox:1.34",
20      +            "image": "busybox:1.34-glibc",
21                   "imagePullPolicy": "IfNotPresent",
22                   "name": "bb1",
23                   "resources": {},
24
25    17m ago, managed by ReplicaSet/busybox-54cd57c4b6
26      Diff:
27      --- a ReplicaSet/busybox-6b4d4bb454       2022-12-17 06:29:30 +0000 UTC (18m ago)
28      +++ b ReplicaSet/busybox-54cd57c4b6       2022-12-17 06:30:08 +0000 UTC (17m ago)
29      @@ -53,7 +53,7 @@
30                     "sleep",
31                     "99999"
32                   ],
33      -            "image": "busybox:1.35",
34      +            "image": "busybox:1.35-musl",
35                   "imagePullPolicy": "IfNotPresent",
36                   "name": "bb2",
37                   "resources": {},
38
39    17m ago, managed by ReplicaSet/busybox-59c5767947, has 1 replicas
40      Diff:
41      --- a ReplicaSet/busybox-54cd57c4b6       2022-12-17 06:30:08 +0000 UTC (17m ago)
42      +++ b ReplicaSet/busybox-59c5767947       2022-12-17 06:30:46 +0000 UTC (17m ago)
43      @@ -65,7 +65,7 @@
44                     "sleep",
45                     "999"
46                   ],
47      -            "image": "busybox:1.34-glibc",
48      +            "image": "busybox:1.34-uclibc",
49                   "imagePullPolicy": "IfNotPresent",
50                   "name": "bb3",
51                   "resources": {},
52
53  Known/recorded manage events:
54    17m ago Updated by kubectl-client-side-apply (metadata, spec)
55    31s ago Updated by kube-controller-manager (metadata, status)
56  Events:
57    ScalingReplicaSet 39m ago from deployment-controller: Scaled up replica set busybox-56b5d787f4 to 1
58    ScalingReplicaSet 18m ago from deployment-controller: Scaled up replica set busybox-6b4d4bb454 to 1
59    ScalingReplicaSet 18m ago from deployment-controller: Scaled down replica set busybox-56b5d787f4 to 0 from 1
60    ScalingReplicaSet 17m ago from deployment-controller: Scaled up replica set busybox-54cd57c4b6 to 1
61    ScalingReplicaSet 17m ago from deployment-controller: Scaled down replica set busybox-6b4d4bb454 to 0 from 1
62    ScalingReplicaSet 17m ago from deployment-controller: Scaled up replica set busybox-59c5767947 to 1
63    ScalingReplicaSet 17m ago from deployment-controller: Scaled down replica set busybox-54cd57c4b6 to 0 from 1

参考

Support

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

buy me a coffee
Share

Profile

author

Masa

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

buy me a coffee