100 Kubernetes Commands With Examples
date
Apr 27, 2024
slug
kubernetes-commands-with-examples
author
status
Public
tags
DevOps
Kubernetes
Guide
summary
The Kubernetes command-line tool,
kubectl
โthough extremely helpfulโis flavored with numerous commands with several options. Searching for the right command or syntax can be like finding a needle in a haystack.type
Post
category
๐ข DevOps Blogs
updatedAt
Apr 27, 2024 07:07 AM
Introduction:
The Kubernetes command-line tool,
kubectl
โthough extremely helpfulโis flavored with numerous commands with several options. Searching for the right command or syntax can be like finding a needle in a haystack.Thatโs why, we have compiled 100 essential
kubectl
commands with code examples in this article to help you streamline your Kubernetes management tasks.What is kubectl?
Kubectl
is a command-line tool that is used to control and interact with Kubernetes clusters through the Kubernetes cluster control plane.ย The Kubernetes cluster consists of two important planes: the control planeโwith the Kubernetes components that run within itโand the workloads and machines that run on the control plane componentsโnodes and their components.The components of the
Kubernetes control plane
include the kube-scheduler
, etcd
, kube-controller-manager
, cloud-controller-manager
, container runtime
and kube-apiserver
(which exposes the Kubernetes API to kubectl
).Kubectl
controls the Kubernetes cluster control plane in order to function as a client of the Kubernetes API server
The API is a HTTP REST API. To run Kubernetes, Kubectl
must send HTTP requests to the Kubernetes API.In order to establish the necessary connection with the API server,
Kubectl
utilizes the clusterโsย kubeconfig
ย file, which contains the required authentication and cluster details.ย
Kubectl Commands Cheat Sheet
- kubectl create
- ๐ ๏ธ Usage: Create Kubernetes resources from files or stdin.
- Example:
kubectl create -f pod.yaml
- Description: Creates a pod using the configuration specified in the pod.yaml file.
- kubectl get
- ๐ Usage: Retrieve Kubernetes resources.
- Example:
kubectl get pods
- Description: Retrieves all pods in the current namespace.
- kubectl describe
- ๐ Usage: Provide detailed information about a Kubernetes resource.
- Example:
kubectl describe pod my-pod
- Description: Describes the pod named my-pod, displaying detailed information including its status, containers, and events.
- kubectl apply
- ๐ Usage: Apply changes to Kubernetes resources defined in YAML or JSON files.
- Example:
kubectl apply -f deployment.yaml
- Description: Applies the changes specified in the deployment.yaml file to the cluster.
- kubectl delete
- ๐๏ธ Usage: Delete Kubernetes resources.
- Example:
kubectl delete pod my-pod
- Description: Deletes the pod named my-pod.
- kubectl exec
- ๐ป Usage: Execute commands inside a running container in a pod.
- Example:
kubectl exec -it my-pod -- /bin/bash
- Description: Starts an interactive shell (/bin/bash) inside the pod named my-pod.
- kubectl logs
- ๐ Usage: Retrieve the logs of a pod.
- Example:
kubectl logs my-pod
- Description: Displays the logs of the pod named my-pod.
- kubectl port-forward
- ๐ Usage: Forward one or more local ports to a pod.
- Example:
kubectl port-forward my-pod 8080:80
- Description: Forwards local port 8080 to port 80 on the pod named my-pod.
- kubectl scale
- โ๏ธ Usage: Scale the number of replicas of a resource.
- Example:
kubectl scale --replicas=3 deployment/my-deployment
- Description: Scales the number of replicas of the deployment named my-deployment to 3.
- kubectl edit
- ๐๏ธ Usage: Edit the resource definition in a text editor.
- Example:
kubectl edit pod my-pod
- Description: Opens the resource definition of the pod named my-pod in a text editor, allowing you to make changes.
More Essential Kubectl Commands
- kubectl rollout
- ๐ Usage: Manage rollouts of updates to Kubernetes resources.
- Example:
kubectl rollout status deployment/my-deployment
- Description: Checks the status of the rollout for the deployment named my-deployment.
- kubectl label
- ๐ท๏ธ Usage: Add or update labels on Kubernetes resources.
- Example:
kubectl label pod my-pod app=backend
- Description: Adds the label app=backend to the pod named my-pod.
- kubectl annotate
- ๐ Usage: Add or update annotations on Kubernetes resources.
- Example:
kubectl annotate pod my-pod description="This is my pod"
- Description: Adds the annotation description="This is my pod" to the pod named my-pod.
- kubectl cluster-info
- โน๏ธ Usage: Display cluster info such as server URL and Kubernetes version.
- Example:
kubectl cluster-info
- Description: Displays information about the Kubernetes cluster.
- kubectl apply -f -
- ๐ Usage: Apply configuration from standard input.
- Example:
cat pod.yaml | kubectl apply -f -
- Description: Applies the configuration defined in pod.yaml piped from standard input.
- kubectl rollout history
- ๐ Usage: View rollout history of a deployment.
- Example:
kubectl rollout history deployment/my-deployment
- Description: Displays the rollout history of the deployment named my-deployment.
- kubectl rollout undo
- โฉ๏ธ Usage: Roll back a deployment to a previous revision.
- Example:
kubectl rollout undo deployment/my-deployment
- Description: Rolls back the deployment named my-deployment to the previous revision.
- kubectl create namespace
- ๐ Usage: Create a new Kubernetes namespace.
- Example:
kubectl create namespace my-namespace
- Description: Creates a new namespace named my-namespace.
- kubectl apply --dry-run
- ๐ซ Usage: Simulate the apply of configuration without executing it.
- Example:
kubectl apply -f pod.yaml --dry-run=client
- Description: Checks if the configuration in pod.yaml can be applied without actually applying it.
- kubectl api-resources
- ๐ฆ Usage: List all available API resources.
- Example:
kubectl api-resources
- Description: Lists all the API resources supported by the Kubernetes API server.
- kubectl create -f <directory>/
- ๐ Usage: Create resources defined in all .yaml files in a directory.
- Example:
kubectl create -f ./my-resources/
- Description: Creates Kubernetes resources defined in all .yaml files located in the my-resources directory.
- kubectl get pods -o wide
- ๐ Usage: Retrieve pods with additional details including node name and IP address.
- Example:
kubectl get pods -o wide
- Description: Displays pods along with additional details such as the node they are running on and their IP addresses.
- kubectl describe node
- ๐ Usage: Provide detailed information about a Kubernetes node.
- Example:
kubectl describe node my-node
- Description: Describes the node named my-node, displaying detailed information including its capacity, allocatable resources, and conditions.
- kubectl rollout pause
- โธ๏ธ Usage: Pause a rollout of a deployment.
- Example:
kubectl rollout pause deployment/my-deployment
- Description: Pauses the rollout of the deployment named my-deployment.
- kubectl rollout resume
- โถ๏ธ Usage: Resume a paused rollout of a deployment.
- Example:
kubectl rollout resume deployment/my-deployment
- Description: Resumes the paused rollout of the deployment named my-deployment.
- kubectl delete namespace
- ๐ Usage: Deletes a Kubernetes namespace and all resources within it.
- Example:
kubectl delete namespace my-namespace
- Description: Deletes the namespace named my-namespace along with all resources within it.
- kubectl get events
- ๐ Usage: Retrieves events from the cluster.
- Example:
kubectl get events
- Description: Retrieves all events from the cluster, displaying information such as type, reason, and message.
- kubectl get pods --show-labels
- ๐ท๏ธ Usage: Displays additional labels associated with pods.
- Example:
kubectl get pods --show-labels
- Description: Displays pods along with all labels associated with them.
- kubectl exec -it my-pod -- ls /app
- ๐ Usage: Executes a command (ls /app) inside a running container in a pod interactively.
- Example:
kubectl exec -it my-pod -- ls /app
- Description: Lists the contents of the /app directory inside the pod named my-pod.
- kubectl create secret
- ๐ Usage: Creates a secret in the cluster.
- Example:
kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=passw0rd
- Description: Creates a secret named my-secret with two key-value pairs: username=admin and password=passw0rd.
- kubectl edit deployment
- ๐๏ธ Usage: Opens the deployment configuration in a text editor, allowing you to make changes.
- Example:
kubectl edit deployment/my-deployment
- Description: Opens the configuration of the deployment named my-deployment in a text editor.
- kubectl rollout restart
- ๐ Usage: Restarts a rollout of a deployment by reapplying the current configuration.
- Example:
kubectl rollout restart deployment/my-deployment
- Description: Restarts the rollout of the deployment named my-deployment.
- kubectl rollout status
- โณ Usage: Checks the status of a rollout for a deployment.
- Example:
kubectl rollout status deployment/my-deployment
- Description: Checks the status of the rollout for the deployment named my-deployment.
- kubectl exec -it my-pod -- sh -c 'echo $ENV_VAR'
- ๐ก Usage: Executes a shell command (echo $ENV_VAR) inside a running container in a pod.
- Example:
kubectl exec -it my-pod -- sh -c 'echo $ENV_VAR'
- Description: Prints the value of the environment variable ENV_VAR inside the pod named my-pod.
- kubectl apply -f deployment.yaml --record
- ๐ Usage: Applies changes to a deployment and records the changes in the revision history.
- Example:
kubectl apply -f deployment.yaml --record
- Description: Applies the changes specified in the deployment.yaml file to the deployment and records the changes in the revision history.
- kubectl get pods --field-selector=status.phase=Running
- ๐ Usage: Retrieves pods with a specific status phase, such as Running.
- Example:
kubectl get pods --field-selector=status.phase=Running
- Description: Retrieves all pods in the current namespace that are in the Running phase.
- kubectl delete pod --grace-period=0 --force my-pod
- โ ๏ธ Usage: Forcefully deletes a pod without waiting for the grace period.
- Example:
kubectl delete pod --grace-period=0 --force my-pod
- Description: Forcefully deletes the pod named my-pod without waiting for the grace period to elapse.
- kubectl describe service
- ๐ Usage: Provides detailed information about a Kubernetes service.
- Example:
kubectl describe service my-service
- Description: Describes the service named my-service, displaying detailed information including its endpoints and selectors.
- kubectl create deployment
- ๐ Usage: Creates a deployment using the specified image.
- Example:
kubectl create deployment my-deployment --image=my-image:tag
- Description: Creates a deployment named my-deployment using the image my-image:tag.
- kubectl get deployment -o yaml
- ๐ Usage: Retrieves deployments and outputs the result in YAML format.
- Example:
kubectl get deployment -o yaml
- Description: Retrieves all deployments in the current namespace and outputs the result in YAML format.
- kubectl scale deployment
- โ๏ธ Usage: Scales the number of replicas of a deployment.
- Example:
kubectl scale deployment/my-deployment --replicas=3
- Description: Scales the deployment named my-deployment to have 3 replicas.
- kubectl rollout history deployment
- ๐ Usage: Displays the revision history of a deployment.
- Example:
kubectl rollout history deployment/my-deployment
- Description: Shows the revision history of the deployment named my-deployment.
- kubectl rollout undo deployment --to-revision=
- โฉ๏ธ Usage: Rolls back a deployment to a specific revision.
- Example:
kubectl rollout undo deployment/my-deployment --to-revision=3
- Description: Rolls back the deployment named my-deployment to the third revision.
- kubectl apply -f pod.yaml --namespace=
- ๐ Usage: Applies a YAML file to a specific namespace.
- Example:
kubectl apply -f pod.yaml --namespace=my-namespace
- Description: Applies the configuration specified in pod.yaml to the namespace my-namespace.
- kubectl logs -f my-pod
- ๐ Usage: Streams the logs of a pod continuously.
- Example:
kubectl logs -f my-pod
- Description: Continuously streams the logs of the pod named my-pod to the terminal.
- kubectl get svc
- ๐ก Usage: Retrieves information about services in the cluster.
- Example:
kubectl get svc
- Description: Retrieves information about all services in the current namespace.
- kubectl get pods -n
- ๐ท๏ธ Usage: Retrieves pods from a specific namespace.
- Example:
kubectl get pods -n my-namespace
- Description: Retrieves all pods from the namespace my-namespace.
- kubectl delete -f pod.yaml
- ๐๏ธ Usage: Deletes resources specified in a YAML file.
- Example:
kubectl delete -f pod.yaml
- Description: Deletes the resources specified in the pod.yaml file.
- kubectl rollout status deployment
- โณ Usage: Checks the status of a deployment rollout.
- Example:
kubectl rollout status deployment/my-deployment
- Description: Checks the status of the rollout for the deployment named my-deployment.
- kubectl exec -it my-pod -- /bin/bash
- ๐ป Usage: Starts an interactive shell inside a pod.
- Example:
kubectl exec -it my-pod -- /bin/bash
- Description: Opens an interactive shell (/bin/bash) inside the pod named my-pod, allowing you to execute commands within it.
51. Apply YAML files recursively ๐
Command:
kubectl apply -f ./my-resources/ --recursive
Description:
Applies all YAML files located in the specified directory and its subdirectories.
52. Rollout History Details ๐
Command:
kubectl rollout history deployment/my-deployment --revision=3
Description:
Displays details of a specific revision in the rollout history of a deployment.
53. Rollback Deployment โช
Command:
kubectl rollout undo deployment/my-deployment --to-revision=2
Description:
Rolls back a deployment to a specific revision.
54. Validate Configuration ๐ ๏ธ
Command:
kubectl apply -f pod.yaml --validate=true
Description:
Validates the configuration file before applying changes.
55. Retrieve Pod Logs ๐
Command:
kubectl logs my-pod --tail=100
Description:
Retrieves the last 100 lines of logs from a pod.
56. Get Services with Details ๐
Command:
kubectl get services -o wide
Description:
Retrieves services with additional details including node port and cluster IP.
57. Retrieve Pods by Status Phase ๐
Command:
kubectl get pods --field-selector=status.phase!=Running
Description:
Retrieves pods with a status phase other than Running.
58. Forcefully Delete Pod โ
Command:
kubectl delete pod my-pod --force --grace-period=0
Description:
Forcefully deletes a pod without waiting for the grace period.
59. Describe Service Details ๐
Command:
kubectl describe service my-service
Description:
Provides detailed information about a Kubernetes service.
60. Expose Deployment as Service ๐
Command:
kubectl expose deployment my-deployment --type=LoadBalancer --port=80 --target-port=8080
Description:
Exposes a deployment as a service with specified type, port, and target port.
61. Retrieve Labeled Deployments ๐ท๏ธ
Command:
kubectl get deployments -l app=my-app
Description:
Retrieves deployments labeled with specific criteria.
62. Pause Deployment Rollout โธ๏ธ
Command:
kubectl rollout pause deployment/my-deployment
Description:
Pauses the rollout of a deployment.
63. Resume Deployment Rollout โถ๏ธ
Command:
kubectl rollout resume deployment/my-deployment
Description:
Resumes the rollout of a deployment.
64. Retrieve Logs by Container ๐๏ธ
Command:
kubectl logs my-pod --container=nginx
Description:
Retrieves logs from a specific container within a pod.
65. Validate Configuration (Dry Run) ๐งพ
Command:
kubectl apply -f pod.yaml --dry-run=client
Description:
Validates the configuration file without actually applying changes.
66. Retrieve Pods Sorted by Creation Time โฒ๏ธ
Command:
kubectl get pods --sort-by=.metadata.creationTimestamp
Description:
Retrieves pods sorted by creation timestamp.
67. Describe Persistent Volume Claim ๐ฆ
Command:
kubectl describe persistentvolumeclaim my-pvc
Description:
Provides detailed information about a persistent volume claim.
68. Monitor Deployment Rollout Status ๐
Command:
kubectl rollout status deployment/my-deployment --watch
Description:
Continuously monitors the status of a deployment rollout.
69. Retrieve Pods by Pending Status โณ
Command:
kubectl get pods --field-selector=status.phase=Pending
Description:
Retrieves pods with a status phase of Pending.
70. Create Generic Secret from File ๐
Command:
kubectl create secret generic my-secret --from-file=./my-secret-file
Description:
Creates a generic secret from a file.
71. Restart Deployment Rollout ๐
Command:
kubectl rollout restart deployment/my-deployment
Description:
Restarts a rollout of a deployment by reapplying the current configuration.
72. Add Label to Namespace ๐ท๏ธ
Command:
kubectl label namespace my-namespace env=dev
Description:
Adds a label to a namespace.
73. Delete Deployment โ
Command:
kubectl delete deployment my-deployment
Description:
Deletes a deployment.
74. Retrieve Pods from Specific Namespace ๐๏ธ
Command:
kubectl get pods --namespace=my-namespace
Description:
Retrieves pods from a specific namespace.
75. Describe Secret Details ๐คซ
Command:
kubectl describe secret my-secret
Description:
Provides detailed information about a secret.
76. Delete Service โ
Command:
kubectl delete service my-service
Description:
Deletes a service.
77. Retrieve Node Information ๐
Command:
kubectl get nodes
Description:
Retrieves information about nodes in the cluster.
78. Create ConfigMap from Literals โ๏ธ
Command:
kubectl create configmap my-config --from-literal=key1=value1 --from-literal=key2=value2
Description:
Creates a config map from literal values.
79. Rollout History Details ๐
Command:
kubectl rollout history deployment/my-deployment --revision=3
Description:
Displays details of a specific revision in the rollout history of a deployment.
80. Display Pod Resource Usage ๐
Command:
kubectl top pods
Description:
Displays resource usage (CPU and memory) of pods in the cluster.
81. Explain Pod Structure ๐
Command:
kubectl explain pod
Description:
Provides documentation about the Pod resource, including all its fields and their descriptions.
82. Delete Namespace and Resources ๐๏ธ
Command:
kubectl delete namespace my-namespace
Description:
Deletes a namespace and all resources within it.
83. Retrieve Persistent Volume Information ๐ฆ
Command:
kubectl get pv
Description:
Retrieves information about persistent volumes in the cluster.
84. Check Rollout Status with Timeout โฒ๏ธ
Command:
kubectl rollout status deployment/my-deployment --timeout=2m
Description:
Checks the status of a rollout and waits for a specific timeout before exiting.
85. Apply Configuration to Namespace ๐
Command:
kubectl apply -f pod.yaml --namespace=my-namespace
Description:
Applies a configuration file to a specific namespace.
86. Retrieve Secret Information ๐
Command:
kubectl get secrets
Description:
Retrieves information about secrets in the cluster.
87. Create NodePort Service ๐
Command:
kubectl create service nodeport my-service --tcp=80:8080
Description:
Creates a NodePort service to expose a deployment on a specific port.
88. Simulate Rollback without Action ๐
Command:
kubectl rollout undo deployment/my-deployment --to-revision=2 --dry-run
Description:
Simulates rolling back a deployment to a specific revision without actually performing the rollback.
89. Validate Configuration (Dry Run) ๐งพ
Command:
kubectl create -f pod.yaml --dry-run=client
Description:
Validates a configuration file without actually creating the resource.
90. Start Interactive Shell in Pod ๐
Command:
kubectl exec -it my-pod --container=my-container -- /bin/bash
Description:
Starts an interactive shell inside a specific container within a pod.
91. Create Role within Namespace ๐ท๏ธ
Command:
kubectl create role my-role --verb=get --resource=pods
Description:
Creates a role within a namespace with specific permissions.
92. Apply Changes with Record ๐
Command:
kubectl apply -f deployment.yaml --namespace=my-namespace --record
Description:
Applies changes to a deployment within a specific namespace and records the changes.
93. Describe Persistent Volume Details ๐
Command:
kubectl describe persistentvolume my-pv
Description:
Provides detailed information about a persistent volume.
94. Create Service Account ๐ก๏ธ
Command:
kubectl create serviceaccount my-service-account
Description:
Creates a service account within a namespace.
95. Retrieve Events Sorted by Timestamp ๐ฐ๏ธ
Command:
kubectl get events --sort-by=.metadata.creationTimestamp
Description:
Retrieves events sorted by creation timestamp.
96. Describe Ingress Resources ๐
Command:
kubectl describe ingresses.extensions
Description:
Provides detailed information about Ingress resources.
97. Simulate Rollback without Action (Dry Run) ๐
Command:
kubectl rollout undo deployment/my-deployment --dry-run=client
Description:
Simulates rolling back a deployment to the previous revision without actually performing the rollback.
98. Scale Deployment with Record ๐
Command:
kubectl scale deployment/my-deployment --replicas=5 --record
Description:
Scales the number of replicas of a deployment and records the change.
99. Delete Secret โ
Command:
kubectl delete secret my-secret
Description:
Deletes a secret.
100. Retrieve Ingress Information ๐
Command:
kubectl get ingress
Description:
Retrieves information about Ingress resources in the cluster.
Thank you for reading my blog โฆ:)
ยฉ Copyrights: ProDevOpsGuy
Support ๐ซถ
- Help spread the word about ProDevOpsGuy by sharing it on social media and recommending it to your friends. ๐ฃ๏ธ
- You can also sponsor ๐ on GitHub Sponsors // ๐๏ธ Support Our Work ๐๏ธ
ย
Thank you for reading this long article.
Feedback on typos and content is always welcome.
#DevOps#Guide#Kubernetes