In this article, we will learn the difference between a context and a cluster in Kubernetes.
What is a Kubernetes Config file?
A Kubernetes configuration file is a YAML file that contains information about the cluster, such as the cluster name, the cluster’s certificate authority (CA) certificate, and the cluster’s private key. It also contains information about the user, such as the user’s certificate and private key. The configuration file is used by the kubectl command-line tool to authenticate and access the cluster.
What is a Cluster?
A Kubernetes cluster is a set of physical or virtual machines (nodes) that work together to run containerized applications. It consists of a master node that manages the entire cluster and multiple worker nodes that run the containers. The master node is responsible for scheduling and coordinating tasks, while the worker nodes execute those tasks.
Clusters in Kubernetes are identified by their respective Certifate Authority (CA) certificate and private key. The CA certificate is used to sign the certificates of all the nodes in the cluster. The private key is used to sign the certificates of all the nodes in the cluster. The CA certificate and private key are stored in the Kubernetes configuration file.
For example, in your Kubernetes configuration file, you might have the following:
In this example, the development cluster is identified by the CA certificate and private key. The test cluster is identified by the server IP address and is not authenticated.
What is a User?
In Kubernetes, users represent individual users or processes that interact with the cluster. Each user has its own credentials (e.g., certificates, tokens) to authenticate and access the cluster’s resources. A user can have access to multiple Kubernetes clusters. For example, you might have the following:
- name: developer
- name: experimenter
What is a Namespace?
Namespaces provide a way to divide a single Kubernetes cluster into multiple virtual clusters. They are like separate virtual clusters within the same physical cluster. Namespaces help in organizing and isolating resources, so different teams or projects can have their resources without interfering with each other.
What is a Context?
A context in Kubernetes is a way to manage multiple clusters and their authentication information. It includes information about the cluster, the user, and the namespace to be used. You can have different contexts to switch between different Kubernetes clusters easily.
Each context contains a Kubernetes cluster, a user, and a namespace. The current context is the cluster that is currently the default for kubectl, and is the argument passed to the –context flag. You can specify other contexts on the command line using the –context flag.
For example, you might have the following:
In this case, referring to
Context1 means using the
development cluster with the
developer user in the
frontend namespace . Referring to
Context2 means using the
test cluster with the
It is important to note that the
Context terminology is only used by the
kubectl command-line tool. The Kubernetes API does not use the term
How to create a new context?
You can create a new context using the
kubectl config set-context command. Example:
kubectl config set-context Context1 --cluster=development --user=developer --namespace=frontend
How to switch between contexts?
You can easily switch between contexts using the
kubectl config use-context command. Example:
kubectl config use-context Context1
How to list all contexts?
You can list all contexts using the
kubectl config get-contexts command. Example:
kubectl config get-contexts
In this article, we learned the difference between a context and a cluster in Kubernetes. We also learned about the Kubernetes configuration file, clusters, users, namespaces, and contexts.