Goglides Dev 🌱

Cover image for CKA/CKD-Kubectl Config – Usage
Balkrishna Pandey
Balkrishna Pandey

Posted on with Sanjog Pandey • Updated on

CKA/CKD-Kubectl Config – Usage

Whether you are new to Kubernetes or an experienced user, understanding the basics of kubectl config and how to use it effectively is essential for working with your cluster.

The kubectl config file contains all the necessary information about clusters, contexts, and users that you need to interact with your Kubernetes cluster. This file can be found in your user directory, default at ~\.kube\config.

The main components of kubectl config are clusters, contexts, and users. Each cluster contains information about the server and certificate data for that cluster, while each context includes details about the cluster and user as well as the namespace that you are currently using.

Take a look at this sample kubeconfig file, save this file to ~/.kube/sample-config and export KUBECONFIG variable as follows export KUBECONFIG=~/.kube/sample-config

Sample kubeconfig file

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
  name: goglides-dev
- cluster:
  name: goglides-scratch

users:
- name: mr-goglides-dev
- name: mr-goglides-qa

contexts:
- context:
  name: goglides-dev-frontend
- context:
  name: goglides-dev-backend
- context:
  name: goglides-dev-scratch
Enter fullscreen mode Exit fullscreen mode

To use kubectl config effectively, you will need to understand the purpose and functionality of these different components. Some key considerations include knowing how to create new clusters and contexts, managing user credentials, and migrating between different configurations or namespaces.
Some of the example of cluster specific configuration are as follows:

Lets do some operation using this config file,

  • Get List of clusters: kubectl config get-clusters Output:
NAME
goglides-dev
goglides-scratch
Enter fullscreen mode Exit fullscreen mode
  • Get list of users: kubectl config get-users Output:
NAME
mr-goglides-dev
mr-goglides-qa
Enter fullscreen mode Exit fullscreen mode
  • Gets list of context: kubectl config get-contexts Output:
CURRENT   NAME                    CLUSTER   AUTHINFO   NAMESPACE
      goglides-dev-backend                         
      goglides-dev-frontend                        
      goglides-dev-scratch  
Enter fullscreen mode Exit fullscreen mode
  • Use context: You can use context as follows,
kubectl config use-context goglides-dev-backend 
Enter fullscreen mode Exit fullscreen mode

Output:

Switched to context "goglides-dev-backend".
Enter fullscreen mode Exit fullscreen mode

And if you check the output you will "CURRENT" context has "*" on it.

kubectl config get-contexts
Enter fullscreen mode Exit fullscreen mode

Output:

CURRENT   NAME                    CLUSTER   AUTHINFO   NAMESPACE
*         goglides-dev-backend                         
        goglides-dev-frontend                        
        goglides-dev-scratch   
Enter fullscreen mode Exit fullscreen mode
  • Display current context.
kubectl config current-context
Enter fullscreen mode Exit fullscreen mode

Output:

goglides-dev-backend
Enter fullscreen mode Exit fullscreen mode
  • View config
  • in yaml view
kubectl config view
Enter fullscreen mode Exit fullscreen mode
  • in json format
kubectl config view -o json
Enter fullscreen mode Exit fullscreen mode

Output:

View Context

  • you can also get specific value using -o jsonpath
kubectl config view   -o jsonpath='{.users}'
Enter fullscreen mode Exit fullscreen mode

Output:

[{"name":"mr-goglides-dev","user":{}},{"name":"mr-goglides-qa","user":{}}]
Enter fullscreen mode Exit fullscreen mode
  • Delete specied cluster from kubeconfig.
kubectl config delete-cluster goglides-dev
Enter fullscreen mode Exit fullscreen mode

Output:

deleted cluster goglides-dev from /Users/bpandey/.kube/sample-config
Enter fullscreen mode Exit fullscreen mode

Now if you look at the cluster config you will following,
Cluster Config

And get cluster returns only one cluster

Get Cluster

  • Add cluster to kubeconfig You can add cluster back using
kubectl config set-cluster goglides-dev
Enter fullscreen mode Exit fullscreen mode

Get cluster should return 2 clusters now.

kubectl config get-clusters
Enter fullscreen mode Exit fullscreen mode

Output:

NAME
goglides-scratch
goglides-dev
Enter fullscreen mode Exit fullscreen mode

You can add more information to cluster for example api endpoints and skip tls verfication,

kubectl config set-cluster goglides-dev --server=https://127.0.0.1 --insecure-skip-tls-verify=true
Enter fullscreen mode Exit fullscreen mode

Now if you cat your sample-config you will following

cat ~/.kube/sample-config 
Enter fullscreen mode Exit fullscreen mode

Output:

apiVersion: v1
clusters:
- cluster:
    insecure-skip-tls-verify: true
    server: https://127.0.0.1
  name: goglides-dev
...
Enter fullscreen mode Exit fullscreen mode

There are other combinations of this command; if you know anything interesting use cases you are solving using this command, do not forget to share it with us in the comments! We look forward to hearing from you.

That's all for now! Thanks for reading! Stay tuned for more updates on #kubernetes, #cka #ckd in general, and its use cases.

Top comments (0)