5月
27
背景
目前越来越多的应用都是运行在kubernetes环境中,kubernetes可以为应用提供更加稳定的运行环境,但也导致了调试不方便问题,通常线上问题我们会通过arthas工具进行调试,在落地实施我们发现
- 有些客户(比如银行客户)对线上环境管理非常严格,根本不会给你进入环境的权限
- 在docker环境中调试不方便等问题
如果arthas能够通过web连接应用将极大方便程序的调试,因此arthas也提供了Arthas Tunnel进行远程调试。
安装Arthas Tunnel
Arthas Tunnel是一个spring boot的程序包,因此你可以直接运行,也可以打包为docker运行,本次采用直接运行方式
- 下载最新版本Arthas Tunnel
https://arthas.aliyun.com/download/arthas-tunnel-server/latest_version?mirror=aliyun
- 运行arthas tunnel
nohup java -jar arthas-tunnel-server-3.6.1-fatjar.jar >log.out 2>&1 &
nohup可以让程序后台运行
Spring Boot集成
spring boot应用只需引入以下依赖即可
<dependency> <groupId>com.taobao.arthas</groupId> <artifactId>arthas-spring-boot-starter</artifactId> <version>${arthas.version}</version> </dependency>
${arthas.version}表示要引入arthas的版本好,目前最新是3.6.1
需要增加两个配置信息
#指定客户端id,在web上需要指定agentid才能连接指定应用中 arthas.agent-id=apaas #arthas tunnel地址,tunnel有两个端口,默认8080是web访问端口,7777是和agent通信的地址 arthas.tunnel-server=ws://10.1.6.38:7777/ws
docker集成
arthas在docker中经常无法正常工作,原因是一般docker要求精简,所以只安装jre没有安装jdk,缺少jdk的相关工具,arthas就无法正常运行,两种解决方案
- 使用包含jdk的镜像作为基础镜像
FROM openjdk:8-jdk
- 使用不包含jdk的镜像,但通过tini这个工具作为入口,arthas无法attache进程号为1的进程
FROM openjdk:8u212-alpine VOLUME /tmp ADD app.jar app.jar ENV LANG zh_CN.uft8 ENV JVM_OPTS="" RUN apk add --no-cache tini ENTRYPOINT ["/sbin/tini","--"] CMD ["sh","-c","java -Xmx1024m -Djava.security.egd=file:/dev/./urandom -jar /app.jar"]
web console
如果一切顺利,当spring boot应用启动会自动连接arthas tunnel,访问http://arthas-tunnes-host:8080
就能进行访问

连接指定的agent,需要输入agentid,点击connect进行连接,如果要获取所有的agent,可以访问http://arthas-tunnes-host:8080/actuator/arthas
,访问该接口需要用户登录,默认用户名是arthas,密码在arthas的启动日志里,比如
Using generated security password: ffe0bc95-1135-4ee1-95cb-4c80e0ff4828
剩下的操作就跟cli一样,这里就不再详细说明
Address: https://zhengjianfeng.cn/?p=648
no comment untill now