leaseweb knowledge base logo - white color on the orange background

Create a Token-based Kubeconfig

Create a Token-based Kubeconfig

Preface

With this method we will create a ServiceAccount and a token that can then be retrieved and assign role and permission.

ServiceAccount (SA)

Creation of the ServiceAccount is done as follow:

Assign Role

We then assign a role in this example we will assign Cluster Admin role:

Secret and Token

We will then need to create a secret and retrieve the token from this secret.

We create a manifest token-secret-admin.yml:

token-secret-admin.yml

We deploy the manifest:

We then retrieve the token:

You can also just retrieve the decoded token like this:

kubeconfig

We can now build the kubeconfig with the ServiceAccount and its token.

Like mention earlier in this document we copy from the kubeconfig downloaded from the customer portal as the base for our new kubeconfig

Then modified the users part it like this without touching the cluster part:

laurent-token.kubeconfig

Validation

Because we already assigned the cluster admin role to this ServiceAccount we should be able to get the cluster info:

Conclusion

We can see that there is multiple way to authenticate against the API server, we demonstrate with certificates and bearer token but you can also see more ways on the official Kubernetes documentation in the authentication section.