Introduction
Kubeconfig files are used to configure access to a Kubernetes cluster. They specify the following information:
- The cluster to use
- The user to authenticate with
- The context to use (which combines a cluster and user)
By default, kubectl uses the ~/.kube/config file as the kubeconfig file. However, you can specify a different kubeconfig file with the --kubeconfig flag.
Start the Minikube Cluster
Before creating resources, you need a running Kubernetes cluster. Minikube is a lightweight Kubernetes environment that runs on your local machine.
Navigate to your working directory:
Open the terminal and navigate to the default project folder:
cd /home/labex/projectStart Minikube:
Start Minikube to initialize a Kubernetes cluster:
minikube start- This command sets up a single-node Kubernetes cluster on your local machine.
- Minikube may take a few minutes to start depending on your system's performance.
Verify Minikube is running:
Check the status of the Minikube cluster:
minikube status- Look for components like
kubeletandapiserverlisted asRunning. - If the cluster is not running, rerun
minikube start.
- Look for components like
If you encounter issues starting Minikube. Use minikube delete to reset the environment if needed.
View the Kubeconfig File
Before modifying the kubeconfig file, let's view its contents. Use the following command to view the contents of the kubeconfig file:
kubectl config view
This command displays the entire kubeconfig file, including all clusters, users, and contexts.
Add a Cluster to the Kubeconfig File
To add a cluster to the kubeconfig file, use the kubectl config set-cluster command. This command requires the name of the cluster, the server URL, and the certificate authority (CA) data. Here's an example:
kubectl config set-cluster my-cluster \
--server=https://kubernetes.default.svc \
--certificate-authority=/home/labex/.minikube/ca.crt
This command adds a cluster named my-cluster to the kubeconfig file, with the specified server URL and CA data.
Add a User to the Kubeconfig File
To add a user to the kubeconfig file, use the kubectl config set-credentials command. This command requires the name of the user, the user's client certificate, and the user's client key. Here's an example:
kubectl config set-credentials my-user \
--client-certificate=/home/labex/.minikube/profiles/minikube/client.crt \
--client-key=/home/labex/.minikube/profiles/minikube/client.key
This command adds a user named my-user to the kubeconfig file, with the specified client certificate and key.
Create a Context in the Kubeconfig File
To create a context in the kubeconfig file, use the kubectl config set-context command. This command requires the name of the context, the cluster to use, and the user to authenticate with. Here's an example:
kubectl config set-context my-context \
--cluster=my-cluster \
--user=my-user
This command creates a context named my-context in the kubeconfig file, using the my-cluster cluster and the my-user user.
Use a Context in the Kubeconfig File
To use a context in the kubeconfig file, use the kubectl config use-context command. This command requires the name of the context to use. Here's an example:
kubectl config use-context my-context
This command sets the current context to my-context, so all subsequent kubectl commands will use the specified cluster and user.
Summary
In this lab, you learned how to modify kubeconfig files using the kubectl command-line tool. You learned how to add a cluster, a user, and a context to the kubeconfig file, and how to use a context to authenticate to a Kubernetes cluster.


