Configure kubectl with a cluster

Rationale TL;DR

You have created a cluster with kops (or maybe bare-metal) and you want to use those same credentials elsewhere (like with a new laptop, in your CI/CD…).

Requirements

You need the API url, a user and a password. Because we assumed kops (or bare metal) deploys, the user we have is probably an admin user. Doesn’t matter right now.

First, we need to configure the credentials. We need to name those credentials so we can refer to them afterwards:

kubectl config set-credentials kubernetes_user/user.kubernetes.com --username=kubernetes_user --password=kubernetes_password

Where kubernetes_user/user.kubernetes.com is the name we decided to give to those credentials. This is totally arbitrary name.

Moving on, we need to set up a cluster. Like we did before:

kubectl config set-cluster kubernetes_cluster/cluster.kubernetes.com --insecure-skip-tls-verify=true --server=https://cluster.kubernetes.com

We named that cluster cluster.kubernetes.com.

Finally, we need to link those credentials with the cluster:

kubectl config set-context default/context.kubernetes.com/kubernetes_user --user=kubernetes_user/user.kubernetes.com --namespace=default --cluster=kubernetes_cluster/cluster.kubernetes.com

We’re naming our context default/context.kubernetes.com/kubernetes_user, using the user and clusters we declared before.