Kubernetes: Configure Context and Switching Contexts in Multiple Clusters | Part 4

Sarasa Gunawardhana
3 min readOct 7, 2019

Overview

This article brings you to learn how to create contexts and mange contexts according to your requirements.

Introduction

In real situations, We have to keep multiple clusters such as Test, Stage, and Production. To manage each cluster you have to access those from time to time and might be a headache to switch between clusters.

But in Kubernetes, They have introduced contexts to work on multiple clusters easier.

Contexts make easier to control clusters on remote servers.

Step 01: Install Kubectl on the local machine.

1.If you want to install the latest version

curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl

2. To download a specific version.

Remember to install with the same version of the server’s kubectl version.

For example, to download version v1.14.3 on Linux, type:

curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.15.0/bin/linux/amd64/kubectl

3. Make the kubectl binary executable.

chmod +x ./kubectl

4. Move the binary into your PATH.

sudo mv ./kubectl /usr/local/bin/kubectl

5. Test to ensure the version you installed is up-to-date:

kubectl version

Step 02: Copy kube config file from server to local

You might remember that we set up kubectl in Master Node. There we created a folder called “.Kube” inside Home Directory. Then we copied autogenerated kubeadm’s admin.conf to $HOME/.kube/config.

--- Don't run these commands again. We already ran on Master Node
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/configsudo chown $(id -u):$(id -g) $HOME/.kube/config

So get a copy of $HOME/.kube/config file. Next in your local machine, create a folder.

mkdir -p $HOME/.kube

And create a file called config and paste the content of master node’s kubectl conf file.

nano $HOME/.kube/config #you can use any command to create a file

when we run kubectl commands, it looks the configuration first inside the .kube/config , that’s why we create folder and name the configuration file like this.

Example of config file:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
name: development

users:
- name: developer

contexts:
- context:
name: dev-environment

you can below commands to see kube config file:

$ kubectl config view

As per this configuration, its context name is dev-environment which is associated with the Kubernetes cluster.

lets see how the full configuration file for one context:

apiVersion: v1
clusters:
- cluster:
certificate-authority: fake-ca-file
server: https://1.2.3.4
name: development
contexts:
- context:
cluster: development
namespace: storage
user: developer
name: dev-environment
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
user:
client-certificate: fake-cert-file
client-key: fake-key-file

Step 03: Configure Multiple contexts on your local machine.

The situation when you have multiple clusters such as test, stage, and development, you have to edit the Kube config file in local machine and have to add separately cluster, contexts and users.

The output shows the two clusters, two users, and three contexts:

apiVersion: v1
clusters:
- cluster:
certificate-authority: fake-ca-file
server: https://1.2.3.4
name: development
- cluster:
insecure-skip-tls-verify: true
server: https://5.6.7.8
name: scratch
contexts:
- context:
cluster: development
namespace: frontend
user: developer
name: dev-frontend
- context:
cluster: development
namespace: storage
user: developer
name: dev-storage
- context:
cluster: scratch
namespace: default
user: experimenter
name: exp-scratch
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
user:
client-certificate: fake-cert-file
client-key: fake-key-file
- name: experimenter
user:
password: some-password
username: exp
source : https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/

You actually create a one kube config file from three separate cofiguration files.

Step 04: Switching contexts

When you handle multiple clusters using contexts, you might need to know these commands.

To list all Kubernetes contexts.

$ kubectl config view -o jsonpath='{.contexts[*].name}' | tr " " "\n"

To check your current context.

$ kubectl config current-context

To switch context from current context.

$ kubectl config use-context <context_name>

Next article brings you, How to setup Kubenetes cluster easily

If you like, Feel free to clap for this article which make me happy. :D :D

Did you find this guide helpful? Make sure to subscribe to my newsletter so you don’t miss the next article with useful deployment tips!

--

--

Sarasa Gunawardhana

Senior DevSecOps Engineer | Full Stack Developer | Tech Blogger | SLIIT