KubernetesKubectl

Kubectl

Kubectl is a powerful tool for interacting with Kubernetes clusters. For installation, see official documentation. After installation, a kube config file is required to function. The file is located in cluster overview dashboard (click on the cluster name in the upper left dropdown menu).

kube config

Copy contents of this file into $HOME/.kube/config and change permissions to 700 (chmod 700 $HOME/.kube/config).

NOTE: If the file $HOME/.kube/config does not exist, go ahead and create it, then copy the contents

It is possible to have multiple cluster configurations in one config file.

Multiple clusters

One person can posses access rights to multiple clusters. To work with them, every cluster’s config must be available in either:

  • $HOME/.kube/config
  • in separate file but then -f [filename] must be specified when using kubectl

It is much more convenient to squash multiple cluster configs into one because you do not have to worry where your namespace lies.

Merging configs

If your config already has some content, it looks similar to:

config
apiVersion: v1
kind: Config
clusters:
- name: "kuba-cluster"
  cluster:
    server: "https://rancher.cloud.e-infra.cz/k8s/clusters/c-qgxlf"
 
users:
- name: "kuba-cluster"
  user:
    token: [token]
 
 
contexts:
- name: "kuba-cluster"
  context:
    user: "kuba-cluster"
    cluster: "kuba-cluster"
 
current-context: "kuba-cluster"

To add another cluster, go to its config file in Rancher and do not copy whole file, only necessary parts, namely:

  • item under clusters section which will be placed as new item under clusters in existing config
  • item under contexts section which will be placed as new item under contexts in existing config
  • item under users section which will be placed as new item under users in existing config

This is config you want to add. Copy only highlighted parts.

config
apiVersion: v1
kind: Config
clusters:
- name: "hdhu-cluster"
  cluster:
    server: "https://rancher.cloud.e-infra.cz/k8s/clusters/c-sprdw"
 
users:
- name: "hdhu-cluster"
  user:
    token: [token]
 
contexts:
- name: "hdhu-cluster"
  context:
    user: "hdhu-cluster"
    cluster: "hdhu-cluster"
 
current-context: "hdhu-cluster"

and place them in existing config file. The token value is unique but always the same across all clusters for every logged in person . Therefore you could have only item under users section and change to this value in each user occurrence in contexts. Nevertheless, it is simpler and not so prone to errors when the value is copied and used as is. Final file looks similar to this:

config
apiVersion: v1
kind: Config
clusters:
- name: "kuba-cluster"
  cluster:
    server: "https://rancher.cloud.e-infra.cz/k8s/clusters/c-qgxlf"
- name: "hdhu-cluster"
  cluster:
    server: "https://rancher.cloud.e-infra.cz/k8s/clusters/c-sprdw"
 
users:
- name: "kuba-cluster"
  user:
    token: [token]
- name: "hdhu-cluster"
  user:
    token: [token]
 
contexts:
- name: "kuba-cluster"
  context:
    user: "kuba-cluster"
    cluster: "kuba-cluster"
- name: "hdhu-cluster"
  context:
    user: "hdhu-cluster"
    cluster: "hdhu-cluster"
 
current-context: "kuba-cluster"