Reducing a Kubernetes cluster's attack surface requires checking API permissions, Pod admission, network isolation and data protection together. These steps use an application namespace in a self-managed cluster: inspect and dry-run first, then roll out in stages. Managed services may expose different control-plane settings.
Confirm Permissions and Recovery First
The examples assume Kubernetes v1.25 or newer, Pod Security Admission enabled, and permission to inspect authorization and modify the target namespace. Replace the test namespace app with your own. Preserve existing configuration, a working administrator access path and backups whose restoration has been tested. Schedule control-plane, node and etcd changes in a maintenance window.
Restrict network access to the API server, kubelet and etcd; inspect authentication, RBAC bindings, the Node authorizer and NodeRestriction admission configuration. Do not assume installer or cloud defaults are identical. The official security checklist covers control-plane, node and workload controls (checked 2026-11-28).
Use an administrator session allowed to impersonate the target identity. Replace alice@example.com and its groups with the real identity and groups. Impersonating a user does not automatically include identity-provider groups; add --as-group when needed.
kubectl auth can-i get pods -n app --as=alice@example.com
kubectl auth can-i get secrets -n app --as=alice@example.com
kubectl -n app get rolebindings
kubectl get clusterrolebindings
You should see yes for the first result if needed for the job and no for the second if Secret access is unnecessary. Investigate roles, bindings, groups and other authorizers when results differ. These queries cover only the specified requests and identity; they do not prove other permissions are absent.
Prefer namespace Roles and RoleBindings and separate workload identities. Set automountServiceAccountToken: false on Pods that do not need API access. list or watch on Secrets can expose their contents, and permission to create Pods can indirectly grant access to Secrets in the same namespace.
Stage Pod Security Admission
Pod Security Admission returns warnings to the requester in warn mode, adds audit annotations in audit mode, and rejects noncompliant Pod creates and checked updates in enforce mode. Enforcement does not check workload templates directly; some updates and exempt objects are also excluded. First dry-run against existing Pods, then observe; changing labels does not automatically evict existing Pods. The commands change warn and audit labels on app: record the previous labels and confirm admission and audit policy are enabled.
kubectl label --dry-run=server --overwrite namespace app \
pod-security.kubernetes.io/enforce=restricted
kubectl label --overwrite namespace app \
pod-security.kubernetes.io/warn=restricted \
pod-security.kubernetes.io/audit=restricted
You should see violation warnings in the dry run for noncompliant existing Pods. After the second command succeeds, kubectl get namespace app --show-labels should show both labels. Observe normal deployments, fix manifests and test before setting enforce to restricted; pin the policy version as documented to avoid an implicit policy change on upgrade.
Restricted generally requires non-root execution, disabled privilege escalation, seccomp and dropped Linux capabilities. Images or writable directories may need changes. Save the previous labels before enforcing; if deployment is blocked, restore the previous enforce label or remove the label added in this change, then investigate rejected objects. Do not apply the same policy indiscriminately to system namespaces.
Verify Network Traffic, Not Just Policies
The following object isolates ingress to all Pods in app; it does not restrict egress. It works only when the CNI implements and enforces NetworkPolicy. It can interrupt service: test the necessary allow rules outside production first, save existing policies, and delete only the newly added object if rolling back.
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: default-deny-ingress
namespace: app
spec:
podSelector: {}
policyTypes:
- Ingress
Save as deny-ingress.yaml and run kubectl apply -f deny-ingress.yaml; you should see the empty selector and Ingress in kubectl -n app get networkpolicy default-deny-ingress -o yaml. This confirms persistence, not enforcement. From known test Pods, test both a connection that should be denied and one permitted by an allow policy; confirm the target service was reachable beforehand. Allow rules are additive, and a timeout can also have another network cause. The NetworkPolicy documentation explains plugin requirements and policy composition.
Design and test egress restrictions separately for DNS, application dependencies and cloud metadata access. Implementations differ in node and address translation handling; the ingress rule above does not protect the metadata service by itself.
Protect Data, Images and Audit Records
API data encryption at rest is distinct from disk encryption. Check that the API server encryption configuration covers Secrets and that the first provider for writes actually encrypts; identity does not. A new configuration does not automatically rewrite old objects. Follow the encryption guide to verify migration, backups and key recovery; never print real keys in logs or terminals. Losing decryption keys can make data unreadable.
Protect etcd network access and client certificates; restrict access to backups and audit logs and set retention periods. Review audit policy to avoid logging Secret contents. Pin images by digest, scan for known vulnerabilities and verify signatures and trusted sources; neither scanning nor signatures guarantee that runtime vulnerabilities are absent.
Limitations
- Where a provider manages etcd, the API server or node settings, use its documented equivalent controls rather than copying self-managed settings.
- NetworkPolicy alone is not complete multi-tenant isolation; hostNetwork, existing allow rules and CNI behavior affect results.
- Admission and network changes can interrupt deployments or traffic. Confirm the previous configuration, administrator access and application recovery steps before rollout.
FAQ
Does a Restricted label fix existing Pods?
No. The label controls admission; it does not change or evict Pods already running. Use a server-side dry run to identify violations, repair workload templates and test replacement Pods before enabling enforcement. A running Pod does not prove a new replica will be admitted.
Can old keys be removed after Secret encryption is enabled?
Not immediately. Older objects or backups may still need them. Follow the documented rewrite and verification process, retain the keys and recovery path required for old backups, and remove old keys only after the migration and key-rotation plan are complete.
Why can traffic still reach a Pod after a NetworkPolicy is created?
The object alone does not prove the network plugin enforces it, and other policies selecting the Pod can add allowed traffic. Check CNI support and configuration, selectors and ingress direction; then compare expected allow and deny flows from known sources while ruling out service and routing failures.
Related Providers
- Trivy: scan images and Kubernetes manifests in a build pipeline.
- Vault: evaluate for centralized secret management or dynamic credentials.
Community & Resources
- Security Hardening: explore controls for other infrastructure layers.
- Container security practices: additional image and runtime checks.
- Kubernetes v1.37 upgrade checks: review release changes and upgrade prerequisites.
Original source: Kubernetes Documentation ↗