背景:
k8s 集群中,Java应用容器中添加即时工具分析诊断arthas、netstat
1.预先下载好arthas-packaging-3.1.1-bin.zip文件,在Dockerfile同目录下,并且重命名为arthas.zip
wget
mv arthas-packaging-3.1.1-bin.zip arthas.zip
2.netstat工具在alpine下为ospd-netstat,在Dockerfile直接apk add 添加即可
3.以下为Dokcerfile文件(注:需要openjdk,如果是jre的,会缺少dt.jar,tools.jar arthas工具会使用到;需要安装tini工具,因为默认java 应用跑在容器中会是1 pid号,arthas会无法调用)
FROM openjdk:8-jdk-alpine
COPY repositories /etc/apk/
RUN apk add --no-cache mesa-gl \
ttf-freefont \
ospd-netstat \
curl \
tini \
jq \
ttf-dejavu \
tzdata && \
mkdir -p /usr/local/arthas && \
rm -rf /var/cache/apk/*
设置环境变量
ENV TZ=Asia/Shanghai
设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
COPY simhei.ttf /usr/share/fonts/
COPY simkaigb2312.ttf /usr/share/fonts/
COPY simsun.ttc /usr/share/fonts/simsun.ttc
COPY arthas.zip /tmp/
ADD ./health.sh /
RUN unzip /tmp/arthas.zip -d /usr/local/arthas && \
rm -rf /tmp/arthas.zip
4.镜像打包,并推至私有仓库
docker build -t 192.168.3.10:5001/library/javabase:v8.0.22 .
docker push 192.168.3.10:5001/library/javabase:v8.0.22
5.问题总结:
如果未安装tini工具,并且在启动时,预先执行
ENTRYPOINT ["/sbin/tini", "--"] ,在执行 CMD ["sh","-c","java -Xms1500m -Xmx1500M -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -jar /usr/local/factoring_web/xxxx.jar"]
不然会如下报错:
[ERROR] Start arthas failed, exception stack trace:
com.sun.tools.attach.AttachNotSupportedException: Unable to get pid of LinuxThreads manager thread
at sun.tools.attach.LinuxVirtualMachine.<init>(LinuxVirtualMachine.java:86)
at sun.tools.attach.LinuxAttachProvider.attachVirtualMachine(LinuxAttachProvider.java:78)
at com.sun.tools.attach.VirtualMachine.attach(VirtualMachine.java:250)
at com.taobao.arthas.core.Arthas.attachAgent(Arthas.java:75)
at com.taobao.arthas.core.Arthas.<init>(Arthas.java:28)
at com.taobao.arthas.core.Arthas.main(Arthas.java:113)
[INFO] Attach process 1 success.
[INFO] arthas-client connect 127.0.0.1 3658
Connect to telnet server error: 127.0.0.1 3658
java.net.ConnectException: Connection refused (Connection refused)
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:188)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:209)
at com.taobao.arthas.client.TelnetConsole.main(TelnetConsole.java:268)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.taobao.arthas.boot.Bootstrap.main(Bootstrap.java:503)
正常执行成功:
/usr/local/arthas # java -jar arthas-boot.jar
Picked up JAVA_TOOL_OPTIONS: -javaagent:/home/admin/.opt/ArmsAgent/arms-bootstrap-1.7.0-SNAPSHOT.jar -Darms.licenseKey=fqkkah9ke2@1f6f53b91f6e3fd -Darms.appName=test-factoring-web -Darms.agent.env=ACSK8S -Darms.agent.args=
2019-08-06 17:28:47 WARN JDK version=1.8.0_212
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See #StaticLoggerBinder for further details.
2019-08-06 17:28:50 WARN arms agent start cost: 3755ms
[INFO] arthas-boot version: 3.1.1
[INFO] Found existing java process, please choose one and hit RETURN.
•[1]: 6 /usr/local/factoring_web/app.jar
1
[INFO] arthas home: /usr/local/arthas
[INFO] Try to attach process 6
Picked up JAVA_TOOL_OPTIONS: -javaagent:/home/admin/.opt/ArmsAgent/arms-bootstrap-1.7.0-SNAPSHOT.jar -Darms.licenseKey=fqkkah9ke2@1f6f53b91f6e3fd -Darms.appName=test-factoring-web -Darms.agent.env=ACSK8S -Darms.agent.args=
2019-08-06 17:31:17 WARN JDK version=1.8.0_212
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See #StaticLoggerBinder for further details.
2019-08-06 17:31:20 WARN arms agent start cost: 3623ms
[INFO] Attach process 6 success.
[INFO] arthas-client connect 127.0.0.1 3658
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |.-.
| | | || |\ \ | | | | | || | | |.-' |
--'--'--' '--'--' --'--'--'--'`-----'
wiki https://alibaba.github.io/arthas
tutorials https://alibaba.github.io/arthas/arthas-tutorials
version 3.1.1
pid 6
time 2019-08-06 17:31:21