ZBLOG

k8s给serviceAccount绑定多个Secret

在 Kubernetes 中,您可以为一个 ServiceAccount 绑定多个 Secret。

要将多个 Secret 绑定到一个 ServiceAccount,您需要创建一个 RoleBinding 或 ClusterRoleBinding,并将其绑定到 ServiceAccount 上。以下是一个例子:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: myapp-secrets
  namespace: default
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole # or just "Role" if you're using a non-cluster-scoped role
  name: secret-reader # the name of the role that grants access to Secrets
subjects:
- kind: ServiceAccount
  name: myapp-sa # the name of the ServiceAccount to bind to the role
---
apiVersion: v1
kind: Secret
metadata:
  name: my-secret1 # the first Secret to bind to the ServiceAccount
type: Opaque
data:
  username: YWRtaW4= # base64-encoded value for "admin"
---
apiVersion: v1 
kind: Secret 
metadata:
  name : my-secret2 # the second Secret to bind to the ServiceAccount 
type : Opaque 
data :
   password : MWYyZDFlMmU2N2Rm # base64-encoded value for "1f2d1e2e67df"

上面的 YAML 文件包括两个部分。第一部分是 RoleBinding 对象,它将 secret-reader 角色授权给名为 myapp-sa 的服务账户,并在默认命名空间中创建了一个名称为 myapp-secrets 的对象。第二部分是两个 Secret 对象,这些对象将绑定到 myapp-sa 服务账户。在这个例子中,我们有两个名称为 my-secret1my-secret2 的 Secrets。

请注意,在您创建 RoleBinding 或 ClusterRoleBinding 时,您需要指定一个角色(例如上面的 secret-reader 角色),该角色定义了对哪些 Kubernetes 资源的访问权限。然后,通过将 ServiceAccount 指定为主体,并将 RoleBinding 绑定到 ServiceAccount 上来授予该 ServiceAccount 对这些资源的访问权限。

如果您想给不同命名空间中的 ServiceAccount 绑定多个 Secret,则需要创建具有正确命名空间的 RoleBinding 或 ClusterRoleBinding,并将其绑定到相应命名空间中的 ServiceAccount 上。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?