Lab 1: RBAC & Service Accounts
Create a local cluster with kind
kind runs Kubernetes inside Docker. Perfect for security labs without cloud costs.
$ kind create cluster --name bootcamp $ kubectl cluster-info --context kind-bootcamp $ kubectl get nodes
Create scoped RBAC
Scoped role that only allows reading pods and services in one namespace.
$ kubectl create namespace bootcamp-app $ cat > role.yaml << 'EOF' apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: bootcamp-app name: pod-reader rules: - apiGroups: [""] resources: ["pods", "services"] verbs: ["get", "list", "watch"] EOF $ kubectl apply -f role.yaml $ cat > binding.yaml << 'EOF' apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: namespace: bootcamp-app name: read-pods subjects: - kind: ServiceAccount name: app-reader namespace: bootcamp-app roleRef: kind: Role name: pod-reader apiGroup: rbac.authorization.k8s.io EOF $ kubectl apply -f binding.yaml
Test access controls
The service account can read pods in its namespace but nothing else.
# Check what the service account can do: $ kubectl auth can-i list pods --namespace bootcamp-app --as system:serviceaccount:bootcamp-app:app-reader # yes $ kubectl auth can-i create pods --namespace bootcamp-app --as system:serviceaccount:bootcamp-app:app-reader # no $ kubectl auth can-i list pods --namespace kube-system --as system:serviceaccount:bootcamp-app:app-reader # no