Guides

Compare two Kubernetes namespaces

How to diff staging against production in Kubernetes: what kubectl can do, where its output gets noisy, and comparing whole namespaces object by object.

Updated

"It works in staging" usually means something differs between staging and production: an image tag, an environment variable, a ConfigMap key, a Service nobody created in production. Finding it means comparing two namespaces, often in two clusters, and Kubernetes has no command for that.

With kubectl

First, list what exists on each side. kubectl get all leaves out ConfigMaps, Secrets, Ingresses and custom resources, so ask the API server for every kind that can be listed in a namespace:

for kind in $(kubectl --context staging api-resources --verbs=list --namespaced -o name); do
  kubectl --context staging -n shop get "$kind" -o name
done | sort > staging.txt
# repeat with --context production > production.txt
diff staging.txt production.txt

That shows what exists on only one side, along with Pods, ReplicaSets, Events and Endpoints you'll want to ignore. Then compare each object that exists on both:

diff <(kubectl --context staging -n shop get deploy checkout -o yaml) \
     <(kubectl --context production -n shop get deploy checkout -o yaml)

The diff is mostly noise: uid, resourceVersion, creationTimestamp, managedFields, status, cluster IPs. Strip it with yq before comparing:

yq 'del(.metadata.uid, .metadata.resourceVersion, .metadata.creationTimestamp,
        .metadata.generation, .metadata.managedFields, .status)'

Secrets need care too: -o yaml prints their values, so a diff puts production credentials in your terminal history.

This works for one object. For a namespace with forty, it's an afternoon, and it's out of date by the time you finish.

With Klystiq

Compare namespaces does the whole namespace at once, in one cluster or across two.

  1. Open your staging and production contexts. Klystiq reads your existing kubeconfig.
  2. Choose Compare namespaces in the sidebar, or from a Namespace's actions menu.
  3. Pick the two sides: a context and namespace for each.

Every object lands in one of three groups:

  • Only on one side, such as the Service nobody created in production;
  • Differs, with the fields that changed and any image change called out;
  • Same.

Each has a side-by-side diff. Every namespaced kind is included, custom resources too. Objects a controller made, such as a ReplicaSet's Pods or a CronJob's Jobs, are left out because their owners are compared.

The noise is already gone: status, server bookkeeping, owner UIDs, cluster-assigned addresses and per-cluster CA bundles are ignored. Secret values are compared by fingerprint, so you learn that a password differs without either value ever being shown.

Compare with what's in Git

To check what's running against your manifests instead of against another cluster, use Local manifests. It takes a folder of YAML, a Kustomize directory or a Helm chart, and reports each object in each context as Same, Differs, Missing or Error. Only the fields your manifest sets count, the way server-side apply sees them, so defaults the API server fills in don't show up as differences.

Fix it

When you find the difference, Review and apply… opens the object with the usual server-side dry run and diff before anything is written. Mark production read-only first if you only want to look; Klystiq then refuses every write to it.

The Comparing clusters docs cover every option.

Try it on your own clusters.

Download Klystiq, point it at the kubeconfig you already have, and see all of your clusters in one window. Your kubeconfig has been through enough; Klystiq only reads it. Free for personal use; a license for work.