Kubernetes¶
Scope: designing and running application workloads on Kubernetes clusters. For building the container images, see Docker.
Entries are sorted by score from 1 to 10 (criteria): 9โ10 start here or strongly recommended, 6โ8 recommended, 1โ5 further reading or conditional.
Adopted references¶
- 9 Security checklist (Kubernetes): the baseline for cluster and workload security reviews.
- 8 Kubernetes configuration good practices (Kubernetes): the baseline for manifests.
- 8 CIS Kubernetes Benchmark: a hardening benchmark for clusters.
- 7 Production best practices checklist (LearnKube): a practical workload readiness checklist.
- 7 Helm chart best practices and Dockerfile best practices.
- 7 OpenGitOps principles: declarative, versioned, pulled and continuously reconciled delivery.
Key rules¶
Workloads¶
- 9
K8S-1Give every container CPU and memory requests and a memory limit. Set CPU limits only where needed, for example on sensitive workloads. (Kubernetes, Resource management for pods and containers, LearnKube, Production best practices, Kubernetes, Security checklist) - 9
K8S-2Configure readiness, liveness and startup probes for their distinct purposes. Check back-end services only in the readiness probe, never in the liveness probe. (Kubernetes, Liveness, readiness and startup probes, Configure probes) - 8
K8S-4Run multiple replicas, spread them across nodes and zones, and protect them with PodDisruptionBudgets. (Kubernetes, Topology spread constraints, Disruptions) - 8
K8S-5Deploy images by digest, never by a mutable tag such as:latest. Tags can be moved to point to different images; digests can't. Let a bot keep the digests current, asSEC-15in general/security.md asks. (Kubernetes, Images) - 7
K8S-3Fit graceful shutdown into the Pod's termination grace period: setterminationGracePeriodSecondsto cover your slowest shutdown, and use apreStophook if the app needs time before it receivesSIGTERM. HandlingSIGTERMitself isOPS-15in general/operations-reliability.md. (Kubernetes, Pod lifecycle: termination) - 5
K8S-6Scale stateless workloads horizontally with the HorizontalPodAutoscaler. (Kubernetes, Autoscaling workloads) - 4
K8S-7Apply the recommendedapp.kubernetes.io/*labels. (Kubernetes, Recommended labels)
Security¶
- 9
K8S-8Grant least-privilege RBAC, and give each workload its own service account. (Kubernetes, RBAC good practices, RBAC: service account permissions, Service accounts) - 8
K8S-9Enforce the Restricted Pod Security Standard for application namespaces. (Kubernetes, Pod Security Standards) - 8
K8S-10Start each namespace from a default-deny NetworkPolicy and allow only the traffic you need. (Kubernetes, Security checklist, Network policies) - 8
K8S-11Encrypt Secrets at rest, restrict who can read them, and consider an external secret store. (Kubernetes, Good practices for Secrets) - 5
K8S-12Decide your tenancy model (namespaces, clusters) explicitly. (Kubernetes, Multi-tenancy)
Delivery¶
- 6
K8S-13Let an agent pull the declared state from Git and continuously reconcile the cluster to it (GitOps). Keeping the manifests in Git isOPS-14in general/operations-reliability.md. (OpenGitOps principles)
How to enforce¶
Tools that check the key rules above automatically, in CI, the IDE or a commit hook. "Partly" means the tool covers only some of what the rule asks.
- 8 Pod Security Admission: the built-in admission controller. Label a namespace
pod-security.kubernetes.io/enforce: restrictedand non-compliant pods are rejected. ChecksK8S-9. - 6 kube-linter checks: lints manifests in CI. Its default checks cover the
latesttag, missing CPU requests and memory limits, missing anti-affinity or topology spread, the default service account, pods without a NetworkPolicy, and access to Secrets; probe checks are opt-in. ChecksK8S-1andK8S-5, and partlyK8S-2,K8S-4,K8S-8,K8S-10andK8S-11. - 6 Kyverno policy library: admission policies that enforce rules inside the cluster, for example requests and memory limits, a default-deny NetworkPolicy for every new namespace, a PodDisruptionBudget created for every new Deployment, probes and the
app.kubernetes.io/namelabel. ChecksK8S-1andK8S-10, and partlyK8S-2,K8S-4andK8S-7. - 5 Polaris checks: an alternative to kube-linter, with reliability, efficiency and security checks that run in CI. It flags missing probes, the
latesttag, missing PodDisruptionBudgets, and missing requests and memory limits (turn offcpuLimitsMissingto matchK8S-1). ChecksK8S-1,K8S-2andK8S-5, and partlyK8S-4andK8S-8. - 5 OPA Gatekeeper library: an alternative policy engine, with policies for disallowed image tags, required probes and container requests. Checks
K8S-5, and partlyK8S-1andK8S-2.
Tutorials and further reading¶
- 7 Kubernetes tutorials (official).