真题解析2
题目
Context
绑定到 Deployment 的 ServiceAccount 的 Role 授予过度宽松的权限,完成以下项目以减少权限集。
Task
一个名为 nginx 的现有 Deployment 已在 namespace monitoring 中运行。
编辑绑定到 Deployment 的 ServiceAccount test-sa-3 的现有 Role,仅允许只对 endpoints 类型的资源执行 get 操作。
在 namespace monitoring 中创建一个名为 role-2 ,并仅允许对 namespaces 类型的资源执行 delete 操作的新 Role。
创建一个名为 role-2-binding 的新 RoleBinding,将 role-2 绑定到 Deployment 的 ServiceAccount。
解析
查询 SA test-sa-3 绑定的是哪个 Role(若没有可以直接 kubectl get role -n monitoring)
kubectl get rolebinding -n monitoring -o yaml | grep -i test-sa-3 -C 6
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: web-role
subjects:
- kind: ServiceAccount
name: test-sa-3
namespace: monitoring
kind: List
metadata:
resourceVersion: ""根据查询到的 Role ,编辑 Role 的权限
kubectl edit role web-role -n monitoring
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
creationTimestamp: "2024-07-30T16:35:31Z"
name: web-role
namespace: monitoring
resourceVersion: "283204"
uid: 5074362c-e822-444c-810e-c0fa2d06ef2e
rules:
- apiGroups:
- "" # 根据 kubectl api-resources 查看所操作资源具体属于哪个 API 组
resources:
- endpoints # 修改为 endpoints
verbs:
- get # 修改为 get创建 role-2
# vim role-2.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: monitoring
name: role-2
rules:
- apiGroups: [""] # "" 标明 core API 组
resources: ["namespaces"]
verbs: ["delete"]
kubectl create -f role-2.yaml
# kubectl create role role-2 --verb=delete --resource=namespaces -n monitoring创建 role-2-binding
kubectl create rolebinding role-2-binding \
--role=role-2 \
--serviceaccount=monitoring:test-sa-3 \
-n monitoringLast updated