EN: Read in English
Dunia K6: Pengawal
RBAC: role dan binding
Anda akan belajar: cara beri orang atau bot satu kad tugas untuk satu zon, tidak lebih.
Teks BM belum disemak oleh penutur asli.
Disemak dengan Kubernetes 1.37 pada 31 Okt 2026
Masalahnya
Bot CI anda cuma kemas kini satu benda: Deployment shop. Hari ini ia pegang kunci induk seluruh kluster. Kunci apa patut ia bawa?
Pilih satu jawapan.
Tepat apa yang perlu, tidak lebih. Itulah least privilege: Beri setiap pekerja kunci untuk stesennya sahaja. Tiada lebih. (hak paling sedikit).
Tengok
Kubernetes kenal dua jenis “siapa”. Orang bawa ID sendiri. Robot dapat lencana staf: ServiceAccount: Lencana staf untuk robot: identiti yang pod atau bot CI guna dengan API.. Asingkan mereka.
Seret kad ke dalam kotak. Atau ketik kad, kemudian ketik kotak.
Tengok
kind-dev namespace defaultRole: Kad tugas: kata kerja apa pada benda apa, dalam satu zon. ClusterRole berfungsi di semua zon. ialah kad tugas. Ia senaraikan kata kerja, seperti patch, pada benda, seperti deployments. Ia hanya berfungsi dalam satu zon.
Cuba
Kad sahaja tak buat apa-apa. RoleBinding: Pin kad tugas pada orang atau bot, dalam satu zon. ClusterRoleBinding pin di semua tempat. pin kad pada lencana, dalam satu zon. Sambungkan bot CI pada deployer, dalam shop sahaja.
Sambung wayar sehingga setiap jawapan sepadan. Beri yang perlu sahaja.
Siapa (palam)
- ci-bot (ServiceAccount · ci)
- mei (pengguna)
Role (soket)
- Role deployer · shop
get, list, patch · deployments
- ClusterRole cluster-admin · seluruh kluster
* · *
Wayar (binding)
Belum ada wayar. Tiada siapa boleh buat apa-apa.
Boleh atau tidak? (kubectl auth can-i)
kubectl auth can-i patch deployments -n shop --as=system:serviceaccount:ci:ci-botnosepatutnya yes belum lagiTiada wayar yang beri hak ini.kubectl auth can-i patch deployments -n default --as=system:serviceaccount:ci:ci-botnosepatutnya no sepadanTiada wayar yang beri hak ini.kubectl auth can-i get secrets -n shop --as=system:serviceaccount:ci:ci-botnosepatutnya no sepadanTiada wayar yang beri hak ini.
Tanya soalan anda sendiri
kubectl auth can-i get pods -n shop --as=system:serviceaccount:ci:ci-botno
Petunjuk
Siapa: ci-bot. Role: deployer. Di mana: shop.Tengok
Role berfungsi dalam satu zon. ClusterRole berfungsi di semua zon, dan untuk benda tanpa zon, seperti node. Asingkan ini.
Seret kad ke dalam kotak. Atau ketik kad, kemudian ketik kotak.
Cuba
Sekarang di terminal. Apply kad tugas. Kemudian pin pada ci-bot dengan RoleBinding.
kind-dev namespace defaultPetunjuk
--serviceaccount ambil zon:nama, contohnya default:ci-bot.Teka
RBAC tiada peraturan “tolak”. Bot ada dua RoleBinding. Satu benarkan padam pod. Satu lagi tidak. Bolehkah ia padam pod?
Pilih satu jawapan.
Anda berjaya!
Kini anda boleh beri bot satu kad tugas untuk satu zon.
Nak tahu lebih (pilihan)
RoleBinding boleh tunjuk pada ClusterRole. Kad itu hanya berfungsi dalam zon binding tersebut. ClusterRole terbina: view, edit, admin, cluster-admin. Pod dapat token ServiceAccount yang cepat tamat. Tetapkan automountServiceAccountToken: false bila aplikasi tak pernah panggil API.
Misi sebenar
kubectl create serviceaccount ci-botkubectl create role deployer --verb=get,list,patch --resource=deploymentskubectl create rolebinding ci-bot-deployer --role=deployer --serviceaccount=default:ci-botkubectl describe rolebinding ci-bot-deployer
0 daripada 4 langkah siap · Tanda anda disimpan.
English
Your CI bot only updates one thing: the shop Deployment. Today it holds a master key to the whole cluster. Which key should it carry?
Exactly what it needs, nothing more. That is least privilege.
Kubernetes knows two kinds of “who”. People bring their own ID. Robots get a staff badge: a ServiceAccount. Sort them.
A Role is a job card. It lists verbs, like patch, on things, like deployments. It works in one zone only.
A card does nothing alone. A RoleBinding pins it to a badge, in one zone. Wire the CI bot to deployer, in shop only.
A Role works in one zone. A ClusterRole works in every zone, and for things with no zone, like nodes. Sort these.
Now in the terminal. Apply the job card. Then pin it to ci-bot with a RoleBinding.
--serviceaccount takes zone:name, like default:ci-bot.RBAC has no “deny” rule. The bot has two RoleBindings. One allows deleting pods. One does not. Can it delete pods?
You can now give a bot a job card for one zone.
Anda dah siapkan ini. Latihan lagi pun bagus!
Dokumentasi rasmi: Using RBAC authorization · Service accounts · RBAC good practices
Anda dah belajar 25 minit. Rehat sekejap bantu otak anda.
Pecutan selesai! Fokus yang hebat. Regangkan badan seminit?