ZBLOG

k8s设置token只能被特定POD使用

要让一个 Service Account Token 只能被特定的 Pod 使用,可以使用 Kubernetes 中的 RBAC(Role-Based Access Control)机制。以下是具体步骤:

  1. 创建一个拥有该 Service Account 权限的 Cluster Role 或 Role。
  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: ClusterRole
  3. metadata:
  4. name: pod-reader
  5. rules:
  6. - apiGroups: [""]
  7. resources: ["pods"]
  8. verbs: ["get", "watch", "list"]
  1. 创建一个绑定了该 Role 的 Cluster Role Binding 或 Role Binding,并指定对应的 Service Account。
  1. apiVersion: rbac.authorization.k8s.io/v1
  2. kind: ClusterRoleBinding
  3. metadata:
  4. name: pod-reader-binding
  5. roleRef:
  6. kind: ClusterRole
  7. name: pod-reader
  8. apiGroup: rbac.authorization.k8s.io
  9. subjects:
  10. - kind: ServiceAccount
  11. name: <service-account-name>
  12. namespace: <namespace>
  13. # 或者
  14. apiVersion: rbac.authorization.k8s.io/v1beta1
  15. kind: RoleBinding
  16. metadata:
  17. name : pod-reader-binding
  18. namespace : default
  19. roleRef:
  20. apiGroup : rbac.authorization.k8s.io
  21. kind : ClusterRole
  22. name : pod-reader
  23. subjects :
  24. - kind : ServiceAccount
  25. name : my-sa
  26. 3. 在需要使用该 Service Account Pod 中添加 annotation,以指定使用该 Service Account

apiVersion: v1 kind: Pod metadata:

  1. name: my-pod-with-sa-token
  2. annotations:
  3. kubernetes.io/service-account.name: <service-account-name>

spec:

  1. containers:
  2. - name: my-container
  3. image: nginx
  4. ports:
  5. - containerPort: 80

”`

注意,在上面的示例中,<service-account-name> 指定了该 Pod 所使用的 Service Account 的名称。此外,在绑定 Role 和 Service Account 的时候,可以根据需要指定命名空间。

通过以上步骤,只有在使用了该 annotation 的 Pod 才能使用该 Service Account Token 进行 API 访问。

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://golang.0voice.com/?id=5665

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?