Kubernetes Security: A Practical Guide to Hardening Your Cluster
Kubernetes gives teams a powerful way to deploy and scale applications, but its flexibility also means security depends on several layers working together. A secure cluster is not created by one setting: it comes from limiting access, reducing workload privileges, controlling network paths, protecting credentials, and continuously checking what is running.
This guide is a practical starting point for platform engineers, DevOps teams, and developers operating Kubernetes clusters. Apply the examples in a non-production environment first and adapt them to your cluster distribution, runtime, and networking plugin.
1. Start with least-privilege access
Avoid giving users or service accounts the built-in `cluster-admin` role unless they genuinely need cluster-wide administration. Prefer namespace-scoped Roles and RoleBindings, and grant only the verbs and resources required for a task.
For example, this role allows read-only access to Pods in one namespace:
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: pod-reader
namespace: app
rules:
- apiGroups: [""]
resources: ["pods"]
verbs: ["get", "list", "watch"]Bind the role to a specific user or service account with a RoleBinding in the same namespace. Review permissions regularly, especially after team or workload changes. Remember that permissions to create Pods can indirectly grant access to Secrets or host resources depending on the cluster's configuration.
2. Harden Pods and containers
Run applications as non-root where possible, prevent privilege escalation, and drop Linux capabilities the application does not need. Use Pod Security Standards—typically the `restricted` profile for workloads that can support it—and enforce policy with the admission mechanism available in your cluster.
A starting security context for a container:
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]Set an explicit non-root `runAsUser` when your image supports it. A read-only root filesystem may require writable volumes for temporary files. Avoid privileged containers and host namespace access unless there is a reviewed operational need.
3. Protect credentials and Secrets
Do not bake credentials into container images, commit them to source control, or pass them as command-line arguments. Restrict access to Kubernetes Secrets with RBAC, enable encryption at rest for the cluster's datastore, and consider an external secret manager for sensitive production credentials.
