创建受控的config文件

在Kubernetes(K8s)中,可以通过使用 Role-Based Access Control(RBAC)来生成权限受控的配置(config)。

以下以agile-system命名空间为例

创建 ServiceAccount

kubectl create serviceaccount normal-user -n agile-system

创建 Role 或 ClusterRole

接下来,需要创建一个 Role 或 ClusterRole 对象,来定义一组权限。

Role 适用于命名空间级别的权限,而 ClusterRole 适用于整个集群级别的权限。可以使用 YAML 文件定义 Role 或 ClusterRole,例如:

---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: agile-system
  name: role-user1
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "list", "watch"]

创建 RoleBinding 或 ClusterRoleBinding

接下来,需要创建一个 RoleBinding 或 ClusterRoleBinding 对象,将 ServiceAccount 与 Role 或 ClusterRole 关联起来。

RoleBinding 用于命名空间级别的绑定,而 ClusterRoleBinding 用于整个集群级别的绑定。可以使用 YAML 文件定义 RoleBinding 或 ClusterRoleBinding,例如:

---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: rb-user1
  namespace: agile-system
subjects:
- kind: ServiceAccount
  name: normal-user
  namespace: agile-system
roleRef:
  kind: Role
  name: role-user1
  apiGroup: rbac.authorization.k8s.io

生成权限受控的配置

kubectl create --namespace agile-system --serviceaccount normal-user user1

最后更新于