Skip to main content

Command Palette

Search for a command to run...

5.1 : The Ultimate kubectl Cheat Sheet: 20 Commands Every Developer Should Know

Updated
5 min read

Kubernetes 5.1: The Ultimate kubectl Cheat Sheet: 20 Commands Every Developer Should Know

Kubernetes can feel like a giant, complex machine. And while it is powerful, controlling it doesn't have to be intimidating! Think of kubectl as your remote control to that Kubernetes machine. This cheat sheet provides 20 essential kubectl commands that every developer should know, making managing your applications on Kubernetes a breeze.

What is kubectl?

Simply put, kubectl is the command-line tool that lets you interact with your Kubernetes cluster. You use it to deploy applications, inspect and manage cluster resources, and view logs. It's your primary interface for telling Kubernetes what to do.

Analogy: Imagine you're managing a virtual city (your Kubernetes cluster). kubectl is like your city planner's console, allowing you to deploy buildings (Pods), manage traffic (Services), and allocate resources (Storage).

Let's Dive into the Commands!

We'll break down the commands into logical categories for easier learning:

1. Basic Information & Cluster Health:

  • kubectl version: Shows the version of the kubectl client and the Kubernetes server. Think of it as checking the software version of your remote control and the TV itself to ensure compatibility.

  • kubectl cluster-info: Displays information about the Kubernetes cluster, including master endpoints and available services. This is like getting a city map to understand the infrastructure.

  • kubectl get nodes: Lists all nodes in the cluster. Nodes are the worker machines where your applications actually run. Like listing all the blocks in your virtual city.

2. Managing Pods (Your Applications):

  • kubectl create -f <pod.yaml>: Creates a pod based on a YAML configuration file. This is like deploying a pre-designed building to your virtual city. pod.yaml contains the instructions for what the pod should run.

    • Example: kubectl create -f my-app-pod.yaml
  • kubectl get pods: Lists all pods in the current namespace. This command shows you all the running buildings in your virtual city.

  • kubectl describe pod <pod-name>: Provides detailed information about a specific pod. Useful for troubleshooting. This is like inspecting a specific building to see its internal components and status.

  • kubectl logs <pod-name>: Displays the logs from a pod. This is like checking the activity log of a building to see what's happening inside.

    • Example: kubectl logs my-app-pod - shows the logs of the 'my-app-pod' pod.
  • kubectl exec -it <pod-name> -- /bin/bash: Opens a shell inside a running pod. Allows you to directly interact with the application. This is like entering a building to troubleshoot from within.

  • kubectl delete pod <pod-name>: Deletes a pod. This is like demolishing a building. Be careful!

3. Managing Deployments (Scaling and Updating):

  • kubectl create -f <deployment.yaml>: Creates a deployment based on a YAML configuration file. Deployments manage your pods, ensuring the desired number of replicas are running. This is like planning a whole district with multiple buildings of the same type.

  • kubectl get deployments: Lists all deployments in the current namespace.

  • kubectl describe deployment <deployment-name>: Provides detailed information about a deployment.

  • kubectl scale deployment <deployment-name> --replicas=<number>: Scales the number of replicas in a deployment. This is like adding more buildings of the same design to the district.

    • Example: kubectl scale deployment my-app-deployment --replicas=3 - Scales the 'my-app-deployment' to 3 replicas.
  • kubectl rollout restart deployment <deployment-name>: Restarts a deployment, triggering a rolling update. Useful for applying configuration changes. This is like renovating all the buildings in the district without disrupting the entire city.

  • kubectl delete deployment <deployment-name>: Deletes a deployment. This also deletes the pods managed by that deployment.

4. Services (Exposing your applications):

  • kubectl create -f <service.yaml>: Creates a service based on a YAML configuration file. Services expose your application to the outside world or other parts of your cluster. This is like building roads and public transport to make buildings accessible.

  • kubectl get services: Lists all services in the current namespace.

  • kubectl describe service <service-name>: Provides detailed information about a service.

5. Namespaces (Organization):

  • kubectl get namespaces: Lists all namespaces in the cluster. Namespaces are like different sections of your virtual city, allowing you to isolate resources.

  • kubectl create namespace <namespace-name>: Creates a new namespace.

    • Example: kubectl create namespace development
  • kubectl config set-context --current --namespace=<namespace-name>: Sets the default namespace for subsequent kubectl commands. This is like setting the default area you are working in your virtual city.

Practical Real-World Example:

Let's say you have a simple web application. You would:

  1. Create a deployment.yaml to define how to deploy your application's pods.

  2. Use kubectl create -f deployment.yaml to deploy it.

  3. Scale it with kubectl scale deployment my-web-app --replicas=3 to handle more traffic.

  4. Create a service.yaml to expose your application to the outside world.

  5. Use kubectl create -f service.yaml to create the service.

  6. Check the logs with kubectl logs <pod-name> if you notice any issues.

Challenge & Solution:

Challenge: "I deployed my application, but it's not accessible from outside the cluster!"

Solution: This usually means you haven't properly configured a Service to expose your deployment.

  1. Check if a Service exists: kubectl get services

  2. If not, create a Service: Use kubectl create -f service.yaml (ensure service.yaml is correctly configured with the right port mappings and selector to match your deployment's labels).

  3. If a Service exists, check its type: If it's ClusterIP, it's only accessible within the cluster. You might need to change it to NodePort or LoadBalancer depending on your environment. You can edit the service definition with kubectl edit service <service-name>.

Architectural Diagram (Simplified):

 +---------------------+     kubectl Commands    +---------------------+
 |     Your Machine    | <------------------------ |  Kubernetes Master  |
 +---------------------+                         +---------------------+
                                                          |
                                                          | API Server
                                                          |
  +---------------------+     Scheduled Pods     +---------------------+
  |      Node 1         | <------------------------ |      Your App     |
  +---------------------+                         +---------------------+
                                                          |
  +---------------------+                         +---------------------+
  |      Node 2         | <------------------------ |      Your App     |
  +---------------------+                         +---------------------+

Explanation:

  1. You run kubectl commands from your machine.

  2. kubectl talks to the Kubernetes Master, which is the control plane.

  3. The Master schedules your pods onto the available Nodes (worker machines).

  4. Your application runs inside those pods.

Conclusion:

This cheat sheet provides a solid foundation for interacting with your Kubernetes cluster. Mastering these commands will empower you to deploy, manage, and troubleshoot your applications effectively. As you become more comfortable, explore the vast array of other kubectl options and configurations to unlock the full potential of Kubernetes! Remember to refer to the official Kubernetes documentation for more in-depth information. Happy Kuberneting!

More from this blog

TechZen

136 posts