Kubectl is a command line utility used to control and manage Kubernetes clusters and objects running it them. Kubectl enables you to create, modify and delete various Kubernetes resources such as Deployments, Pods, Services, switching contexts and even to access container shell.

We’ll start with the installation of kubectl then move ahead to the configurations required to be more efficient when managing Kubernetes clusters from the CLI with kubectl. Note that you should have a working Kubernetes cluster before using this guide. Check any of our guides below.

How To Deploy Lightweight Kubernetes Cluster in 5 minutes with K3s

Deploy Production Ready Kubernetes Cluster with Ansible & Kubespray

How To run Local Kubernetes Cluster in Docker Containers

How to setup 3 node Kubernetes Cluster on Ubuntu with Weave Net CNI

Install Kubectl on Linux and macOS

Through installation of Kubernetes cluster, you must have installed kubectl as a basic requirement. But this means you need to login to your Master node to run kubectl commands. You can install kubectl on your local Linux or macOS machine.

Install kubectl on Linux

curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Confirm your installation of kubectl.

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3", GitCommit:"b3cbbae08ec52a7fc73d334838e18d17e8512749", GitTreeState:"clean", BuildDate:"2019-11-13T11:23:11Z", GoVersion:"go1.12.12", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.3-k3s.2", GitCommit:"e7e6a3c4e9a7d80b87793612730d10a863a25980", GitTreeState:"clean", BuildDate:"2019-11-18T18:31:23Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"linux/amd64"}

Install kubectl on macOS

For macOS, run the following commands.

curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/darwin/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl
kubectl version

Configure Kubectl

The kubectl tool looks for a file named config in the $HOME/.kube directory, but a separate file can be specified using --kubeconfig option. The kubeconfig files helps you to organize information about clusters, users, namespaces, and authentication mechanisms.

$ ls $HOME/.kube/config
/home/jmutai/.kube/config

In this configuration you need to set the following elements:

  • clusters – To configure access to a cluster, you need to know the location of the cluster and have credentials to access it. In the cluster section, you’ll set certificate-authority-data, access URL and the name for your cluster.
  • context: A context element is used to group access parameters under a convenient name. Each context in the configuration file should have hree parameters: cluster, namespace, and user.
  • users: Specify used to access and its credentials.

For a single cluster, your configuration file will look similar to below.

cat .kube/config 
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: 76dd75552cb14f3085445277a2091c6c
    username: admin

Get the Cluster URL, CA data, and user credentials then substitute in the file.

Kubectl configuration for multiple clusters

Below is a template configuration file for four Kubernetes clusters, namely:

  • k8s-dev – context is k8s-dev, user is k8s-dev-admin.
  • k8s-staging – context is k8s-staging, user is k8s-staging-admin.
  • k8s-qa – context is k8s-qa, user is k8s-qa-admin.
  • k8s-prod – context is k8s-prod, user is k8s-prod-admin.

Each cluster has a unique name, associated context and user.

apiVersion: v1
kind: Config
preferences: {}

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

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

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

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


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

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

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

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

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

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

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

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

Modify the template to fit your use case and paste the contents inside $HOME/.kube/config.

Switching between contexts with kubectl

View current contexts:

$ kubectl config get-contexts
CURRENT   NAME           CLUSTER        AUTHINFO             NAMESPACE
*         k8s-dev        k8s-dev        k8s-dev-admin        kube-system
          k8s-staging    k8s-staging    k8s-staging-admin    kube-system
          k8s-qa         k8s-qa         k8s-qa-admin         kube-system
          k8s-prod       k8s-prod       k8s-prod-admin       kube-system

To switch to a different context, use:

$ kubectl config use-context k8s-prod
Switched to context "k8s-prod".

$ kubectl config use-context k8s-staging
Switched to context "k8s-staging".

Test:

$  kubectl get nodes
NAME           STATUS   ROLES    AGE   VERSION
k3s-master01   Ready    master   10d   v1.16.3-k3s.2
k3s-worker01   Ready    <none>   9d    v1.16.3-k3s.2
k3s-worker02   Ready    <none>   9d    v1.16.3-k3s.2

Easy Context and Namespace switching with kubectx and kubens

kubectx helps you switch between clusters back and forth and and kubens helps you switch between Kubernetes namespaces smoothly:

Install kubectx and kubens

wget https://raw.githubusercontent.com/ahmetb/kubectx/master/kubectx
wget https://raw.githubusercontent.com/ahmetb/kubectx/master/kubens
chmod +x kubectx kubens
sudo mv kubens kubectx /usr/local/bin

Usage pages:

$ kubectx --help
USAGE:
  kubectx                       : list the contexts
  kubectx <NAME>                : switch to context <NAME>
  kubectx -                     : switch to the previous context
  kubectx -c, --current         : show the current context name
  kubectx <NEW_NAME>=<NAME>     : rename context <NAME> to <NEW_NAME>
  kubectx <NEW_NAME>=.          : rename current-context to <NEW_NAME>
  kubectx -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
                                  (this command won't delete the user/cluster entry
                                  that is used by the context)

$ kubens --help
USAGE:
  kubens                    : list the namespaces in the current context
  kubens <NAME>             : change the active namespace of current context
  kubens -                  : switch to the previous namespace in this context
  kubens -c, --current      : show the current namespace
  kubens -h,--help          : show this message

Examples:

# Get all contexts
$ kubectx
k8s-dev 
k8s-staging
k8s-qa
k8s-prod

# Switch to prod context
$ kubectx k8s-prod

# Get all namespaces in  k8s-prod context
$ kubens

# Switch to a namespace
$ kubens <namespacename>

See below gifs for demo:

kubectx demo GIF
kubens demo GIF

I hope our guide was helpful in your journey to Kubernetes management with kubectl and other tools like kubectx and kubens.

More guides:

Setup Kubernetes / OpenShift Dynamic Persistent Volume Provisioning with GlusterFS and Heketi

How To Configure Kubernetes Dynamic Volume Provisioning With Heketi & GlusterFS