Skip to content

kubectl config set-credentials with token

kubectl config set-credentials is a command-line utility used to modify the kubeconfig file, specifically to define or update a user context using a bearer token for authentication^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md]. This is particularly useful when interacting with Kubernetes clusters, such as when logging into the Kubernetes Dashboard, where a token serves as the primary credential for verifying user identity^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md].

Usage

To configure a user entry with a token, the general syntax requires specifying the user name (cluster context) and the token string. The basic command format is:

[kubectl config set-credentials](<./kubectl-config-set-credentials.md>) <USER_NAME> --token="<TOKEN_STRING>"

Practical Example

In a typical workflow—such as setting up access for a local environment like Docker Desktop—you would first retrieve the authentication token (often from a Kubernetes [[Secret]])^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md]. Once retrieved, this token is passed to the command to update the configuration^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md].

For example, the following command sets the credentials for a user named docker-desktop using a variable ${TOKEN}:

[kubectl config set-credentials](<./kubectl-config-set-credentials.md>) docker-desktop --token="${TOKEN}"

^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md]

This command updates the kubeconfig file, associating the specified token with the docker-desktop user entry, thereby allowing subsequent kubectl commands or dashboard logins to authenticate using this token^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md].

Sources

^[400-devops__06-Kubernetes__k8s-ithelp__Day5__README.md]