Kubernetes Best Practices

Chapter 1 · Practice 2 of 5

Label every object with the recommended app labels

From Workloads and manifests

Why it matters

Services, Deployments, PodDisruptionBudgets and NetworkPolicies all find Pods through label selectors. Inconsistent labels lead to Services that route to nothing, budgets that protect the wrong Pods and policies that miss a workload.

Kubernetes documents a shared set of recommended labels under the app.kubernetes.io prefix:

  • app.kubernetes.io/name: the application, such as checkout
  • app.kubernetes.io/instance: this installation, such as checkout-prod
  • app.kubernetes.io/version: the version you deployed
  • app.kubernetes.io/component and app.kubernetes.io/part-of: where it fits
  • app.kubernetes.io/managed-by: the tool that applied it, such as Helm

Keep selectors small and stable. A Deployment's spec.selector cannot be changed after creation, so select on name and instance, never on version, which changes with every release.

To check, run kubectl get pods --show-labels and confirm each Service selects the Pods you expect with kubectl get endpointslices -l kubernetes.io/service-name=<service>.

Example

YAML
metadata:
  labels:
    app.kubernetes.io/name: checkout
    app.kubernetes.io/instance: checkout-prod
    app.kubernetes.io/version: "4.2.1"
    app.kubernetes.io/component: api
    app.kubernetes.io/managed-by: helm
spec:
  selector:
    matchLabels:
      app.kubernetes.io/name: checkout
      app.kubernetes.io/instance: checkout-prod