Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
Role-Based Access Control (RBAC) is a crucial aspect of Kubernetes security. In this hands-on guide, we’ll walk through the process of setting up RBAC, creating roles and role bindings, and testing user permissions in a Kubernetes cluster.
Prerequisites#
- A Kubernetes cluster
kubectlconfigured with admin access- Basic understanding of Kubernetes concepts
Step 1: Setting Up a User#
First, let’s create a new user named ‘krishna’:
1openssl genrsa -out krishna.key 20482openssl req -new -key krishna.key -out krishna.csr -subj "/CN=krishna"Now, create a CertificateSigningRequest and approve it:
1# Create CSR2cat <<EOF | kubectl apply -f -3apiVersion: certificates.k8s.io/v14kind: CertificateSigningRequest5metadata:6 name: krishna-csr7spec:8 request: $(cat krishna.csr | base64 | tr -d '\n')9 signerName: kubernetes.io/kube-apiserver-client10 expirationSeconds: 86400 # one day11 usages:12 - client auth13EOF14
15# Approve CSR16kubectl certificate approve krishna-csr17
18# Get the certificate19kubectl get csr krishna-csr -o jsonpath='{.status.certificate}'| base64 -d > krishna.crtStep 2: Checking Default Permissions#
Switch to the krishna context:
1kubectl config set-credentials krishna --client-key=krishna.key --client-certificate=krishna.crt2kubectl config set-context krishna-context --cluster=your-cluster-name --user=krishna3kubectl config use-context krishna-contextNow, try to create a pod:
1kubectl run nginx --image=nginxYou should see an error message indicating that krishna doesn’t have permission to create pods.
Step 3: Creating a Role#
Switch back to the admin context:
1kubectl config use-context admin-contextCreate a Role for reading pods:
1apiVersion: rbac.authorization.k8s.io/v12kind: Role3metadata:4 namespace: default5 name: pod-reader6rules:7 - apiGroups: [""]8 resources: ["pods"]9 verbs: ["get", "watch", "list"]Apply this Role:
1kubectl apply -f pod-reader-role.yamlStep 4: Creating a RoleBinding#
Create a RoleBinding to associate the Role with krishna:
1apiVersion: rbac.authorization.k8s.io/v12kind: RoleBinding3metadata:4 name: read-pods5 namespace: default6subjects:7 - kind: User8 name: krishna9 apiGroup: rbac.authorization.k8s.io10roleRef:11 kind: Role12 name: pod-reader13 apiGroup: rbac.authorization.k8s.ioApply this RoleBinding:
1kubectl apply -f read-pods-rolebinding.yamlStep 5: Testing Permissions#
Switch back to krishna’s context:
1kubectl config use-context krishna-contextNow, try these operations:
-
Create a pod:
Terminal window 1kubectl run nginx --image=nginxExpected: Permission denied
-
List pods:
Terminal window 1kubectl get podsExpected: Success
-
Create a deployment:
Terminal window 1kubectl create deployment nginx-deploy --image=nginxExpected: Permission denied
Conclusion#
In this hands-on guide, we’ve walked through the process of implementing RBAC in Kubernetes. We created a new user, defined a Role with specific permissions, bound that Role to our user, and tested the resulting access controls.
Key takeaways:
- RBAC allows fine-grained control over what users can do in a Kubernetes cluster.
- Roles define permissions, while RoleBindings associate those permissions with users.
- Always follow the principle of least privilege when assigning permissions.
Remember, RBAC is a powerful tool for securing your Kubernetes cluster. Use it wisely to ensure that users and services have only the permissions they need to function.
This blog post template provides a step-by-step guide to implementing and understanding RBAC in Kubernetes, based on the task you described. It includes practical commands, explanations of expected outcomes, and key takeaways. The content is structured to be both informative and hands-on, suitable for readers who want to learn by doing.