The API request
Every request passes three gates
Who are you? May you? Is it acceptable?
Everything in a cluster is done by sending a request to the A P I server, so securing a cluster starts there. Follow one request from a user called Alice. Authentication. The server works out who is calling, from a certificate or a token. If it cannot, the answer is four oh one. Authorisation. Is Alice allowed to do this, to this kind of object, in this namespace? If not, four oh three. Admission. Now the object itself is inspected against the cluster's policies, and may be changed or refused. Only then is it saved.
RBAC
A Role says what; a RoleBinding says who
A Role lists actions. A RoleBinding gives it to someone.
The second gate is run by rules called R back. A role is a list of permitted actions: here, get, list and watch, on pods, in the namespace shop. A role binding hands that role to someone, here Alice. Now test it. Alice lists pods in shop. Allowed. She tries to delete one. No rule mentions delete, so no. She lists pods in another namespace. The binding does not reach there. She asks for secrets. The role only names pods.
RBAC · Going deeper · 1 of 2
Lab: grant a permission and test it
Create a role and a binding, then ask can-i.
RBAC, written down · Going deeper · 2 of 2
The Role and the binding as written, and how far each pair reaches
rules in the Role; subjects and roleRef in the binding.
Service accounts
Pods have identities too
A pod runs as a service account, and holds its token.
People are not the only callers. Programs inside pods call the A P I as well, and they need an identity. Every pod runs as a service account. A token for that account is placed inside the pod as a file. It expires quickly and is renewed automatically. When the program calls the A P I, it sends the token. And the same R back rules decide what it may do. So think about it from an attacker's side: break into the pod, read the file, and you hold whatever rights that account was given.
The token · Going deeper
The identity is a file in the pod, good for about an hour
serviceAccountName picks the account. The token is mounted and renewed.
Security context
Limit what a container can do to its node
Not root, no escalation, read-only files, no extra powers.
The other question is what a container can do to the machine beneath it. A few lines in the pod's manifest decide that. Refuse to run as root. Forbid the process from gaining more rights after it starts. Make the container's own file system read only, so an intruder cannot install tools. Drop the special kernel powers that root would have. And filter out system calls that ordinary programs never use. Rather than checking these one by one, Kubernetes defines three standard levels, and a namespace can be told to enforce one.
Pod Security Admission · Going deeper
One label on a namespace enforces a level; two more only warn
enforce refuses. warn and audit only report.
Images
Check an image before it is allowed to run
Scan it, sign it, and verify at the door.
Last, the contents. An image is built from your code, on top of a base system and many packages you did not write. So it goes through checks on its way to the cluster. A scanner lists every package inside and compares each with public records of known flaws. Serious findings stop the pipeline. The image is signed, which proves who built it. And at the cluster's admission gate, a policy can refuse any image that is unsigned, or that comes from a registry you do not trust.
Lesson complete
You finished “Security in the cluster”
Nicely done.