如何创建 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发起请求了:

相关推荐
魔尔助理顾问2 小时前
一个简洁高效的Flask用户管理示例
后端·python·flask
李长渊哦6 小时前
使用Druid连接池优化Spring Boot应用中的数据库连接
数据库·spring boot·后端
Marcel1116 小时前
WSL2使用Kind创建K8S集群时出现IPV6网络创建失败
云原生·kubernetes·kind
web135085886356 小时前
【Spring Boot】Spring AOP动态代理,以及静态代理
spring boot·后端·spring
nbsaas-boot7 小时前
Go 自动升级依赖版本
开发语言·后端·golang
zzyh1234567 小时前
springcloud的组件及作用
后端·spring·spring cloud
尚学教辅学习资料8 小时前
基于SpringBoot的图书借阅小程序+LW参考示例
spring boot·后端·小程序·java毕设·图书借阅
山海不说话8 小时前
从零搭建微服务项目Base(第5章——SpringBoot项目LogBack日志配置+Feign使用)
spring boot·后端·spring·spring cloud·微服务·logback
databook9 小时前
『Python底层原理』--CPython的变量实现机制
后端·python
初尘屿风9 小时前
小程序类毕业设计选题题目推荐 (29)
spring boot·后端·学习·微信·小程序·课程设计