متقدم
البحث فيأدلة «كيف»
طريقة البحث
  • اكتب المهمة بكلماتك (find port، open firewall، disk full) أو الصق رسالة الخطأ بين علامتي تنصيص.
  • اختر «الإجابة السريعة» لترى الأوامر فقط، أو مجالًا لتبقى في مجال واحد.
  • "كلمتان" تجد العبارة بالضبط.
  • الكلمات العادية تجد الأسماء والنصوص والأمثلة.
  • ↑ ↓ للتنقل · Enter للفتح · Esc للإغلاق · / للبحث
جرّب:

كيف 129Kubernetes · Security

How to give a user read-only access with RBAC

Developers should see their app in production — Pods, logs, events — but not change or delete it, and not read Secrets. Kubernetes RBAC does this with a Role (what is allowed) and a RoleBinding (who gets it, where). This how-to gives a group read-only access to one namespace, adds log access, keeps Secrets out, and checks the result with kubectl auth can-i.

  • 5 الدروس
  • 1 الأوامر

هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.

$ مستخدم عادي# بصلاحية root (أو مع sudo)

129.1

The quick answer

kubectl create rolebinding devs-view -n shop --clusterrole=view --group=shop-devs
kubectl auth can-i list pods -n shop --as=alice --as-group=shop-devs
yes
kubectl auth can-i delete pods -n shop --as=alice --as-group=shop-devs
no
kubectl auth can-i get secrets -n shop --as=alice --as-group=shop-devs
no

The built-in **view ClusterRole reads almost everything in a namespace except Secrets**.

الأوامر في هذا الدرس

129.2

Step by step

  1. Use the built-in roles first:
ClusterRoleGives
viewRead most objects (no Secrets, no RBAC objects).
editRead and change most objects (no RBAC).
adminFull control in the namespace, also RoleBindings.
cluster-adminEverything everywhere — almost never for people.
  1. Bind to a group, not to single users — groups come from your identity provider (OIDC, Entra ID, IAM). Binding view with a RoleBinding limits it to that namespace (quick answer).
  2. Logs and port-forward? view already includes pods/log. For debugging extras, add a small Role:
rbac/devs-debug.yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: devs-debug
  namespace: shop
rules:
  - apiGroups: [""]
    resources: ["pods/portforward"]
    verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: devs-debug
  namespace: shop
subjects:
  - kind: Group
    name: shop-devs
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: devs-debug
  apiGroup: rbac.authorization.k8s.io

Do not give pods/exec in production lightly — it is a shell inside the app (and can read Secrets mounted there).

  1. Check before telling people "done" — impersonate them:
kubectl auth can-i --list -n shop --as=alice --as-group=shop-devs
kubectl auth can-i get pods/log -n shop --as=alice --as-group=shop-devs
  1. Service accounts (tools, CI): the same idea with --serviceaccount=NAMESPACE:NAME; give them only what they use, and turn off auto-mounted tokens where not needed.
  2. Review regularly: who has cluster-admin?
kubectl get clusterrolebindings -o jsonpath='{range .items[?(@.roleRef.name=="cluster-admin")]}{.metadata.name}{"  "}{.subjects[*].name}{"\n"}{end}'

الأوامر في هذا الدرس

129.3

Check that it worked

The user (or group) can list Pods, read logs and events in shop, and gets Forbidden for delete, edit, Secrets and other namespaces.

129.4

Common problems

ProblemCauseFix
Forbidden for everythingThe group name in the binding does not match the token's group.Check the group claim from your IdP; test with --as-group.
User can read SecretsUsed edit/admin, or a custom Role with secrets.Use view; remove secrets from custom roles.
Access in all namespacesA ClusterRoleBinding was used.Use a RoleBinding per namespace.
kubectl logs forbiddenMissing pods/log (custom role).Add pods/log with get.
Can not change a RoleBinding laterroleRef can not be edited.Delete and create the binding again.
A ServiceAccount has too many rightsOld bindings / default token.Review bindings; automountServiceAccountToken: false.

الأوامر في هذا الدرس

129.5

Go further

1Which built-in ClusterRole gives read access without Secrets?

view.

2How do you test what a user may do without logging in as them?

الأوامر في هذا الدرس

كل الأوامر في هذا الفصل