Top 30 Kubernetes Commands with Syntax and Examples

S3CloudHub - Sep 6 - - Dev Community

Kubernetes has become the de-facto orchestration platform for managing containerized applications at scale. Whether you’re deploying, scaling, or monitoring applications, mastering kubectl commands is essential for managing your Kubernetes clusters efficiently. Here, we explore the top 30 Kubernetes commands, complete with syntax and examples, that every developer and DevOps engineer should know.

Image description
1. kubectl get pods
Syntax: kubectl get pods
Example: Lists all pods in the current namespace

kubectl get pods
Enter fullscreen mode Exit fullscreen mode

2. kubectl get nodes
Syntax: kubectl get nodes
Example: Lists all nodes in the cluster.

kubectl get nodes
Enter fullscreen mode Exit fullscreen mode

3. kubectl get services
Syntax: kubectl get svc
Example: Lists all services in the namespace.

kubectl get svc
Enter fullscreen mode Exit fullscreen mode

4. kubectl describe pod
Syntax: kubectl describe pod
Example: Provides detailed information about a pod.

kubectl describe pod my-pod
Enter fullscreen mode Exit fullscreen mode

5. kubectl create
Syntax: kubectl create -f
Example: Creates resources from a YAML file.

kubectl create -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

6. kubectl apply
Syntax: kubectl apply -f
Example: Updates resources defined in a YAML file.

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

7. kubectl delete
Syntax: kubectl delete -f
Example: Deletes the resources defined in the file.

kubectl delete -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

8. kubectl logs
Syntax: kubectl logs
Example: Fetches the logs of a pod.

kubectl logs my-pod
Enter fullscreen mode Exit fullscreen mode

9. kubectl exec
Syntax: kubectl exec -it --
Example: Executes a command in a running pod.

kubectl exec -it my-pod -- /bin/bash
Enter fullscreen mode Exit fullscreen mode

10. kubectl scale
Syntax: kubectl scale --replicas= /
Example: Scales the number of replicas in a deployment.

kubectl scale --replicas=3 deployment/my-deployment
Enter fullscreen mode Exit fullscreen mode

11. kubectl edit
Syntax: kubectl edit
Example: Allows inline editing of resources like deployments.

kubectl edit deployment my-deployment
Enter fullscreen mode Exit fullscreen mode

12. kubectl port-forward
Syntax: kubectl port-forward :
Example: Forwards a local port to a pod’s port.

kubectl port-forward my-pod 8080:80
Enter fullscreen mode Exit fullscreen mode

13. kubectl expose
Syntax: kubectl expose --port= --target-port=
Example: Exposes a resource as a new service.

kubectl expose deployment my-deployment --port=80 --target-port=8080
Enter fullscreen mode Exit fullscreen mode

14. kubectl get configmaps
Syntax: kubectl get configmaps
Example: Lists all ConfigMaps in the namespace.

kubectl get configmaps
Enter fullscreen mode Exit fullscreen mode

15. kubectl get secrets
Syntax: kubectl get secrets
Example: Lists all secrets.

kubectl get secrets
Enter fullscreen mode Exit fullscreen mode

16. kubectl apply -k
Syntax: kubectl apply -k
Example: Applies resource configurations from a Kustomize directory.

kubectl apply -k ./my-kustomization/
Enter fullscreen mode Exit fullscreen mode

17. kubectl cp
Syntax: kubectl cp :
Example: Copies files between a local machine and a pod.

kubectl cp ./file.txt my-pod:/tmp/
Enter fullscreen mode Exit fullscreen mode

18. kubectl rollout status
Syntax: kubectl rollout status deployment/
Example: Monitors the status of a rollout.

kubectl rollout status deployment/my-deployment
Enter fullscreen mode Exit fullscreen mode

19. kubectl rollout undo
Syntax: kubectl rollout undo deployment/
Example: Rolls back a deployment to a previous version.

kubectl rollout undo deployment/my-deployment
Enter fullscreen mode Exit fullscreen mode

20. kubectl get events
Syntax: kubectl get events
Example: Lists recent cluster events.

kubectl get events
Enter fullscreen mode Exit fullscreen mode

21. kubectl top pod
Syntax: kubectl top pod
Example: Shows resource usage of pods.

kubectl top pod
Enter fullscreen mode Exit fullscreen mode

22. kubectl get namespaces
Syntax: kubectl get namespaces
Example: Lists all namespaces.

kubectl get namespaces
Enter fullscreen mode Exit fullscreen mode

23. kubectl describe node
Syntax: kubectl describe node
Example: Provides detailed information about a node.

kubectl describe node my-node
Enter fullscreen mode Exit fullscreen mode

24. kubectl delete pod
Syntax: kubectl delete pod
Example: Deletes a specific pod.

kubectl delete pod my-pod
Enter fullscreen mode Exit fullscreen mode

25. kubectl get deployments
Syntax: kubectl get deployments
Example: Lists all deployments in the namespace.

kubectl get deployments
Enter fullscreen mode Exit fullscreen mode

26. kubectl create namespace
Syntax: kubectl create namespace
Example: Creates a new namespace.

kubectl create namespace my-namespace
Enter fullscreen mode Exit fullscreen mode

27. kubectl config use-context
Syntax: kubectl config use-context
Example: Switches to another context.

kubectl config use-context my-cluster
Enter fullscreen mode Exit fullscreen mode

28. kubectl delete service
Syntax: kubectl delete service
Example: Deletes a specific service.

kubectl delete service my-service
Enter fullscreen mode Exit fullscreen mode

29. kubectl get replicasets
Syntax: kubectl get rs
Example: Lists all ReplicaSets.

kubectl get rs
Enter fullscreen mode Exit fullscreen mode

30. kubectl drain
Syntax: kubectl drain --ignore-daemonsets
Example: Safely drains a node for maintenance.

kubectl drain my-node --ignore-daemonsets
Enter fullscreen mode Exit fullscreen mode

Explore more detailed content and step-by-step guides on our YouTube channel:-
image alt text here

Connect with Us!
Stay connected with us for the latest updates, tutorials, and exclusive content:

WhatsApp:- https://www.whatsapp.com/channel/0029VaeX6b73GJOuCyYRik0i
facebook:- https://www.facebook.com/S3CloudHub
youtube:- https://www.youtube.com/@s3cloudhub
github:- https://github.com/S3CloudHubRepo

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .