Ask the API what an identity can do
kubectl auth can-i list pods --as=system:serviceaccount:gnu-k8s-fundamentals:reader -n gnu-k8s-fundamentals- can-i is a useful RBAC diagnostic.
- The answer should reflect bindings, not assumptions.
Introduce least privilege for API access and safer container execution using ServiceAccounts, Roles/Bindings and securityContext.
Pods use ServiceAccounts as workload identities for Kubernetes API access. Applications should not automatically receive broader API permissions than they need.
Roles contain namespaced permission rules. RoleBindings attach those rules to users, groups or ServiceAccounts in a namespace. ClusterRoles and ClusterRoleBindings apply to cluster-scoped or reusable permission sets.
runAsNonRoot, allowPrivilegeEscalation=false, dropped Linux capabilities, read-only root filesystems and seccomp profiles reduce container privileges. Exact settings must still match the application’s runtime needs.
RBAC governs Kubernetes API authorization. NetworkPolicy governs allowed Pod network traffic when the cluster networking implementation enforces it. They solve different problems.
kubectl auth can-i list pods --as=system:serviceaccount:gnu-k8s-fundamentals:reader -n gnu-k8s-fundamentalsGoal: Create a ServiceAccount, Role and RoleBinding, verify permissions, and run a non-root Pod.
cat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: ServiceAccount
metadata:
name: reader
namespace: gnu-k8s-fundamentals
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: gnu-k8s-fundamentals
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get","list","watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: pod-reader
namespace: gnu-k8s-fundamentals
subjects:
- kind: ServiceAccount
name: reader
namespace: gnu-k8s-fundamentals
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: pod-reader
EOFkubectl auth can-i list pods --as=system:serviceaccount:gnu-k8s-fundamentals:reader -n gnu-k8s-fundamentals
kubectl auth can-i delete pods --as=system:serviceaccount:gnu-k8s-fundamentals:reader -n gnu-k8s-fundamentalscat <<'EOF' | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
name: secure-demo
namespace: gnu-k8s-fundamentals
spec:
serviceAccountName: reader
securityContext:
runAsNonRoot: true
runAsUser: 65534
seccompProfile: {type: RuntimeDefault}
containers:
- name: app
image: busybox:1.36
command: ["sh","-c","id; sleep 3600"]
securityContext:
allowPrivilegeEscalation: false
capabilities: {drop: ["ALL"]}
EOF
kubectl -n gnu-k8s-fundamentals wait --for=condition=Ready pod/secure-demo --timeout=90s
kubectl -n gnu-k8s-fundamentals logs secure-demokubectl -n gnu-k8s-fundamentals delete pod secure-demo
kubectl -n gnu-k8s-fundamentals delete rolebinding pod-reader
kubectl -n gnu-k8s-fundamentals delete role pod-reader
kubectl -n gnu-k8s-fundamentals delete serviceaccount readerProduction security combines workload identity, least-privilege API authorization, hardened container execution and network controls.
Open each answer only after you have tried to answer the question yourself.
Authorization to Kubernetes API resources/actions.
Binds Role/ClusterRole permissions to subjects within a namespace.
No. NetworkPolicy controls network traffic; RBAC controls API authorization.