共计 5239 个字符,预计需要花费 14 分钟才能阅读完成。
背景:
k8s 集群中,Java 应用容器中添加即时工具分析诊断 arthas、netstat
1. 预先下载好 arthas-packaging-3.1.1-bin.zip 文件,在 Dockerfile 同目录下,并且重命名为 arthas.zip
wget http://repo1.maven.org/maven2/com/taobao/arthas/arthas-packaging/3.1.1/arthas-packaging-3.1.1-bin.zip
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 http://www.slf4j.org/codes.html#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 http://www.slf4j.org/codes.html#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
$ exit
/usr/local/arthas # netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:3658 0.0.0.0: LISTEN 6/java
tcp 0 0 0.0.0.0:80 0.0.0.0: LISTEN 6/java
tcp 0 0 127.0.0.1:8563 0.0.0.0:* LISTEN 6/java
6. 示例 jenkins 构建,封装至基础镜像中
cat >./target/Dockerfile <<EOF
FROM 192.168.3.10:5001/library/javabase:v8.0.22
RUN \
mkdir -p /usr/local/ocr
ADD ./app.jar /usr/local/ocr/
WORKDIR /usr/local
EXPOSE 80
ENTRYPOINT [“/sbin/tini”, “–“]
CMD [“sh”,”-c”,”java -Xms3000m -Xmx3000m -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -jar /usr/local/ocr/app.jar”]
EOF
ssh root@192.168.255.1 “/bin/bash /usr/jenkins_home/deploy_history/scf-test/deploy.sh test ocr && \
/usr/bin/kubectl apply -f /usr/jenkins_home/deploy_history/scf-test/test-ocr.yaml && \
exit”