Troubleshooting

Troubleshooting in Kubernetes involves identifying and resolving issues with the various resources and workloads running in the cluster. Here are the imperative commands that are particularly useful for troubleshooting:

Logs

Fetches and displays the logs of a specific pod, which is useful for debugging and understanding the behavior of the applications running in the pod.

kubectl logs <pod-name>

Get Logs of a Specific Container

Fetches and displays the logs of a specific container within a pod.

kubectl logs <pod-name> -c <container-name>

Stream Logs

Streams the logs of a pod, useful for monitoring logs in real-time.

kubectl logs -f <pod-name>

Events

Displays detailed information about a specific resource, including events associated with it.

kubectl describe <resource> <name>

List Events

Lists all events in the current namespace, providing an overview of what has happened in the cluster.

kubectl get events

Exec into Container

Executes a command in a container within a pod, allowing interactive access (such as a bash shell) for troubleshooting.

kubectl exec -it <pod-name> -- /bin/bash

Execute a Command in a Specific Container

Executes a command in a specific container within a pod, useful when dealing with multi-container pods.

kubectl exec -it <pod-name> -c <container-name> -- /bin/bash

Port Forwarding

Forwards a local port to a port on a pod, allowing access to a pod's service on the local machine.

kubectl port-forward <pod-name> <local-port>:<remote-port>

Port Forwarding for a Service

Forwards a local port to a port on a service, useful for accessing a service locally.

kubectl port-forward svc/<service-name> <local-port>:<remote-port>

Describe Pod

Displays detailed information about a pod, including its status, events, and resource usage.

kubectl describe pod <pod-name>

Describe Node

Displays detailed information about a node, which can help diagnose node-specific issues.

kubectl describe node <node-name>

Get Pod Info

Retrieves detailed YAML representation of a pod, useful for understanding its configuration and current state.

kubectl get pod <pod-name> -o yaml

Get Node Info

Retrieves detailed YAML representation of a node, useful for understanding its configuration and current state.

kubectl get node <node-name> -o yaml

Debugging a Pod with Debug Container

Attaches a debug container to a running pod for in-depth troubleshooting.

kubectl debug pod/<pod-name> -it --image=<debug-image> --target=<container-name>

View System Logs with journalctl

Fetches and displays logs for a specific system service managed by systemd.

journalctl -u <service-name>

Stream System Logs with journalctl

Streams logs for a specific system service in real-time.

journalctl -u <service-name> -f

View Logs for All Services

Displays the systemd logs for all services with detailed debugging information.

journalctl -xe