Skip to Content
Asterrr's Handbook

kubeconfig and certificates

How kubectl picks a cluster, user and namespace, how to build and switch contexts, how to issue a user certificate with a CertificateSigningRequest, and how to inspect and renew kubeadm certificates.

Exam tasks: 1.1 (manage RBAC: users and credentials), 1.4 (manage the cluster lifecycle: certificates), 5.1 (troubleshoot clusters and nodes)

The decision: which cluster, which identity and which namespace does this command run as, and is the certificate behind that identity still valid?

Every exam task starts with a kubectl config use-context line. Understanding what a kubeconfig holds explains it, and also explains most "connection refused" and "x509" errors.

What a kubeconfig holds

api ~/.kube/configClusterTLS, verified with theCAidentityContext:current-contextclusters: serverURL and CAcertificateusers: clientcertificate andkey, or tokennamespace(optional)kube-apiserverAuthentication,then RBAC
A context joins one cluster, one user and an optional namespace. kubectl always runs against the current context, and the user's credentials are what RBAC evaluates.
~/.kube/config
Default kubeconfig. KUBECONFIG (a colon-separated list) or --kubeconfig override it.
3 lists
clusters, users and contexts, plus the single current-context pointer.
10 years
Lifetime of the kubeadm cluster CAs. Leaf certificates last 1 year.
/etc/kubernetes/pki
Where kubeadm keeps the CA and component certificates. etcd's are in the etcd subdirectory.
600 s
Shortest expirationSeconds a CertificateSigningRequest can ask for.
SectionHoldsTypical edit
clusters[]server URL and the CA that signed the API server certificateWrong port or address after a rebuild
users[]A client certificate and key, a bearer token, or an exec pluginA new user or an expired credential
contexts[]A named triple of cluster, user and namespaceDefault namespace for a task
current-contextThe context kubectl uses nowuse-context at the start of a task

Working with contexts

k config get-contexts                       # all contexts, * marks the current one
k config current-context
k config use-context cka-lab2               # switch cluster and identity
k config set-context --current --namespace=shop   # change the default namespace of this context
k config view --minify                      # only the current context, secrets redacted
k config view --raw --minify                # includes the embedded certificate data
  • --namespace on a single command beats the context's namespace. -A lists every namespace.
  • Set the namespace on the context for a task that touches one namespace only. It saves typing -n and avoids creating objects in default by mistake.
  • Several files merge when KUBECONFIG=a:b is set. k config view --flatten > merged.yaml writes the result as one file.
  • On the exam, each task names the context. Run its command every time, even when you think you are already there.

Creating objects in the wrong namespace

A context without a namespace uses default. A task that says "in namespace shop" and an answer created in default earn nothing, and k get without -n shop will not show your mistake. Check with k get <kind> -A | grep name.

Building a context for a new user

A new human user is a client certificate. Kubernetes has no User object, so the API server trusts the certificate's CN as the username and each O as a group if it was signed by the cluster CA.

Create a key and a certificate request. The -subj sets the identity.

openssl genrsa -out jana.key 2048
openssl req -new -key jana.key -subj "/CN=jana/O=developers" -out jana.csr

Submit it as a CertificateSigningRequest. request is the base64 of the CSR file with no line breaks.

apiVersion: certificates.k8s.io/v1
kind: CertificateSigningRequest
metadata:
  name: jana
spec:
  request: <base64 of jana.csr, one line>
  signerName: kubernetes.io/kube-apiserver-client
  expirationSeconds: 86400
  usages: ["client auth"]

base64 -w0 jana.csr prints the one-line value on Linux.

Approve it, then read the signed certificate.

k certificate approve jana
k get csr jana -o jsonpath='{.status.certificate}' | base64 -d > jana.crt

Add the credentials and a context, then give the user permissions. A certificate proves who the user is, and RBAC decides what they may do.

k config set-credentials jana --client-certificate=jana.crt --client-key=jana.key --embed-certs=true
k config set-context jana@lab --cluster=<cluster-name> --user=jana --namespace=shop
k create rolebinding jana-view -n shop --clusterrole=view --user=jana
k --context jana@lab auth can-i list pods     # yes, once the binding exists

Exam signal

signerName: kubernetes.io/kube-apiserver-client is for user certificates. kubernetes.io/kubelet-serving and kubernetes.io/kube-apiserver-client-kubelet are for kubelets. A CSR with the wrong signer or usages is approved but never issued, and .status.certificate stays empty.

Certificates kubeadm manages

sudo kubeadm certs check-expiration          # every cert, its expiry and its CA
sudo kubeadm certs renew all                 # or one name, such as apiserver
sudo openssl x509 -in /etc/kubernetes/pki/apiserver.crt -noout -dates -subject -ext subjectAltName
CertificateFile under /etc/kubernetes/pkiRenewed by
API server servingapiserver.crtkubeadm certs renew, or a control plane upgrade
API server to kubeletapiserver-kubelet-client.crtSame
etcd serving, peer, health checketcd/server.crt, etcd/peer.crt, etcd/healthcheck-client.crtSame
Admin, scheduler, controller manager kubeconfigs/etc/kubernetes/admin.conf, scheduler.conf, controller-manager.conf (embedded)Same
Kubelet client certificate/var/lib/kubelet/pki/The kubelet rotates it itself
Cluster CAsca.crt, etcd/ca.crt, front-proxy-ca.crtNever, unless you replace them by hand
  • kubeadm upgrade apply and kubeadm upgrade node renew leaf certificates unless you pass --certificate-renewal=false, so a cluster that is upgraded once a year never expires.
  • After renewing, the control plane static Pods still hold the old certificate in memory. Restart them: move each manifest out of /etc/kubernetes/manifests and back, or restart the containers with crictl.
  • Copy the new admin.conf to ~/.kube/config, because the old embedded client certificate is the one that expired.

Renewing but not restarting

kubeadm certs check-expiration shows new dates after renew all, yet kubectl still says certificate has expired. The API server process loaded the old file at start. Restart the static Pods.

Errors and what they point to

What you seeLikely cause
The connection to the server localhost:8080 was refusedNo kubeconfig found. KUBECONFIG is unset and ~/.kube/config is missing
connection refused to the right addressAPI server down. Go to the control plane page
x509: certificate signed by unknown authorityThe kubeconfig CA does not match the cluster, for example after a rebuild
x509: certificate has expired or is not yet validAn expired client or API server certificate, or a wrong clock
x509: certificate is valid for X, not YThe server URL is not in the API server certificate's SANs
Error from server (Forbidden)Authentication worked and RBAC denied it. Check bindings
UnauthorizedAuthentication failed: bad token or certificate not signed by the CA

Scenarios

Scenario
A kubeconfig file defines two clusters and two users. After 'kubectl config use-context staging', a 'kubectl get pods' still lists Pods from the production cluster. The 'staging' context entry names the 'prod' cluster and the 'stg-admin' user. What is the cause?
Scenario · choose 2
A control plane node was built 13 months ago with kubeadm and never upgraded. kubectl now fails with 'x509: certificate has expired'. An operator ran 'sudo kubeadm certs renew all' and the check-expiration output shows new dates, but kubectl still fails with the same error. Which TWO actions finish the fix?
Scenario
A new user's CertificateSigningRequest was approved with 'kubectl certificate approve', but 'kubectl get csr -o jsonpath={.status.certificate}' prints nothing. The request used signerName kubernetes.io/kubelet-serving. What should be done?

Drill

kubectl config use-context cka-lab1

Create a user ravi with a client certificate signed by the cluster, valid for one day. Add the credentials and a context named ravi@lab1 to the kubeconfig, defaulting to namespace ops. Allow ravi to read Pods in ops only. Confirm with auth can-i.

Further reading

On this page