Useful Kubernetes commands
by Alex Arica

Redeploy containers

Redeploy all deployments in a given namespace:

kubectl rollout restart deployment -n [namespace]
                    

Isolate node

Isolate a node to prevent new work being scheduled:

kubectl cordon <node name>
                    

Un-isolate a node:

kubectl uncordon <node name>
                    

Display logs

Display the events:

kubectl get events
kubectl get events -n [namespace]
                    

Display pod's logs:

kubectl logs hello-node-7bf65

# filter logs by label
kubectl logs -l app=hello-node

# stream output
kubectl logs -f hello-node-7bf65

# last hour's logs
kubectl logs --since=1h -lapp=hello-node
                    

Display logs of a Pod's init-container "setup-replica-data-directory":

kubectl logs hello-node-7bf65 -c setup-replica-data-directory
                    

Display a service's logs:

kubectl logs services/hello-node
                    

Connect to a container

Run a command in a pod:

kubectl exec -it <podName> -- [command]
kubectl exec hello-7bf -- nslookup zookeeper
                    

Access in shell mode into a pod:

kubectl exec -it <podName> bash
kubectl exec -it hello-7bf bash
                    

"Get" and "describe" commands

List all resources:

kubectl get all -o wide
                    

You can also use an alias to list resources in Kubernetes

List nodes:

kubectl get nodes -o wide
                    

List pods:

kubectl get pods -o wide
kubectl get pods -w -o wide # -w to stream output

kubectl get pods -o wide --all-namespaces

kubectl get pods -o wide -l app=nginx

# Add the columns for label’s keys “app”, “tier” and “role”
kubectl get pods -Lapp -Ltier -Lrole
                    

View a pod's details:

# get pod’s yaml
kubectl get pod hello-node-7bf657c596-2qfrr -o yaml

# describe pod's properties
kubectl describe pod hello-node-7bf657c596-2qfrr
                    

View a service's details:

kubectl describe services/hello-node
                    

List many resources, services, storage classes, persistence volumes and persistence volume claims:

kubectl get svc,sc,pv,pvc -o wide
                    

Port forwarding

Create a port forwarding of a container to localhost:8080 :

kubectl port-forward svc/wordpress 8080:80