JVM error test

Kubernetes上で動いているJavaアプリがOOMなどで死んだときに備えてどのようのエラーログを取得するかテストする方法 テスト環境はMinikube上で動かしています。 Minikube: Java version:

今回のベースアプリ

github.com

まずDeploymentを作成しDeployします。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellojava
spec:
  replicas: 1
  selector:
    matchLabels:
      name: hellojava
  template:
    metadata:
      labels:
        name: hellojava
    spec:
      containers:
      - name: hellojava
        image: mo053/gs-spring-boot-docker:latest
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        ports:
        - containerPort: 8080

kubectl apply -f deployment -n <namespace> STATUSがRunningになっているのでok

kubectl get pod
NAME                                                  READY   STATUS      RESTARTS      AGE
hellojava-78cffc5867-z8sz5                            1/1     Running     3 (12m ago)   18m

PODの中に入りプロセスを落とす

kubectl exec -it hellojava-78cffc5867-z8sz5 /bin/bash

ps aux | grep java
root           1  0.7  1.1 6653628 184488 ?      Ssl  09:24   0:05 java -jar /app.jar $JAVA_OPTS
root          54  0.0  0.0   6308   728 pts/0    S+   09:37   0:00 grep --color=auto java


kill -SIGSEGV 1

Podのログは以下のように表示される

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007fd124332cd5 (sent by kill), pid=1, tid=1
#
# JRE version: OpenJDK Runtime Environment Microsoft-27990 (11.0.13+8) (build 11.0.13+8-LTS)
# Java VM: OpenJDK 64-Bit Server VM Microsoft-27990 (11.0.13+8-LTS, mixed mode, sharing, tiered, compressed oops, serial gc, linux-amd64)
# Problematic frame:
# C [libpthread.so.0+0xacd5] __pthread_clockjoin_ex+0x255
#
# Core dump will be written. Default location: Core dumps may be processed with "/usr/share/apport/apport %p %s %c %d %P %E" (or dumping to //core.1)
#
# An error report file with more information is saved as:
# //hs_err_pid1.log
#
# If you would like to submit a bug report, please visit:
# https://github.com/microsoft/openjdk/issues
#
[error occurred during error reporting (), id 0xb, SIGSEGV (0xb) at pc=0x00007fd124143941]

Deploymentを次のように更新すると/dumpにhs_error_pidが吐かれるようになる

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hellojava
spec:
  replicas: 1
  selector:
    matchLabels:
      name: hellojava
  template:
    metadata:
      labels:
        name: hellojava
    spec:
      containers:
      - name: hellojava
        image: mo053/gs-spring-boot-docker:latest
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        env:
        - name: JAVA_TOOL_OPTIONS
          value: "-XX:ErrorFile=/dump/error.log -Xmx50m -XX:+CrashOnOutOfMemoryError -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dump/oom.bin"
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /dump
          name: dump-volume
      volumes:
      - name: dump-volume
        emptyDir: {}