侧边栏壁纸
博主头像
云录博主等级

行动起来,活在当下

  • 累计撰写 24 篇文章
  • 累计创建 11 个标签
  • 累计收到 18 条评论

目 录CONTENT

文章目录

【云原生 | Kubernetes 系列】arthas工具分析k8sPod服务

Dylan
2023-12-21 / 0 评论 / 0 点赞 / 162 阅读 / 6584 字 / 正在检测是否收录...
广告 广告

【云原生 | Kubernetes 系列】—内存排查-使用arthas工具分析k8s里面的服务

版权 本文为云篆录原创文章,转载无需和我联系,但请注明来自云篆录 http://www.yunzhuan.site

🍇 使用arthas工具

arthas是Alibaba开源的Java诊断工具。arthas可以帮助我们解决一下问题:

  • 这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
  • 我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
  • 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
  • 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
  • 是否有一个全局视角来查看系统的运行状况?
  • 有什么办法可以监控到JVM的实时运行状态?
  • 怎么快速定位应用的热点,生成火焰图?

如果需要查看更多内容,请访问arthas官网

🍋 k8s里使用arthas

下载arthas

1:进入pod

kubectl exec -it -n namesepase  podName /bin/sh

2:下载arthas

wget https://alibaba.github.io/arthas/arthas-boot.jar

3:启动arthas

java -jar arthas-boot.jar
# java -jar arthas-boot.jar

[INFO] JAVA_HOME: /usr/local/openjdk-8/jre
[INFO] arthas-boot version: 3.6.7
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 8 business-module-1.0.0.jar
1
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.6.7?mirror=aliyun
[INFO] File size: 12.92 MB, downloaded size: 5.51 MB, downloading ...
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.6.7/arthas
[INFO] Try to attach process 8
Picked up JAVA_TOOL_OPTIONS: 
[INFO] Attach process 8 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.                         
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'                        
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.                        
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |                       
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'                        

wiki       https://arthas.aliyun.com/doc                                      
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html                
version    3.6.7                                                              
main_class                                                                    
pid        8                                                                  
time       2023-04-13 13:44:48 

🥭arthas常见使用命令

1:分析面板

dashboard #查看面板

arthas1.png

2:内存分析

heapdump filename.hprof #生成hprof文件
# ls
arthas-boot.jar  bin   business-module-1.0.0.jar  etc          home      lib    media  opt   root  sbin  sys  usr
arthas-output     boot  dev            filename.hprof    lib64  mnt    proc  run   srv   tmp  var

将filename.hprof拷贝出来

kubectl cp -n common-service podName:/filename.hprof ./filename.hprof

利用工具分析,我使用的是JProfiler

3:常用命令

thread -b   #查看线程堵塞

[arthas@7]$ thread -b
No most blocking thread found!
thread  -n  5 #查看最忙的5个线程

[arthas@7]$ thread  -n  5 
"XNIO-1 task-1" Id=33 cpuUsage=6.5% deltaTime=13ms time=1003267ms WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@105a0d47
    at sun.misc.Unsafe.park(Native Method)
    -  waiting on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@105a0d47
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
thread pid   #查看具体线程

[arthas@7]$ thread 33
"XNIO-1 task-1" Id=33 WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@105a0d47
    at sun.misc.Unsafe.park(Native Method)
    -  waiting on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@105a0d47
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2039)
    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1074)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1134)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
jad 包.类    #使用jad查看线上代码  

trace com.itcast.Controller getUserByCode  #方法内部调用路径,并输出方法路径上的每个节点上耗时

trace class method run -n 1 #次数调用限制

trace class method run '#cost > 10' #执行时间调用限制
profiler start  #生成火焰图
profiler stop --format #html默认情况下,
arthas使用3658端口,则可以打开: http://localhost:3658/arthas-output/ 查看到arthas-output目录下面的 profiler 结果,或者直接打开源文件即可。
#-c表达循环周期,这里指定的是5s
monitor -c 5 class method

watch命令

使用watch命令可以查看方法执行时的入参,返回值,异常等信息,并且也可以选择监听的时机,如方法执行之前,方法执行之后。

watch class method returnObj #查看方法返回值

watch class method "{params,returnObj}" #查看方法入参和返回值

watch class method "{params[0],throwExp}" -e #查看异常时的方式信息

异步任务

#查看正在运行的任务
jobs
#将后台任务转为前台
fg 序号
#将前台任务转为后台
bg 序号
#后台执行任务,使用&
trace class method &
jvm

使用jvm命令,查看当前 JVM 的信息
版权 本文为云篆录原创文章,转载无需和我联系,但请注明来自云篆录 http://www.yunzhuan.site
0

评论区