Kubernetes · Chapter 16 of 28

RBAC

By the end of this chapter you can say how the API server decides who you are and what you may do, give a person a certificate signed by the cluster, give a program a ServiceAccount, write Roles and bind them, check any permission with kubectl auth can-i, and call the API from inside a pod with its own token. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Scheduling and teams
  • 5 tasks
  • Kubernetes
  • In the all labs plan

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.

Why you need this

So far you have typed every command as the cluster's admin. Jhatpat now has three more callers. Bhola wants a dashboard in the Aliganj store that shows which pods are running, so his pickers know when an app is down; it must never delete or change anything. The CI pipeline rolls out each new storefront image, about 30 times a week; it needs to update Deployments in one namespace, and nothing else. And the payments gateway key sits in a Secret that only payments may read.

Give all three the admin's kubeconfig, and the dashboard can delete namespaces (chapter 15 deleted one in 11 seconds), a leaked CI token can read the gateway key, and nobody can tell from the logs who did what. Nothing you know yet can say "this caller may do exactly this".

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    bhola-csr.yaml: a certificate for Bhola

    Bhola needs a certificate that the cluster signs, so he can log in.

    1. Use openssl to make a private key and a certificate request. Subject /CN=bhola/O=jhatpat-ops, RSA 2048, no passphrase. Keep the key in the chapter folder.

    2. Encode the request file with base64 on one line. Put it in spec.request of bhola-csr.yaml.

    3. Apply bhola-csr.yaml. It makes a CertificateSigningRequest named bhola.

    4. Approve it with kubectl certificate approve.

    To pass: the request bhola is for the signer kubernetes.io/kube-apiserver-client, with the usage client auth. It is Approved and Issued, and its certificate says CN bhola and O jhatpat-ops. If you deny it by mistake, delete it and apply it again.

  2. Task 2

    dashboard.yaml: a read-only role for Bhola

    Bhola's dashboard may read pods, services and deployments in ch16, and nothing more.

    1. Open dashboard.yaml. Make a Role named dashboard-reader in ch16.

    2. Allow only get, list and watch on pods and services (the core group) and on deployments (the group apps).

    3. Make a RoleBinding named bhola-dashboard. It gives the Role dashboard-reader to the user bhola.

    4. Apply the file. Test it with kubectl auth can-i and --as bhola.

    To pass: bhola can get, list and watch those three in ch16. bhola cannot delete, create, update or patch them, cannot read Secrets, cannot exec into pods, and cannot read anything in ch16-payments.

  3. Task 3

    ci.yaml: the CI deployer's ServiceAccount

    The CI pipeline needs its own identity, with only the rights a rollout needs.

    1. Open ci.yaml. Make the ServiceAccount ci-deployer in ch16.

    2. Make a Role named ci-deploy. Allow only get, list, patch and update on deployments (the group apps).

    3. Make a RoleBinding named ci-deploy. It gives the Role ci-deploy to the ServiceAccount ci-deployer of ch16.

    4. Apply the file. Test it with kubectl auth can-i and --as system:serviceaccount:ch16:ci-deployer.

    To pass: ci-deployer can get, list, patch and update Deployments in ch16. It cannot create or delete Deployments, cannot list, create or delete pods, cannot read Secrets, and cannot patch Deployments in ch16-payments.

  4. Task 4

    ci-shell.yaml: prove it from inside a pod

    Prove the CI's rights from inside a pod that runs as ci-deployer.

    1. Open ci-shell.yaml. Add serviceAccountName ci-deployer to the pod's spec. Apply the file.

    2. Run a shell command in ci-shell with kubectl exec. Use curl to call https://kubernetes.default.svc.

    3. Send the pod's token as the header Authorization: Bearer. Trust the CA in /var/run/secrets/kubernetes.io/serviceaccount/ca.crt.

    4. Ask for ch16's pods (it answers 403). Then GET /apis/apps/v1/namespaces/ch16/deployments (it answers 200).

    To pass: the pod ci-shell runs as ci-deployer. The lab's network log has a request from ci-shell to the API server that got 403, and a GET of ch16's deployments that got 200. A running pod cannot change its ServiceAccount: delete it and apply the file again.

  5. Task 5 · interview question

    Interview: Role or ClusterRole

    Interview question: when do you use a Role, and when a ClusterRole? What does system:masters mean?

    1. Open answer.txt. Write below the lines that start with #.

    2. In two or three sentences of your own, say when you use a Role and when a ClusterRole.

    3. Say what a Role is limited to.

    4. Say what the group system:masters means, and what its members can do.

    To pass: your answer names both Role and ClusterRole, mentions the namespace, names system:masters, and says what its members may do. Lines that start with # do not count. Ammar will ask: "Role or ClusterRole for the CI? And why is nobody at Jhatpat in system:masters?"

What you will learn

  • Authentication and authorization: who you are, then what you may do.
  • Users from client certificates, signed through a CertificateSigningRequest.
  • ServiceAccounts: identities for programs, and the token every pod gets.
  • Roles, ClusterRoles, RoleBindings, ClusterRoleBindings: rules (verbs on resources) and who gets them.
  • kubectl auth can-i, with --as and --list.
  • Calling the API from a pod with curl: 403, then 200 after a binding.

More in Scheduling and teams

Build "RBAC" yourself

Jhatpat delivers groceries in ten minutes from 14 dark stores across Lucknow, and its hand-run servers fall over on festival evenings. You move it onto Kubernetes. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Kubernetes are free. This one comes with the all labs plan.