如何创建 Kubernetes Service Account 并绑定 Cluster Role

关于Service Account,这里引用Kubernetes官网里的一句话:

服务账号(Service Account)为 Pod 中运行的进程提供身份标识, 并映射到 ServiceAccount 对象。当你向 API 服务器执行身份认证时, 你会将自己标识为某个用户(User)

简单来讲,Service Account就是当Pod内部进程需要与Api-Server通信时用于身份认证的账户。

在Pod里使用Service Account简单来说,步骤分4步:

  1. 创建一个Service Account;
  2. 创建一个Cluster Role;
  3. 创建一个RoleBinding将Service Account和Cluster Role关联起来;
  4. 在Pod配置文件里添加Service Account;

1. 在一个命名空间里创建一个Service Account

先创建一个命名空间。

arduino 复制代码
kubectl create namespace cluster-role-demo

在新创建的命名空间里创建一个Service Account。

lua 复制代码
kubectl create serviceaccount app-service-account -n cluster-role-demo

或者通过yaml文件的方式创建。

yaml 复制代码
apiVersion: v1 
kind: ServiceAccount 
metadata: 
  name: app-service-account 
  namespace: cluster-role-demo

2. 创建一个Cluster Role

如果Service Account需要获得整个集群所有命名空间下的资源权限,则需要创建Cluser Role,否则就创建Role即可。 下面是一份创建cluster Role的yaml文件:

vbnet 复制代码
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: app-cluster-role
  namespace: cluster-role-demo
rules:
  - apiGroups:
        - ""
        - apps
        - autoscaling
        - batch
        - extensions
        - policy
        - rbac.authorization.k8s.io
    resources:
      - pods
      - componentstatuses
      - configmaps
      - daemonsets
      - deployments
      - events
      - endpoints
      - horizontalpodautoscalers
      - ingress
      - jobs
      - limitranges
      - namespaces
      - nodes
      - pods
      - persistentvolumes
      - persistentvolumeclaims
      - resourcequotas
      - replicasets
      - replicationcontrollers
      - serviceaccounts
      - services
    verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]

上面yaml文件里apiGroups项配置的是这个Service Account可以访问哪些资源分组,resources项则是更细粒度的配置可以访问哪些资源。 可以使用下面的命令查看有哪些资源:

复制代码
kubectl api-resources

3. 创建ClusterRole Binding

前面我们分别创建了Service Account和Cluster Role,现在要将它们关联起来,这就需要再创建一个新资源ClusterRoleBinding。 使用下面的yaml文件:

yaml 复制代码
apiVersion: rbac.authorization.k8s.io/v1 
kind: ClusterRoleBinding 
metadata: name: app-cluster-role-binding 
subjects: 
- namespace: cluster-role-demo 
  kind: ServiceAccount 
  name: app-service-account 
roleRef: 
  apiGroup: rbac.authorization.k8s.io 
  kind: ClusterRole 
  name: app-cluster-role

4. 检查Service Account是否对某个资源有操作权限

csharp 复制代码
kubectl auth can-i get pods --as=system:serviceaccount:cluster-role-demo:app-service-account

执行后如果返回"yes",则说明有权限。

5. 在Pod里使用Service Account

如上图划红线处,这样就给Pod配置了Service Account。

当我们启动Pod后,进入当容器里面,跳转到上图的目录下,会看到红框框中的文件,这个文件就是对api-server发起请求时需要携带的请求认证Header:

然后我们按照下面的方法就可以向api-server发起请求了:

相关推荐
郝学胜-神的一滴5 分钟前
超越Spring的Summer(一): PackageScanner 类实现原理详解
java·服务器·开发语言·后端·spring·软件构建
Tony Bai8 分钟前
“Go 2,请不要发生!”:如果 Go 变成了“缝合怪”,你还会爱它吗?
开发语言·后端·golang
ghostwritten8 分钟前
春节前夕,运维的「年关」:用 Kubeowler 给集群做一次「年终体检」
运维·云原生·kubernetes
Victor35626 分钟前
Hibernate(91)如何在数据库回归测试中使用Hibernate?
后端
Victor35630 分钟前
MongoDB(1)什么是MongoDB?
后端
Victor3567 小时前
https://editor.csdn.net/md/?articleId=139321571&spm=1011.2415.3001.9698
后端
Victor3567 小时前
Hibernate(89)如何在压力测试中使用Hibernate?
后端
灰子学技术9 小时前
go response.Body.close()导致连接异常处理
开发语言·后端·golang
Gogo81610 小时前
BigInt 与 Number 的爱恨情仇,为何大佬都劝你“能用 Number 就别用 BigInt”?
后端
fuquxiaoguang10 小时前
深入浅出:使用MDC构建SpringBoot全链路请求追踪系统
java·spring boot·后端·调用链分析