首页 新闻 会员 周边

k8s中如何授权 pod 内可以访问指定的 ConfigMap

0
悬赏园豆:30 [已解决问题] 解决于 2021-01-25 11:12

在一个 kubernetes 集群中,想授权 production 中的所有 pod 可以直接访问名为 appsettings.shared.json 的 ConfigMap(无需mount这个ConfigMap),请问如何实现?

k8s
dudu的主页 dudu | 高人七级 | 园豆:31003
提问于:2021-01-25 10:26
< >
分享
最佳答案
0

通过添加 Role 与 RoleBinding 搞定了

1)创建 Role 配置文件 configmap-reader-role.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  namespace: production
  name: configmap-reader
rules:
- apiGroups: [""] # "" indicates the core API group
  resourceNames:
    - appsettings.shared.json
  resources:
    - configmaps
  verbs:
    - get

2)在集群中添加该 Role

kubectl apply -f configmap-reader-role.yaml

3)创建 configmap-reader-rolebinding.yaml

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: read-configmap
  namespace: production
subjects:
- kind: ServiceAccount
  name: default
roleRef:
  kind: Role 
  name: configmap-reader # this must match the name of the Role you wish to bind to
  apiGroup: rbac.authorization.k8s.io

注:default 是 pod 的默认 ServiceAccount

4)在集群中添加该 RoleBinding

kubectl apply -f configmap-reader-rolebinding.yaml
dudu | 高人七级 |园豆:31003 | 2021-01-25 11:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册