Context:Used to group the access parameters under an easily recognizable name in a kubeconfig file.
Its a connection to particular cluster used by kubectl.
Context consist of Cluster Name, Namespace and a user and the configuration (CERTS/KEYS) to access the cluster.
The term context applies only to the client side. K8s server side does not recognize the term 'context'.
Let me start with creating a namespace called "prod" and create a pod inside it.
root@masterk8s:~# kubectl create ns prod
namespace/prod created
root@masterk8s:~#
root@masterk8s:~# kubectl run nginx --image=nginx -n prod
pod/nginx created
root@masterk8s:~#
root@masterk8s:~# kubectl get pods -n prod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 21s
root@masterk8s:~#
When calling the kubectl get pods -n prod, you're retrieving the list of the pods located under the namespace 'prod'.
If you know that you're targetting basically only the 'prod' namespace at the moment, then instead of adding "-n dev" all the time in each of your kubectl commands, you can just switch to the namespace "prod" and issue the commands:
> kubectl get pods
root@masterk8s:~# kubectl config set-context --current --namespace prod
Context "kubernetes-admin@kubernetes" modified.
root@masterk8s:~# kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* kubernetes-admin@kubernetes prod-kubernetes kubernetes-admin prod
root@masterk8s:~#
root@masterk8s:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 4m39s
root@masterk8s:~#
A context is the connection to a specific cluster (username/apiserver host) used by kubectl.
You can manage multiple clusters that way. Namespace is a logical partition inside a specific cluster to manage resources and constraints.
root@masterk8s:~# cat /root/.kube/config | grep -i namespace
namespace: prod
root@masterk8s:~#
Context contains Cluster, Namespace, user.
root@masterk8s:~# kubectl config current-context
kubernetes-admin@kubernetes
root@masterk8s:~#
Based on my setup my context is named kubernetes-admin@kubernetes. Lets take a closer look at the configuration by running:
root@masterk8s:~# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.163.128:6443
name: prod-kubernetes
contexts:
- context:
cluster: prod-kubernetes
namespace: prod
user: kubernetes-admin
name: kubernetes-admin@kubernetes
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
root@masterk8s:~#
You can probably find this same information if you open your KUBECONFIG file in a text editor. NOTE: the KUBECONFIG file is probably not named “kubeconfig”.
Now let’s update our KUBECONFIG with our new namespace that we created. We can do this via kubectl by running the following command:
root@masterk8s:~# kubectl config set-context prod-context --namespace=prod --cluster=prod-kubernetes --user=kubernetes-admin
Context "prod-context" created.
root@masterk8s:~#
You can create your own context name and change the namespace as you see fit.
Once we’ve set a new context, we can re-run the “config view” command to see if our context changed at all.
root@masterk8s:~# kubectl config view
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: DATA+OMITTED
server: https://192.168.163.128:6443
name: prod-kubernetes
contexts:
- context:
cluster: prod-kubernetes
namespace: prod
user: kubernetes-admin
name: kubernetes-admin@kubernetes
- context:
cluster: prod-kubernetes
namespace: prod
user: kubernetes-admin
name: prod-context
current-context: kubernetes-admin@kubernetes
kind: Config
preferences: {}
users:
- name: kubernetes-admin
user:
client-certificate-data: REDACTED
client-key-data: REDACTED
root@masterk8s:~#
Lets switch the context.
root@masterk8s:~# kubectl config use-context prod-context
Switched to context "prod-context".
root@masterk8s:~#
root@masterk8s:~# kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
kubernetes-admin@kubernetes prod-kubernetes kubernetes-admin prod
* prod-context prod-kubernetes kubernetes-admin prod
root@masterk8s:~#
root@masterk8s:~# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 12m
root@masterk8s:~#
Comments
Post a Comment