Simulate JVM Application Faults

    • Specify return value
    • Method Delay
    • Throw custom exceptions
    • Out of memory
    • Fill JVM Code Cache
    • CPU full load in Java
    • Perform customized Groovy or Java script

    Currently, Chaos Mesh uses MutatingAdmissionWebhook to modify the Pod definition and loads Java agent using instead of loading java agent at runtime. Therefore, there are some restrictions when you use JVMChaos:

    • The Webhook support needs to be enabled in Kubernetes.
    • For Pods that exist before you configure MutatingAdmissionWebhook for the namespace, they will not be affected by JVMChaos.
    • JVM in all containers under namespace will load Java agent at the startup stage, and JVMChaos will not unload Java agent after being deleted. If you hope to clean up the Java agent considering the impact that Java agent may have on program behaviors or performance, you can move the workload out of the namespace.

    In addition, creating JVMChaos using Chaos Dashboard is not supported currently.

    The following example shows you the methods and effects of JVMChaos with a specified return value. The YAML files referred in the following steps can be found in . The default work directory for the following steps is in . The default namespace installed by Chaos Mesh is chaos-testing.

    Create the namespace for the application:

    Add the admission-webhook=enabled label for the app namespace, and allow the MutatingAdmissionWebhook of Chaos Mesh to modify Pods under the namespace.

    1. kubectl label ns app admission-webhook=enabled

    Prepare a template for modifications to be made by JVMChaos:

    1. kubectl apply -f sidecar-template.yaml
    2. kubectl apply -f sidecar.yaml
    1. kind: Deployment
    2. metadata:
    3. name: springboot-jvmchaos-demo
    4. namespace: app
    5. spec:
    6. replicas: 1
    7. selector:
    8. matchLabels:
    9. app: springboot-jvmchaos-demo
    10. template:
    11. metadata:
    12. annotations:
    13. admission-webhook.chaos-mesh.org/request: jvmchaos-sidecar
    14. creationTimestamp: null
    15. labels:
    16. app: springboot-jvmchaos-demo
    17. spec:
    18. containers:
    19. imagePullPolicy: IfNotPresent
    20. name: springboot-jvmchaos-demo

    In the above example, the annotation with the value admission-webhook.chaos-mesh.org/request: jvmchaos-sidecar corresponds to the name of ConfigMap in sidecar.yaml of step 1.

    Build application deployment:

    Execute kubectl -n app get pods, and then you can find 1 Pod with a name like springboot-jvmchaos-demo-777d94c5b9-7t7l2 under the namespace app. Wait for READY changes to 1/1 and then execute the following commands:

      The result is as follows:

      1. NAME READY STATUS RESTARTS AGE
      2. springboot-jvmchaos-demo-777d94c5b9-7t7l2 1/1 Running 0 21s

      You can observe the behavior of the jvm-chaos-demo application before injecting faults, for example:

      Map the port of Pod to local using kubectl port-forward:

      1. kubectl -n app port-forward pod/springboot-jvmchaos-demo-777d94c5b9-7t7l2 8080:8080

      The JVMChaos with a specified return value is as follows:

      1. apiVersion: chaos-mesh.org/v1alpha1
      2. kind: JVMChaos
      3. metadata:
      4. name: jvm-return-example
      5. namespace: app
      6. spec:
      7. target: jvm
      8. flags:
      9. value: 'hello chaos mesh!'
      10. matchers:
      11. classname: 'org.chaosmesh.jvm.Application'
      12. methodname: 'hello'
      13. mode: one
      14. selector:
      15. labelSelectors:
      16. app: springboot-jvmchaos-demo

      JVMChaos modifies the return value of hello method to string hello chaos mesh!.

      Inject JVMChaos with a specified value:

      1. kubectl apply -f ./jvm-return-example.yaml

      Use curl or directly access to http://localhost:8080/hello, hello chaos mesh! is expected to be returned:

      1. hello chaos mesh!

      For the meaning of the value of action, refer to:

      NameMeaning
      delaySpecifies method call delay
      returnModifies the return value
      scriptWrites groovy and Java implement scenarios
      cflJava CPU usage overload
      oomOut of memory, supporting oom of heap, stack, and metaspaces
      ccfJVM code cache fill
      tceThrow custom exceptions
      cpfConnection pool full
      tdeThrow the first exception of method declare
      tpfThread pool full

      For the details of action, refer to .