Debugging
Debugging
For typical server processes, we can conveniently use a variety of tools on the host machine to debug; however, containers often contain only the necessary application and usually do not include common debugging tools. So how do we debug processes inside a container online? The simplest method is to start a new container that includes debugging tools.
Let's take a look at how to debug a basic web container.
webserver Container
Write a simple webserver in Go:
Compile it for the Linux platform:
Then create a Docker image using the following build:
Finally, launch the webserver container:
Visit the mapped port 80; the webserver container correctly returns "Hello World":
Create a New Container to Debug the webserver
Create a new container with an image that includes debugging tools or allows easy installation of debugging tools (such as alpine). To facilitate access to the status of the webserver process, the new container shares the pid namespace and net namespace of the webserver container, and adds necessary capabilities:
This allows the new container to directly attach to the webserver process for online debugging, such as tracing the webserver process with strace
:
It is also possible to get the network status of the webserver container:
Of course, you can also access the filesystem of the webserver container:
The Kubernetes community is also proposing adding a kubectl debug
command to start a new container in a Pod in a similar way to debug running processes. More details can be found at https://github.com/kubernetes/community/pull/649.
After translation and rephrasing:
A Guide to Container Troubleshooting
When dealing with typical server processes, swift and effective debugging is often facilitated by a slew of handy tools on our host machine. But imagine you're navigating the lean world of containers—stripped down to essential apps and often devoid of our go-to debugging utilities. The conundrum then becomes: how do you perform live debugging within this contained environment? The key lies in booting up a new container—one that's armed with all the debugging toolkit you'll need.
Let's dive into the nitty-gritty of troubleshooting a no-frills web container.
The Barebones webserver Container
Forge a minimalist Go-based webserver like so:
For the adaptation to the Linux scene, you'd compile:
Next up, whip up a Docker image, lining up the following build commands:
Running the build might look like this:
This cued the stage for the webserver container to be unleashed into action:
Now, when you hit the newly mapped port 80, be greeted by the "Hello World" from your crisp webserver container:
Spawning a New Container to Peer Inside the webserver
Craft a fresh container using an image rich with debugging tools, or one that welcomes tool installation with ease—alpine being a case in point. This new discovery box shares the webserver's PID and networking space for a deep insight into the webserver’s pulse, supplemented with enhanced capabilities:
Embark on a real-time debugging spree by hitching to the webserver's process—unraveling the mysteries with strace
:
Thrill in the ability to sleuth through the webserver's networking activities:
Venturing further, the filesystem within the webserver's container isn't out of reach:
On a broader horizon, the Kubernetes community sails towards simplifying such troubleshooting with a proposed kubectl debug
command. This would introduce new debugging containers into Pods with similar ease, illuminating the way through the often opaque waters of runtime processes. You can chart these developing waters at https://github.com/kubernetes/community/pull/649.
最后更新于