K8s debugging commands

See logs:

kubectl logs <pod-name>


  • -c If you have multiple containers in your Pod, you can choose the container to view.
  • -f continuously stream logs back to the terminal without exiting.

You can also use the exec command to execute commands in running containers:

# attach to bash
kubectl exec -it <pod-name> -- bash

# attach to running process
kubectl attach -it <pod-name>

This will provide you with an interactive shell inside the running container, so that you can perform more debugging. If you don’t have bash or some other terminal within your container, you can always attach to the running process. This is similar to to logs allows you to send input to the process.

You can also copy files to and from a container:

# download file from remote to local
kubectl cp <pod-name>:</path/to/remote/file> </path/to/local/file>

# upload file from local to remote
kubectl cp <pod-name>:</path/to/local/file> </path/to/remote/file>

This will copy a file from a running container to your local machine.

If you want to access your Pod via the network, you can use port-forward command to forward network traffic from local machine to Pod. This enables you to securely tunnel network traffic through to containers that might not be exposed anywhere on the public network.

kubectl port-forward <pod-name> 8080:80

If you’re interested in how your cluster is using resources, you can use top to see the list of resources in use by either nodes or Pods.

kubectl top nodes
kubectl top pods --all-namespaces

By default it only displays Pods in the current namespace. You can use the flag to see resource usage by all Pods in the cluster.