Skip to content
Engineering Bible
Kubernetes

Kubernetes on security and production

RBAC decides who can do what

  • A Role lists allowed actions: verbs such as get and create, on resources such as pods.
  • A RoleBinding grants a Role to a user, group or ServiceAccount in one namespace.
  • ClusterRole and ClusterRoleBinding do the same across the whole cluster.
  • Rules only add access, and there is no deny. Avoid wildcards and the cluster-admin role.

Each app should have its own ServiceAccount

  • A ServiceAccount is the identity a Pod uses when it calls the Kubernetes API.
  • A Pod gets its namespace's default ServiceAccount unless you name another.
  • Create one per app, and grant it only what that app needs.
  • If the app never calls the API, set automountServiceAccountToken: false.

Pod Security Standards block risky Pods

  • There are three levels: Privileged (open), Baseline (sane minimum), Restricted (hardened).
  • Label a namespace to enforce one: pod-security.kubernetes.io/enforce: restricted.
  • Restricted demands non-root, no privilege escalation and all capabilities dropped.
  • This built-in check replaced PodSecurityPolicy, which was removed in v1.25.
securityContext:
  runAsNonRoot: true
  allowPrivilegeEscalation: false
  readOnlyRootFilesystem: true
  capabilities: {drop: [ALL]}
  seccompProfile: {type: RuntimeDefault}
A container securityContext that passes the Restricted level, plus a read-only filesystem.

Run more than one of everything

  • Run at least 2 replicas of each app, spread across zones, with a PodDisruptionBudget.
  • A managed service from a cloud provider runs and patches the control plane for you.
  • If you run it yourself, use several control plane machines and back up etcd regularly.

Stay on a supported version

  • Only the newest three minor versions get fixes. Today that is 1.35, 1.36 and 1.37.
  • Each minor version gets about 12 months of patches, so upgrade at least once a year.
  • Upgrade the control plane first, then the nodes, and never skip a minor version.

A few commands find most problems

  • Pending means no node fits. Check requests, taints and volume claims in the events.
  • CrashLoopBackOff means the container keeps exiting. Restart delays grow up to 5 minutes.
  • “kubectl debug” adds a temporary container with tools the app image does not have.
kubectl get pods                   # what is running?
kubectl describe pod web-1         # events: why not?
kubectl logs web-1 --previous      # the last crash
kubectl get events                 # recent changes
kubectl debug -it web-1 --image=busybox --target=web
Work from the top: status, then events, then logs, then a shell beside the app.
Kubernetes in six pages6 / 6