The Ultimate kubectl Command Guide: 101 Essential Commands for Kubernetes Management

Ram Kumar

Ram Kumar

October 26, 20247 min read

The Ultimate kubectl Command Guide: 101 Essential Commands for Kubernetes Management

kkubectl is the command-line interface for managing Kubernetes clusters, serving as an essential tool for developers and administrators alike. It enables seamless interaction with Kubernetes resources, making it indispensable for deploying, scaling, and troubleshooting applications within the Kubernetes ecosystem.

This guide is the first installment of our series on 101 essential kubectl commands, and in this section, we’ll cover 25 commands designed to streamline your daily tasks, from basic cluster management to advanced resource handling.

To get started, we’ll provide a quick overview of the installation process, recommend the best terminal options, and guide you through configuring your Kubernetes cluster. With this foundation, you'll be well-equipped to navigate and manage your Kubernetes environment effectively.

Getting Started with kubectl

1. Install kubectl

The kubectl command-line tool is available on multiple platforms. Here’s how to install it based on your operating system:

  • On macOS:
brew install kubectl

On Linux:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/

On Windows: Download the latest release from the Kubernetes GitHub repository and move it to your desired location:

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/windows/amd64/kubectl.exe"

Verify the installation with:

kubectl version --client

2. Setting Up a Kubernetes Cluster

You need a Kubernetes cluster to use kubectl. Here are a few popular ways to set up a cluster:

  • Minikube: Deploys a local Kubernetes cluster for testing.
minikube start

Kubernetes in Docker (Kind): Runs a Kubernetes cluster in Docker containers.

kind create cluster
  • Managed Kubernetes Services: Google Kubernetes Engine (GKE), Amazon EKS, and Azure AKS offer fully managed clusters.

3. Configure Your kubectl Context

Configure kubectl to connect to your cluster using contexts, which allow you to switch between clusters.

kubectl config get-contexts          # List available contexts
kubectl config use-context <context> # Switch to a specific context

4. Choosing Your Terminal

The terminal you use with kubectl can make a big difference. Here are some popular choices:

  • iTerm2 (macOS): A versatile terminal with split panes and customization options.
  • Windows Terminal (Windows): Supports multiple tabs and integrates well with WSL.
  • Alacritty (Cross-platform): A fast, GPU-accelerated terminal.
  • VS Code Terminal: Integrated with Visual Studio Code, it allows managing Kubernetes alongside code.

Key Kubernetes Resources

Before diving into commands, here’s a brief description of core Kubernetes resources:

Namespaces

Namespaces in Kubernetes are used to organize and manage resources in isolated environments within a cluster. This allows teams to work in dedicated environments, avoiding conflicts and enabling resource quotas.

Nodes

Nodes are the machines (virtual or physical) that run Kubernetes workloads. Each node contains the services necessary to run “pods” and is managed by the master node.

Pods

A Pod is the smallest and simplest Kubernetes object. It represents a group of containers that share resources and network configurations. Pods enable microservices to work together as a unit.

Services

A Service in Kubernetes defines a stable network endpoint for accessing a set of pods. Services decouple frontend and backend applications by offering reliable connectivity, even when individual pods are added or removed.

Deployments

A Deployment specifies how pods should be created, managed, and scaled. They help maintain a desired number of replicas and allow rolling updates to ensure zero downtime.

ConfigMaps and Secrets

ConfigMaps store configuration data in key-value pairs, whereas Secrets securely hold sensitive information, such as API keys and passwords, which can be injected into containers.

101 Essential kubectl Commands

With these core concepts in mind, let’s dive into the commands! Each command below is categorized by resource type to help you find the most relevant commands quickly.

1. Get Cluster Information

kubectl cluster-info

Provides details on the Kubernetes master and services running within the cluster. This is typically one of the first commands to check cluster status and availability.

2. List All Nodes in the Cluster

kubectl get nodes

Lists all nodes in your Kubernetes cluster, showing their status, roles, and age. Knowing the state of nodes is essential to understand the cluster's current health.

3. Describe a Specific Node

kubectl describe node <node-name>

Gives detailed information about a specific node, including conditions, capacity, allocations, and events. Useful for diagnosing node-related issues.

4. List All Pods in a Namespace

kubectl get pods -n <namespace>

Lists all pods in the specified namespace. It’s an essential command to check if specific application workloads are running as expected.

5. List All Pods Across All Namespaces

kubectl get pods --all-namespaces

Displays all running pods across the entire cluster, regardless of namespace. Useful for a comprehensive view of all workloads in the cluster.

6. Get Detailed Pod Information

kubectl describe pod <pod-name> -n <namespace>

Provides in-depth information about a specific pod, including events, conditions, and container statuses. Handy for debugging pod-level issues.

7. Access Pod Logs

kubectl logs <pod-name> -n <namespace>

Displays logs from a specific pod. Logs are crucial for troubleshooting issues in a running application.

8. Stream Logs from a Pod in Real-Time

kubectl logs -f <pod-name> -n <namespace>

Streams real-time logs from a pod. The -f flag is essential when you need to monitor logs actively for debugging.

9. List Services in a Namespace

kubectl get svc -n <namespace>

Lists all services within a specified namespace. Useful for understanding the connectivity and accessibility of different services within your cluster.

10. Get Deployment Status

kubectl get deployments -n <namespace>

Shows the list of deployments, providing an overview of the desired and current replicas. It’s a quick way to check if deployments are successfully rolling out.

11. Scale a Deployment

kubectl scale deployment <deployment-name> --replicas=<number> -n <namespace>

Modifies the number of replicas for a deployment, allowing you to easily scale applications up or down based on demand.

12. Run a Temporary Pod

kubectl run <pod-name> --image=<image-name> --restart=Never

Creates a temporary pod for testing purposes, which is great for quick troubleshooting or experimentation in the cluster.

13. Delete a Pod

kubectl delete pod <pod-name> -n <namespace>

Removes a pod from the cluster. This command is commonly used to kill malfunctioning pods, which will then automatically restart if part of a deployment or replica set.

14. View ConfigMaps in a Namespace

kubectl get configmaps -n <namespace>

Lists all ConfigMaps within a namespace. ConfigMaps store configuration data for applications, and viewing them is essential when checking environment-specific settings.

15. View Secrets in a Namespace

kubectl get secrets -n <namespace>

Lists all Secrets in a namespace, allowing you to manage sensitive data. Remember that secret values are base64 encoded and not directly viewable.

16. Edit an Existing Resource

kubectl edit <resource-type> <resource-name> -n <namespace>

Opens the resource configuration in your default editor, enabling live edits. Useful for quick adjustments without needing a redeployment.

17. Forward a Pod Port to Localhost

kubectl port-forward <pod-name> <local-port>:<pod-port> -n <namespace>

Forwards a port from the pod to your local machine. It’s useful for testing services within the cluster that aren’t exposed externally.

18. Execute Commands Inside a Pod

kubectl exec -it <pod-name> -n <namespace> -- <command>

Runs a command inside a container within the specified pod, making it useful for troubleshooting and inspecting pod internals.

19. Apply a YAML Configuration File

kubectl apply -f <file.yaml>

Applies configurations from a YAML file. This command is fundamental for creating and updating resources in Kubernetes.

20. Delete Resources Using a YAML File

kubectl delete -f <file.yaml>

Deletes resources defined in a YAML file. Use this to clean up resources no longer needed.

21. List All Namespaces

kubectl get namespaces

Lists all namespaces within the cluster. Namespaces allow logical separation of resources, so it’s helpful to see what’s available in the environment.

22. Describe a Namespace

kubectl apply -f <file.yaml>

Provides detailed information on a specific namespace, including associated resource quotas and limits.

23. View Resource Usage for Nodes

kubectl top nodes

Displays resource utilization (CPU, memory) for each node in the cluster. Monitoring this information helps in managing resource consumption across nodes.

24. View Resource Usage for Pods

kubectl top pods -n <namespace>

Displays CPU and memory usage for each pod in a namespace. This helps track the performance and resource needs of running applications.

25. Create a New Namespace

kubectl create namespace <namespace-name>

Creates a new namespace, which is useful for logically isolating resources within a cluster. This command is often used to set up new environments for applications.

Conclusion

In this first installment of our exploration into kubectl, we’ve covered 25 essential commands that form the backbone of Kubernetes management. From deploying applications and scaling services to monitoring cluster health, these commands empower you to interact effectively with your Kubernetes environment. Mastering these foundational commands will not only streamline your workflow but also enhance your understanding of Kubernetes as a powerful orchestration platform.

As you continue to work with Kubernetes, remember that practice is key. Experiment with these commands in your own clusters to gain hands-on experience. In future installments, we will delve deeper into more advanced commands and use cases, providing you with the tools to become a proficient Kubernetes administrator.

Stay tuned for the next batch of commands, where we’ll uncover even more ways to harness the full potential of kubectl. Happy K8s managing!

Previous: Understanding Higher-Order Components (HOCs) in React: A Deep Dive
Next: Building Secure Fintech Applications with JavaScript fetch API and TypeScript