Kubernetes cluster hardening: control plane, kubelet, Pod Security, and a production checklist
How to harden a Kubernetes cluster layer by layer: private API endpoint, anonymous auth, Node,RBAC, admission, etcd encryption with KMS v2, the kubelet, Pod Security Admission in practice, NetworkPolicy, the CIS Benchmark, and upgrades.
Kubernetes$ kubectl label ns prod pod-security.kubernetes.io/enforce=restricted▍A freshly created Kubernetes cluster works, but it was not designed to withstand an attacker. The API may be exposed to the internet, Secrets sit in etcd unencrypted, any pod can run as root, and every pod can talk to every other pod. Hardening means closing those doors one layer at a time, without breaking what already runs. In this guide, we walk through the control plane, the nodes, and the workloads, checking on a kind cluster running Kubernetes 1.37 what is already secure by default and what you need to turn on. To understand where attacks come from, start with the Kubernetes attack surface.
Control plane: the API is the front door
Private endpoint
The kube-apiserver is the most valuable target in the cluster. In production, it should not accept connections from the internet. On managed services, this is a cluster option: on EKS, the terraform-aws-modules/eks/aws v21 Terraform module already defaults to endpoint_public_access = false, and AKS and GKE offer private clusters. If a public endpoint is unavoidable, restrict the source CIDRs and require a VPN or bastion for administrative access.
Anonymous authentication
By default, the kube-apiserver accepts anonymous requests and treats them as the system:anonymous user. What prevents damage is authorization. In the lab, from inside the control-plane node:
$ curl -sk https://localhost:6443/version
{
"major": "1",
"minor": "37",
...
$ curl -sk https://localhost:6443/api/v1/namespaces
"message": "namespaces is forbidden: User \"system:anonymous\" cannot list resource \"namespaces\" in API group \"\" at the cluster scope",
"code": 403
/version answers because the default RBAC opens a few info and health endpoints to unauthenticated users. The risk shows up when someone creates a binding for system:anonymous or system:unauthenticated. Review those bindings regularly. --anonymous-auth=false closes the door for good, but it can break health checks that hit /livez and /readyz without credentials; recent versions let you restrict anonymous access to those endpoints through the structured authentication configuration. Check the current docs before changing it. The authentication mechanisms are covered in the Kubernetes authentication guide.
Authorization and admission
These are the relevant kube-apiserver flags in the lab (created with kubeadm through kind):
$ docker exec blog-lab-control-plane grep -E -- '--(authorization-mode|enable-admission|etcd|audit-policy)' \
/etc/kubernetes/manifests/kube-apiserver.yaml
- --authorization-mode=Node,RBAC
- --enable-admission-plugins=NodeRestriction
- --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
- --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
- --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
- --etcd-servers=https://127.0.0.1:2379
- --audit-policy-file=/etc/kubernetes/policies/audit-policy.yaml
Node,RBAC: the Node authorizer limits each kubelet to the objects of its own pods; RBAC handles the rest. Never useAlwaysAllow.NodeRestriction: prevents a kubelet from changing other nodes or sensitive labels on its own node. The default plugins, includingPodSecurity, are already on without appearing in the flag.- Your own policies: ValidatingAdmissionPolicy (GA since 1.30) and MutatingAdmissionPolicy (GA since 1.36) let you write CEL rules without an external webhook.
The audit log is also configured here; it has its own guide in Kubernetes audit logging for security.
etcd encryption at rest
Without configuration, Secrets sit in etcd only base64-encoded. Encryption at rest is turned on with --encryption-provider-config pointing to an EncryptionConfiguration:
apiVersion: apiserver.config.k8s.io/v1
kind: EncryptionConfiguration
resources:
- resources:
- secrets
providers:
- kms:
apiVersion: v2
name: kms-your-org
endpoint: unix:///var/run/kmsplugin/socket.sock
timeout: 3s
- identity: {}
The first provider in the list is used for writes; the others are for reading older data. The identity entry at the end lets the server read what was written before encryption. Prefer kms with KMS v2 (GA since 1.29), which delegates the key to an external KMS; KMS v1 has been disabled by default since 1.29. The local providers (aescbc, aesgcm, secretbox) keep the key in a file on the node itself, which protects etcd backups but not a compromised node. After turning it on, rewrite the existing Secrets so they get encrypted:
kubectl get secrets -A -o json | kubectl replace -f -
On managed services, this is an option: on EKS, it is the Terraform module’s encryption_config with an AWS KMS key. This lab has no encryption configured; we did not test it here.
etcd TLS
etcd should accept only clients that present a certificate. On kind, that is already the case:
$ docker exec blog-lab-control-plane grep -E -- '--(client-cert-auth|peer-client-cert-auth|trusted-ca-file)' \
/etc/kubernetes/manifests/etcd.yaml
- --client-cert-auth=true
- --peer-client-cert-auth=true
- --trusted-ca-file=/etc/kubernetes/pki/etcd/ca.crt
On self-managed clusters, also confirm that port 2379 is not reachable from outside the control-plane network. Direct access to etcd equals full access to the cluster.
Kubelet: the API nobody remembers
Every node runs the kubelet with an HTTPS API on port 10250 that can execute commands in containers. Three settings are essential: anonymous authentication off, webhook authorization (the kubelet asks the kube-apiserver), and the read-only port 10255 closed. We read the lab’s configuration without changing anything:
$ docker exec blog-lab-worker cat /var/lib/kubelet/config.yaml
authentication:
anonymous:
enabled: false
webhook:
enabled: true
x509:
clientCAFile: /etc/kubernetes/pki/ca.crt
authorization:
mode: Webhook
rotateCertificates: true
readOnlyPort does not appear, and in KubeletConfiguration the default is 0, which means off. We checked on the node: only ports 10248 (healthz, on localhost) and 10250 were listening, an anonymous call to 10250 was refused, and 10255 refused the connection (curl exits with code 7):
$ curl -sk https://localhost:10250/pods
Unauthorized
$ curl -s -m 2 http://localhost:10255/pods; echo "exit=$?"
exit=7
As command-line flags, the equivalent is --anonymous-auth=false, --authorization-mode=Webhook, and --read-only-port=0. If your provider or installer generates a different configuration, validate it node by node.
Pod Security Admission in practice
Pod Security Admission (GA since 1.25) applies the privileged, baseline, and restricted levels per namespace, in enforce, audit, and warn modes. Before turning on enforce in an existing namespace, simulate it with --dry-run=server. The server itself lists the pods that would violate the level:
$ kubectl label --dry-run=server --overwrite ns d-legado pod-security.kubernetes.io/enforce=restricted
Warning: existing pods in namespace "d-legado" violate the new PodSecurity enforce level "restricted:latest"
Warning: legado: allowPrivilegeEscalation != false, unrestricted capabilities, runAsNonRoot != true, seccompProfile
namespace/d-legado labeled (server dry run)
With the namespace labeled for real, a privileged pod with hostNetwork is rejected immediately:
$ kubectl label ns d-hardening pod-security.kubernetes.io/enforce=restricted \
pod-security.kubernetes.io/enforce-version=latest \
pod-security.kubernetes.io/warn=restricted pod-security.kubernetes.io/audit=restricted
$ kubectl -n d-hardening apply -f priv.yaml
Error from server (Forbidden): error when creating "priv.yaml": pods "privilegiado" is forbidden: violates PodSecurity "restricted:latest": host namespaces (hostNetwork=true), privileged (container "shell" must not set securityContext.privileged=true), allowPrivilegeEscalation != false (...), unrestricted capabilities (...), runAsNonRoot != true (...), seccompProfile (...)
This pod passes every restricted check:
apiVersion: v1
kind: Pod
metadata:
name: compliant
spec:
securityContext:
runAsNonRoot: true
runAsUser: 65534
runAsGroup: 65534
seccompProfile:
type: RuntimeDefault
automountServiceAccountToken: false
containers:
- name: app
image: busybox:1.37
command: ["httpd", "-f", "-p", "8080", "-h", "/tmp"]
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
capabilities:
drop: ["ALL"]
resources:
requests: {cpu: 10m, memory: 16Mi}
limits: {memory: 32Mi}
$ kubectl -n d-hardening get pod conforme
NAME READY STATUS RESTARTS AGE
conforme 1/1 Running 0 0s
$ kubectl -n d-hardening exec conforme -- id
uid=65534(nobody) gid=65534(nobody) groups=65534(nobody)
(In the lab, the pod was named conforme, Portuguese for “compliant”.) One gotcha: Deployments that violate the level are created (with a Warning), but the ReplicaSet cannot create pods. The symptom is 0/1 and FailedCreate events with the same violates PodSecurity message. The warn and audit modes help you catch this earlier.
Network, RBAC, and images
Three layers that have their own guides in this series:
- NetworkPolicy: start every namespace with a default deny (
podSelector: {}andpolicyTypes: [Ingress, Egress]) and allow only what is needed, including DNS. This only works if the CNI enforces the policy. See Kubernetes networking. - Least-privilege RBAC: no
cluster-adminfor applications, Roles per namespace, and care withsecrets,pods/exec, and verbs such asescalate,bind, andimpersonate. See RBAC, ClusterRoles, and RoleBindings. - Image policy: allowed registries, immutable tags or digests, and signature verification at admission. See container image supply chain on Kubernetes.
CIS Benchmark and kube-bench
The CIS Kubernetes Benchmark is the reference checklist for control-plane, etcd, kubelet, and policy configuration. kube-bench, from Aqua Security, automates these checks and runs as a Job in the cluster (kubectl apply -f job.yaml, from the project’s repository). In v0.16.0, the newest generic profile is cis-1.12, aimed at Kubernetes 1.32 and 1.33, and there are specific profiles for EKS, GKE, and AKS. We found no confirmation of a published CIS benchmark for 1.34 or newer. Check the CIS page and the kube-bench repository before treating the result as a formal audit of a 1.37 cluster. We did not run kube-bench in this lab. On managed services, some control-plane checks do not apply, because the provider is responsible for it.
Upgrades are hardening too
Security fixes only reach those who upgrade. Today the versions supported by the community are 1.35, 1.36, and 1.37, and each minor gets about one year of patches. The kubelet can be up to three minors behind the kube-apiserver, but it should not stay there for long. Upgrade one minor at a time, control plane first. The step-by-step for kubeadm, EKS, AKS, and GKE is in how to upgrade a Kubernetes cluster.
Hardening checklist by layer
| Layer | Control | How to verify |
|---|---|---|
| API | Private endpoint or restricted CIDRs | Cluster settings at the provider |
| API | No binding for system:anonymous or system:unauthenticated |
kubectl get clusterrolebindings -o wide |
| API | --authorization-mode=Node,RBAC and NodeRestriction |
kube-apiserver manifest |
| etcd | Encryption at rest with KMS v2 | --encryption-provider-config |
| etcd | mTLS for clients and peers, port 2379 isolated | etcd manifest and firewall |
| Kubelet | Anonymous off, Webhook authz, readOnlyPort: 0 |
/var/lib/kubelet/config.yaml |
| Workloads | Pod Security restricted (or baseline with exceptions) |
Namespace labels |
| Network | Default deny per namespace | kubectl get netpol -A |
| Access | Least-privilege RBAC | kubectl auth can-i --list --as=... |
| Images | Allowed registry and verified signature | Admission policy |
| Operations | Audit log, CIS/kube-bench, and a supported version | Logs, reports, and kubectl version |
Checklist
- API not publicly exposed, or restricted to known CIDRs
- Bindings for anonymous users reviewed and removed
- Secrets encrypted in etcd with KMS v2 and rewritten after the change
- Kubelet with anonymous off, Webhook authz, and port 10255 closed
- Pod Security
restrictedinenforce, tested first with--dry-run=server - NetworkPolicy default deny and least-privilege RBAC in every namespace
- Cluster on a supported version, with upgrades planned
Hardening is not a project with an end date: it is the default configuration you decide never to put off again.