What is kubernetes context and kubernetes context Tutorial

What is kubernetes context and kubernetes context Tutorial

 A kubernetes context is just a set of access parameters that contains a Kubernetes cluster, a user, and a namespace. kubernetes Context is essentially the configuration that you use to access a particular cluster & namespace with a user account.

To View The Current Context

To know what is the current context or To view the current context use below command

kubectl config current-context

master $ kubectl config current-context

kubernetes-admin@kubernetes

It will display the current context which you are using.

View Context Configuration

master $ kubectl config view

apiVersion: v1
clusters:
- cluster:
    certificate-authority-data: DATA+OMITTED
    server: https://172.17.0.110:6443
  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

it will give you current context configuration. you can find this same info in $HOME/.kube/KUBECONFIG OR $HOME/.kube/config file. This file also stores authentication information such as username/passwords, certificates or tokens.

in the above command output you can see contexts section. This section sets our default cluster, namespace and user that kubectl will use with its commands.

Cretae New Context:

kubectl config set-context dev --namespace=development --cluster=lithe-cocoa-92103_kubernetes --user=lithe-cocoa-92103_kubernetes

kubectl config set-context prod --namespace=production --cluster=lithe-cocoa-92103_kubernetes --user=lithe-cocoa-92103_kubernetes

The above commands will create two new contexts. You can alternate against depending on what namespace you wish to work against. By default, the above commands adds two contexts that are saved into file .kube/config. You can now view the contexts and alternate against the two new request contexts depending on which namespace you wish to work against.

Switch to Different Context:

if you want  to operate in the production namespace. we have to switch prod context. use below command to switch into prod context.

kubectl config use-context prod

You can verify your current context by doing the following:

master $ kubectl config current-context

prod

At this point, all requests we make to the Kubernetes cluster from the command line are scoped to the production namespace.

Update Context:

kubectl config set-context dev --namespace=development --cluster=lithe-cocoa-92103_kubernetes --user=lithe-cocoa-92103_kubernetes

To update a context you can use same command which you used to create context. Here you can modify namespace, cluster, user values and hit enter, your dev context will be modified.

List Your Kubernetes Contexts

kubectl config get-contexts

This will show all your configured contexts in Kubernetes.

 

  • What is kubernetes context and kubernetes context Tutorial
  • kubernetes context switch
  • kubernetes context file
  • kubernetes context list
  • kubernetes context user
  • kubectl delete context
  • kubectl connect to cluster
  • kubectl config set-credentials

Leave a Reply

Your email address will not be published. Required fields are marked *