menu
The kubernetes concept (and term) context only applies in the kubernetes client-side, i.e. the place where you run the kubectl command, e.g. your command prompt. The kubernetes server-side doesn't recognise this term 'context'.

As an example, in the command prompt, i.e. as the client:

  • when calling the kubectl get pods -n dev, you're retrieving the list of the pods located under the namespace 'dev'.
  • when calling the kubectl get deployments -n dev, you're retrieving the list of the deployments located under the namespace 'dev'.
If you know that you're targetting basically only the 'dev' namespace at the moment, then instead of adding "-n dev" all the time in each of your kubectl commands, you can just:

  1. Create a context named 'context-dev'.
  2. Specify the namespace='dev' for this context.
  3. Set the current-context='context-dev'.
This way, your commands above will be simplified to as followings:

  • kubectl get pods
  • kubectl get deployments
You can set different contexts, such as 'context-dev', 'context-staging', etc., whereby each of them is targeting different namespace. BTW it's not obligatory to prefix the name with 'context'. You can also the name with 'dev', 'staging', etc.

Source: https://stackoverflow.com/questions/61171487/what-is-the-difference-between-namespaces-and-contexts-in-kubernetes