Kubernetes Commands

, , Comments Off on Kubernetes Commands


How many pods?
kubectl get pods or kubectl get pods --no-headers | wc -l
How many pods are based on a particular label?
kubectl get pods --selector key=value or kubectl get pods -l key=value
How many pods exist across all namespaces?
kubectl get pods -A
Create a new pod.
kubectl run name --image image
What image is used by the pod?
kubectl describe pod name
Which nodes are pods placed on?
kubectl get pods -o wide
What is the state of a container?
kubectl describe pod name
Delete a pod. Note that you can list out pods to delete more than one pod at a time.
kubectl delete pod one two three
Edit a pod. If unable to edit, a temporary file will be created in /tmp which you can apply using kubectl replace -f /tmp/file –force. The option force will delete and recreate the pod kubectl edit pod name Create a pod with labels.
kubectl run name --image=image --labels="this=that"
Create a pod and expose a port.
kubectl run name --image=image --port=port --expose # will create ClusterIP by default
How can I access the service from my laptop?
kubectl port-forward service/name 8080:port
Create a static pod.
kubectl run --restart=Never --image=image --dry-run=client -o yaml > /etc/kubernetes/manifest/name.yaml
Find a pod based on multiple selectors.
kubectl get pods -l key=value1,key2=value2,key3=value3
Create a pod with a container that runs a command.
kubectl run name --image=busybox -- sleep 4800
Show the details of a pod. Note that kubectl describe pod will not display certain information like security context.
kubectl describe pod -o yaml
Monitor pod creation.
kubectl get pods -w

Replica Sets

How many replica sets?
kubectl get replicasets or kubectl get rs
What is the image used by the replica set?
kubectl get rs -o wide
Why is the pod in a replica set not running? Note that a replica set will always ensure a pod is running even when there is an error. Delete the old pods if redefining a replica set.
kubectl describe pods then kubectl describe pods name 
Delete a replica set.
kubectl delete rs name
Edit a replica set.
kubectl edit rs name
Create a replica set. Note that you can only create through a deployment.
kubectl create deployment --image=busybox --replicas=2 somedeployment -- sleep 4800
Describe a replica set.
kubectl describe rs
Scale a replica set. If the replica set is not scaling, review kubectl describe rs for deployment.kubernetes.io/max-replicas as this will limit the number of replicas that can be created.
kubectl scale rs somedeployment-6f55db8d4d --replicas=3


List services. Note that the Kubernetes service runs by default.
kubectl get service
What is the target port of the service?
kubectl get service name 
Show the service selector.
kubectl get service -o wide
Create a node port service to direct traffic to port 80.
kubectl create service nodeport name --tcp=port:80 
How can I access the above service from my laptop? Note if you only specify the target port or :port the local listening port will be provided.
kubectl port-forward service/name 8080:port
Run a pod for testing service configuration.
kubectl run test --image=curlimages/curl --rm -it -- sh