Mastering Kubernetes: A Comprehensive Guide to Every Command

kubernetes commands

Introduction:

Welcome to our comprehensive guide on Kubernetes commands! Kubernetes has become the de facto standard for container orchestration, empowering developers and operations teams to efficiently manage containerized applications at scale. However, mastering Kubernetes commands can be a daunting task due to its vast ecosystem and extensive feature set.

In this blog, we’ll break down each Kubernetes command, providing clear explanations and practical examples to help you understand and utilize them effectively. Whether you’re a beginner or an experienced Kubernetes user, this guide will serve as a valuable resource for navigating the complexities of Kubernetes command-line interface (CLI).

Let’s dive in!


1. kubectl create

Description: kubectl create is used to create Kubernetes resources from configuration files or directly via command-line arguments.

Example:

# Create a deployment named nginx-deployment

kubectl create deployment nginx-deployment –image=nginx:latest

# Create a service named nginx-service of type NodePort to expose the deployment

kubectl create service nodeport nginx-service –tcp=80:80

Explanation: In the example above, we create a deployment named nginx-deployment using the nginx:latest image. Then, we create a service named nginx-service of type NodePort to expose the deployment on port 80.


2. kubectl get

Description: kubectl get is used to retrieve information about Kubernetes resources.

Example:

# Get all pods in the current namespace

kubectl get pods

# Get detailed information about a specific pod

kubectl get pod nginx-deployment-65b9c54f9f-7vmhs -o yaml

Explanation: The first command retrieves information about all pods in the current namespace, while the second command provides detailed information about a specific pod identified by its name (nginx-deployment-65b9c54f9f-7vmhs) in YAML format.


3. kubectl describe

Description: kubectl describe provides detailed information about Kubernetes resources.

Example:

# Describe a pod

kubectl describe pod nginx-deployment-65b9c54f9f-7vmhs

Explanation: This command gives a detailed description of the specified pod, including its status, events, and associated metadata.


4. kubectl apply

Description: kubectl apply is used to apply configuration changes to Kubernetes resources.

Example:

# Apply changes from a YAML configuration file

kubectl apply -f deployment.yaml

Explanation: The command applies the changes defined in the deployment.yaml file to the Kubernetes cluster.


5. kubectl delete

Description: kubectl delete is used to delete Kubernetes resources.

Example:

# Delete a pod

kubectl delete pod nginx-deployment-65b9c54f9f-7vmhs

Explanation: This command deletes the specified pod from the cluster.


6. kubectl exec

Description: kubectl exec allows you to execute commands inside a container running in a pod.

Example:

# Execute a shell command inside a pod

kubectl exec -it nginx-deployment-65b9c54f9f-7vmhs — /bin/bash

# Execute a command directly without entering the shell

kubectl exec nginx-deployment-65b9c54f9f-7vmhs — ls /app

Explanation: The first command opens an interactive shell (/bin/bash) inside the specified pod, allowing you to run multiple commands interactively. The second command executes a single command (ls /app) inside the pod without entering the shell.


7. kubectl logs

Description: kubectl logs retrieves the logs of a container in a pod.

Example:

# Get logs of a container in a pod

kubectl logs nginx-deployment-65b9c54f9f-7vmhs

# Get logs of a specific container in a pod with multiple containers

kubectl logs nginx-deployment-65b9c54f9f-7vmhs -c nginx-container

Explanation: The first command retrieves the logs of all containers in the specified pod. The second command retrieves logs from a specific container (nginx-container) within the same pod.


8. kubectl scale

Description: kubectl scale is used to scale the number of replicas of a deployment, replicaset, or statefulset.

Example:

# Scale a deployment to 3 replicas

kubectl scale deployment nginx-deployment –replicas=3

Explanation: This command scales the number of replicas of the nginx-deployment to 3, effectively increasing the number of instances running the application.


9. kubectl edit

Description: kubectl edit allows you to edit the configuration of Kubernetes resources directly in the default text editor.

Example:

# Edit the configuration of a deployment

kubectl edit deployment nginx-deployment

Explanation: This command opens the deployment configuration in the default text editor (usually vi or nano), allowing you to make changes to the configuration interactively.


10. kubectl rollout

Description: kubectl rollout manages rollouts of updates to Kubernetes resources like deployments.

Example:

# View the rollout history of a deployment

kubectl rollout history deployment/nginx-deployment

# Roll back to a previous revision of a deployment

kubectl rollout undo deployment/nginx-deployment –to-revision=2

Explanation: The first command retrieves the rollout history of the nginx-deployment, while the second command rolls back the deployment to a specific revision (in this case, revision 2).


11. kubectl port-forward

Description: kubectl port-forward allows you to forward one or more local ports to a pod.

Example:

# Forward local port 8080 to port 80 of a pod

kubectl port-forward pod/nginx-pod 8080:80

# Forward local port 9090 to port 9090 of a service

kubectl port-forward service/nginx-service 9090:9090

Explanation: The first command forwards local port 8080 to port 80 of the pod nginx-pod. The second command forwards local port 9090 to port 9090 of the service nginx-service.


12. kubectl rollout status

Description: kubectl rollout status checks the status of a rollout.

Example:

# Check the status of a deployment rollout

kubectl rollout status deployment/nginx-deployment

Explanation: This command checks the status of the rollout for the nginx-deployment and provides information about whether the rollout is complete or still in progress.


13. kubectl label

Description: kubectl label is used to add or update labels on resources.

Example:

# Add a label to a pod

kubectl label pod/nginx-pod app=nginx

# Update a label on a service

kubectl label service/nginx-service environment=production

Explanation: The first command adds a label app=nginx to the pod nginx-pod. The second command updates the label environment to production on the service nginx-service.


14. kubectl annotate

Description: kubectl annotate is used to add or update annotations on resources.

Example:

# Add an annotation to a pod

kubectl annotate pod/nginx-pod description=”Frontend web server”

# Update an annotation on a service

kubectl annotate service/nginx-service owner=”John Doe”

Explanation: The first command adds an annotation description with the value “Frontend web server” to the pod nginx-pod. The second command updates the annotation owner to “John Doe” on the service nginx-service.


15. kubectl rollout pause/resume

Description: kubectl rollout pause and kubectl rollout resume are used to pause and resume a rollout, respectively.

Example:

# Pause a deployment rollout

kubectl rollout pause deployment/nginx-deployment

# Resume a deployment rollout

kubectl rollout resume deployment/nginx-deployment

Explanation: The first command pauses the rollout of the nginx-deployment, while the second command resumes the paused rollout.


16. kubectl top

Description: kubectl top provides resource usage statistics for nodes and pods.

Example:

# Get CPU and memory usage of nodes

kubectl top nodes

# Get CPU and memory usage of pods

kubectl top pods

Explanation: These commands provide resource usage statistics such as CPU and memory usage for nodes and pods in the cluster.


17. kubectl exec -ti

Description: kubectl exec -ti is an extension of the kubectl exec command that opens an interactive session with a pod.

Example:

# Open an interactive shell inside a pod

kubectl exec -ti nginx-pod — /bin/bash

Explanation: This command opens an interactive terminal (-ti) inside the specified pod (nginx-pod), allowing you to run multiple commands interactively.


18. kubectl logs -f

Description: kubectl logs -f follows the log output of a container in real-time.

Example:

# Stream logs of a container in a pod

kubectl logs -f nginx-deployment-65b9c54f9f-7vmhs

Explanation: This command streams the logs of the specified container (nginx-deployment-65b9c54f9f-7vmhs) in real-time, allowing you to monitor the container’s output as it changes.


19. kubectl rollout restart

Description: kubectl rollout restart restarts a rollout of a deployment.

Example:

# Restart a deployment rollout

kubectl rollout restart deployment/nginx-deployment

Explanation: This command restarts the rollout of the nginx-deployment, effectively triggering a new rollout with the existing configuration.


20. kubectl run

Description: kubectl run creates and manages deployments or jobs.

Example:

# Create a new deployment

kubectl run nginx –image=nginx:latest –port=80

# Create a job to run a one-time task

kubectl run –generator=run-pod/v1 my-job –image=busybox — /bin/sh -c “echo Hello, Kubernetes!”

Explanation: The first command creates a new deployment named nginx using the nginx:latest image. The second command creates a job named my-job to run a one-time task that echoes “Hello, Kubernetes!”.


21. kubectl cp

Description: kubectl cp copies files and directories to and from containers.

Example:

# Copy a file from a pod to the local filesystem

kubectl cp nginx-pod:/var/log/nginx/access.log ./access.log

# Copy a file from the local filesystem to a pod

kubectl cp ./config.ini nginx-pod:/etc/nginx/config.ini

Explanation: The first command copies the access.log file from the nginx-pod to the local filesystem. The second command copies the config.ini file from the local filesystem to the nginx-pod.


22. kubectl patch

Description: kubectl patch is used to modify specific fields of a resource using a JSON or YAML patch.

Example:

# Patch the image of a container in a deployment

kubectl patch deployment nginx-deployment -p ‘{“spec”:{“template”:{“spec”:{“containers”:[{“name”:”nginx”,”image”:”nginx:1.21.1″}]}}}}’

Explanation: This command patches the image of the nginx container in the nginx-deployment to use the nginx:1.21.1 image.


23. kubectl rollout history

Description: kubectl rollout history displays the rollout history of a deployment.

Example:

# Display rollout history of a deployment

kubectl rollout history deployment/nginx-deployment

Explanation: This command shows a revision history of changes to the nginx-deployment, including the revision number, trigger, and status.


24. kubectl edit -n

Description: kubectl edit -n allows you to edit resources in a specific namespace.

Example:

# Edit a deployment in a specific namespace

kubectl edit deployment/nginx-deployment -n development

Explanation: This command opens the deployment nginx-deployment for editing in the development namespace.


25. kubectl rollout pause/resume -n

Description: kubectl rollout pause/resume -n pauses or resumes a rollout in a specific namespace.

Example:

# Pause a deployment rollout in a specific namespace

kubectl rollout pause deployment/nginx-deployment -n staging

# Resume a deployment rollout in a specific namespace

kubectl rollout resume deployment/nginx-deployment -n staging

Explanation: These commands pause or resume the rollout of the nginx-deployment in the staging namespace.


26. kubectl apply -f dir/

Description: kubectl apply -f dir/ applies all resources in a directory.

Example:

# Apply all resources in a directory

kubectl apply -f ./manifests/

Explanation: This command applies all Kubernetes resource manifests found in the manifests/ directory.


27. kubectl scale –replicas=

Description: kubectl scale –replicas= scales the number of replicas of a deployment, replicaset, or statefulset to a specific value.

Example:

# Scale a deployment to 5 replicas

kubectl scale deployment nginx-deployment –replicas=5

Explanation: This command scales the number of replicas of the nginx-deployment to 5.


28. kubectl rollout undo –to-revision=

Description: kubectl rollout undo –to-revision= rolls back a deployment to a specific revision.

Example:

# Roll back a deployment to a specific revision

kubectl rollout undo deployment/nginx-deployment –to-revision=3

Explanation: This command rolls back the nginx-deployment to revision 3.


29. kubectl delete -f

Description: kubectl delete -f deletes resources defined in a YAML or JSON file.

Example:

# Delete resources defined in a YAML file

kubectl delete -f deployment.yaml

Explanation: This command deletes all resources defined in the deployment.yaml file.


30. kubectl rollout history -n

Description: kubectl rollout history -n displays the rollout history of a deployment in a specific namespace.

Example:

# Display rollout history of a deployment in a specific namespace

kubectl rollout history deployment/nginx-deployment -n production

Explanation: This command shows the rollout history of the nginx-deployment in the production namespace.


31. kubectl proxy

Description: kubectl proxy creates a proxy server between your machine and Kubernetes API server. It allows you to access the Kubernetes API from your local machine.

Example:

# Start a proxy server

kubectl proxy

Explanation: This command starts a proxy server on your local machine, enabling you to interact with the Kubernetes API using tools like curl or through the Kubernetes dashboard.


32. kubectl rollout status -w

Description: kubectl rollout status -w continuously checks the status of a rollout until it’s complete.

Example:

# Continuously monitor the status of a deployment rollout

kubectl rollout status deployment/nginx-deployment -w

Explanation: This command continuously monitors the status of the nginx-deployment rollout until it’s complete, updating the status in real-time.


33. kubectl explain

Description: kubectl explain provides documentation for Kubernetes resources and their fields.

Example:

# Display documentation for a specific resource

kubectl explain pod.spec.containers

Explanation: This command displays detailed documentation for the fields of a specific Kubernetes resource, in this case, the containers field within the spec section of a Pod.


34. kubectl rollout pause/resume -w

Description: kubectl rollout pause/resume -w pauses or resumes a rollout and waits until it’s complete.

Example:

# Pause a deployment rollout and wait until it’s paused

kubectl rollout pause deployment/nginx-deployment -w

# Resume a deployment rollout and wait until it’s resumed

kubectl rollout resume deployment/nginx-deployment -w

Explanation: These commands pause or resume the rollout of the nginx-deployment and wait until the operation is complete before exiting.


35. kubectl diff

Description: kubectl diff shows a diff between the current live object configuration and the configuration that would be applied.

Example:

# Show the diff between current and proposed configuration

kubectl diff -f deployment.yaml

Explanation: This command compares the current live configuration in the cluster with the proposed changes defined in the deployment.yaml file, showing the differences before applying the changes.


36. kubectl auth can-i

Description: kubectl auth can-i checks whether the current user has the specified permissions.

Example:

# Check if the current user can create deployments

kubectl auth can-i create deployments

Explanation: This command checks whether the current user has permission to create deployments in the Kubernetes cluster.


These Kubernetes commands provide further functionality and flexibility in managing Kubernetes clusters and resources efficiently. Incorporate them into your workflow as needed to streamline your Kubernetes operations.

Leave a Reply

Your email address will not be published. Required fields are marked *