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
Command | Description |
kubectl cluster-info | Displays the address of the Kubernetes master and services running in the cluster |
kubectl get nodes | Lists all the nodes in your cluster, providing information on their status, roles and age |
kubectl describe node | Shows detailed information about a specific node, including labels, annotations, resource usage, and conditions |
kubectl get nodes -o wide | Provides additional information about each node, including the version of Kubernetes they are running |
kubectl get events | Lists recent events in the cluster, helping to diagnose issues |
2. Pod Management Commands
Command | Description |
kubectl get pods | Lists 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/sh | Opens 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
Command | Description |
kubectl get deployments | Lists 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
Command | Description |
kubectl get services | Lists 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
Command | Description |
kubectl get configmaps | Lists 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 secrets | Lists 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
Command | Description |
kubectl get namespaces | Lists 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
Command | Description |
kubectl get resourcequota | ists 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
Command | Description |
kubectl get hpa | Lists 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
Command | Description |
kubectl get all | Lists 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 yaml | Retrieves detailed information about a specific resource in YAML format |
10. Context and Configuration Commands
Command | Description |
kubectl config current-context | Displays the name of the current context in use |
kubectl config get-contexts | Lists all contexts configured in your kubectl configuration |
kubectl config use-context <context-name> | Changes the current context to the specified one. |
kubectl config view | Displays the current kubectl configuration settings |
11. Additional Important Commands
Command | Description |
kubectl get pv | Lists all persistent volumes in the cluster, providing insights into storage resources |
kubectl get pvc | Lists all persistent volume claims in the current namespace, indicating the requested storage by pods |
kubectl get ingress | Lists 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>.yaml | Applies changes defined in a YAML file, creating or updating resources as needed |
kubectl get all --all-namespaces | Lists all resources across all namespaces, providing a broader view of your cluster |
kubectl top pods | Displays resource usage (CPU and memory) metrics for pods, useful for monitoring performance |
kubectl get <resource-type> <resource-name> -o yaml > <resource-name>.yaml | Exports 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.
Subscribe to my newsletter
Read articles from Anurag Hatole directly inside your inbox. Subscribe to the newsletter, and don't miss out.