Join our Discord Server
Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps Enthusiast👨‍💻 | Python🐍 |

A Complete Guide to Kubectl Commands

3 min read

Introduction

kubectl is a command-line tool for interacting with Kubernetes clusters, enabling users to manage, inspect, and debug workloads in the cluster. Whether you’re deploying applications, viewing logs, or scaling services, kubectl is the primary interface for these operations. This guide provides a comprehensive overview of essential kubectl commands, walking you through their usage to efficiently manage your Kubernetes cluster.

By the end of this guide, you will understand the most common kubectl commands and how to use them effectively in your workflow.

Prerequisites

Before using kubectl:

  1. A Kubernetes cluster running locally (e.g., via Minikube) or on a cloud provider (AWS, GCP, etc.).
  2. kubectl installed on your machine. Follow these instructions to install kubectl.
  3. Access to the Kubernetes cluster, with necessary permissions.

Configure Access to Your Cluster

Before issuing any kubectl commands, ensure your cluster is configured in the tool. You can switch between clusters and namespaces using the following:

Check Kubernetes Configurations

kubectl config view

This displays the current configuration file used by kubectl, including active contexts, clusters, and users.

Switch Between Contexts

kubectl config use-context CONTEXT_NAME

This switches kubectl to the specified context (i.e., cluster environment).

Set Namespace

Namespaces help organize resources within a cluster. To set a default namespace for your commands:

kubectl config set-context --current --namespace=NAMESPACE_NAME

This makes the specified namespace the default for subsequent commands.

Working with Pods

Pods are the smallest deployable units in Kubernetes. They contain one or more containers that share storage and network resources.

List Pods

kubectl get pods

This lists all the pods running in your cluster’s default namespace. To view pods in a specific namespace:

kubectl get pods -n NAMESPACE_NAME

Get Detailed Pod Information

For more information about a specific pod:

kubectl describe pod POD_NAME

This provides details such as container images, environment variables, events, and current status.

View Pod Logs

To view the logs of a pod (helpful for troubleshooting):

kubectl logs POD_NAME

For a pod with multiple containers, specify the container name:

kubectl logs POD_NAME -c CONTAINER_NAME

Create a Pod

Pods are usually created through deployment manifests, but you can also create one directly via kubectl:

kubectl run my-pod --image=nginx

This creates a pod named my-pod running an Nginx container.

Delete a Pod

To remove a pod from your cluster:

kubectl delete pod POD_NAME

This stops and removes the pod.

Managing Deployments

A Deployment ensures the desired number of pod replicas are running. If a pod crashes, the Deployment automatically starts a new one.

Create a Deployment

kubectl create deployment my-deployment --image=nginx

This creates a deployment called my-deployment, managing a single pod running an Nginx container.

Scale a Deployment

To scale a deployment up or down:

kubectl scale deployment my-deployment --replicas=3

This adjusts the number of replicas in the deployment to 3.

View Deployment Status

kubectl get deployments

This shows the status of all deployments in the default namespace.

Update a Deployment

To update the image of a deployment, use:

kubectl set image deployment/my-deployment nginx=nginx:1.19.10

This updates the Nginx container in the my-deployment deployment to version 1.19.10.

Rollback a Deployment

To undo a recent change to a deployment:

kubectl rollout undo deployment/my-deployment

This reverts the deployment to its previous state.

Delete a Deployment

To remove a deployment and its associated pods:

kubectl delete deployment my-deployment

This stops the deployment and removes its pods.

Managing Services

Services expose your application to other applications or users. Kubernetes provides several types of services, such as ClusterIP, NodePort, and LoadBalancer.

Create a Service

Expose a deployment as a service using:

kubectl expose deployment my-deployment --type=ClusterIP --port=80

This creates a service exposing my-deployment on port 80 inside the cluster.

List Services

kubectl get services

This lists all services in your cluster.

Delete a Service

kubectl delete service my-deployment

This removes the service, but does not affect the deployment or its pods.

Working with ConfigMaps and Secrets

ConfigMaps and Secrets are used to inject configuration data or sensitive information into your pods.

Create a ConfigMap

To create a ConfigMap from a file:

kubectl create configmap my-config --from-file=config-file.conf

To create it from a literal value:

kubectl create configmap my-config --from-literal=key1=value1

View ConfigMaps

kubectl get configmaps

This shows all ConfigMaps in your namespace.

Create a Secret

Secrets store sensitive information, such as passwords or API tokens. To create a secret:

kubectl create secret generic my-secret --from-literal=password=mypassword

View Secrets

kubectl get secrets

Secrets are base64-encoded by default for security purposes.

Troubleshooting

Kubernetes provides various commands to inspect and debug workloads when things go wrong.

Get Pod Events

Events give insight into actions happening in your cluster. To view events for a specific pod:

kubectl get events --field-selector involvedObject.name=POD_NAME

This shows events related to the specified pod.

Debug a Pod

To run a command inside a running pod, use kubectl exec. For example, to open a shell session:

kubectl exec -it POD_NAME -- /bin/bash

This opens a bash session inside the pod, enabling you to debug from within.

Inspect Cluster Resources

To inspect your cluster’s node status:

kubectl get nodes

This lists all nodes in your cluster. To view node details:

kubectl describe node NODE_NAME

Port Forwarding

You can use port forwarding to access a pod’s service locally. For example:

kubectl port-forward pod/POD_NAME 8080:80

This forwards traffic from your local port 8080 to the pod’s port 80.

Conclusion

In this guide, you have learned the essential kubectl commands to manage Kubernetes workloads, from creating pods and deployments to troubleshooting and managing configurations. Mastering these commands is crucial for efficiently managing Kubernetes clusters and maintaining a smooth workflow.

For more advanced use cases and best practices, refer to the official Kubernetes documentation.

Have Queries? Join https://launchpass.com/collabnix

Abraham Dahunsi Web Developer 🌐 | Technical Writer ✍️| DevOps Enthusiast👨‍💻 | Python🐍 |
Join our Discord Server
Index