Kubernetes Cluster Defense & RBAC Hardening Playbook
Production hardening for Kubernetes: Role-Based Access Control (RBAC) audit, service account token projection, admission controllers, and network policies.
Production hardening for Kubernetes: Role-Based Access Control (RBAC) audit, service account token projection, admission controllers, and network policies.
1. The Danger of Over-Permissive ClusterRoles
Binding pods or service accounts to cluster-admin or broad resources: ["*"], verbs: ["*"] allows an attacker who achieves remote code execution in any pod to compromise the entire Kubernetes control plane.
# Vulnerable: Insecure ClusterRole granting wildcard privileges
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: app-operator
rules:
- apiGroups: ["*"]
resources: ["*"]
verbs: ["*"]2. Hardened Least-Privilege Role & RoleBinding
Restrict permissions strictly to the target namespace, specify exact resources, and limit verbs to read-only where possible:
# Secure: Scoped Role restricted to read ConfigMaps in the 'backend' namespace
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
namespace: backend
name: configmap-reader
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: bind-configmap-reader
namespace: backend
subjects:
- kind: ServiceAccount
name: backend-api-sa
namespace: backend
roleRef:
kind: Role
name: configmap-reader
apiGroup: rbac.authorization.k8s.io3. Disabling Default Service Account Token Auto-Mounting
By default, Kubernetes mounts an API token into every pod at /var/run/secrets/kubernetes.io/serviceaccount/. If your application does not query the Kubernetes API, disable auto-mounting:
apiVersion: v1
kind: ServiceAccount
metadata:
name: backend-api-sa
namespace: backend
automountServiceAccountToken: false4. Zero-Trust NetworkPolicy (Default Deny Ingress/Egress)
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-all
namespace: backend
spec:
podSelector: {}
policyTypes:
- Ingress
- Egress