A lot of people struggle with the adoption of Kubernetes particularly moving from Infrastructure operations background, myself included. Following on from last post titled Kubernetes Explained , I have had some exposure with Kubernetes in my current role and want to share some common commands to query a Kubernetes cluster.
- Create a resource from a file (Kubernetes works with file types of .yml, .yaml and .json )
kubectl apply -f ./my_file.yaml
- List all pods in the namespace
kubectl get pods
- Get the documentation for pod manifests
kubectl explain pods
- List all services in the namespace:
kubectl get services
- List all pods in all namespaces:
kubectl get pods --all-namespaces
- List all pods in a namespace with the labelled APP1:
kubectl get pods -l APP1
- List all pods in the current namespace, with more details
kubectl get pods -o wide
List all pods sorted by status of restart count
kubectl get pods --sort-by="{.status.containerStatuses[:1].restartCount}"
List a particular deployment
kubectl get deployment [deployment name]
- List a particular namespace
kubectl get ns [namespace name]
- List all running pods in namespace
kubectl get pods –field-selector=status.phase=Running
- Get YAML of a pod
kubectl get pod [pod name] -o yaml
- Describe command displays pod/ node details with verbose output
kubectl describe pods [pod name]
kubectl describe nodes [pod name]
- Delete a pod
kubectl delete pod [pod name]
- Delete a node
kubectl delete node [node name]
- Mark my-node as unschedulable
kubectl cordon my-node
- Drain a node
kubectl drain [node name]
- Mark my-node as schedulable
kubectl uncordon [node name]
- List Events sorted by timestamp
kubectl get events --sort-by=.metadata.creationTimestamp
- Dump pod logs (stdout)
kubectl logs [pod name]
- Scale a replicaset named ‘AAA’ to 3 replicas
kubectl scale --replicas=3 rs/AAA
- If the deployment named XYZ’s current size is 2, scale XYZ to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/XYZ
- Show metrics for a given pod and its containers
kubectl top pod [pod name] --containers
- Show metrics for a given pod and sort it by ‘cpu’ or ‘memory’
kubectl top pod [pod name] --sort-by=cpu