liveBook · Manning

Kubeconfig

As explained in Road to Kubernetes

A kubeconfig is a configuration file that specifies Kubernetes cluster connections, credentials, and contexts for client authentication.

A kubeconfig file lets a Kubernetes client authenticate to a cluster. A managed cluster can generate one for its root service account, which has broad access. Downloading that file gives you a way to connect, but also gives you a secret to protect. section 8.1.2 section 8.1.3

Treat a kubeconfig like a password:

Connect kubectl to a cluster

After downloading the kubeconfig and installing kubectl, put the file at ~/.kube/config, or point KUBECONFIG to its absolute path. For a macOS, Linux, or WSL terminal session, run `export KUBECONFIG=/path/to/kubeconfig.yaml`, then `kubectl get nodes`. The environment-variable approach makes it easier to use different kubeconfig files for different projects; set it again in a new terminal session. section 8.2.3

The Kubernetes Dashboard offers another connection path: download the cluster’s kubeconfig, open the Dashboard, select Kubeconfig rather than token, and choose the downloaded file to sign in. section 8.2.1

Listing 8.2 Setting KUBECONFIG in VSCode
{
	...
	"settings": {
		...
		"terminal.integrated.env.osx": {
            "KUBECONFIG": "${workspaceFolder}/.kube/kubeconfig.yaml",
            "PY_PORT": "8080",
            "JS_PORT": "3000"
        },
        "terminal.integrated.env.windows": {
            "KUBECONFIG": "${workspaceFolder}\\.kube\\kubeconfig.yaml",
            "PY_PORT": "8080",
            "JS_PORT": "3000"
        },
        "terminal.integrated.env.linux": {
            "KUBECONFIG": "${workspaceFolder}/.kube/kubeconfig.yaml",
            "PY_PORT": "8080",
            "JS_PORT": "3000"
        },
	}
}

The VSCode terminal settings example assigns KUBECONFIG to a project-local file path on macOS, Windows, and Linux, so terminal commands can use that project’s cluster configuration. section 8.2.3

Automated deployment needs the same access. A GitHub Actions workflow writes a kubeconfig supplied through `secrets.KUBECONFIG_SA` to `~/.kube/config`, checks Pods and Deployments with kubectl, then applies manifests and restarts the application Deployments. The kubeconfig must belong to a service account with appropriate permissions. section 8.5.2

How Road to Kubernetes covers it

How deep it goes
Shows file placement, KUBECONFIG setup, security precautions, and workflow use; defers creating and managing service accounts.
Examples
Uses a downloaded managed-cluster file, `kubectl get nodes`, VSCode terminal settings, and a GitHub Actions deployment workflow.
What it assumes
Starts with a provisioned cluster, a downloaded kubeconfig, and installed kubectl; explains how service-account access relates to the file.
What you can do after
Operate/configure kubectl with a downloaded kubeconfig, verify cluster access, and supply credentials to a deployment workflow.
How it connects
Links service accounts and Kubernetes API permissions to kubectl, the Kubernetes Dashboard, and GitHub Actions.

Also explained in

Compare how 3 books explain kubeconfig →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.