当前位置:   article > 正文

Helm 用户指南-系列(7)-RBAC

helm rbac=true时启用的是v1bate1

RBAC-基于角色的访问控制


Kubernetes中,最佳的做法是,为特定的应用程序的服务帐户授予角色,确保应用程序在指定的范围内运行。要详细了解服务帐户权限请阅读官方Kubernetes文档.

Bitnami写了一个在集群中配置RBAC的指导,可让你了解RBAC基础知识。

我在网址 https://whmzsu.github.io/helm-doc-zh-cn/ 不断更新,同时也会搬运到这里,大家有兴趣加入https://github.com/whmzsu/helm-doc-zh-cn/的可以给我提交意见和建议。

本指南面向希望对Helm限制如下权限的用户:

  1. Tiller将资源安装到特定namespace能力
  2. 授权Helm客户端对Tiller实例的访问

Tiller和基于角色的访问控制

可以在配置Helm时使用--service-account <NAME>参数将服务帐户添加到Tiller 。前提条件是必须创建一个角色绑定,来指定预先设置的角色role和服务帐户service account 名称。

在前提条件下,并且有了一个具有正确权限的服务帐户,就可以像这样运行一个命令来初始化Tiller: helm init --service-account <NAME>

Example: 服务账户带有cluster-admin 角色权限

  1. $ kubectl create serviceaccount tiller --namespace kube-system
  2. serviceaccount "tiller" created

文件 rbac-config.yaml:

  1. apiVersion: v1
  2. kind: ServiceAccount metadata: name: tiller
  3. namespace: kube-system
  4. --- apiVersion: rbac.authorization.k8s.io/v1beta1
  5. kind: ClusterRoleBinding metadata: name: tiller
  6. roleRef: apiGroup: rbac.authorization.k8s.io
  7. kind: ClusterRole name: cluster-admin
  8. subjects: - kind: ServiceAccount name: tiller
  9. namespace: kube-system

Note: cluster-admin角色是在Kubernetes集群中默认创建的,因此不必再显式地定义它。.

  1. $ kubectl create -f rbac-config.yaml
  2. serviceaccount "tiller" created
  3. clusterrolebinding "tiller" created
  4. $ helm init --service-account tiller

在特定namespace中部署Tiller,并仅限于在该namespace中部署资源

在上面的例子中,我们让Tiller管理访问整个集群。当然,Tiller正常工作并不一定要为它设置集群管理员访问权限。我们可以指定Role和RoleBinding来将Tiller的范围限制为特定的namespace,而不是指定ClusterRole或ClusterRoleBinding。

  1. $ kubectl create namespace tiller-world
  2. namespace "tiller-world" created
  3. $ kubectl create serviceaccount tiller --namespace tiller-world
  4. serviceaccount "tiller" created

定义允许Tiller管理namespace tiller-world 中所有资源的角色 ,文件role-tiller.yaml:

  1. kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: name: tiller-manager
  3. namespace: tiller-world
  4. rules: - apiGroups: ["", "extensions", "apps"] resources: ["*"] verbs: ["*"]
  1. $ kubectl create -f role-tiller.yaml
  2. role "tiller-manager" created

文件 rolebinding-tiller.yaml,

  1. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: name: tiller-binding
  3. namespace: tiller-world
  4. subjects: - kind: ServiceAccount name: tiller
  5. namespace: tiller-world
  6. roleRef: kind: Role name: tiller-manager
  7. apiGroup: rbac.authorization.k8s.io
  1. $ kubectl create -f rolebinding-tiller.yaml
  2. rolebinding "tiller-binding" created

之后,运行helm init来在tiller-world namespace中安装Tiller 。

  1. $ helm init --service-account tiller --tiller-namespace tiller-world
  2. $HELM_HOME has been configured at /Users/awesome-user/.helm. Tiller (the Helm server side component) has been installed into your Kubernetes Cluster. Happy Helming!
  3. $ helm install nginx --tiller-namespace tiller-world --namespace tiller-world
  4. NAME: wayfaring-yak
  5. LAST DEPLOYED: Mon Aug 7 16:00:16 2017
  6. NAMESPACE: tiller-world
  7. STATUS: DEPLOYED
  8. RESOURCES: ==> v1/Pod
  9. NAME READY STATUS RESTARTS AGE
  10. wayfaring-yak-alpine 0/1 ContainerCreating 0 0s

Example: 在一个namespace中部署Tiller,并限制它在另一个namespace部署资源

在上面的例子中,我们让Tiller管理它部署所在的namespace。现在,让我们限制Tiller的范围,将资源部署在不同的namespace中!

下面例子中,让我们在myorg-system namespace中安装Tiller,并允许Tiller在myorg-users namespace中部署资源。

  1. $ kubectl create namespace myorg-system
  2. namespace "myorg-system" created
  3. $ kubectl create serviceaccount tiller --namespace myorg-system
  4. serviceaccount "tiller" created

role-tiller.yaml中,定义了一个允许Tiller管理所有myorg-users资源的角色:

  1. kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: name: tiller-manager
  3. namespace: myorg-users
  4. rules: - apiGroups: ["", "extensions", "apps"] resources: ["*"] verbs: ["*"]
  1. $ kubectl create -f role-tiller.yaml
  2. role "tiller-manager" created

将 service account 与那个role绑定. rolebinding-tiller.yaml,

  1. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: name: tiller-binding
  3. namespace: myorg-users
  4. subjects: - kind: ServiceAccount name: tiller
  5. namespace: myorg-system
  6. roleRef: kind: Role name: tiller-manager
  7. apiGroup: rbac.authorization.k8s.io
  1. $ kubectl create -f rolebinding-tiller.yaml
  2. rolebinding "tiller-binding" created

我们还需要授予Tiller访问权限来读取myorg-system中的configmaps,以便它可以存储release信息。如 role-tiller-myorg-system.yaml:

  1. kind: Role apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: namespace: myorg-system
  3. name: tiller-manager
  4. rules: - apiGroups: ["", "extensions", "apps"] resources: ["configmaps"] verbs: ["*"]
  1. $ kubectl create -f role-tiller-myorg-system.yaml
  2. role "tiller-manager" created

相应的role 绑定. 如 rolebinding-tiller-myorg-system.yaml:

  1. kind: RoleBinding apiVersion: rbac.authorization.k8s.io/v1beta1
  2. metadata: name: tiller-binding
  3. namespace: myorg-system
  4. subjects: - kind: ServiceAccount name: tiller
  5. namespace: myorg-system
  6. roleRef: kind: Role name: tiller-manager
  7. apiGroup: rbac.authorization.k8s.io
  1. $ kubectl create -f rolebinding-tiller-myorg-system.yaml
  2. rolebinding "tiller-binding" created

Helm 和基于角色的访问控制

在pod中运行Helm客户端时,为了让Helm客户端与Tiller实例进行通信,需要授予某些特权。具体来说,Helm客户端需要能够创建pods,转发端口并能够在Tiller运行的namespace中列出pod(这样它才可以找到Tiller)。

Example: 在一个namespace中部署helm,与在另一个namespace中与Tiller交互

在这个例子中,我们将假设Tiller在名为tiller-world 的namespace中运行,并且Helm客户端在helm-world中运行。默认情况下,Tiller在kube-system namespace中运行。

helm-user.yaml:

  1. apiVersion: v1
  2. kind: ServiceAccount metadata: name: helm
  3. namespace: helm-world
  4. --- apiVersion: rbac.authorization.k8s.io/v1beta1
  5. kind: Role metadata: name: tiller-user
  6. namespace: tiller-world
  7. rules: - apiGroups: - "" resources: - pods/portforward
  8. verbs: - create
  9. - apiGroups: - "" resources: - pods
  10. verbs: - list
  11. --- apiVersion: rbac.authorization.k8s.io/v1beta1
  12. kind: RoleBinding metadata: name: tiller-user-binding
  13. namespace: tiller-world
  14. roleRef: apiGroup: rbac.authorization.k8s.io
  15. kind: Role name: tiller-user
  16. subjects: - kind: ServiceAccount name: helm
  17. namespace: helm-world
  1. $ kubectl create -f helm-user.yaml
  2. serviceaccount "helm" created
  3. role "tiller-user" created
  4. rolebinding "tiller-user-binding" created
本文转自kubernetes中文社区- Helm 用户指南-系列(7)-RBAC
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/246483
推荐阅读
相关标签
  

闽ICP备14008679号