CKAD field manual

Kubernetes kubeconfig example

A kubeconfig names a cluster, a user, and a context that ties them together. The current context is the one kubectl uses.

An officer slides a glowing keycard into the lock of a blast door. An officer slides a glowing keycard into the lock of a blast door.
Write a file
kubectl config set-cluster lab --server=https://10.0.0.1:6443 --certificate-authority=/etc/kubernetes/pki/ca.crt --embed-certs=true --kubeconfig=/root/lab.kubeconfig
kubectl config set-credentials lab-admin --client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt --client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key --embed-certs=true --kubeconfig=/root/lab.kubeconfig
kubectl config set-context lab --cluster=lab --user=lab-admin --namespace=default --kubeconfig=/root/lab.kubeconfig
kubectl config use-context lab --kubeconfig=/root/lab.kubeconfig
Read it back
kubectl config current-context --kubeconfig=/root/lab.kubeconfig
kubectl config view --kubeconfig=/root/lab.kubeconfig --minify
KUBECONFIG=/root/lab.kubeconfig kubectl config current-context
Namespace of the current context
kubectl config set-context --current --namespace=default

Fields

kubectl config set-cluster
Server URL and cluster CA. No namespace here. --embed-certs=true copies the PEM into the file.
kubectl config set-credentials
Client cert and key for the user name. --embed-certs=true, or the file only stores paths from this node.
kubectl config set-context
--cluster, --user, --namespace. This adds the context. --current changes the namespace of the one already in use.
kubectl config use-context
Sets current-context. set-context alone leaves the file on whatever context was current.
kubectl config current-context
Prints the one string kubectl will use. Empty means use-context has not been run on this file.
kubectl config view
--minify prints the current context. Add --kubeconfig when the task names a file.
KUBECONFIG
Same switch as --kubeconfig, for the one command. Unset, kubectl reads ~/.kube/config.

Watch

  • The namespace is a field of the context. set-cluster takes the server and the CA. It has no namespace flag.
  • set-context adds the entry. use-context sets current-context. A file can list a context and still have no current context.
  • --kubeconfig writes that file. Without it, kubectl edits ~/.kube/config. KUBECONFIG= is the same switch for a single command. --current edits whichever file is active.
  • --embed-certs=true stores the PEM in the file. Without it, the file stores paths that exist only on this node. On kubeadm the cluster CA is /etc/kubernetes/pki/ca.crt. Use the server and cert paths the task prints.

Official docs kubeconfig files Configure access to multiple clusters

Practice these objects on a live cluster →