Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Arthas in Docker Container

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use Arthas in the Docker container". In the daily operation, I believe that many people have doubts about how to use Arthas in the Docker container. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Arthas in the Docker container". Next, please follow the editor to study!

What can Arthas do for you?

Arthas is Alibaba's open source Java diagnostic tool, which is loved by developers.

Arthas can help you solve problems like the following when you are at a loss as to what to do:

From which jar package was this class loaded? Why are all kinds of related Exception reported? Why didn't the code I changed be executed? Is it because I don't have commit? Is the branch wrong? If you can't debug online if you encounter a problem, can you only republish it by adding a log? Online encounter a user's data processing problems, but online also can not debug, offline can not be reproduced! Is there a global perspective to view the health of the system? Is there any way to monitor the real-time running status of JVM? How to quickly locate the hot spots of the application and generate a flame map? How can I find an instance of a class directly from within JVM?

Arthas supports JDK 6 layers, supports Linux/Mac/Windows, adopts command line interaction mode, and provides rich automatic completion functions of Tab, which further facilitates the location and diagnosis of problems.

This article is not about how to use arthas. What I'm going to talk about here is how to use arthas in our docker container.

In view of the complexity of using arthas in the docker container, we need to find the container id, we need to copy the entire arthas directory into the container, we need to enter the container, and users who need to switch to the target service need to start arthas. These steps are not friendly to many students who are not familiar with the linux command and docker command.

So I wrote a script that can directly replace the above steps, as shown in the following figure:

Directly after the script, enter the complete service name (here is the IMAGE name of the container), and you can use it. It is simple and convenient.

Usage: first of all, you need to extract the arhas-bin.zip on the linux server, and the decompression is the arthas software. Make sure docker is installed on this machine

Arhas-bin.zip download directory: https://github.com/alibaba/arthas/releases

Put the arthasDocker.sh script in the arthas directory you just unzipped, open the script, edit the ARTHAS_PATH variable, and change it to the directory you placed on arthas.

The content of the arthasDocker.sh script:

#! / bin/bash## author: dijia478# date: 2020-8-20 18 desc 14 desc: this script needs to be placed in the arthas directory and copied into the docker container along with the entire directory. The main purpose is to switch the target service in the container. And start arthasecho "start querying the process id and users of the target service." PID= `ps-eo pid,user=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-o args | grep java | grep-v grep | awk'{print $1} '`echo "the process id of the target service is ${PID}" USER= `ps-eo pid. User=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa-o args | grep java | grep-v grep | awk'{print $2} '`echo "the user of the target service is ${USER}" if [[!-d "/ home/$ {USER}"]] then mkdir-p / home/$ {USER} echo "create directory / home/$ {USER}" fichmod 777 / home/$ {USER} echo "start switching users and start arthas..." the arthas path under arthas... "# needs to be modified And be consistent with the startArthas.sh script ARTHAS_PATH= "/ opt/arthas" su ${USER}-c "java-jar ${ARTHAS_PATH} / arthas-client.jar 127.0.0.1 3658-c 'stop'" su ${USER}-c "java-jar ${ARTHAS_PATH} / arthas-boot.jar ${PID}"

Put the startArthas.sh script on the linux server, suggest putting it in the ~ directory, open the script, edit the ARTHAS_PATH variable, and change it to the directory you placed by arthas. Then give the script execution permission

The content of the startArthas.sh script:

#! / bin/bash## author: dijia478# date: 2020-9-18 10 desc: this script is mainly used to launch the arthas diagnostic tool to diagnose the java service if in a docker [[${1} = =']] then echo "Please select a service:" sudo docker ps | awk'NR > 1 {print $2} 'exit 0fiecho "to start looking for a container for service ${1}." DOCKER_LIST= `sudo docker ps | awk 'NR > 1 {print $2}' `FLAG=0for I in ${DOCKER_LIST [@]} do if [[${I} = = ${1}]] then FLAG=1 break fidoneif [[${FLAG} = = 0]] then DOCKER_NAME= `sudo docker ps | awk'NR > 1 {print $2}'| grep ${1} `if [[${DOCKER_NAME} = =']] then echo "the container for the service was not found Please re-select the service: "sudo docker ps | awk'NR > 1 {print $2} 'else echo" Please enter the full name of the service: "sudo docker ps | awk' NR > 1 {print $2}'| grep ${1} fielse ID= `sudo docker ps-- filter ancestor=$ {1} | awk'{print $1}'| sed-n '2p' `echo" find container ${ID} "echo" start copying arthas into the container. "# the arthas path below needs to be modified And be consistent with the arthasDocker.sh script ARTHAS_PATH= "/ opt/arthas" sudo docker exec-it ${ID} / bin/bash-c "rm-rf ${ARTHAS_PATH}" sudo docker cp ${ARTHAS_PATH} ${ID}: ${ARTHAS_PATH} echo "copy completed" echo is about to enter the container. " Sudo docker exec-it ${ID} / bin/bash-c "bash ${ARTHAS_PATH} / arthasDocker.sh" fi

Finally, just run the startArthas.sh script directly.

At this point, the study on "how to use Arthas in a Docker container" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report