

WHAT IS KUBERNETES CONTEXT HOW TO
In this section, you’ll look at how to configure access to other clusters in a more complex situation. # Allows you to switch between contexts using their nameĪdvanced Usage of kubectl config set-context To do this, you can to use the following command: Basic Usage of kubectl config set-contextīefore you start modifying your kubeconfig, it’s helpful to know how to display its content. If the specified context already exists in the kubeconfig, this command is going to modify that entry with the parameters that are passed. This command also accepts the name of the context to be changed (or -current if you want to change the current context), as well as -user, -cluster, and -namespace options. What this says is that you can create or modify contexts in your kubeconfig file with the command kubectl config set-context. current=false: Modify the current context Kubectl config set-context gce -user=cluster-admin # Set the user field on the gce context entry without touching other values Specifying a name that already exists will merge new fields on top of existing values for those fields. Which brings us to kubectl set-context, which is used to set a context entry in kubeconfig. Interestingly, it also contains current-context which means all of the kubectl commands are going to run in that context unless that is changed. In this example, the kubeconfig contains only one cluster, user, and context. Users: The users section identifies each user by a unique name and includes the relevant authentication information, which could be client certificates (as is the case here), bearer tokens, or an authenticating proxy.Note that in the example, the namespace information is omitted because it’s using the default namespace. Each context consists of the cluster name, user, and namespace that you can access when you invoke it. Contexts: This is the most relevant section to this tutorial, since it lists the available contexts.Each cluster contains details about the URL for the Kubernetes API server (where the cluster accepts commands from kubectl), certificate authority, and a name to identify the cluster. Clusters: This section lists all the clusters that you have access to.Typically there are three parts to a kubeconfig: Let’s take a look at a cross section of a typical kubeconfig structure to see how cluster, user, and context tie up together. The Kubernetes API server itself doesn’t recognize context the way it does other objects such as pods, deployments, or namespaces. It’s important to understand that a Kubernetes context only applies to the client side. In Kubernetes, a context is an entity defined inside kubeconfig to alias cluster parameters with a human-readable name. In this article, you’ll take a closer look at how kubeconfig is structured, how to define contexts in kubeconfig, and how to effectively use set-context to manipulate different contexts. This can be done with the kubectl config set-context command. To use that command, though, you need to configure contexts in your kubeconfig. You can quickly switch between clusters by using the kubectl config use-context command. This allows you to define multiple contexts in your configuration file, which you can then use to target multiple Kubernetes clusters, or the same cluster with a different set of users or namespaces. You can think of Kubernetes contexts as a kind of shortcut that allows you to access cluster, user, and namespace parameters conveniently. This is where Kubernetes contexts come into the picture. Combined with multiple clusters, every kubectl command is going to look something like kubectl -namespace -kubeconfig …, which is both time consuming and an easy place for errors to slip in. If your clusters have multiple namespaces, another pain point appears: the need to specify -namespace or -n every time you use kubectl. If you are working with multiple Kubernetes clusters, it can quickly become cumbersome to manage configuration files like this as you are switching from one cluster to the other.
