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

相关推荐
Rust语言中文社区4 分钟前
【Rust日报】 CEL与Rust实现接近原生速度的解释执行
开发语言·后端·rust
武子康7 分钟前
大数据-247 离线数仓 - 电商分析 Hive 拉链表实战:订单历史状态增量刷新、闭链逻辑与错误排查
大数据·后端·apache hive
Natalia_Portman10 分钟前
springboot整合DolphinDB
java·数据库·spring boot·后端·db
小码哥_常18 分钟前
JWT从入门到精通:一文解锁生成、验证与防篡改秘籍
后端
小码哥_常19 分钟前
Spring Boot 实现分片上传+断点续传+实时进度条,彻底解决大文件上传痛点!
后端
壹米饭20 分钟前
QuestDB 磁盘满故障恢复实战指南
数据库·后端
程序员牛奶20 分钟前
把 Redis 持久化讲透:RDB、AOF、重写、恢复与生产选型
后端
我叫黑大帅25 分钟前
Go 标准库 net/http 包都能干嘛?
后端·面试·go
江畔何人初33 分钟前
Gateway API 的核心组件与作用
运维·网络·云原生·kubernetes·gateway
一只鹿鹿鹿44 分钟前
研发中心数据安全管理规定(文件)
java·运维·开发语言·数据库·后端