Skip to content

kubeconfig structure

The kubeconfig file is a configuration file used to organize information about clusters, users, namespaces, and authentication mechanisms.^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]

Core Components

The file is typically structured using YAML format and contains several main parameters^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]:

  • clusters: Lists the cluster endpoints, including the URL of the kube-apiserver and certificate authority data.
  • contexts: Defines groups of access parameters under a specific name. Each context maps a cluster, a user, and optionally a namespace.
  • users: Defines authentication credentials for users, such as client certificates, client keys, or bearer tokens.
  • current-context: Specifies the context name that is currently active for kubectl commands.

Role of Contexts

In Kubernetes, a Context acts as a client-side alias for connection parameters^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]. When a user switches to a specific context, subsequent kubectl commands will target the cluster and namespace defined in that context using the credentials of the specified user^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]. It is important to note that the kube-apiserver does not recognize "contexts"; instead, the client resolves these parameters before sending the request^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].

Configuration Example

A standard kubeconfig file generally follows this structure^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]:

apiVersion: v1
kind: Config

clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://kubernetes.docker.internal:6443
  name: docker-desktop

contexts:
- context:
    cluster: docker-desktop
    user: docker-desktop
  name: docker-desktop

current-context: docker-desktop

preferences: {}

users:
- name: docker-desktop
  user:
    client-certificate-data: REDACTED
    client-key-data: REDACTED

Management Commands

The kubectl config command is used to manage these entries^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]:

  • View: kubectl config view displays the current configuration^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].
  • Set Context: kubectl config set-context <NAME> --cluster=<CLUSTER> --user=<USER> --namespace=<NAMESPACE> creates or updates a context^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].
  • Use Context: kubectl config use-context <NAME> switches the active context^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].
  • Set Cluster: kubectl config set-cluster <NAME> --server=<SERVER> --certificate-authority=<PATH> defines a cluster endpoint^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].
  • Set Credentials: kubectl config set-credentials <NAME> --client-certificate=<PATH> --client-key=<PATH> defines a user's authentication methods^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md].

Sources

^[400-devops-06-kubernetes-k8s-ithelp-day28-readme.md]