Pull down to refresh

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
thumbnail
kube-commands-banner.png
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

  1. 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.
  1. kubectl get
      • ๐Ÿ“‹ Usage: Retrieve Kubernetes resources.
      • Example: kubectl get pods
      • Description: Retrieves all pods in the current namespace.
  1. 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.
  1. 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.
  1. kubectl delete
      • ๐Ÿ—‘๏ธ Usage: Delete Kubernetes resources.
      • Example: kubectl delete pod my-pod
      • Description: Deletes the pod named my-pod.
  1. 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.
  1. kubectl logs
      • ๐Ÿ“œ Usage: Retrieve the logs of a pod.
      • Example: kubectl logs my-pod
      • Description: Displays the logs of the pod named my-pod.
  1. 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.
  1. 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.
  1. 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

  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. kubectl create namespace
      • ๐ŸŒ Usage: Create a new Kubernetes namespace.
      • Example: kubectl create namespace my-namespace
      • Description: Creates a new namespace named my-namespace.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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.
  1. 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. ๐Ÿ—ฃ๏ธ
ย 

Thank you for reading this long article.
Feedback on typos and content is always welcome.

#DevOps#Guide#Kubernetes