introduction
In modern Java application development, diagnosis and debugging are an indispensable link. With the popularity of microservice architectures, the complexity of applications continues to increase, and traditional debugging methods often seem to be incompetent. Arthas, as an open source Java diagnostic tool from Alibaba, provides a solution to monitor, diagnose and debug Java applications in real time without modifying code. This article will introduce the basic concepts of Arthas in detail and provide step-by-step instructions on how to integrate it into Java business images for efficient diagnosis and debugging in Kubernetes environments.
What is Arthas?
Arthas is a powerful Java diagnostic tool open sourced by Alibaba. It can monitor, diagnose and debug Java applications at runtime without modifying code or restarting the application. Arthas supports a variety of features, including but not limited to:
- Real-time monitoring of JVM status: Check memory usage, thread status, GC information, etc.
- Time-consuming analysis of method execution: Positioning performance bottlenecks and optimizing code.
- View class loading information: Understand the class loading situation and troubleshoot the class loading problems.
- Dynamically modify the code: Dynamically modify the code logic without restarting the application.
These features of Arthas make it one of the indispensable tools for Java developers, especially in production environments, to quickly locate and solve problems.
Why do we need to integrate Arthas into Java business image?
In microservice architecture, applications are usually run in container orchestration platforms such as Kubernetes. Traditional debugging methods, such as remote debugging, often require complex configuration and network access rights, and pose certain security risks in the production environment. Integrating Arthas into Java business images can bring the following benefits:
- Convenience: No additional configuration is required, use Arthas directly in the container for diagnosis.
- Real-time: Can perform real-time monitoring and debugging while the application is running, and quickly locate problems.
- Security: Through Kubernetes access control, restrict the use permissions of Arthas and reduce security risks.
How to integrate Arthas into Java business image?
1. Download Arthas
First, we need to download the Arthas installation package. You can download it through the following command:
wget /
2. Modify Dockerfile
Suppose we have an existing Java business image with the Dockerfile as follows:
FROM openjdk:8u342-jdk COPY ysx-server-api/target/ / ENV TZ='Asia/Shanghai'
In order to integrate Arthas into this image, we need to add steps to download Arthas in the Dockerfile. The modified Dockerfile is as follows:
# Basic mirrorFROM openjdk:8u342-jdk # Set the time zoneENV TZ='Asia/Shanghai' # Copy the business JAR packageCOPY ysx-server-api/target/ / # Download ArthasRUN wget / -O / # Start the commandCMD ["java", "-jar", "/"]
3. Build a mirror
Run the following command in the directory where the Dockerfile is located to build the image:
docker build -t myapp-with-arthas .
4. Run the container
When starting the container, you can use thedocker exec
Go into the container and use Arthas:
docker run -d --name myapp myapp-with-arthas docker exec -it myapp /bin/bash
Start Arthas inside the container:
java -jar /
5. Use Arthas
After starting Arthas, select the Java process to be diagnosed, and you can use various commands of Arthas for diagnosis and debugging. For example, usedashboard
Command to view real-time monitoring information, usetrace
The execution of the command tracking method is time-consuming, etc.
Using Arthas in Kubernetes
If your image is built and deployed to Kubernetes (k8s) and does not need to automatically run Arthas when the container starts, then you can remove the DockerfileCMD
, and use Arthas manually for diagnosis only if needed. The following are the optimized Dockerfile and related instructions:
Optimized Dockerfile
# Basic mirrorFROM openjdk:8u342-jdk # Set the time zoneENV TZ='Asia/Shanghai' # Copy the business JAR packageCOPY ysx-server-api/target/ / # Download ArthasRUN wget / -O / # CMD is not required, because k8s will specify the startup command through the yaml file
Using Arthas in Kubernetes
After your image is deployed to Kubernetes, if you need to use Arthas for diagnosis, you can go into the container and run Arthas through the following steps:
Find the target pod
Use the following command to find your business pod:
kubectl get pods -n <namespace>
Enter the container
passkubectl exec
Enter the container to the target Pod:
kubectl exec -it <pod-name> -n <namespace> -- /bin/bash
Start Arthas
Run Arthas in a container:
java -jar /
Then select your Java process (usually), you can use Arthas for diagnosis.
Things to note
Mirror size:Arthas will increase the image volume. If it is sensitive to image size, you can remove Arthas from the image in the production environment, or use a separate debug image.
Security: Arthas has strong debugging capabilities. It is recommended to use only in test or debug environments. It should be used with caution in production environments.
Kubernetes debugging image: If you do not want to include Arthas in your business image, you can build a debug image containing Arthas separately, and debug it through Kubernetes' ephemeral containers or sidecar mode when needed.
Summarize
As a powerful Java diagnostic tool, Arthas can monitor, diagnose and debug Java applications in real time without modifying code. By integrating it into Java business image, we can easily diagnose and debug in Kubernetes environment. This article details how to integrate Arthas into Java business images and provides steps and precautions for using Arthas in Kubernetes. I hope this article can help you better utilize Arthas in actual development and improve the stability and performance of your application.
The above is the detailed content of the process steps for integrating Arthas into Java business images. For more information about integrating Arthas into Java images, please pay attention to my other related articles!