Kubernetes Commands sheet

Kubernetes Commands sheet

Command line tool (kubectl)

  • Kubernetes provides a command line tool for communicating with a Kubernetes cluster's control plane, using the Kubernetes API.

  • kubectl is a Kubernetes command-line tool that allows you to run commands against Kubernetes clusters. You can use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

  • The descriptions are taken directly from the official documentation at kubernetes.io, and each will link to the appropriate documentation page should you require more information or a deeper dive.


  1. kubectl create: This command is used to create Kubernetes resources from files or stdin.

     kubectl create -f pod.yaml
    
  2. kubectl get: It is used to retrieve Kubernetes resources.

     kubectl get pods
    
  3. kubectl describe: This command provides detailed information about a Kubernetes resource.

     kubectl describe pod my-pod
    
    • This command describes the pod named my-pod, displaying detailed information including its status, containers, and events
  4. kubectl apply: It applies changes to Kubernetes resources defined in YAML or JSON files.

     kubectl apply -f deployment.yaml
    
  5. kubectl delete: It is used to delete Kubernetes resources

     kubectl delete pod my-pod
    
    • This command deletes the pod named my-pod
  6. kubectl exec: This command executes commands inside a running container in a pod.

     kubectl exec -it my-pod -- /bin/bash
    
    • This command starts an interactive shell (/bin/bash) inside the pod named my-pod
  7. kubectl logs: It retrieves the logs of a pod.

     kubectl logs my-pod
    
    • This command displays the logs of the pod named my-pod
  8. kubectl port-forward: This command forwards one or more local ports to a pod

     kubectl port-forward my-pod 8080:80
    
    • This command forwards local port 8080 to port 80 on the pod named my-pod
  9. kubectl scale: It scales the number of replicas of a resource

     kubectl scale --replicas=3 deployment/my-deployment
    
    • This command scales the number of replicas of the deployment named my-deployment to 3.
  10. kubectl edit: This command edits the resource definition in a text editor

    kubectl edit pod my-pod
    
    • This command opens the resource definition of the pod named my-pod in a text editor, allowing you to make changes
  11. kubectl rollout: This command manages rollouts of updates to Kubernetes resources

    kubectl rollout status deployment/my-deployment
    
    • This command checks the status of the rollout for the deployment named my-deployment
  12. kubectl label: It adds or updates labels on Kubernetes resources.

    kubectl label pod my-pod app=backend
    
    • This command adds the label app=backend to the pod named my-pod.
  13. kubectl annotate: This command adds or updates annotations on Kubernetes resources

    kubectl annotate pod my-pod description="This is my pod"
    
    • This command adds the annotation description="This is my pod" to the pod named my-pod
  14. kubectl apply -f -: This command applies configuration from the standard input.

    cat pod.yaml | kubectl apply -f -
    
    • This command applies the configuration defined in pod.yaml piped from the standard input
  15. kubectl cluster-info: It displays cluster info such as server URL and Kubernetes version

    kubectl cluster-info
    
    • This command displays information about the Kubernetes cluster.
  16. kubectl rollout history: This command views rollout history of a deployment

    kubectl rollout history deployment/my-deployment
    
    • This command displays the rollout history of the deployment named my-deployment.
  17. kubectl rollout undo: It rolls back deployment to a previous revision

    kubectl rollout undo deployment/my-deployment
    
    • This command rolls back the deployment named my-deployment to the previous revision
  18. kubectl create namespace: This command creates a new Kubernetes namespace

    kubectl create namespace my-namespace
    
  19. kubectl apply --dry-run: It simulates the apply of configuration without actually executing it

    kubectl apply -f pod.yaml --dry-run=client
    
    • This command checks if the configuration in pod.yaml can be applied without actually applying it.
  20. kubectl api-resources: This command lists all available API resources

    kubectl api-resources
    
    • This command lists all the API resources supported by the Kubernetes API server
  21. kubectl create -f : This command creates resources defined in all .yaml files in a directory

    kubectl create -f ./my-resources/
    
    • This command creates Kubernetes resources defined in all .yaml files located in the my-resources directory
  22. kubectl get pods -o wide: It retrieves pods with additional details including node name and IP address

    kubectl get pods -o wide
    
  23. kubectl describe node: This command provides detailed information about a Kubernetes node.

    kubectl describe node my-node
    
  24. kubectl rollout pause: It pauses a rollout of a deployment

    kubectl rollout pause deployment/my-deployment
    
    • This command pauses the rollout of the deployment named my-deployment
  25. kubectl rollout resume: This command resumes a paused rollout of a deployment

    kubectl rollout resume deployment/my-deployment
    
    • This command resumes the paused rollout of the deployment named my-deployment.
  26. kubectl delete namespace: It deletes a Kubernetes namespace and all resources within it.

    kubectl delete namespace my-namespace
    
  27. kubectl get events: This command retrieves events from the cluster.

    kubectl get events
    
    • This command retrieves all events from the cluster, displaying information such as type, reason, and message
  28. kubectl get pods --show-labels: It displays additional labels associated with pods.

    kubectl get pods --show-labels
    
  29. kubectl exec -it my-pod -- ls /app: This command executes a command (ls/app) inside a running container in a pod interactively.

    kubectl exec -it my-pod -- ls /app
    
    • This command lists the contents of the /app directory inside the pod named my-pod
  30. kubectl create secret: It creates a secret in the cluster.

    kubectl create secret generic my-secret --from-literal=username=admin --from-literal=password=passw0rd
    
    • This command creates a secret named my-secret with two key-value pairs: username=admin and password=passw0rd
  31. kubectl edit deployment: This command opens the deployment configuration in a text editor, allowing you to make changes.

    kubectl edit deployment/my-deployment
    
  32. kubectl rollout restart: It restarts a rollout of a deployment by reapplying the current configuration

    kubectl rollout restart deployment/my-deployment
    
  33. kubectl rollout status: This command checks the status of a rollout for a deployment.

    kubectl rollout status deployment/my-deployment
    
  34. kubectl exec -it my-pod -- sh -c 'echo $ENV_VAR': This command executes a shell command (echo $ENV_VAR) inside a running container in a pod.

    
    kbectl exec -it my-pod -- sh -c 'echo $ENV_VAR'
    
    • This command prints the value of the environment variable ENV_VAR inside the pod named my-pod.
  35. kubectl apply -f deployment.yaml --record: It applies changes to a deployment and records the changes in the revision history.

    kubectl apply -f deployment.yaml --record
    
    • This command applies the changes specified in the deployment.yaml file to the deployment and records the changes in the revision history.
  36. kubectl get pods --field-selector=status.phase=Running: This command retrieves pods with a specific status phase, such as Running

    kubectl get pods --field-selector=status.phase=Running
    
    • This command retrieves all pods in the current namespace that are in the Running phase
  37. kubectl delete pod --grace-period=0 --force my-pod: It forcefully deletes a pod without waiting for the grace period.

    kubectl delete pod --grace-period=0 --force my-pod
    
    • This command forcefully deletes the pod named my-pod without waiting for the grace period to elapse
  38. kubectl describe service: This command provides detailed information about a Kubernetes service

    kubectl describe service my-service
    
  39. kubectl create deployment: It creates a deployment using the specified image.

    kubectl create deployment my-deployment --image=my-image:tag
    
  40. kubectl get deployment -o yaml: This command retrieves deployments and outputs the result in YAML format.

    kubectl get deployment -o yaml
    
    • This command retrieves all deployments in the current namespace and outputs the result in YAML format
  41. kubectl scale deployment: This command scales the number of replicas of a deployment.

    kubectl scale deployment/my-deployment --replicas=3
    
  42. kubectl rollout history deployment: It displays the revision history of a deployment.

    kubectl rollout history deployment/my-deployment
    
  43. kubectl rollout undo deployment --to-revision=: This command rolls back a deployment to a specific revision.

    kubectl rollout undo deployment/my-deployment --to-revision=3
    
  44. kubectl apply -f pod.yaml --namespace=: It applies a YAML file to a specific namespace.

    kubectl apply -f pod.yaml --namespace=my-namespace
    
    • This command applies the configuration specified in pod.yaml to the namespace my-namespace.
  45. kubectl logs -f my-pod: This command streams the logs of a pod continuously.

    kubectl logs -f my-pod
    
  46. kubectl get svc: It retrieves information about services in the cluster.

    kubectl get svc
    
  47. kubectl get pods -n : This command retrieves pods from a specific namespace.

    kubectl get pods -n my-namespace
    
    • This command retrieves all pods from the namespace my-namespace
  48. kubectl delete -f pod.yaml: It deletes resources specified in a YAML file.

    kubectl delete -f pod.yaml
    
  49. kubectl rollout status deployment/my-deployment: This command checks the status of a deployment rollout.

    kubectl rollout status deployment/my-deployment
    
  50. kubectl exec -it my-pod -- /bin/bash: This command starts an interactive shell inside a pod.

    kubectl exec -it my-pod -- /bin/bash
    
    • This command opens an interactive shell (/bin/bash) inside the pod named my-pod, allowing you to execute commands within it.
  51. kubectl apply -f --recursive: This command applies all YAML files in a directory and its subdirectories.

    kubectl apply -f ./my-resources/ --recursive
    
  52. kubectl rollout history deployment/my-deployment --revision=3: It displays details of a specific revision in the rollout history of a deployment.

    kubectl rollout history deployment/my-deployment --revision=3
    
    • This command shows details of the third revision in the rollout history of the deployment named my-deployment.
  53. This command rolls back a deployment to a specific revision

    kubectl rollout undo deployment/my-deployment --to-revision=2
    
  54. It validates the configuration file before applying changes.

    kubectl apply -f pod.yaml --validate=true
    
    • This command validates the pod.yaml file before applying changes to the cluster.
  55. This command retrieves the last 100 lines of logs from a pod.

    kubectl logs my-pod --tail=100
    
  56. It retrieves services with additional details including node port and cluster IP.

    kubectl get services -o wide
    
  57. This command retrieves pods with a status phase other than Running.

    kubectl get pods --field-selector=status.phase!=Running
    
  58. It forcefully deletes a pod without waiting for the grace period.

    kubectl delete pod my-pod --force --grace-period=0
    
  59. This command provides detailed information about a Kubernetes service.

    kubectl describe service my-service
    
  60. It exposes a deployment as a service with a specified type, port, and target port.

    kubectl expose deployment my-deployment --type=LoadBalancer --port=80 --target-port=8080
    
  61. This command retrieves deployments labeled with app=my-app.

    kubectl get deployments -l app=my-app
    
  62. It pauses the rollout of a deployment.

    kubectl rollout pause deployment/my-deployment
    
  63. This command resumes the rollout of a deployment.

    kubectl rollout resume deployment/my-deployment
    
  64. It retrieves logs from a specific container within a pod.

    kubectl logs my-pod --container=nginx
    
    • This command retrieves logs from the container named nginx within the pod my-pod.
  65. This command validates the configuration file without actually applying changes.

    kubectl apply -f pod.yaml --dry-run=client
    
    • This command checks if the configuration in pod.yaml can be applied without actually applying it.
  66. It retrieves pods sorted by creation timestamp.

    kubectl get pods --sort-by=.metadata.creationTimestamp
    
    • This command retrieves all pods in the current namespace sorted by their creation timestamp in ascending order.
  67. This command provides detailed information about a persistent volume claim.

    kubectl describe persistentvolumeclaim my-pvc
    
    • This command describes the persistent volume claim named my-pvc, displaying detailed information including its status and storage class.
  68. It continuously monitors the status of a deployment rollout.

    kubectl rollout status deployment/my-deployment --watch
    
    • This command continuously monitors the status of the rollout for the deployment named my-deployment.
  69. This command retrieves pods with a status phase of Pending.

    kubectl get pods --field-selector=status.phase=Pending
    
  70. It creates a generic secret from a file.

    kubectl create secret generic my-secret --from-file=./my-secret-file
    
    • This command creates a generic secret named my-secret from the contents of the file my-secret-file.
  71. This command restarts a rollout of a deployment by reapplying the current configuration.

    kubectl rollout restart deployment/my-deployment
    
    • This command restarts the rollout of the deployment named my-deployment.
  72. It adds a label to a namespace.

    kubectl label namespace my-namespace env=dev
    
  73. This command deletes a deployment.

    kubectl delete deployment my-deployment
    
  74. It retrieves pods from a specific namespace.

    kubectl get pods --namespace=my-namespace
    
  75. This command provides detailed information about a secret.

    kubectl describe secret my-secret
    
  76. It deletes a service.

    kubectl delete service my-service
    
  77. This command retrieves information about nodes in the cluster.

    kubectl get nodes
    
  78. It creates a config map from literal values.

    kubectl create configmap my-config --from-literal=key1=value1 --fromliteral=key2=value2
    
    • This command creates a config map named my-config with two key-value pairs: key1=value1 and key2=value2.
  79. This command displays details of a specific revision in the rollout history of a deployment.

    kubectl rollout history deployment/my-deployment --revision=3
    
    • This command shows details of the third revision in the rollout history of the deployment named my-deployment.
  80. It displays resource usage (CPU and memory) of pods in the cluster.

    kubectl top pods
    
  81. This command provides documentation about the Pod resource, including all its fields and their descriptions.

    kubectl explain pod
    
  82. It deletes a namespace and all resources within it.

    kubectl delete namespace my-namespace
    
  83. This command retrieves information about persistent volumes in the cluster.

    kubectl get pv
    
  84. It checks the status of a rollout and waits for a specific timeout before exiting.

    
    kubectl rollout status deployment/my-deployment --timeout=2m
    
    • This command checks the status of the rollout for the deployment named mydeployment and waits for a maximum of 2 minutes.
  85. This command applies a configuration file to a specific namespace.

    kubectl apply -f pod.yaml --namespace=my-namespace
    
  86. It retrieves information about secrets in the cluster.

    kubectl get secrets
    
  87. This command creates a NodePort service to expose a deployment on a specific port.

    kubectl create service nodeport my-service --tcp=80:8080
    
  88. It simulates rolling back a deployment to a specific revision without actually performing the rollback.

    kubectl rollout undo deployment/my-deployment --to-revision=2 --dry-run
    
  89. This command validates a configuration file without actually creating the resource.

    kubectl create -f pod.yaml --dry-run=client
    
  90. This command starts an interactive shell inside a specific container within a pod.

    kubectl exec -it my-pod --container=my-container -- /bin/bash
    
    • This command opens an interactive shell (/bin/bash) inside the container named my-container within the pod named my-pod
  91. This command creates a role within a namespace.

    kubectl create role my-role --verb=get --resource=pods
    
    • This command creates a role named my-role with permissions to get pods within the namespace.
  92. It applies changes to a deployment within a specific namespace and records the changes.

    kubectl apply -f deployment.yaml --namespace=my-namespace --record
    
  93. This command provides detailed information about a persistent volume.

    kubectl describe persistentvolume my-pv
    
  94. It creates a service account within a namespace.

    kubectl create serviceaccount my-service-account
    
  95. This command retrieves events sorted by creation timestamp.

    kubectl get events --sort-by=.metadata.creationTimestamp
    
    • This command retrieves all events in the current namespace sorted by their creation timestamp in ascending order.
  96. It provides detailed information about an Ingress resource.

    kubectl describe ingresses.extensions
    
    • This command describes all Ingress resources in the current namespace, displaying detailed information about each Ingress.
  97. It simulates rolling back deployment to the previous revision without actually performing the rollback.

    kubectl rollout undo deployment/my-deployment --dry-run=client
    
  98. This command scales the number of replicas of a deployment to 5 and records the change.

    kubectl scale deployment/my-deployment --replicas=5 --record
    
  99. It deletes a secret.

    kubectl delete secret my-secret
    
  100. This command retrieves information about Ingress resources in the cluster.

    kubectl get ingress