Skip to content

2.2 kubectl

1. config

Play

kubectl

we’ll explore how to use Kubectl to manage your Kubernetes clusters, focusing on configuration, retrieving resources, working with namespaces, describing resources, and checking versions.

1.1 Configuring Kubectl for Multiple Clusters

Kubectl can point to multiple Kubernetes clusters, making it easy to switch between development, staging, and production environments. The configuration is managed through contexts, which are stored in a kubeconfig file. Here’s how you can configure and switch contexts:

1. Checking Your Current Context

To find out which Kubernetes cluster your Kubectl is currently pointing to, use the following command:

Terminal window
kubectl config current-context

This command outputs the name of the current context, indicating the active cluster we are pointing at.

2. Listing All Available Contexts

To see all the contexts available in your kubeconfig file, run:

Terminal window
kubectl config get-contexts

This command displays a table of all contexts, clusters, and users, with a * next to the currently active context.

Output:

Terminal window
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
* docker-desktop docker-desktop docker-desktop

3. Switching to a Different Context

To switch to another context, such as a production environment, use:

Terminal window
kubectl config use-context [context-name]

Replace [context-name] with the name of the context you want to switch to. This command changes the active cluster to the specified context.

4. Using Multiple Kubeconfig Files

If you manage multiple clusters with different configuration files, you can temporarily set the KUBECONFIG environment variable:

Terminal window
export KUBECONFIG=$HOME/.kube/config:$HOME/.kube/config2

This command merges the configurations from both files, allowing Kubectl to use them simultaneously.


1.2 Retrieving Kubernetes Resources

One of the primary tasks in managing Kubernetes clusters is retrieving information about the resources. Kubectl provides several commands for this purpose:

1. Listing Pods

To list all pods in the current namespace, use:

Terminal window
kubectl get pods

This command outputs a table with pod names, statuses, and other details.

2. Listing Deployments

To list all deployments in the current namespace, run:

Terminal window
kubectl get deployments

This command provides information about all deployments, including the number of replicas and their current status.

3. Listing Services

To see all services running in the current namespace, use:

Terminal window
kubectl get services

This command displays a table listing each service’s name, type, cluster IP, external IP, and ports.

4. Listing ConfigMaps

To list all config maps, which store non-confidential data in key-value pairs, use:

Terminal window
kubectl get configmaps

This command shows a table with all config maps in the current namespace.

5. Listing Secrets

To view all secrets, which store sensitive information like passwords and tokens, run:

Terminal window
kubectl get secrets

This command outputs a table of all secrets, showing their names and types.


1.3 Working with Namespaces

Namespaces in Kubernetes allow you to create virtual clusters within a physical cluster, enabling better resource management and isolation.

1. Listing All Namespaces

To see all namespaces in your cluster, use:

Terminal window
kubectl get namespaces

This command lists all namespaces, showing their names and statuses.

2. Creating a New Namespace

To create a new namespace called test, run:

Terminal window
kubectl create namespace test

This command creates a new namespace that can be used to isolate resources.

3. Listing Pods in a Specific Namespace

To list all pods within a specific namespace, use:

Terminal window
kubectl get pods -n [namespace-name]

Replace [namespace-name] with the name of the namespace you’re interested in. This command outputs a table of pod details for the specified namespace.

Example
Terminal window
PS> kubectl get pods -n kube-system
NAME READY STATUS RESTARTS AGE
coredns-76f75df574-45v4c 1/1 Running 0 6d14h
coredns-76f75df574-mqxf4 1/1 Running 0 6d14h
etcd-docker-desktop 1/1 Running 0 6d14h
kube-apiserver-docker-desktop 1/1 Running 0 6d14h
kube-controller-manager-docker-desktop 1/1 Running 0 6d14h
kube-proxy-6k46f 1/1 Running 0 6d14h
kube-scheduler-docker-desktop 1/1 Running 0 6d14h
storage-provisioner 1/1 Running 0 6d14h
vpnkit-controller 1/1 Running 0 6d14h

1.4 Describing Resources

For a more in-depth look at the state and events related to specific resources, Kubectl’s describe command is invaluable.

1. Describing a Pod

To get detailed information about a specific pod, including its status, events, and resource usage, use:

Terminal window
kubectl describe pod [pod-name] -n [namespace-name]

Replace [pod-name] and [namespace-name] with the appropriate pod and namespace names. This command provides a wealth of information useful for debugging and monitoring.

2. Describing a Node

To investigate the details of a specific node, especially when facing infrastructure issues, run:

Terminal window
kubectl describe node [node-name]

This command provides comprehensive information about the node, including its conditions, capacity, and recent events.


1.5 Checking Versions

Knowing the versions of both your Kubectl client and Kubernetes server is crucial, especially when managing clusters with different versions.

1. Checking Kubectl and Kubernetes Versions

To see the version of your Kubectl client and the Kubernetes server, use:

Terminal window
kubectl version

This command outputs the client version (Kubectl) and the server version (Kubernetes cluster).