Create the namespace and working folder
The lab starts by isolating the RBAC exercise inside namespace `role` and creating a folder to hold certificate artifacts.
kubectl create namespace role mkdir role cd role
This page visualizes the RBAC client-certificate flow as a lab runbook: create the namespace, generate user certificates, bind the Role, wire the certificate into kubeconfig, switch context, confirm the namespace, and test both allowed and forbidden operations. See RBAC Concepts for the full reference.
Use the stage chips to focus on one phase, or show the whole end-to-end path.
The lab starts by isolating the RBAC exercise inside namespace `role` and creating a folder to hold certificate artifacts.
kubectl create namespace role mkdir role cd role
The private key stays with the user, and the CSR carries the identity request. The most important field is the common name set to `user3`.
sudo openssl genrsa -out user3.key 2048 sudo openssl req -new -key user3.key -out user3.csr Common Name (e.g., your name or your server's hostname) []: user3
Once the cluster CA signs the CSR, the resulting `user3.crt` becomes a client certificate the API server can trust.
sudo openssl x509 -req \ -in user3.csr \ -CA /etc/kubernetes/pki/ca.crt \ -CAkey /etc/kubernetes/pki/ca.key \ -CAcreateserial \ -out user3.crt \ -days 500
The `Role` contains verbs and resources, while the `RoleBinding` connects those rules to subject `user3` inside namespace `role`.
kubectl create -f role.yaml kubectl get roles -n role kubectl create -f rolebinding.yaml kubectl get rolebinding -n role
This is the handoff point where local certificate files are attached to the logical kubeconfig user named `user3`.
kubectl config set-credentials user3 \ --client-certificate=/home/labsuser/role/user3.crt \ --client-key=/home/labsuser/role/user3.key
The new context combines the target cluster, authenticated user, and default namespace.
kubectl config set-context user3-context \ --cluster=kubernetes \ --namespace=role \ --user=user3 kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kubernetes-admin@kubernetes kubernetes kubernetes-admin
user3-context kubernetes user3 role
After `kubectl config use-context user3-context`, every request is sent as `user3` to namespace `role` unless you override it.
kubectl config use-context user3-context sudo chmod 666 user3.key user3.crt kubectl get pods kubectl create deployment test --image=docker.io/httpd -n role kubectl get deployment
The same authenticated user is accepted by the API server, but RBAC blocks `configmaps` because the Role does not grant that permission.
kubectl create configmap my-config \ --from-literal=key1=config1 \ --kubeconfig=config error: failed to create configmap: configmaps is forbidden: User "user3" cannot create resource "configmaps" in API group "" in the namespace "role"
The markdown also includes optional file movement steps when you want to run `kubectl` as this user from another machine or worker node.
scp user3.crt worker-node-1:/role/ scp user3.key worker-node-1:/role/ mkdir -p /role cd /role vi user3.crt vi user3.key
Each artifact has a specific job in the overall flow.
User private key. It proves the client owns the certificate and must be protected.
Unsigned identity request sent to the cluster CA. It contains the subject details including common name.
Signed client certificate trusted by the API server, representing user `user3`.
The context entry that switches the namespace to `role` and the auth identity to `user3`.
These are the most useful command checkpoints to demo live during training.
Confirms the new user and namespace entry exists.
kubectl config get-contexts
Moves the active client identity from admin to `user3`.
kubectl config use-context user3-context
Shows a request that should succeed if the Role is correct.
kubectl get pods kubectl create deployment test --image=docker.io/httpd -n role
Shows RBAC rejecting a disallowed resource type.
kubectl create configmap my-config --from-literal=key1=config1
The lab is successful when these outcomes are true at the same time.
The API server trusts the certificate signed by the cluster CA and recognizes the client as `user3`.
The user can run the verbs and resources granted by the Role in namespace `role`.
The user is blocked from `configmaps` because no matching RBAC rule was granted.
The active context defaults the user into namespace `role` until another context is selected.
This walkthrough demonstrates classic X.509 client-certificate authentication. In many enterprises, human users are integrated through OIDC or a webhook-backed identity layer, while certificates remain common for admin access, cluster bootstrap, or automation.