By default kubectl stores the user's configuration under ~/.kube/config.
To create the file, we just need to run kubectl with the config option and set the name of the cluster:
cloud_user@client:~$ kubectl config set-cluster kubernetes-the-hard-way
Cluster "kubernetes-the-hard-way" set.
cloud_user@client:~$ cat ~/.kube/config
apiVersion: v1
clusters:
- cluster:
server: ""
name: kubernetes-the-hard-way
contexts: null
current-context: ""
kind: Config
preferences: {}
users: null
We can then add the rest of the settings, like the IP address of the API server, and the certificates signed by the CA.
cloud_user@client:~$ kubectl config set clusters.kubernetes-the-hard-way.server https://172.31.23.61:6443
cloud_user@client:~$ kubectl config set-cluster kubernetes-the-hard-way --embed-certs=true --certificate-authority kthw/ca.pem
cloud_user@client:~$ kubectl config set-credentials admin --client-certificate=kthw/admin.pem --client-key=kthw/admin-key.pem
Then create the user and the context.
A context is a group of access parameters. Each context contains a Kubernetes cluster, a user, and a namespace.
The current context is the cluster that is currently the default for kubectl
cloud_user@client:~$ kubectl config set-credentials admin --client-certificate=kthw/admin.pem --client-key=kthw/admin-key.pem
cloud_user@client:~$ kubectl config set-context kubernetes-the-hard-way --cluster=kubernetes-the-hard-way --user=admin
cloud_user@client:~$ kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://172.31.23.61:6443
name: kubernetes-the-hard-way
contexts:
- context:
cluster: kubernetes-the-hard-way
user: admin
name: kubernetes-the-hard-way
current-context: ""
kind: Config
preferences: {}
users:
- name: admin
user:
client-certificate: /home/cloud_user/kthw/admin.pem
client-key: /home/cloud_user/kthw/admin-key.pem
The current-context is still empty. So the last thing we need to do is specify that we want to use the newly created context.
cloud_user@client:~$ kubectl config use-context kubernetes-the-hard-way
Now we should be able to get details about our cluster
cloud_user@client:~$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
wrk01.kube.com NotReady <none> 4d5h v1.18.6
wrk02.kube.com NotReady <none> 4d5h v1.18.6