kubectl - Essential Commands

Anurag HatoleAnurag Hatole
6 min read

Kubernetes is an essential tool for modern cloud-native applications, and kubectl is the command-line interface for interacting with Kubernetes clusters. This guide covers various kubectl commands categorized by functionality, providing you with a solid understanding of how to manage your Kubernetes environment.

1. Cluster Management Commands

CommandDescription
kubectl cluster-infoDisplays the address of the Kubernetes master and services running in the cluster
kubectl get nodesLists all the nodes in your cluster, providing information on their status, roles and age
kubectl describe nodeShows detailed information about a specific node, including labels, annotations, resource usage, and conditions
kubectl get nodes -o wideProvides additional information about each node, including the version of Kubernetes they are running
kubectl get eventsLists recent events in the cluster, helping to diagnose issues

2. Pod Management Commands

CommandDescription
kubectl get podsLists all pods in the current namespace, showing their status, restart counts, and age
kubectl describe pod <pod-name>Displays detailed information about a specific pod, including events, container status, and resource requests
kubectl logs <pod-name>Retrieves the logs from a specific pod’s containers, useful for debugging
kubectl delete pod <pod-name>Deletes a specified pod from the cluster
kubectl run <pod-name> --image=<image-name>Creates a new pod using the specified image
kubectl exec -it <pod-name> -- /bin/shOpens an interactive terminal session inside a pod for debugging purposes
kubectl get pod <pod-name> -o jsonpath='{.status.podIP}'Retrieves the IP address of a specified pod

3. Deployment Management Commands

CommandDescription
kubectl get deploymentsLists all deployments in the current namespace, showing their replicas and current status
kubectl describe deployment <deployment-name>Displays detailed information about a specific deployment, including its strategy, replicas, and pod template
kubectl scale deployment <deployment-name> --replicas=<number>Scales the specified deployment to the desired number of replicas
kubectl set image deployment/<deployment-name> <container-name>=<new-image>Updates the container image of a running deployment
kubectl rollout undo deployment/<deployment-name>Rolls back to the previous version of a deployment
kubectl rollout status deployment/<deployment-name>Displays the status of a deployment rollout, indicating whether it's completed or in progres

4. Service Management Commands

CommandDescription
kubectl get servicesLists all services in the current namespace, showing their type and endpoints
kubectl describe service <service-name>Displays detailed information about a specific service, including its selectors and endpoints
kubectl expose deployment <deployment-name> --type=LoadBalancer --name=<service-name>Exposes a deployment as a service with a LoadBalancer type
kubectl delete service <service-name>Deletes a specified service from the cluster
kubectl get endpoints <service-name>Displays the endpoints associated with a specific service

5. ConfigMap and Secret Management Commands

CommandDescription
kubectl get configmapsLists all ConfigMaps in the current namespace
kubectl describe configmap <configmap-name>Displays detailed information about a specific ConfigMap
kubectl create configmap <configmap-name> --from-literal=<key>=<value>Creates a new ConfigMap from literal values
kubectl get secretsLists all Secrets in the current namespace
kubectl describe secret <secret-name>Displays detailed information about a specific Secret
kubectl create secret generic <secret-name> --from-literal=<key>=<value>Creates a new Secret using literal values

6. Namespace Management Commands

CommandDescription
kubectl get namespacesLists all namespaces in the cluster
kubectl create namespace <namespaces-name>Creates a new namespace
kubectl delete namespace <namespaces-name>Deletes a specified namespace and all its resources
kubectl get all -n <namespaces-naame>Lists all resources in the specified namespace

7. Resource Quotas and Limits Commands

CommandDescription
kubectl get resourcequotaists all resource quotas in the current namespace
kubectl describe resourcequota <quota-name>Displays detailed information about a specific resource quota
kubectl create quota <quota-name> --hard=pods=<number> ,requests.cpu=<value>,limits.cpu=<value>Creates a new resource quota to limit resource usage
kubectl get pod <pod-name> -o jsonpath='{.spec.containers[*].resources}'Retrieves resource limits set for a specific pod's containers.

8. Cluster Autoscaler and Horizontal Pod Autoscaler Commands

CommandDescription
kubectl get hpaLists all Horizontal Pod Autoscalers in the current namespace
kubectl autoscale deployment <deployment-name> --min=<min-replicas> --max=<max-replicas> --cpu-percent=<cpu-taget>Creates a Horizontal Pod Autoscaler for a deployment
kubectl describe hpa <hpa-name>Displays detailed information about a specific Horizontal Pod Autoscaler

9. Accessing and Managing Resources

CommandDescription
kubectl get allLists all resources (pods, services, deployments, etc.) in the current namespace
kubectl get <resource-type>Replace <resource-type> (e.g., pods, services) to list that specific type of resource
kubectl get <resource-type> <resource-name> -o yamlRetrieves detailed information about a specific resource in YAML format

10. Context and Configuration Commands

CommandDescription
kubectl config current-contextDisplays the name of the current context in use
kubectl config get-contextsLists all contexts configured in your kubectl configuration
kubectl config use-context <context-name>Changes the current context to the specified one.
kubectl config viewDisplays the current kubectl configuration settings

11. Additional Important Commands

CommandDescription
kubectl get pvLists all persistent volumes in the cluster, providing insights into storage resources
kubectl get pvcLists all persistent volume claims in the current namespace, indicating the requested storage by pods
kubectl get ingressLists all ingress resources, which manage external access to services in a cluster
kubectl describe ingress <ingress-name>Displays detailed information about a specific ingress resource
kubectl port-forward <pod-name> <local-port>:<port-port>Forwards one or more local ports to a pod, allowing you to access services running in the cluster from your local machine
kubectl apply -f <file-name>.yamlApplies changes defined in a YAML file, creating or updating resources as needed
kubectl get all --all-namespacesLists all resources across all namespaces, providing a broader view of your cluster
kubectl top podsDisplays resource usage (CPU and memory) metrics for pods, useful for monitoring performance
kubectl get <resource-type> <resource-name> -o yaml > <resource-name>.yamlExports the configuration of a resource to a YAML file, allowing for backup or replication

Conclusion

With this comprehensive guide to kubectl commands, you are now equipped to manage your Kubernetes clusters efficiently. Whether you're deploying applications, managing resources, or troubleshooting issues, mastering these commands will enhance your Kubernetes experience.

As you continue your journey in cloud-native technologies, remember to explore more advanced features and capabilities of Kubernetes to fully leverage its potential.


4
Subscribe to my newsletter

Read articles from Anurag Hatole directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Anurag Hatole
Anurag Hatole

👋 Hey there! I’m Anurag, a final-year BCA student with a solid foundation in the MERN stack and a deep passion for Cloud and DevOps technologies, currently learning and expanding my knowledge in these areas.