- To get cluster information:
kubectl cluster-info
- To view all the nodes present in the cluster
kubectl get nodes
- To describe a specific node
kubectl describe node
- To drain a node for maintenance
kubectl drain node
Namespace commands
kubectl create namespace
Kubectl get namespace
Kubectl describe namespace <namespace-name>
- Switch to a different namespace
kubectl config set-context –current –namespace=<namespace-name>
- Edit and update the namespace definition.
kubectl edit namespace <namespace-name>
kubectl delete namespace <namespace-name>
Creating Resources using YAML file
- Create an object imperatively
kubectl create -f <resource-definition.yaml>
- Update a resource from a YAML file.
kubectl apply -f <resource-definition.yaml>
- Create a resource by using the URL
kubectl apply -f https://url-to-resource-definition.yaml
For Viewing and Finding Resources
- List all resources of a specific type.
kubectl get <resource-type>
- List all resources with additional details.
kubectl get <resource-type> -o wide
- Describe a specific resource.
kubectl describe <resource-type> <resource-name>
- List all resources in a specific namespace.
kubectl get <resource-type> -n <namespace>
- List all resources with a specific label.
kubectl get <resource-type> -l <label-key>=<label-value>
- List resources with a specific label selector.
kubectl get <resource-type> -l <label-selector>
- List all resources sorted by a specific field.
kubectl get <resource-type> -sort-by=<field>
- List resources with a specific field selector.
kubectl get <resource-type> –field-selector=<field-selector>
For Deleting Resources
kubectl delete <resource-type> <resourse-name>
- Delete multiple resources.
kubectl delete <resource-type1> <resourse-name1> <resource-type2> <resourse-name2>
- Delete all resources of a specific type.
kubectl delete <resource-type> -all
- Delete resources by using YAML file
kubectl delete -f <resource-definition.yaml>
- Delete the resource by using URL.
kubectl delete -f https://url-to-resource-definition.yaml