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

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

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:

3. Make the kubectl binary executable.

4. Move the binary into your PATH.

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

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.

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

And create a file called config and paste the content of master node’s kubectl conf 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:

you can below commands to see kube config file:

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:

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:

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.

To check your current context.

To switch context from current context.

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!

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sarasa Gunawardhana

Sarasa Gunawardhana

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