Accessing Multiple Clusters

Accessing Multiple Clusters

TL;DR

  • Target a cluster for a rollout with the --context flag
  • Target a cluster for a rollout with the --kubeconfig flag

Multi-Cluster Targeting

Motivation

It is common for users to need to deploy different Variants of an Application to different clusters. This can be done by configuring the different Variants using different kustomization.yaml’s, and targeting each variant using the --context or --kubeconfig flag.

Note: The examples shown in this chapter store the Resource Config in a directory matching the name of the cluster (i.e. as it is referred to be context).

Targeting a Cluster via Context

The kubeconfig file allows multiple contexts to be specified, each with a different cluster + auth.

List Contexts

List the contexts in the kubeconfig file

  1. kubectl config get-contexts
  1. CURRENT NAME CLUSTER AUTHINFO NAMESPACE
  2. us-central1-c us-central1-c us-central1-c
  3. * us-east1-c us-east1-c us-east1-c
  4. us-west2-c us-west2-c us-west2-c

Print a Context

Print information about the current context

  1. kubectl config --kubeconfig=config-demo view --minify
  1. apiVersion: v1
  2. clusters:
  3. - cluster:
  4. certificate-authority: fake-ca-file
  5. server: https://1.2.3.4
  6. name: development
  7. contexts:
  8. - context:
  9. cluster: development
  10. namespace: frontend
  11. user: developer
  12. name: dev-frontend
  13. current-context: dev-frontend
  14. kind: Config
  15. preferences: {}
  16. users:
  17. - name: developer
  18. user:
  19. client-certificate: fake-cert-file
  20. client-key: fake-key-file

Specify a Context Flag

Specify the kubeconfig context as part of the command.

Note: In this example the kustomization.yaml exists in a directory whose name matches the name of the context.

  1. export CLUSTER=us-west2-c; kubectl apply -k ${CLUSTER} --context=${CLUSTER}

Switch to use a Context

Switch the current context before running the command.

Note: In this example the kustomization.yaml exists in a directory whose name matches the name of the context.

  1. # change the context to us-west2-c
  2. kubectl config use-context us-west2-c
  3. # deploy Resources from the ./us-west2-c/kustomization.yaml
  4. kubectl apply -k ./us-west2-c

Targeting a Cluster via Kubeconfig

Alternatively, different kubeconfig files may be used for different clusters. The kubeconfig may be specified with the --kubeconfig flag.

Note: In this example the kustomization.yaml exists in a directory whose name matches the name of the directory containing the kubeconfig.

  1. kubectl apply -k ./us-west2-c --kubeconfig /path/to/us-west2-c/config

More Info

For more information on configuring kubeconfig and contexts, see the Configure Access to Multiple Clusters k8s.io document.