要在线上查看gc情况,进入k8s容器的bash,ps -ef 发现目标的java线程的进程号是1,运行jmap 报错:
Unable to get pid of LinuxThreads manage thread
查询了一下,原来是1是默认的启动进程,Dockerfile的ENTRYPOINT的执行默认就是 1。
优雅解法
https://zhuanlan.zhihu.com/p/59796137
https://cloud.tencent.com/developer/article/1838250
修改Dcokerfile java启动方式
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ENV TZ=Asia/Shanghai
RUN apk add --no-cache tini
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
ADD target/diversion-0.0.1-SNAPSHOT.jar diversion-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["tini","java","-Djava.security.egd=file:/dev/./urandom","-jar","/xxxx-0.0.1-SNAPSHOT.jar"]
构建并上传镜像到镜像仓库
docker build -t ccr.ccs.tencentyun.com/xxxx/xxxx:xxxx
docker push ccr.ccs.tencentyun.com/xxxx/xxxx:xxxx
部署应用并测试
cat > test.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: test
spec:
replicas: 1
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
selector:
matchLabels:
app: test
template:
metadata:
labels:
app: test
spec:
containers:
- name: test
image: ccr.ccs.tencentyun.com/xxxx/xxxx:xxxx
env:
- name: SPRING_PROFILES_ACTIVE
value: "official"
ports:
- containerPort: test
resources:
requests:
memory: "256M"
cpu: "250m"
limits:
memory: "1024M"
cpu: "500m"
imagePullSecrets:
- name: tencent
---
apiVersion: v1
kind: Service
metadata:
name: test
labels:
app: test
spec:
ports:
- port: 8081
protocol: TCP
targetPort: 8081
selector:
app: test
EOF
kubectl apply -f test.yaml -n test
kubectl exec -it xxxxxxx sh -n test
top
几番查找。幡然醒悟,只要不要让那个java进程直接启动就好了。
修改Dockerfile
RUN echo "java -jar xxx.jar" > /run.sh && chmod 777 /run.sh
ENTRYPOINT ["/bin/sh","/run.sh"]
之后进入容器,ps -ef ,之前的java进程变成了6。