RBAC
Author: Fabrice JAMMES (LinkedIn).
Objective
Give a pod the right to read the Services of its namespace, and nothing else, with a Role and a RoleBinding, then check the rights with kubectl auth can-i.
The lab works on a cluster shared by the trainees: the namespaces end with your account.
Setup
Exercise 1: namespaces and Services
Create two namespaces, foo-$ID and bar-$ID, and a ClusterIP Service in each one: foo-service and bar-service, on port 80.
Exercise 2: a pod which calls the API server
Create the pod curl-custom-sa of kubectl-proxy.yaml in foo-$ID. Which service account does it use? Its ambassador container runs kubectl proxy with the token of this service account, on localhost:8001.
From its main container, list the Services of foo-$ID with curl. What happens?
Exercise 3: Role and RoleBinding
In foo-$ID, create:
- a
Roleservice-reader, which allowsgetandlistonservices - a
RoleBindingservice-reader-binding, which grants this Role to the service accountdefaultoffoo-$ID
Then check the rights of the service account with kubectl auth can-i, without calling the API from the pod.
Exercise 4: test from the pod
From the pod, list the Services of foo-$ID, then of bar-$ID.
Questions
- Which service account does a pod use if
serviceAccountNameis not set? Does it need a token if it does not call the API? - How do you find all the subjects which can read the Secrets of a namespace?
Clean up
The 2_0_RBAC_simple.sh script of the k8s-advanced repository runs the same scenario.