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
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
- Get list of users:
kubectl config get-users
Output:
NAME
mr-goglides-dev
mr-goglides-qa
- Gets list of context:
kubectl config get-contexts
Output:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
goglides-dev-backend
goglides-dev-frontend
goglides-dev-scratch
- Use context: You can use context as follows,
kubectl config use-context goglides-dev-backend
Output:
Switched to context "goglides-dev-backend".
And if you check the output you will "CURRENT" context has "*" on it.
kubectl config get-contexts
Output:
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* goglides-dev-backend
goglides-dev-frontend
goglides-dev-scratch
- Display current context.
kubectl config current-context
Output:
goglides-dev-backend
- View config
- in
yaml
view
kubectl config view
- in
json
format
kubectl config view -o json
Output:
- you can also get specific value using
-o jsonpath
kubectl config view -o jsonpath='{.users}'
Output:
[{"name":"mr-goglides-dev","user":{}},{"name":"mr-goglides-qa","user":{}}]
- Delete specied cluster from kubeconfig.
kubectl config delete-cluster goglides-dev
Output:
deleted cluster goglides-dev from /Users/bpandey/.kube/sample-config
Now if you look at the cluster config you will following,
And get cluster returns only one cluster
- Add cluster to kubeconfig You can add cluster back using
kubectl config set-cluster goglides-dev
Get cluster should return 2 clusters now.
kubectl config get-clusters
Output:
NAME
goglides-scratch
goglides-dev
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
Now if you cat your sample-config you will following
cat ~/.kube/sample-config
Output:
apiVersion: v1
clusters:
- cluster:
insecure-skip-tls-verify: true
server: https://127.0.0.1
name: goglides-dev
...
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)