涅槃を目指す in はてな

人生に迷う様を書きます

Kubernetes The Hard Way On VirtualBox 11日目

Kubernetesを雰囲気で使わないための修行Kubernetes The Hard Way On VirtualBoxの11日目。

今日はRBACの設定です。

RBAC for Kubelet Authorization

今回はRBACの設定をして、Kubernetes API Serverが各workerノードのKubelet APIにアクセス出来るようにします。 Kubelet APIにアクセスするのは、メトリクスやログの収集やコマンドの実行に必要です。

Kubelet APIにアクセスし、Podの管理に関連する最も一般的なタスクを実行するための権限を持つsystem:kube-apiserver-to-kubelet ClusterRoleを作成します。

master-1,master-2で以下のコマンドを実行します。

cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: system:kube-apiserver-to-kubelet
rules:
  - apiGroups:
      - ""
    resources:
      - nodes/proxy
      - nodes/stats
      - nodes/log
      - nodes/spec
      - nodes/metrics
    verbs:
      - "*"
EOF

こんなエラーが出た

error: unable to recognize "STDIN": no matches for kind "ClusterRole" in version "rbac.authorization.k8s.io/v1beta1"

ClusterRoleがない?いやドキュメントにも有るし、そんな訳ないよな。

と思いきや、Deprecated API Migration Guide | Kubernetesによると

The rbac.authorization.k8s.io/v1beta1 API version of ClusterRole, ClusterRoleBinding, Role, and RoleBinding is no longer served as of v1.22.

とあるな。ワイのバージョンは

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T18:03:20Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"22", GitVersion:"v1.22.0", GitCommit:"c2b5237ccd9c0f1d600d3072634ca66cefdf272f", GitTreeState:"clean", BuildDate:"2021-08-04T17:57:25Z", GoVersion:"go1.16.6", Compiler:"gc", Platform:"linux/amd64"}

1.22だからダメなんか?

Migrate manifests and API clients to use the rbac.authorization.k8s.io/v1 API version, available since v1.8.

rbac.authorization.k8s.io/v1 を使うのか。

コマンドを以下のよう修正。

cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  annotations:
    rbac.authorization.kubernetes.io/autoupdate: "true"
  labels:
    kubernetes.io/bootstrapping: rbac-defaults
  name: system:kube-apiserver-to-kubelet
rules:
  - apiGroups:
      - ""
    resources:
      - nodes/proxy
      - nodes/stats
      - nodes/log
      - nodes/spec
      - nodes/metrics
    verbs:
      - "*"
EOF

実行結果

vagrant@master-1:~$ cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
> apiVersion: rbac.authorization.k8s.io/v1
> kind: ClusterRole
> metadata:
>   annotations:
>     rbac.authorization.kubernetes.io/autoupdate: "true"
>   labels:
>     kubernetes.io/bootstrapping: rbac-defaults
>   name: system:kube-apiserver-to-kubelet
> rules:
>   - apiGroups:
>       - ""
>     resources:
>       - nodes/proxy
>       - nodes/stats
>       - nodes/log
>       - nodes/spec
>       - nodes/metrics
>     verbs:
>       - "*"
> EOF
clusterrole.rbac.authorization.k8s.io/system:kube-apiserver-to-kubelet created

OKOK!

Kubernetes API Serverは、--kubelet-client-certificateフラグで定義されたクライアント証明書を使用して、system:kube-apiserverユーザーとしてKubeletに認証をかけます。

system:kube-apiserver-to-kubeletクラスターロールをsystem:kube-apiserverユーザにバインドさせます。

コレも先ほどと同様、公式はapiVersion: rbac.authorization.k8s.io/v1beta1でしたけど、apiVersion: rbac.authorization.k8s.io/v1に変えてます

cat <<EOF | kubectl apply --kubeconfig admin.kubeconfig -f -
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: system:kube-apiserver
  namespace: ""
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:kube-apiserver-to-kubelet
subjects:
  - apiGroup: rbac.authorization.k8s.io
    kind: User
    name: kube-apiserver
EOF

今日はここまで! お読み頂き有難うございました!