kakakakakku blog

Weekly Tech Blog: Keep on Learning!

kubectl で Taint を一覧するコマンド例

Kubernetes でノードの Taint を確認するときに「一覧する」コマンドがなくて困るときがある.--show-labels オプションのように --show-taints オプションがあったら良いのに!例えば kubectl describe node xxx | grep Taints コマンドを実行すれば「ノードごとに」確認することはできるけど,ノードが多いと面倒だったりする.今回は個人的に使っているコマンド例をまとめる.もっとイイ Tips があったら教えて欲しいのだ💡

コマンド例

最初にコマンド例をまとめておく.実際に調べると -o custom-columns を使うコマンドもあれば -o go-template-o jsonpath を使うコマンドもある.また kubectl コマンドに依存せず jq でまとめるコマンドもある.普段使いをするためにあまり複雑にしたくなく,今回は -o custom-columns を使うコマンドと jq を使うコマンドを紹介する.

# -o custom-columns を使う
$ kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints

# jq を使う
$ kubectl get nodes -o json | jq '.items[]|{name:.metadata.name, taints:.spec.taints}'

フォーラムにも関連するスレッドがあった.

discuss.kubernetes.io

動作確認

今回は kind を使って構築した Kubernetes クラスタを使う.

$ kubectl get nodes
NAME                 STATUS   ROLES           AGE   VERSION
kind-control-plane   Ready    control-plane   15m   v1.24.0
kind-worker          Ready    <none>          15m   v1.24.0
kind-worker2         Ready    <none>          15m   v1.24.0
kind-worker3         Ready    <none>          15m   v1.24.0

あくまでサンプルとして kind-worker2kind-worker3Taint を付けておく.

$ kubectl taint nodes kind-worker2 gpu=true:NoSchedule
node/kind-worker2 tainted

$ kubectl taint nodes kind-worker3 gpu=true:NoSchedule
node/kind-worker3 tainted

-o custom-columns を使うと以下のように Taint を確認できる.map 表記だったりするけど,シンプルだと思う.

$ kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints
NAME                 TAINTS
kind-control-plane   [map[effect:NoSchedule key:node-role.kubernetes.io/master] map[effect:NoSchedule key:node-role.kubernetes.io/control-plane]]
kind-worker          <none>
kind-worker2         [map[effect:NoSchedule key:gpu value:true]]
kind-worker3         [map[effect:NoSchedule key:gpu value:true]]

jq を使うと縦長にはなるけど,結果としてはわかりやすく確認できる.やはり jq は便利!

$  kubectl get nodes -o json | jq '.items[]|{name:.metadata.name, taints:.spec.taints}'
{
  "name": "kind-control-plane",
  "taints": [
    {
      "effect": "NoSchedule",
      "key": "node-role.kubernetes.io/master"
    },
    {
      "effect": "NoSchedule",
      "key": "node-role.kubernetes.io/control-plane"
    }
  ]
}
{
  "name": "kind-worker",
  "taints": null
}
{
  "name": "kind-worker2",
  "taints": [
    {
      "effect": "NoSchedule",
      "key": "gpu",
      "value": "true"
    }
  ]
}
{
  "name": "kind-worker3",
  "taints": [
    {
      "effect": "NoSchedule",
      "key": "gpu",
      "value": "true"
    }
  ]
}

次は kind-worker3Taint を追加する.

$ kubectl taint nodes kind-worker3 spot=true:PreferNoSchedule
node/kind-worker3 tainted

-o custom-columns を使うと以下のように Taint を確認できる.右に長くなってしまうけどまだ許容範囲だと思う.

$ kubectl get nodes -o custom-columns=NAME:.metadata.name,TAINTS:.spec.taints
NAME                 TAINTS
kind-control-plane   [map[effect:NoSchedule key:node-role.kubernetes.io/master] map[effect:NoSchedule key:node-role.kubernetes.io/control-plane]]
kind-worker          <none>
kind-worker2         [map[effect:NoSchedule key:gpu value:true]]
kind-worker3         [map[effect:PreferNoSchedule key:spot value:true] map[effect:NoSchedule key:gpu value:true]]

Taint を追加しても jq だとわかりやすく確認できる.

$ kubectl get nodes -o json | jq '.items[]|{name:.metadata.name, taints:.spec.taints}'
{
  "name": "kind-control-plane",
  "taints": [
    {
      "effect": "NoSchedule",
      "key": "node-role.kubernetes.io/master"
    },
    {
      "effect": "NoSchedule",
      "key": "node-role.kubernetes.io/control-plane"
    }
  ]
}
{
  "name": "kind-worker",
  "taints": null
}
{
  "name": "kind-worker2",
  "taints": [
    {
      "effect": "NoSchedule",
      "key": "gpu",
      "value": "true"
    }
  ]
}
{
  "name": "kind-worker3",
  "taints": [
    {
      "effect": "PreferNoSchedule",
      "key": "spot",
      "value": "true"
    },
    {
      "effect": "NoSchedule",
      "key": "gpu",
      "value": "true"
    }
  ]
}

関連記事

kakakakakku.hatenablog.com