kakakakakku blog

Weekly Tech Blog: Keep on Learning!

kubelet が他ノードのラベルを操作しないように制限できる NodeRestriction を試した

Kubernetes で有効化できる Admission Plugin である「NodeRestriction」の動作確認をした.簡単にまとめておく!

「NodeRestriction」kubelet に対して Node / Pod の操作範囲を制限できる.具体例を挙げると kubelet によるラベル操作を自ノードに制限し,他ノードを拒否できる.また特定のプレフィックスを付けたラベルは kubelet によって操作できないようにも制限できる.詳しくは以下のドキュメントに載っている.

kubernetes.io

検証環境

今回は kubeadm を使って構築した Kubernetes v1.23.1 クラスタを使う.以下のように controlplanenode01 ノードで構成している.

$ kubectl get nodes
NAME           STATUS   ROLES                  AGE   VERSION
controlplane   Ready    control-plane,master   10d   v1.23.1
node01         Ready    <none>                 10d   v1.23.1

そして kube-apiserver--enable-admission-plugins オプションを確認すると「NodeRestriction」も有効化してある.

apiVersion: v1
kind: Pod
metadata:
  name: kube-apiserver
  namespace: kube-system
spec:
  containers:
  - command:
    - kube-apiserver
    - --authorization-mode=Node,RBAC
    - --enable-admission-plugins=NodeRestriction

(中略)

さらに今回試す構成図とラベル操作の関係を図にまとめた!

  • my-label=my-value
  • node-restriction.kubernetes.io/my-label=my-value

f:id:kakku22:20220412134309p:plain

検証 1 : ラベル操作

まず,controlplane ノードの kubelet の操作を前提にするために /etc/kubernetes/kubelet.conf を読み込んでおく.

controlplane $ export KUBECONFIG=/etc/kubernetes/kubelet.conf

さっそくラベル my-label=my-valuecontrolplanenode01 ノードに設定する.「NodeRestriction」を有効化しているため controlplane ノードは成功する.しかし node01 ノードは失敗する.

controlplane $ kubectl label node controlplane my-label=my-value
node/controlplane labeled

controlplane $ kubectl label node node01 my-label=my-value
Error from server (Forbidden): nodes "node01" is forbidden: node "controlplane" is not allowed to modify node "node01"

検証 2 : ラベル操作 (node-restriction.kubernetes.io/)

「NodeRestriction」を有効化していると node-restriction.kubernetes.io/ プレフィックスを付けたラベルの更新は「自ノードだとしても」拒否される.ドキュメントには「ワークロードを隔離するために予約されている」と書かれている.以下のように controlplane ノードでも失敗する.

controlplane $ kubectl label node controlplane node-restriction.kubernetes.io/my-label=my-value
Error from server (Forbidden): nodes "controlplane" is forbidden: is not allowed to modify labels: node-restriction.kubernetes.io/my-label

検証 3 : NodeRestriction を無効化する

最後は「NodeRestriction」を無効化する./etc/kubernetes/manifests/kube-apiserver.yamlkube-apiserver--enable-admission-plugins オプションを削除する.すると以下のように controlplanenode01 ノードで全てのラベル操作ができてしまう.

controlplane $ export KUBECONFIG=/etc/kubernetes/kubelet.conf

controlplane $ kubectl label node controlplane my-label=my-value
node/controlplane labeled

controlplane $ kubectl label node node01 my-label=my-value
node/node01 labeled

controlplane $ kubectl label node controlplane node-restriction.kubernetes.io/my-label=my-value
node/controlplane labeled

参考 : kube-bench

参考として「NodeRestriction」CIS Kubernetes Benchmark でも検知できる.kube-apiserver「NodeRestriction」を有効化していないと kube-bench で FAIL になる.

controlplane $ kube-bench

(中略)

[FAIL] 1.2.16 Ensure that the admission control plugin NodeRestriction is set (Automated)

(中略)

1.2.16 Follow the Kubernetes documentation and configure NodeRestriction plug-in on kubelets.
Then, edit the API server pod specification file /etc/kubernetes/manifests/kube-apiserver.yaml
on the master node and set the --enable-admission-plugins parameter to a
value that includes NodeRestriction.
--enable-admission-plugins=...,NodeRestriction,...

github.com

関連記事

kakakakakku.hatenablog.com