100 Kubernetes Commands
100 Kubernetes Commands
This command forwards local port 8080 to port 80 on the pod named my-pod.
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. For
example:
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.
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. For instance:
kubectl label pod my-pod app=backend
This command adds the label app=backend to the pod named my-pod.
14. kubectl cluster-info: It displays cluster info such as server URL and
Kubernetes version. For instance:
kubectl cluster-info
15. kubectl apply -f -: This command applies configuration from the standard
input. For example:
cat pod.yaml | kubectl apply -f -
16. kubectl rollout history: This command views rollout history of a deployment.
For instance:
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 a deployment to a previous revision. For
example:
kubectl rollout undo deployment/my-deployment
20. kubectl api-resources: This command lists all available API resources. For
instance:
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. For example:
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. For instance:
kubectl get pods -o wide
This command displays pods along with additional details such as the node
they are running on and their IP addresses.
23. kubectl describe node: This command provides detailed information about a
Kubernetes node. For example:
kubectl describe node my-node
This command deletes the namespace named my-namespace along with all
resources within it.
27. kubectl get events: This command retrieves events from the cluster. For
example:
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. For instance:
kubectl get pods --show-labels
This command displays pods along with all labels associated with them.
29. kubectl exec -it my-pod -- ls /app: This command executes a command (ls
/app) inside a running container in a pod interactively. For example:
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. For instance:
kubectl create secret generic my-secret --from-literal=username=admin
--from-literal=password=passw0rd
33. kubectl rollout status: This command checks the status of a rollout for a
deployment. For example:
kubectl rollout status deployment/my-deployment
This command checks the status of the rollout for the deployment named 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. For
instance:
kubectl 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.
This command retrieves all pods in the current namespace that are in
the Running phase.
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. For instance:
kubectl describe service my-service
40. kubectl get deployment -o yaml: This command retrieves deployments and
outputs the result in YAML format. For instance:
kubectl get deployment -o yaml
41. kubectl scale deployment: This command scales the number of replicas of a
deployment. For example:
kubectl scale deployment/my-deployment --replicas=3
This command shows the revision history of the deployment named my-deployment.
43. kubectl rollout undo deployment --to-revision=: This command rolls back
a deployment to a specific revision. For example:
This command rolls back the deployment named my-deployment to the third revision.
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. For instance:
This command continuously streams the logs of the pod named my-pod to the
terminal.
46. kubectl get svc: It retrieves information about services in the cluster. For
example:
This command retrieves information about all services in the current namespace.
47. kubectl get pods -n : This command retrieves pods from a specific
namespace. For instance:
48. kubectl delete -f pod.yaml: It deletes resources specified in a YAML file. For
example:
This command checks the status of the rollout for the deployment named my-
deployment.
50. kubectl exec -it my-pod -- /bin/bash: This command starts an interactive
shell inside a pod. For example:
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. For example:
This command applies all YAML files located in the my-resources directory and its
subdirectories.
This command rolls back the deployment named my-deployment to the second
revision.
This command validates the pod.yaml file before applying changes to the cluster.
55. kubectl logs my-pod --tail=100: This command retrieves the last 100 lines of
logs from a pod. For example:
This command retrieves the last 100 lines of logs from the pod named my-pod.
56. kubectl get services -o wide: It retrieves services with additional details
including node port and cluster IP. For instance:
This command retrieves services along with additional details such as node port and
cluster IP.
This command retrieves all pods in the current namespace that are not in
the Running phase.
This command forcefully deletes the pod named my-pod without waiting for the
grace period to elapse.
59. kubectl describe service my-service: This command provides detailed
information about a Kubernetes service. For instance:
This command resumes the paused rollout of the deployment named my-deployment.
This command retrieves logs from the container named nginx within the pod my-pod.
This command retrieves all pods in the current namespace sorted by their creation
timestamp in ascending order.
This command describes the persistent volume claim named my-pvc, displaying
detailed information including its status and storage class.
This command continuously monitors the status of the rollout for the deployment
named my-deployment.
This command retrieves all pods in the current namespace that are in
the Pending phase.
This command creates a generic secret named my-secret from the contents of the
file my-secret-file.
This command adds the label env=dev to the namespace named my-namespace.
77. kubectl get nodes: This command retrieves information about nodes in the
cluster. For example:
This command shows details of the third revision in the rollout history of the
deployment named my-deployment.
80. kubectl top pods: It displays resource usage (CPU and memory) of pods in
the cluster. For instance:
81. kubectl explain pod: This command provides documentation about the Pod
resource, including all its fields and their descriptions. For example:
This command deletes the namespace named my-namespace along with all resources
within it.
83. kubectl get pv: This command retrieves information about persistent volumes
in the cluster. For example:
kubectl get pv
This command retrieves information about all persistent volumes in the cluster.
This command checks the status of the rollout for the deployment named my-
deployment and waits for a maximum of 2 minutes.
85. kubectl apply -f pod.yaml --namespace=my-namespace: This command
applies a configuration file to a specific namespace. For example:
This command applies the configuration specified in pod.yaml to the namespace my-
namespace.
86. kubectl get secrets: It retrieves information about secrets in the cluster. For
instance:
This command retrieves information about all secrets in the current namespace.
This command simulates rolling back the deployment named my-deployment to the
second revision without actually performing the rollback.
This command validates the configuration in pod.yaml without actually creating the
pod.
This command opens an interactive shell (/bin/bash) inside the container named my-
container within the pod named my-pod.
91. kubectl create role: This command creates a role within a namespace. For
example:
This command creates a role named my-role with permissions to get pods within the
namespace.
This command describes the persistent volume named my-pv, displaying detailed
information including its capacity and access modes.
This command retrieves all events in the current namespace sorted by their creation
timestamp in ascending order.
This command describes all Ingress resources in the current namespace, displaying
detailed information about each Ingress.
97. kubectl rollout undo deployment/my-deployment --dry-run=client: It
simulates rolling back a deployment to the previous revision without actually
performing the rollback. For example:
This command simulates rolling back the deployment named my-deployment to the
previous revision without actually performing the rollback.
This command scales the deployment named my-deployment to have 5 replicas and
records the change.
100. kubectl get ingress: This command retrieves information about Ingress
resources in the cluster. For example:
This command retrieves information about all Ingress resources in the current
namespace.