# How to debug in Kubernetes

Debugging applications running in Kubernetes can be a bit more involved than debugging traditional applications, but there are several tools and techniques available to assist with this process. Here are some common methods for debugging in Kubernetes:

1. **Logs**:
    
    * Access container logs using `kubectl logs <pod_name>` or `kubectl logs <pod_name> -c <container_name>` for multi-container pods.
        
    * You can also use a log aggregation tool like Elasticsearch, Fluentd, and Kibana (EFK) stack or Prometheus and Grafana for centralized logging and monitoring.
        
2. **Exec into a container**:
    
    * Use `kubectl exec -it <pod_name> -- <command>` to execute a command inside a running container.
        
    * This is useful for inspecting the container's filesystem, running debugging tools, or attaching a debugger.
        
3. **Attach a debugger**:
    
    * For applications that support debuggers, you can attach a debugger to a running container using `kubectl port-forward` to forward a local port to the container's debug port.
        
    * This allows you to use your local IDE or debugging tools to connect to the running application in the container.
        
4. **Kubernetes Dashboard**:
    
    * The Kubernetes Dashboard provides a web-based UI for managing and monitoring your Kubernetes cluster.
        
    * It can be useful for visualizing the state of your pods, containers, and resources, as well as accessing logs and executing commands.
        
5. **Debugging sidecar containers**:
    
    * You can add a debugging sidecar container to your pod that shares the file system with the main application container.
        
    * This sidecar container can be used to run debugging tools, inspect files, or even attach a debugger to the main application process.
        
6. **Ephemeral debugging containers**:
    
    * You can create a new container in the same pod as your application container using `kubectl debug` (requires the EphemeralContainers feature gate to be enabled).
        
    * This debugging container can share the filesystem and network namespace with the application container, making it easier to inspect and debug the running application.
        
7. **Remote debugging**:
    
    * For applications that support remote debugging, you can configure your application to expose a debugging port and use `kubectl port-forward` to access it from your local machine.
        
    * This allows you to use your preferred debugging tools and IDE to remotely debug the running application in the container.
        
8. **Debugging Node.js applications**:
    
    * For Node.js applications, you can use the `node-inspect` or `--inspect` flag to enable the V8 inspector and attach a debugger to the running process.
        
    * You can then use tools like Chrome DevTools or Visual Studio Code to debug your Node.js application running in a Kubernetes container.
        
9. **Debugging Java applications**:
    
    * For Java applications, you can enable remote debugging by passing the appropriate Java Virtual Machine (JVM) arguments to your application.
        
    * You can then use an IDE like IntelliJ IDEA or Eclipse to attach a remote debugger to the running Java process in the container.
        
10. **Monitoring and tracing tools**:
    
    * Tools like Prometheus and Zipkin can help you monitor and trace your applications running in Kubernetes, providing valuable insights into their behavior and performance.
        
    * These tools can assist in identifying and debugging issues related to performance, latency, or distributed system interactions.
        

Remember that debugging in Kubernetes may require additional setup and configuration compared to traditional application environments. It's also a good practice to build debugging capabilities into your applications from the start, such as exposing health check endpoints, enabling logging, and supporting remote debugging.
