When using kubectl, a context is a grouping of clusters, user credentials, and namespaces. kubectl uses information stored in these contexts to communicate with any cluster.
When we set up our local cluster using Minikube, it creates a default minikube context for us. We can confirm this by running kubectl config current-context:
$ kubectl config current-context
minikube
kubectl gets its configuration from the file specified by the KUBECONFIG environment variable. This was set in our .profile file to $HOME/.kube/config. If we look inside it, we will see that it is very similar to the config we downloaded from DigitalOcean:
apiVersion: v1
clusters:
- cluster:
certificate-authority: ~/.minikube/ca.crt
server: https://10.122.98.148:8443
name: minikube
contexts:
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
user:
client-certificate: ~/.minikube/client.crt
client-key: ~/.minikube/client.key
The ~/.kube/config file records the IP address of the cluster's master API server, the credentials for our client to interact with it, and grouped the cluster information and user credentials together in the context object.
For kubectl to interact with our new DigitalOcean Hobnob cluster, we must update the KUBECONFIG environment variable to include our new configuration file.
First, copy the configuration file from DigitalOcean to a new file:
$ cp downloads/hobnob-kubeconfig.yaml ~/.kube/
Now, edit your ~/.profile file and update the KUBECONFIG environment variable to include the new configuration file:
export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/hobnob-kubeconfig.yaml
Save and source the file to make it apply to the current shell:
$ . ~/.profile
Now, when we run kubectl config view, we will see that configuration from both of our files has merged together:
$ kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: REDACTED
server: https://8b8a5720059.k8s.ondigitalocean.com
name: do-nyc1-hobnob
- cluster:
certificate-authority: ~/.minikube/ca.crt
server: https://10.122.98.148:8443
name: minikube
contexts:
- context:
cluster: do-nyc1-hobnob
user: do-nyc1-hobnob-admin
name: do-nyc1-hobnob
- context:
cluster: minikube
user: minikube
name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: do-nyc1-hobnob-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
- name: minikube
user:
client-certificate: ~/.minikube/client.crt
client-key: ~/.minikube/client.key
Now, to make kubectl interact with our DigitalOcean cluster instead of our local cluster, all we have to do is change the context:
$ kubectl config use-context do-nyc1-hobnob
Switched to context "do-nyc1-hobnob".
Now, when we run kubectl cluster-info, we get information about the remote cluster instead of the local one:
$ kubectl cluster-info
Kubernetes master is running at https://8b8a5720059.k8s.ondigitalocean.com
KubeDNS is running at https://8b8a5720059.k8s.ondigitalocean.com/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy