Docker & Kubernetes
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
Exercise 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.
Show example solution
export KUBECONFIG=~/.kube/config:~/.kube/uretim-eu.yaml
kubectl config view --flatten > ~/.kube/birlesik.yaml
kubectl config delete-context eski-test
Test yourself
-
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
- It's faster
- It needs no certificate
- It works on all clusters
Show answer
It gets a short-lived token from a tool (aws, gcloud) each time; no long-lived secret sits in the file — Long-lived tokens in a kubeconfig are dangerous if leaked.
-
Write the merged configuration into a single file.
kubectl config view ____ > ~/.kube/birlesik.yaml
Show answer
kubectl config view --flatten > ~/.kube/birlesik.yaml
-
Match.
- clusters
- API address and CA
- users
- Credentials
- contexts
- Cluster + user + namespace
- current-context
- Active target
Next lesson: Cross-cluster service