--- title: KubernetesのRBACでNamespace毎に専用ServiceAccountを作成 tags: ["Kubernetes", "RBAC"] categories: ["Dev", "CaaS", "Kubernetes", "RBAC"] date: 2018-02-14T17:59:14Z updated: 2018-02-14T18:50:31Z --- Kubernetesインストール後、Adminアカウントをみんなで共有する運用はありえないので、最低限`Namespace`毎に権限を絞る方法をメモ。 以下、[RBAC](https://kubernetes.io/docs/admin/authorization/rbac/)が有効になっていること前提。 > GKEの場合、“Legacy Authorization”をDisabledにする必要がある。 > ![rbac on gke](https://projectriff.io/images/rbac-on.png) `boby`という`ServiceAccount`が`blog`という`Namespace`でのみ開発できるようにするケースで説明。(自分の環境向けには`body`と`blog`を読みかえれば良い。) `Namespace`作成。 ``` kubectl create namespace blog ``` `ServiceAccount`を作成すると同時にこの`ServiceAccount`に`RoleBinding`を作成。Bindする`Role`または`ClusterRole`はここでは雑に`ClusterRole`の`edit`にする。(もう少し絞りたい場合はドキュメント参照) 上記を実現するため、次のコマンドで`blog-boby.yml`を作成する。 ``` yaml cat < blog-boby.yml apiVersion: v1 kind: ServiceAccount metadata: name: boby-sa namespace: blog --- kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: boby-rb namespace: blog roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: edit subjects: - kind: ServiceAccount name: boby-sa EOF ``` 設定する。 ``` kubectl apply -f blog-boby.yml ``` この`ServiceAccount`用のconfigファイルを生成するヘルパースクリプトとして、[zlabjp/kubernetes-scripts](https://github.com/zlabjp/kubernetes-scripts)で提供されている`create-kubeconfig`を使います。 ``` curl -LJOs https://github.com/zlabjp/kubernetes-scripts/raw/master/create-kubeconfig && chmod +x create-kubeconfig && sudo mv create-kubeconfig /usr/local/bin/ ``` config作成。 ``` create-kubeconfig boby-sa -n blog > config ``` 環境変数`KUBECONFIG`にこのファイルパスを指定して動作確認。 ``` $ KUBECONFIG=config kubectl get pod No resources found. $ KUBECONFIG=config kubectl auth can-i create pod yes $ KUBECONFIG=config kubectl get pod -n kube-system Error from server (Forbidden): pods is forbidden: User "system:serviceaccount:blog:boby-sa" cannot list pods in the namespace "kube-system": Unknown user "system:serviceaccount:blog:boby-sa" $ KUBECONFIG=config kubectl auth can-i create pod -n kube-system no - Unknown user "system:serviceaccount:blog:boby-sa" ``` > `Unable to connect to the server: x509: certificate is valid for kubernetes, kubernetes.default, kubernetes.default.svc, kubernetes.default.svc.cluster.local, not localhost `というようなエラーが出る場合は、 > > `config`ファイルの`certificate-authority-data: ...`の行を削除して、`insecure-skip-tls-verify: true`に変える。 > つまり次のようにする。 > > ``` yaml > # ... > clusters: > - cluster: > insecure-skip-tls-verify: true > server: https://your-master-ip-or-host:8443 > name: foobar > # ... > ``` 動作確認できたら、このファイルを開発者に渡し、`~/.kube/config`にコピーしてもらう。 ``` cp config ~/.kube/config ``` 複数の`Namespace`にBindしたい場合は、`RoleBinding`を複数定義する。 `ServiceAccount`および`RoleBinding`を削除したい場合は、 ``` kubectl delete -f blog-boby.yml ``` --- [CFCR (Cloud Foundry Container Runtime)](https://github.com/cloudfoundry-incubator/kubo-release/releases) 0.14.0ではOpenID Connect対応が入るはずなので、 リリースされたらUAAとの連携を行う。 * https://github.com/cloudfoundry-incubator/kubo-release/pull/101 * https://github.com/frodenas/uaa-k8s-oidc-helper