kubectl
The kubectl
command-line tool is the primary way to interact with Kubernetes clusters. Below is a comprehensive list of kubectl
commands, organized by category. This includes commands for managing resources, configuring the cluster, and obtaining cluster information.
1. Basic Commands
- View Kubernetes cluster information:
kubectl cluster-info
- Get the Kubernetes server version:
kubectl version
- Display help for kubectl commands:
kubectl help
2. Context and Configuration
- View the current context:
kubectl config current-context
- List all contexts:
kubectl config get-contexts
- Switch to a different context:
kubectl config use-context <context-name>
- View the entire kubeconfig file:
kubectl config view
- Set a new context:
kubectl config set-context <context-name>
- Unset (delete) a context:
kubectl config unset contexts.<context-name>
3. Cluster Management
- Get cluster component statuses:
kubectl get componentstatuses
- List nodes in the cluster:
kubectl get nodes
- Get detailed information about a node:
kubectl describe node <node-name>
4. Pod Management
- List all pods in the default namespace:
kubectl get pods
- List all pods in all namespaces:
kubectl get pods --all-namespaces
- Describe a specific pod:
kubectl describe pod <pod-name>
- Create a pod from a YAML file:
kubectl apply -f <pod.yaml>
- Delete a pod:
kubectl delete pod <pod-name>
- Get pod logs:
kubectl logs <pod-name>
- Execute a command in a pod:
kubectl exec -it <pod-name> -- <command>
5. Service Management
- List all services:
kubectl get svc
- Describe a service:
kubectl describe svc <service-name>
- Create a service from a YAML file:
kubectl apply -f <service.yaml>
- Delete a service:
kubectl delete svc <service-name>
6. Deployment Management
- List all deployments:
kubectl get deployments
- Describe a deployment:
kubectl describe deployment <deployment-name>
- Create a deployment from a YAML file:
kubectl apply -f <deployment.yaml>
- Scale a deployment:
kubectl scale deployment <deployment-name> --replicas=<number>
- Delete a deployment:
kubectl delete deployment <deployment-name>
7. Namespace Management
- List all namespaces:
kubectl get namespaces
- Describe a namespace:
kubectl describe namespace <namespace-name>
- Create a new namespace:
kubectl create namespace <namespace-name>
- Delete a namespace:
kubectl delete namespace <namespace-name>
8. ConfigMap and Secret Management
- List all configmaps:
kubectl get configmaps
- Describe a configmap:
kubectl describe configmap <configmap-name>
- Create a configmap from a file:
kubectl create configmap <configmap-name> --from-file=<file-path>
- Delete a configmap:
kubectl delete configmap <configmap-name>
- List all secrets:
kubectl get secrets
- Describe a secret:
kubectl describe secret <secret-name>
- Create a secret from a file:
kubectl create secret generic <secret-name> --from-file=<file-path>
- Delete a secret:
kubectl delete secret <secret-name>
9. Job and CronJob Management
- List all jobs:
kubectl get jobs
- Describe a job:
kubectl describe job <job-name>
- Create a job from a YAML file:
kubectl apply -f <job.yaml>
- Delete a job:
kubectl delete job <job-name>
- List all cronjobs:
kubectl get cronjobs
- Describe a cronjob:
kubectl describe cronjob <cronjob-name>
- Create a cronjob from a YAML file:
kubectl apply -f <cronjob.yaml>
- Delete a cronjob:
kubectl delete cronjob <cronjob-name>
10. PersistentVolume (PV) and PersistentVolumeClaim (PVC) Management
- List all PVs:
kubectl get pv
- Describe a PV:
kubectl describe pv <pv-name>
- List all PVCs:
kubectl get pvc
- Describe a PVC:
kubectl describe pvc <pvc-name>
- Create a PVC from a YAML file:
kubectl apply -f <pvc.yaml>
- Delete a PVC:
kubectl delete pvc <pvc-name>
11. Ingress and Network Policy Management
- List all ingresses:
kubectl get ingresses
- Describe an ingress:
kubectl describe ingress <ingress-name>
- Create an ingress from a YAML file:
kubectl apply -f <ingress.yaml>
- Delete an ingress:
kubectl delete ingress <ingress-name>
- List all network policies:
kubectl get networkpolicies
- Describe a network policy:
kubectl describe networkpolicy <networkpolicy-name>
- Create a network policy from a YAML file:
kubectl apply -f <networkpolicy.yaml>
- Delete a network policy:
kubectl delete networkpolicy <networkpolicy-name>
12. Role-Based Access Control (RBAC)
- List all roles:
kubectl get roles
- Describe a role:
kubectl describe role <role-name>
- Create a role from a YAML file:
kubectl apply -f <role.yaml>
- Delete a role:
kubectl delete role <role-name>
- List all rolebindings:
kubectl get rolebindings
- Describe a rolebinding:
kubectl describe rolebinding <rolebinding-name>
- Create a rolebinding from a YAML file:
kubectl apply -f <rolebinding.yaml>
- Delete a rolebinding:
kubectl delete rolebinding <rolebinding-name>
13. Resource Quota and Limits
- List all resource quotas:
kubectl get resourcequotas
- Describe a resource quota:
kubectl describe resourcequota <resourcequota-name>
- Create a resource quota from a YAML file:
kubectl apply -f <resourcequota.yaml>
- Delete a resource quota:
kubectl delete resourcequota <resourcequota-name>
- List all limit ranges:
kubectl get limitranges
- **Describe a limit range:**
kubectl describe limitrange <limitrange-name>
- Create a limit range from a YAML file:
kubectl apply -f <limitrange.yaml>
- Delete a limit range:
kubectl delete limitrange <limitrange-name>
14. Advanced Commands
- Port-forward a local port to a port on a pod:
kubectl port-forward <pod-name> <local-port>:<pod-port>
- Copy files to/from a pod:
kubectl cp <local-path> <pod-name>:<container-path> kubectl cp <pod-name>:<container-path> <local-path>
- Run a pod interactively (e.g., for troubleshooting):
kubectl run -i --tty <pod-name> --image=<image-name> -- sh
- Debug a container within a pod:
kubectl debug <pod-name> --image=<debugger-image>
15. Miscellaneous Commands
- Explain a resource and its fields:
kubectl explain <resource>
- Apply changes to a resource from a YAML/JSON file:
kubectl apply -f <resource.yaml>
- Delete resources by type and name:
kubectl delete <resource-type> <resource-name>
- Get the resource’s status:
kubectl get <resource> -o wide
- Get resource usage (CPU/memory) metrics:
kubectl top nodes kubectl top pods
This list provides a comprehensive set of kubectl
commands that you can use to manage a Kubernetes cluster.