What is troubleshooting in Kubernetes?
Troubleshooting in Kubernetes includes techniques to debug and resolve issues pertaining to containerized applications. Let’s go over how to use port forwarding, container logs, and cluster events to troubleshoot issues within Kubernetes.
Forwarding ports for troubleshooting:
It may be useful to gain network access to a container. A common use case is to access the admin console for a database or messaging service. To do this the kubectl CLI provides the port-forward command that forwards a local port to a pod port. A port-forwarding mapping forwards connections to a single pod. The port-forwarding mapping exists only in the workstation where the kubectl client runs.
Here is an example of the kubectl port-forward command syntax:
$ kubectl port-forward db 30306 3306
The above command forwards port 30306 from the developer workstation to port 3306 on the db pod, where a MySQL server (inside a container) accepts network connections. To learn more about port forwarding click here.
Accessing container logs
Use the Kubectl logs command to view logs in running containers and pods. You can use kubectl logs to retrieve logs from a previous instantiation of a container with --previous flag, in case the container has crashed. If your pod has multiple containers, you should specify which container’s logs you want to access by appending a container name to the command. See the kubectl logs documentation for more details.
The section references the Kubernetes documentation on basic logging.
Accessing cluster events
Cluster events provide high-level details for logging and auditing a Kubernetes cluster. These events signal actions like starting or destroying a pod. Use the following command to list events sorted by timestamp.
kubectl get events --sort-by=.metadata.creationTimestamp
You can also get pod specific events using the following command
Kubetctl describe pod <pod-name>
See Kubernetes documentation for more details.
Accessing Running Containers:
While using the kubectl logs command can be useful for viewing any output sent by a container, it does not necessarily display all of the available debugging information if the application sends logs to a file. Other troubleshooting scenarios may require inspecting the container environment as seen by processes inside the container, for example, to verify external network connectivity. An exec command allows creating new processes inside a running container, and have these process standard output and input redirected to the user terminal. The following is the kubectl exec command:
kubectl exec -it shell-demo -- /bin/bash
What are other resources for learning how to troubleshoot in Kubernetes?
- Getting files into and out of containers
- Debugging Pending Pods
- A kubectl cheatsheet for viewing and finding K8s resources
- Kubernetes for Docker Users