Pages

Friday, November 16, 2018

Kubernetes - Context

 
 Kubernetes supports virtual clusters. I.e we can create virtual clusters backed by the same physical cluster. We can then provide these virtual cluster like dev, test etc to the application teams. These virtual clusters are called as name spaces. 

When ever we create a pod or when we try to get the list of pods, kubectl will use the default namespace. So whenever we need to deploy a pod,it gets deployed to the default namespace. If we need to deploy to a particular namespace, we need to run “kubectl create -f <yml> --namespace=dev” 

This can be hard as we need to provide the namespace for all kubectl commands to create workload or to list workload. Kubernetes provides us a way for not providing the namespace element with kubectl command every time. This is done by using context. 

NameSpaces are easy to create and use but it is also easy to deploy code into a wrong namespace. The other way to avoid using wrong namespace is to use context and this context can be set using kubectl context command 

Context - A Context in a kubernetes is a combination of cluster information,user for authentication and a namespace 

List the current context using, 
[root@ip-172-31-12-239 ~]# kubectl config current-context 
kubernetes-admin@kubernetes 

List all config info including contexts,  
[root@ip-172-31-12-239 ~]# kubectl config view 
apiVersion: v1 
clusters: 
- cluster: 
   certificate-authority-data: DATA+OMITTED 
 name: kubernetes 
contexts: 
- context: 
   cluster: kubernetes 
   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 

To check which context is in use, 
[root@ip-172-31-12-239 ~]# kubectl config get-contexts 
CURRENT   NAME                                       CLUSTER      AUTHINFO     NAMESPACE 
*               kubernetes-admin@kubernetes   kubernetes   kubernetes-admin 

We can see the “*” before the kubernetes-admin@kubernetes which means that we are using this context that belongs to the kubernetes cluster. 

Lets create a context and see how it works, 
[root@ip-172-31-12-239 ~]# kubectl config set-context project1 --namespace=project1 --cluster=kubernetes --user=kubernetes-admin 
Context "project1" created. 

List the available contexts using, 
[root@ip-172-31-12-239 ~]# kubectl config get-contexts 
CURRENT   NAME                                CLUSTER        AUTHINFO           NAMESPACE 
*         kubernetes-admin@kubernetes   kubernetes    kubernetes-admin 
          project1                                    kubernetes    kubernetes-admin  project1 

Change the context to project1 using, 
[root@ip-172-31-12-239 ~]# kubectl config use-context project1 
Switched to context "project1". 

Make sure you are using the new context, 
[root@ip-172-31-12-239 ~]# kubectl config get-contexts 
CURRENT   NAME                                      CLUSTER     AUTHINFO             NAMESPACE 
                 kubernetes-admin@kubernetes  kubernetes   kubernetes-admin 
*               project1                                   kubernetes   kubernetes-admin  project1 

Now we are in project1 context and whenever we try to deploy things or list things, kubectl will use the current context which is “project1” 

This way we can let application teams to deploy things to context without providing the context name to the kubectl command. These context also eliminate deploying things to wrong namespaces.

No comments :

Post a Comment