CodeBlanks Logo Icon CodeBlanks

Kubeconfig

Learn to connect to multiple clusters with kubeconfig.

Kubeconfig

A kubeconfig consists of three lists: clusters (API address and CA), users (credentials) and the contexts joining them (cluster + user + default namespace). current-context names the active one:

apiVersion: v1
kind: Config
clusters:
  - name: uretim-eu
    cluster:
      server: https://eu.k8s.sirket.com
      certificate-authority-data: LS0tLS1CRUdJTi…
users:
  - name: ali-eu
    user:
      exec:
        apiVersion: client.authentication.k8s.io/v1beta1
        command: aws
        args: [eks, get-token, --cluster-name, uretim-eu]
contexts:
  - name: uretim-eu
    context:
      cluster: uretim-eu
      user: ali-eu
      namespace: magaza
current-context: uretim-eu

Multiple files

The KUBECONFIG environment variable takes several files separated by colons; kubectl merges them all. config view --flatten writes the merge into a single file. Contexts can be renamed and deleted:

export KUBECONFIG=~/.kube/config:~/.kube/uretim-eu.yaml:~/.kube/uretim-tr.yaml
kubectl config view --flatten > ~/.kube/birlesik.yaml
kubectl config rename-context arn:aws:eks:eu-central-1:123:cluster/uretim-eu uretim-eu
kubectl config delete-context eski-test

Practice Merge kubeconfigs

Write three commands: export KUBECONFIG as ~/.kube/config:~/.kube/uretim-eu.yaml; write the merge with --flatten to ~/.kube/birlesik.yaml; delete the eski-test context.

Starter code

terminal
# buraya yaz

Sample solution

terminal
export KUBECONFIG=~/.kube/config:~/.kube/uretim-eu.yaml
kubectl config view --flatten > ~/.kube/birlesik.yaml
kubectl config delete-context eski-test

Expected output

deleted context eski-test from /home/ali/.kube/config

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the advantage of an exec-based user credential?

  • It gets a short-lived token from a tool (aws, gcloud) each time; no long-lived secret sits in the file Correct answer
  • It's faster
  • It needs no certificate
  • It works on all clusters

Explanation: Long-lived tokens in a kubeconfig are dangerous if leaked.

2 Write the merged configuration into a single file.

Question
kubectl config view ____ > ~/.kube/birlesik.yaml
Answer
kubectl config view --flatten > ~/.kube/birlesik.yaml

Explanation: --flatten inlines the references.

3 Match.

Term Meaning
clusters API address and CA
users Credentials
contexts Cluster + user + namespace
current-context Active target

All lessons in Expert