Kubernetes has become the de facto standard for container orchestration, providing a powerful platform for managing containerized applications at scale. At the heart of interacting with a Kubernetes cluster is kubectl
, the command-line tool that allows you to run commands against Kubernetes clusters. Whether you're a seasoned Kubernetes operator or just getting started, knowing the most commonly used kubectl
commands can significantly streamline your workflows. In this article, we'll explore 25 essential kubectl
commands that every Kubernetes user should know.
1. View Cluster Info
To get an overview of your cluster's status, use:
kubectl cluster-info
This command provides the addresses of the Kubernetes master and services.
2. Get Nodes
To list all nodes in your cluster:
kubectl get nodes
This command shows the nodes' statuses, roles, and other details.
3. Get Pods in a Namespace
To see all pods within a specific namespace:
kubectl get pods -n <namespace>
Replace <namespace>
with your desired namespace.
4. Get All Pods in All Namespaces
For a comprehensive view of all pods across namespaces:
kubectl get pods --all-namespaces
This is useful for cluster-wide monitoring.
5. Describe a Pod
To get detailed information about a specific pod:
kubectl describe pod <pod_name> -n <namespace>
This command provides in-depth details about the pod's state and events.
6. Create a Resource from a YAML File
To create resources such as pods, services, or deployments from a YAML file:
kubectl apply -f <filename>.yaml
Ensure your YAML file is correctly formatted.
7. Delete a Resource from a YAML File
To delete resources defined in a YAML file:
kubectl delete -f <filename>.yaml
This command helps clean up resources when they are no longer needed.
8. Scale a Deployment
To adjust the number of replicas in a deployment:
kubectl scale deployment <deployment_name> --replicas=<number_of_replicas> -n <namespace>
Scaling deployments helps manage load and availability.
9. Get Services
To list all services in a namespace:
kubectl get svc -n <namespace>
Services manage how applications communicate within the cluster.
10. Expose a Deployment as a Service
To create a service for a deployment:
kubectl expose deployment <deployment_name> --type=<service_type> --name=<service_name> -n <namespace>
Service types include ClusterIP, NodePort, LoadBalancer, etc.
11. Get Logs from a Pod
To retrieve logs from a specific pod:
kubectl logs <pod_name> -n <namespace>
Logs are crucial for debugging and monitoring.
12. Stream Logs from a Pod
To continuously stream logs from a pod:
kubectl logs -f <pod_name> -n <namespace>
This command is helpful for real-time debugging.
13. Execute a Command in a Pod
To run commands inside a running pod:
kubectl exec -it <pod_name> -n <namespace> -- <command>
Use this for tasks like debugging or running diagnostics.
14. Get ConfigMaps
To list all ConfigMaps in a namespace:
kubectl get configmaps -n <namespace>
ConfigMaps are used to manage configuration data.
15. Get Secrets
To list all secrets in a namespace:
kubectl get secrets -n <namespace>
Secrets are used to manage sensitive data.
16. Create a Namespace
To create a new namespace:
kubectl create namespace <namespace_name>
Namespaces help organize and separate cluster resources.
17. Delete a Namespace
To delete an existing namespace:
kubectl delete namespace <namespace_name>
Be cautious, as this will delete all resources within the namespace.
18. Get Deployments
To list all deployments in a namespace:
kubectl get deployments -n <namespace>
Deployments manage how applications are rolled out and scaled.
19. Describe a Deployment
To get detailed information about a deployment:
kubectl describe deployment <deployment_name> -n <namespace>
This command provides status and event information for deployments.
20. Get ReplicaSets
To list all ReplicaSets in a namespace:
kubectl get rs -n <namespace>
ReplicaSets ensure the specified number of pod replicas are running.
21. Get Events
To view events in a namespace:
kubectl get events -n <namespace>
Events provide insights into what is happening within the cluster.
22. Get Persistent Volume Claims
To list all Persistent Volume Claims (PVCs) in a namespace:
kubectl get pvc -n <namespace>
PVCs manage storage resources in Kubernetes.
23. Create a Service Account
To create a new service account:
kubectl create serviceaccount <serviceaccount_name> -n <namespace>
Service accounts provide identities for processes that run in pods.
24. Get Roles
To list all roles in a namespace:
kubectl get roles -n <namespace>
Roles define permissions within a namespace.
25. Get Role Bindings
To list all role bindings in a namespace:
kubectl get rolebindings -n <namespace>
Role bindings associate roles with users or service accounts.
Conclusion
Mastering these 25 kubectl
commands will enhance your ability to manage Kubernetes clusters effectively. From basic cluster information retrieval to advanced resource management, these commands are essential tools for any Kubernetes operator. Keep practicing and exploring more commands to deepen your Kubernetes expertise. Happy clustering!