ZBLOG

k8s给serviceAccount test绑定多个Secret

假设您已经创建了两个 Secret 对象,它们的名称分别为 secret1secret2。现在,您可以按照以下步骤将这些 Secret 绑定到名为 test 的 ServiceAccount:

  1. 创建一个 RoleBinding 对象,将 secret-reader 角色授权给 ServiceAccount。
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: test-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: test # the name of the ServiceAccount to bind to the role
  1. 创建两个 Secret 对象,并将它们绑定到 ServiceAccount。
apiVersion: v1 
kind: Secret 
metadata:
  name : secret1 
type : Opaque 
data :
   username : YWRtaW4= # base64-encoded value for "admin"

---

apiVersion: v1 
kind: Secret 
metadata:
  name : secret2 
type : Opaque 
data :
   password : MWYyZDFlMmU2N2Rm # base64-encoded value for "1f2d1e2e67df"

---

apiVersion: v1 
kind: ServiceAccount 
metadata:
  name : test 

---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding  
metadata:
  name : secret-test-binding  
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: secret-reader # the name of the role that grants access to Secrets 
subjects:
- kind: ServiceAccount
  name: test
  namespace: default

---

apiVersion: v1
kind: Pod 
metadata:
  name : test-pod 
spec:
  serviceAccountName: test
  containers:
    - image : nginx 
      name : nginx-container 

在上述 YAML 文件中,我们创建了两个 Secret 对象,分别命名为 secret1secret2。接着,我们创建一个名为 test 的 ServiceAccount,并使用 RoleBinding 将 secret-reader 角色授予该 ServiceAccount。

最后,我们创建一个 Pod 对象,并指定将使用 test ServiceAccount 运行它。这样,在该 Pod 中就可以访问绑定到 test ServiceAccount 上的所有 Secret 对象。

请注意,在 RoleBinding 或 ClusterRoleBinding 中指定的角色(例如上面的 secret-reader 角色)必须具有访问 Secret 对象的权限。如果您需要自定义角色,请确保正确设置权限以及限制对敏感数据的访问。

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

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

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

联系我们

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

微信号:3007537140

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

关注我们
x

注册

已经有帐号?