Powerful kubectl Commands for Kubernetes | Manage Resources, Interact with Clusters, Debug & More

Powerful kubectl Commands for Kubernetes | Manage Resources, Interact with Clusters, Debug & More

Autopublished from RSS Original article

Kubectl is a powerful command-line interface for running commands against Kubernetes clusters. Below are some commonly used kubectl commands:

Please note that this is not a comprehensive list of all possible kubectl commands. Some commands may have been omitted for brevity and clarity. As always, use the kubectl --help or kubectl <command> --help command to see more details about a specific command.

  1. Basic kubectl Commands:

  2. kubectl version – Print the client and server version information.

  3. kubectl cluster-info – Display information about the cluster.
  4. kubectl config view – Show the kubeconfig settings.
  5. kubectl config use-context [context] – Switch to another kubeconfig context.
  6. kubectl config current-context – Show the current context.
  7. kubectl config set-context --current --namespace=[namespace] – Set a default namespace for the current context.

  8. Kubectl Commands for Interacting with Resources:

  9. kubectl get [resource-type] – List resources of a specific type.

  10. kubectl describe [resource-type/resource-name] – Show detailed information about a resource.
  11. kubectl logs [pod-name] – Print the logs for a container in a pod.
  12. kubectl exec -it [pod-name] -- [command] – Execute a command on a container in a pod.
  13. kubectl port-forward [pod-name] [local-port:pod-port] – Forward a local port to a port on the pod.

  14. Kubectl Commands for Managing Resources:

  15. kubectl create -f [file-name.yaml] – Create a resource from a file.

  16. kubectl apply -f [file-name.yaml] – Apply a configuration to a resource.
  17. kubectl delete -f [file-name.yaml] – Delete a resource defined in a file.
  18. kubectl delete [resource-type/resource-name] – Delete a resource.

  19. Kubectl Commands for Debugging:

  20. kubectl logs -f [pod-name] – Stream pod logs.

  21. kubectl exec -it [pod-name] -- /bin/bash – Start a bash session in a pod.
  22. kubectl top pod [pod-name] – Show metrics for a specific pod.

  23. Kubectl Advanced Commands:

  24. kubectl scale --replicas=[num] [resource-type/resource-name] – Scale a resource.

  25. kubectl rollout status [resource-type/resource-name] – Check the status of a resource rollout.
  26. kubectl rollout undo [resource-type/resource-name] – Roll back a resource to the previous version.
  27. kubectl rollout history [resource-type/resource-name] – Show the history of rollouts for a resource.
  28. kubectl label [resource-type/resource-name] key=value – Add a label to a resource.
  29. kubectl annotate [resource-type/resource-name] key=value – Add an annotation to a resource.

Remember to replace [resource-type], [resource-name], [pod-name], [num], [file-name.yaml], [context], [namespace], [command], [local-port], and [pod-port] with appropriate values based on your specific use case.

Did you find this article valuable?

Support Abhay Singh by becoming a sponsor. Any amount is appreciated!