Kubernetes

Cheat Sheet

Kubectl

Kubectl Cheat Sheet: Top 25 Commands for Kubernetes Management

Vatsal Bajpai
Vatsal Bajpai
8 min read·
Cover Image for Kubectl Cheat Sheet: Top 25 Commands for Kubernetes Management

Kubernetes is a powerful container orchestration platform, and kubectl is its command-line tool that allows you to interact with your Kubernetes cluster. Whether you're deploying applications, managing resources, or troubleshooting issues, knowing the right kubectl commands can significantly enhance your productivity. In this blog, we’ll cover the top 25 kubectl commands that every Kubernetes administrator should know.

1. Get Cluster Information

kubectl cluster-info
  • This command provides information about the Kubernetes cluster, including the master and services.

2. Get Nodes

kubectl get nodes
  • This command lists all the nodes in the cluster along with their status, roles, and other details.

3. Get Pods

kubectl get pods
  • This command retrieves a list of all pods in the current namespace. To see pods in all namespaces, use:
kubectl get pods --all-namespaces

4. Get Services

kubectl get services
  • This command lists all services in the current namespace. To see services in all namespaces, use:
kubectl get services --all-namespaces

5. Describe a Pod

kubectl describe pod <pod-name>
  • This command provides detailed information about a specific pod, including its events, status, and resource usage.

6. Create a Resource from a YAML File

kubectl apply -f <file.yaml>
  • This command creates or updates a resource defined in a YAML file.

7. Delete a Pod

kubectl delete pod <pod-name>
  • This command deletes a specific pod. To delete all pods with a specific label, use:
kubectl delete pods -l <label-key>=<label-value>

8. Scale a Deployment

kubectl scale deployment <deployment-name> --replicas=<number>
  • This command scales a deployment to the specified number of replicas.

9. Get Logs from a Pod

kubectl logs <pod-name>
  • This command retrieves logs from a specific pod. To get logs from a specific container in a pod, use:
kubectl logs <pod-name> -c <container-name>

10. Execute a Command in a Pod

kubectl exec -it <pod-name> -- <command>
  • This command allows you to execute a command inside a running pod. The -it flag is used for interactive terminal access.

11. Get Resource Usage

kubectl top pods
  • This command shows the resource usage (CPU and memory) of all pods in the current namespace. To see node usage, use:
kubectl top nodes

12. Get Events

kubectl get events
  • This command lists all events in the current namespace, which can help in troubleshooting.

13. Port Forwarding

kubectl port-forward <pod-name> <local-port>:<pod-port>
  • This command forwards a local port to a port on a pod, allowing you to access services running in the pod.

14. Get ConfigMaps

kubectl get configmaps
  • This command lists all ConfigMaps in the current namespace.

15. Describe a ConfigMap

kubectl describe configmap <configmap-name>
  • This command provides detailed information about a specific ConfigMap.

16. Get Secrets

kubectl get secrets
  • This command lists all secrets in the current namespace.

17. Describe a Secret

kubectl describe secret <secret-name>
  • This command provides detailed information about a specific secret.

18. Get Deployments

kubectl get deployments
  • This command lists all deployments in the current namespace.

19. Rollback a Deployment

kubectl rollout undo deployment/<deployment-name>
  • This command rolls back a deployment to the previous version.

20. Get Persistent Volumes

kubectl get pv
  • This command lists all persistent volumes in the cluster.

21. Get Persistent Volume Claims

kubectl get pvc
  • This command lists all persistent volume claims in the current namespace.

22. Get Namespaces

kubectl get namespaces
  • This command lists all namespaces in the cluster.

23. Create a Namespace

kubectl create namespace <namespace-name>
  • This command creates a new namespace in the cluster.

24. Set the Current Context

kubectl config use-context <context-name>
  • This command sets the current context to the specified context, allowing you to switch between different clusters or namespaces.

25. Get Cluster Resource Quotas

kubectl get resourcequotas
  • This command lists all resource quotas in the current namespace.

I hope this blog post provides valuable insights into DevOps, Kubernetes and cost-saving benefits. If you have any questions or need further assistance, feel free to ask!

If you like this, follow us on Twitter and LinkedIn and explore our platform to help save you more cloud costs - gravitycloud.ai


footer

Share this Article: