Skip to content

Working across multiple Kubernetes clusters

Operate many clusters without mistakes — context discipline, kubeconfig hygiene, kubectx/kubens, federation patterns, and GitOps as the source of truth.

Running one cluster is a tooling problem; running ten is a discipline problem. The clusters look the same, the commands look the same, and the only thing standing between you and a wrong-environment incident is the structure you put around it. This article covers the practices that let a small team operate many clusters — context naming, kubeconfig hygiene, the kubectx/kubens shortcuts, and the GitOps pattern that removes “which cluster am I on” from the daily question list.

How to verify

What you can see from your machine:

kubectl config get-contexts
kubectl config current-context
ls -la ~/.kube/configs.d/

If you have many clusters, one file per cluster is the cleanest layout. From CI or a controller cluster (running Argo CD or Flux), the multi-cluster source of truth is its Application/Cluster registrations:

argocd cluster list
kubectl get clusters.argoproj.io -n argocd

What’s happening

Multi-cluster operation has two layers: the human layer (engineers running kubectl) and the automated layer (CI, GitOps controllers, fleet-management tools). Both need a stable, unambiguous way to name and reach clusters.

For the human layer, the kubeconfig is the entry point — see /en/knowledge-base/kubectl-basics-contexts/ for the basics. The multi-cluster discipline on top: one kubeconfig file per cluster under ~/.kube/configs.d/, merged via KUBECONFIG=$(ls ~/.kube/configs.d/*.yaml | tr '\n' ':'), context names that immediately reveal environment and region (prod-us-east, staging-eu-west), and a prompt that changes colour for production contexts. kubectx and kubens shortcut context and namespace switches; both have fuzzy completion.

For the automated layer, GitOps changes the question entirely. With Argo CD or Flux installed on a controller cluster and registered against the rest, the source of truth becomes “what’s in the Git repository,” not “what’s in this cluster.” A change is committed, the controller reconciles it onto every targeted cluster, and operators stop reaching into clusters directly. The kubectl --context=prod-us move becomes the diagnostic exception, not the routine.

Federation in the original sense (kubefed) never reached production maturity and is largely dead. The patterns that did stick: ApplicationSets in Argo CD that generate Applications per-cluster from a template, Flux’s MultiClusterServiceMesh, and external DNS controllers that aggregate Services from many clusters into a single DNS apex.

The procedure

  1. One kubeconfig per cluster, named for what it does. Stop using the kubeconfigs cloud CLIs spit out — rename them on receipt:

    mkdir -p ~/.kube/configs.d
    aws eks update-kubeconfig --name prod-us-east \
      --alias prod-us-east \
      --kubeconfig ~/.kube/configs.d/prod-us-east.yaml

    Then in your shell rc:

    export KUBECONFIG=$(ls ~/.kube/configs.d/*.yaml | tr '\n' ':')
  2. Use kubectx and kubens for fast context/namespace switching:

    # macOS
    brew install kubectx
    # Linux
    sudo apt-get install kubectx

    kubectx lists contexts and switches; kubens does the same for namespaces.

  3. Annotate the prompt with the context. A shell snippet that turns prod red catches half the wrong-cluster mistakes before they happen. See /en/knowledge-base/kubectl-basics-contexts/ for the zsh example.

  4. Register clusters with Argo CD so the GitOps controller can reach them. Run on the controller cluster:

    argocd login argocd.example.com --sso
    argocd cluster add prod-us-east --name prod-us-east
    argocd cluster add prod-eu-west --name prod-eu-west
    argocd cluster list

    Cluster credentials are stored as Secrets in the controller cluster’s argocd namespace.

  5. Generate per-cluster Applications from a template with ApplicationSet — one definition for “deploy the platform stack everywhere”:

    apiVersion: argoproj.io/v1alpha1
    kind: ApplicationSet
    metadata:
      name: platform-stack
      namespace: argocd
    spec:
      generators:
        - list:
            elements:
              - cluster: prod-us-east
                url: https://api.prod-us-east...
              - cluster: prod-eu-west
                url: https://api.prod-eu-west...
      template:
        metadata: { name: 'platform-{{cluster}}' }
        spec:
          project: default
          source:
            repoURL: git@github.com:example/platform-gitops.git
            targetRevision: main
            path: 'clusters/{{cluster}}'
          destination:
            server: '{{url}}'
            namespace: platform
          syncPolicy:
            automated: { prune: true, selfHeal: true }
  6. For the few commands you still run by hand against many clusters — read-only diagnostics, status checks — use a loop over context names rather than a federation tool:

    for ctx in $(kubectl config get-contexts -o name | grep prod-); do
      echo "=== $ctx ==="
      kubectl --context=$ctx get nodes
    done
  7. Audit access — who has credentials for which clusters. The trail is in ~/.kube for humans and in the GitOps controller’s cluster-Secret list for automation:

    kubectl -n argocd get secret -l argocd.argoproj.io/secret-type=cluster

Common pitfalls

  • Cloud CLI default kubeconfig names are unwieldy and look identical for prod and staging (arn:aws:eks:eu-west-1:...:cluster/...) — alias them on creation, every time.
  • Merging kubeconfigs with duplicate context names silently picks the first match — KUBECONFIG= order is fragile; rename contexts in the source files.
  • An exec plugin like aws eks get-token runs on every kubectl call — prompt integrations that call kubectl per prompt become unusably slow.
  • A controller cluster that goes down takes the GitOps loop with it — back up the Argo CD configuration to Git and make the controller cluster itself disaster-recoverable, not just the workload clusters.
  • “Federation” patterns (multi-cluster Services, cluster-API-shared etcd) are easy to write up and painful to operate; reach for them only when you’ve exhausted the simpler “many independent clusters, GitOps on top” approach.

We operate multi-cluster fleets for clients with GitOps as the source of truth, controller clusters that mirror the workload clusters’ DR posture, per-cluster kubeconfigs handed out to engineers via OIDC, and a strict no-direct-cluster-mutation rule outside of break-glass. See /en/solutions/multi-region-environments/.