EN: Read in English
Dunia K6: Pengawal
Tanya dulu: kubectl auth can-i
Anda akan belajar: tanya kluster apa yang anda, atau bot, boleh buat, sebelum apa-apa rosak.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Kubernetes 1.37 pada 31 Okt 2026
Masalahnya
Kerja CI anda gagal pukul 2 pagi. Ini ralatnya.
Error from server (Forbidden): secrets is forbidden: User "system:serviceaccount:default:ci-bot" cannot list resource "secrets" in API group "" in the namespace "default"Macam mana anda boleh tahu sebelum ia berjalan?
Pilih satu jawapan.
Tengok
kind-dev namespace defaultkubectl auth can-i tanya pintu 2 soalan ya atau tidak. Ia tak ubah apa-apa. --as biar anda impersonate (menyamar) bot itu: tanya seolah-olah anda bot itu.
Cuba
Bina soalan: bolehkah bot patch deployment?
Ketik kepingan ikut turutan yang betul.
Teka
RoleBinding bot itu duduk dalam zon default. Bolehkah ia patch deployment dalam kube-system?
Pilih satu jawapan.
Cuba
Sekarang semak tekaan anda. Tanya kluster, sebagai bot, tentang kube-system.
kind-dev namespace defaultPetunjuk
-n kube-system pilih zonnya.Semak
Baca wayarnya. Adakah Kubernetes akan kata ya atau tidak?
Baca wayar-wayar ini. Kubernetes akan kata yes atau no?
Siapa (palam)
- ci-bot (ServiceAccount · ci)
- mei (pengguna)
Role (soket)
- Role deployer · shop
get, list, patch · deployments
- ClusterRole pod-reader · seluruh kluster
get, list · pods, pods/log
Wayar (binding)
- ci-bot ke deployerRoleBinding · dalam namespace shop
Tunjuk ci-bot-deployer sebagai YAML
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: ci-bot-deployer namespace: shop subjects: - kind: ServiceAccount name: ci-bot namespace: ci roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: deployer - mei ke pod-readerClusterRoleBinding · di semua tempat (ClusterRoleBinding)
Tunjuk mei-pod-reader sebagai YAML
apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRoleBinding metadata: name: mei-pod-reader subjects: - kind: User name: mei apiGroup: rbac.authorization.k8s.io roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: pod-reader
Boleh atau tidak? (kubectl auth can-i)
kubectl auth can-i patch deployments -n shop --as=system:serviceaccount:ci:ci-botkubectl auth can-i patch deployments -n default --as=system:serviceaccount:ci:ci-botkubectl auth can-i list pods -n ci --as=meikubectl auth can-i delete pods -n shop --as=mei
Tanya soalan anda sendiri
kubectl auth can-i get pods -n shop --as=system:serviceaccount:ci:ci-botno
Anda berjaya!
Kini anda boleh tanya kluster sebelum bertindak, sebagai sesiapa pun.
Nak tahu lebih (pilihan)
kubectl auth can-i --list --as=… senaraikan semua yang identiti itu boleh buat. can-i keluar dengan 0 untuk ya dan 1 untuk tidak, jadi skrip boleh gunanya. Untuk guna --as, anda sendiri perlu hak impersonate. Admin kluster memilikinya.
Misi sebenar
kubectl auth can-i create deploymentskubectl auth can-i list pods --as=system:serviceaccount:default:defaultkubectl auth can-i --list --as=system:serviceaccount:default:default
0 daripada 3 langkah siap · Tanda anda disimpan.
English
Your CI job crashed at 2 a.m. Here is the error.
Error from server (Forbidden): secrets is forbidden: User "system:serviceaccount:default:ci-bot" cannot list resource "secrets" in API group "" in the namespace "default"How could you have known before the run?
kubectl auth can-i asks gate 2 a yes or no question. It changes nothing. --as lets you impersonate the bot: ask as if you were it.
Build the question: may the bot patch deployments?
The bot’s RoleBinding lives in the default zone. May it patch deployments in kube-system?
Now check your guess. Ask the cluster, as the bot, about kube-system.
-n kube-system picks the zone.Read the wires. Will Kubernetes say yes or no?
You can now ask the cluster before you act, as anyone.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: kubectl auth can-i · Checking API access · User impersonation
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?