【kubectl 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.
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...

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

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

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. - kubectl-status/pkg/plugin/templates at mas...

インストール

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

kubectl krew install status

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

【Kubernetes】Krewでkubectlのプラグインを管理する
はじめにkubectlのプラグインを管理できるKrewについて紹介していきます。KrewとはKrewは、kubectlコマンドのプラグインマネージャーになります。kubectlのプラグインの検索やインストール、アップデートが楽にで...

使い方

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

kubectl status {resource}

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

kubectl status deploy

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

❯ kubectl status deploy

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

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

kubectl status pod

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

❯ kubectl status pod

Pod/busybox-59c5767947-qcp95 -n default, created 8m ago by ReplicaSet/busybox-59c5767947 Running BestEffort
  Current: Pod is Ready
  Managed by busybox application
  PodScheduled -> Initialized -> ContainersReady -> Ready for 7m
  Containers:
    bb1 (busybox:1.34-glibc) Running for 8m and Ready
    bb2 (busybox:1.35-musl) Running for 8m and Ready
    bb3 (busybox:1.34-uclibc) Running for 7m and Ready
  Known/recorded manage events:
    8m ago Updated by kube-controller-manager (metadata, spec)
    7m ago Updated by kubelet (status)
  Events:
    Scheduled 8m ago from default-scheduler: Successfully assigned default/busybox-59c5767947-qcp95 to docker-desktop
    Pulled 8m ago from kubelet,docker-desktop: Container image "busybox:1.34-glibc" already present on machine
    Created 8m ago from kubelet,docker-desktop: Created container bb1
    Started 8m ago from kubelet,docker-desktop: Started container bb1
    Pulled 8m ago from kubelet,docker-desktop: Container image "busybox:1.35-musl" already present on machine
    Created 8m ago from kubelet,docker-desktop: Created container bb2
    Started 8m ago from kubelet,docker-desktop: Started container bb2
    Pulling 8m ago from kubelet,docker-desktop: Pulling image "busybox:1.34-uclibc"
    Pulled 7m ago from kubelet,docker-desktop: Successfully pulled image "busybox:1.34-uclibc" in 4.718522272s
    Created 7m ago from kubelet,docker-desktop: Created container bb3
    Started 7m ago from kubelet,docker-desktop: Started container bb3

Deploymentのrevision間のdiff

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

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

kubectl status deploy --include-rollout-diffs
❯ kubectl status deploy --include-rollout-diffs

Deployment/busybox -n default, created 39m ago, gen:4 rev:4
  Current: Deployment is available. Replicas: 1
  Managed by busybox application
  desired:1, existing:1, ready:1, updated:1, available:1
  Progressing NewReplicaSetAvailable, ReplicaSet "busybox-59c5767947" has successfully progressed. for 39m, last update was 17m ago
  Available MinimumReplicasAvailable, Deployment has minimum availability. for 30s
  Rollouts:
    39m ago, managed by ReplicaSet/busybox-56b5d787f4
    18m ago, managed by ReplicaSet/busybox-6b4d4bb454
      Diff:
      --- a ReplicaSet/busybox-56b5d787f4       2022-12-17 06:08:09 +0000 UTC (39m ago)
      +++ b ReplicaSet/busybox-6b4d4bb454       2022-12-17 06:29:30 +0000 UTC (18m ago)
      @@ -41,7 +41,7 @@
                     "sleep",
                     "9999"
                   ],
      -            "image": "busybox:1.34",
      +            "image": "busybox:1.34-glibc",
                   "imagePullPolicy": "IfNotPresent",
                   "name": "bb1",
                   "resources": {},

    17m ago, managed by ReplicaSet/busybox-54cd57c4b6
      Diff:
      --- a ReplicaSet/busybox-6b4d4bb454       2022-12-17 06:29:30 +0000 UTC (18m ago)
      +++ b ReplicaSet/busybox-54cd57c4b6       2022-12-17 06:30:08 +0000 UTC (17m ago)
      @@ -53,7 +53,7 @@
                     "sleep",
                     "99999"
                   ],
      -            "image": "busybox:1.35",
      +            "image": "busybox:1.35-musl",
                   "imagePullPolicy": "IfNotPresent",
                   "name": "bb2",
                   "resources": {},

    17m ago, managed by ReplicaSet/busybox-59c5767947, has 1 replicas
      Diff:
      --- a ReplicaSet/busybox-54cd57c4b6       2022-12-17 06:30:08 +0000 UTC (17m ago)
      +++ b ReplicaSet/busybox-59c5767947       2022-12-17 06:30:46 +0000 UTC (17m ago)
      @@ -65,7 +65,7 @@
                     "sleep",
                     "999"
                   ],
      -            "image": "busybox:1.34-glibc",
      +            "image": "busybox:1.34-uclibc",
                   "imagePullPolicy": "IfNotPresent",
                   "name": "bb3",
                   "resources": {},

  Known/recorded manage events:
    17m ago Updated by kubectl-client-side-apply (metadata, spec)
    31s ago Updated by kube-controller-manager (metadata, status)
  Events:
    ScalingReplicaSet 39m ago from deployment-controller: Scaled up replica set busybox-56b5d787f4 to 1
    ScalingReplicaSet 18m ago from deployment-controller: Scaled up replica set busybox-6b4d4bb454 to 1
    ScalingReplicaSet 18m ago from deployment-controller: Scaled down replica set busybox-56b5d787f4 to 0 from 1
    ScalingReplicaSet 17m ago from deployment-controller: Scaled up replica set busybox-54cd57c4b6 to 1
    ScalingReplicaSet 17m ago from deployment-controller: Scaled down replica set busybox-6b4d4bb454 to 0 from 1
    ScalingReplicaSet 17m ago from deployment-controller: Scaled up replica set busybox-59c5767947 to 1
    ScalingReplicaSet 17m ago from deployment-controller: Scaled down replica set busybox-54cd57c4b6 to 0 from 1

参考

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