共计 1628 个字符,预计需要花费 5 分钟才能阅读完成。
在一个 Kubernetes 集群中,有时候我们希望授权特定的 Pod 可以直接访问某个 ConfigMap,而无需将这个 ConfigMap 挂载到 Pod 中。本文将介绍如何通过添加 Role 和 RoleBinding 来实现这一目标。
通过创建 Role 和 RoleBinding,我们可以精确地控制 Pod 对 ConfigMap 的访问权限,从而提高系统的安全性和灵活性。
解决方案
步骤 1:创建 Role 配置文件
首先,我们需要创建一个 Role 配置文件,用于定义允许访问 ConfigMap 的权限。创建一个名为 `configmap-reader-role.yaml` 的文件,并将以下内容添加到文件中:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: production
name: configmap-reader
rules:
- apiGroups: [""]
resourceNames:
- appsettings.shared.json
resources:
- configmaps
verbs:
- get
在上述配置中,我们定义了一个名为 `configmap-reader` 的 Role,它允许在 `production` 命名空间中的 Pod 获取名为 `appsettings.shared.json` 的 ConfigMap。
步骤 2:添加 Role 到集群中
使用以下命令将上一步中创建的 Role 添加到集群中:
kubectl apply -f configmap-reader-role.yaml
这样,Role 就会被创建并生效。
步骤 3:创建 RoleBinding 配置文件
接下来,我们需要创建一个 RoleBinding 配置文件,用于将之前创建的 Role 绑定到特定的 ServiceAccount 上。创建一个名为 `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
apiGroup: rbac.authorization.k8s.io
在上述配置中,我们定义了一个名为 `read-configmap` 的 RoleBinding,将之前创建的 Role(`configmap-reader`) 绑定到 `production` 命名空间下的默认 ServiceAccount。
步骤 4:添加 RoleBinding 到集群中
使用以下命令将上一步中创建的 RoleBinding 添加到集群中: 文章来源:https://www.toymoban.com/diary/problem/683.html
kubectl apply -f configmap-reader-rolebinding.yaml
这样,RoleBinding 就会被创建并生效。 文章来源地址 https://www.toymoban.com/diary/problem/683.html
到此这篇关于在 Kubernetes(k8s) 中如何授权 Pod 内可以访问指定的 ConfigMap 的文章就介绍到这了, 更多相关内容可以在右上角搜索或继续浏览下面的相关文章,希望大家以后多多支持 TOY 模板网!
原文地址:https://www.toymoban.com/diary/problem/683.html
如若转载,请注明出处:如若内容造成侵权 / 违法违规 / 事实不符,请联系站长进行投诉反馈,一经查实,立即删除!