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

AWS By Jul 14, 2023 No Comments

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:
  • kubectl version – Print the client and server version information.
  • kubectl cluster-info – Display information about the cluster.
  • kubectl config view - Show the kubeconfig settings.
  • kubectl config use-context [context] - Switch to another kubeconfig context.
  • kubectl config current-context - Show the current context.
  • kubectl config set-context --current --namespace=[namespace] - Set a default namespace for the current context.
  1. Kubectl Commands for Interacting with Resources:
  • kubectl get [resource-type] - List resources of a specific type.
  • kubectl describe [resource-type/resource-name] - Show detailed information about a resource.
  • kubectl logs [pod-name] - Print the logs for a container in a pod.
  • kubectl exec -it [pod-name] -- [command] - Execute a command on a container in a pod.
  • kubectl port-forward [pod-name] [local-port:pod-port] - Forward a local port to a port on the pod.

Also Read: How to Set Up an EKS Cluster with eksctl command line

  1. Kubectl Commands for Managing Resources:
  • kubectl create -f [file-name.yaml] - Create a resource from a file.
  • kubectl apply -f [file-name.yaml] - Apply a configuration to a resource.
  • kubectl delete -f [file-name.yaml] - Delete a resource defined in a file.
  • kubectl delete [resource-type/resource-name] - Delete a resource.
  1. Kubectl Commands for Debugging:
  • kubectl logs -f [pod-name] - Stream pod logs.
  • kubectl exec -it [pod-name] -- /bin/bash - Start a bash session in a pod.
  • kubectl top pod [pod-name] - Show metrics for a specific pod.

Also Read: Commonly Used Docker Commands Cheat Sheet

  1. Kubectl Advanced Commands:
  • kubectl scale --replicas=[num] [resource-type/resource-name] - Scale a resource.
  • kubectl rollout status [resource-type/resource-name] - Check the status of a resource rollout.
  • kubectl rollout undo [resource-type/resource-name] - Roll back a resource to the previous version.
  • kubectl rollout history [resource-type/resource-name] - Show the history of rollouts for a resource.
  • kubectl label [resource-type/resource-name] key=value - Add a label to a resource.
  • 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.


I'm Abhay Singh, an Architect with 9 Years of It experience. AWS Certified Solutions Architect.

No Comments

Leave a comment

Your email address will not be published. Required fields are marked *