How to Manage Single & Multiple Kubernetes Clusters using kubectl & kubectx in Linux

Kubectl is a command line utility & it is used to control and manage Kubernetes clusters and objects running. It allow we can create, modify and delete Kubernetes resources such as Deployments, Pods, Services, switching contexts and access container shell.

Install Kubectl on Linux

Configure Kubectl

  • Create a kubeconfig file & it helps to organize information about clusters, users, namespaces, and authentication mechanisms.
mkdir -p $HOME/.kube
&
vim -p $HOME/.kube/config

In kubeconfig files, we can set the following elements:

  • clusters – we need to know the location of the cluster and have credentials to access it. In the cluster, we’ll set certificate-authority-data, access URL and the name of cluster.
  • context:It is used to group access parameters.Each context in the configuration file should have three parameters: cluster, namespace, and user.
  • users: used to access and its credentials.

Configure single or multiple Kubernetes cluster

Configuration for  single cluster.

For example:

Open Config file.

vim .kube/config 

Provide the following lines.

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUJWakNCL3FBREFnRUNBZ0VBTUFvR0NDcUdTTTQ5QkFNQ01DTXhJVEFmQmdOVkJBTU1HR3N6Y3kxelpYSjIKWlhJdFkyRkFNVFUzTkRNNE16ZzJOVEFlRncweE9URXhNakl3TURVeE1EVmFGdzB5T1RFeE1Ua3dNRFV4TURWYQpNQ014SVRBZkJnTlZCQU1NR0dzemN5MXpaWEoyWlhJdFkyRkFNVFUzTkRNNE16ZzJOVEJaTUJNR0J5cUdTTTQ5CkFnRUdDQ3FHU000OUF3RUhBMElBQkpsb3NSY1FRTHlsL28yeFltQ0l4eHdsZ1F3ZTdlU1dxQmNaRFQ3Q2xJcXoKNnB4R24yb2w3MHM3N3dpcTNaRnkrSW0vdFhHSW16Y3N6MHFNYUpjUy9rV2pJekFoTUE0R0ExVWREd0VCL3dRRQpBd0lDcERBUEJnTlZIUk1CQWY4RUJUQURBUUgvTUFvR0NDcUdTTTQ5QkFNQ0EwY0FNRVFDSUVJcjZ6NGRMUUw1Ck8wSUN3ejBWUEdhYUs0bEU3bFU3SmJXTWhoRk9vcDh1QWlBKzZhcG9NMFVtZ1IxYkFBeWNaS0VHL3AzQWRhWmEKMWV3TGxmUkxiWkJwa3c9PQotLS0tLUVORCBDRVJUSUZJQ0FURS0tLS0tCg==
    server: https://127.0.0.1:6443
  name: default
contexts:
- context:
    cluster: default
    user: default
  name: default
current-context: default
kind: Config
preferences: {}
users:
- name: default
  user:
    password: user-password
    username: user-name

Configuration for multiple clusters.

For example:

Open Config file.

.kube/config 

Provide the following lines.

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
    certificate-authority-data:
    server:
  name: k8s-prod

- cluster:
    certificate-authority-data:
    server:
  name: k8s-stage

- cluster:
    certificate-authority-data:
    server:
  name: k8s-dev

- cluster:
    certificate-authority-data:
    server:
  name: k8s-testing


contexts:
- context:
    cluster: k8s-prod
    user: k8s-prod-admin
  name: k8s-prod

- context:
    cluster: k8s-stage
    user: k8s-stage-admin
  name: k8s-stage

- context:
    cluster: k8s-dev
    user: k8s-dev-admin
  name: k8s-dev

- context:
    cluster: k8s-testing
    user: k8s-testing-admin
  name: k8s-testing

users:
- name: k8s-dev-admin
  user:
    password: .......
    username: .......

- name: k8s-stage-admin
  user:
    client-certificate-data:
    client-key-data:

- name: k8s-deloper-admin
  user:
    client-certificate-data:
    client-key-data:

- name: k8s-testing-admin
  user:
    client-certificate-data:
    client-key-data:

View current contexts:

kubectl config get-contexts

Chnage current context to a different context.

kubectl config use-context k8s-prod
or
kubectl config use-context k8s-testing

Test the nodes.

kubectl get nodes

Change Context and Namespace using kubectx and kubens.

  • kubectx helps to change the clusters and kubens helps to change Kubernetes namespaces.

Install kubectx and kubens.

##Install kubectx & Kubens

wget https://raw.githubusercontent.com/ahmetb/kubectx/master/kubectx
wget https://raw.githubusercontent.com/ahmetb/kubectx/master/kubens

##Provide Permission
chmod +x kubectx kubens

##Change directory
mv kubens kubectx /usr/local/bin

List all contexts.

kubectx

Here is the command output.

k8s-prod
k8s-stage
k8s-dev
k8s-testing

Chnage to prod context.

kubectx k8s-prod

List all namespaces in k8s-prod context.

kubens

Change to a namespace.

kubens <namespacename>

 

Leave a Reply