Helm 部署在 GKE 上的權限問題

Zz Chen
smalltownslowmedia
Published in
5 min readJun 3, 2018

最近試著把一些原本運行在 GKE 上的專案,試著用 Helm 來重整一下。馬上就碰到了 Tiller 操作 Kubernetes 時的權限問題,順手筆記一下希望讓大家可以繞過這個坑。

如果照著手冊使用 helm init安裝完 Helm 之後,使用 helm list 指令,會看到這樣的錯誤:

$ helm list
Error: configmaps is forbidden: User "system:serviceaccount:kube-system:default" cannot list configmaps in the namespace "kube-system": Unknown user "system:serviceaccount:kube-system:default"

如果仔細去看 helm init 幫我們安裝在 GKE 上的 Tiller 會發現權限問題:

$ kubectl logs -f --namespace=kube-system tiller-deploy-f9b8476d-ktzft[storage] 2018/05/31 08:32:50 listing all releases with filter
[storage/driver] 2018/05/31 08:32:50 list: failed to list: configmaps is forbidden: User "system:serviceaccount:kube-system:default" cannot list configmaps in the namespace "kube-system": Unknown user "system:serviceaccount:kube-system:default"

Helm 這麼方便的原因,主要就是因為 Tiller 常駐在 Kubernetes cluster 中,方便 Helm client 透過 Tiller 來操作 Kubernetes。而這個錯誤主因就是 Tiller 沒有權限對 Kubernetes 操作。找了一下相關的資料,解決方法就是在安裝 Tiller 之前,我們先建立一組對應權限的 Service Account,然後在安裝 Tiller 時,告訴他使用這個預先設定好的 Service Account 來操作 Kubernetes。

另外,因為要透過 Kubernetes 的 RBAC (role-based access control) 來建立 Service Account,所以需要先在 IAM console 中授權 Admin 權限給操作 kubectl 的這個 user,不然在下面建立 ClusterRole 的時候,也會有權限問題。

透過 IAM 授權 GKE Admin 權限

GCP Console > GCP IAM Console > 新增「Kubernetes Engine Admin」權限

Grant GKE Admin Role to user

準備 Service Account 的 Manifest

# setup-helm-rbac.yamlapiVersion: v1
kind: ServiceAccount
metadata:
name: tiller
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: tiller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: tiller
namespace: kube-system

建立 Service Account

$ kubectl create -f setup-helm-rbac.yaml

刪除原本的 Tiller

$ kubectl delete deployment tiller-deploy --namespace=kube-system
$ kubectl delete svc tiller-deploy --namespace=kube-system

重新初始化 Helm 並且指定要使用的 Service Account

$ helm init --service-account tiller

確定新的 Tiller pod 運行起來以後,再嘗試 helm list 就不會再出現錯誤囉!

[1] https://cloud.google.com/kubernetes-engine/docs/how-to/role-based-access-control

[2] https://github.com/kubernetes/helm/issues/2687#issuecomment-315791403

[3] http://jayunit100.blogspot.com/2017/07/helm-on.html

--

--

Zz Chen
smalltownslowmedia

熱血創業咖、熱愛開源專案,喜愛鑽研新技術,捲起袖子將新技術應用於日常專案的黑手