github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/drivers/java.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: 'Drivers: Java'
     4  description: The Java task driver is used to run Jars using the JVM.
     5  ---
     6  
     7  # Java Driver
     8  
     9  Name: `java`
    10  
    11  The `java` driver is used to execute Java applications packaged into a Java Jar
    12  file. The driver requires the Jar file to be accessible from the Nomad
    13  client via the [`artifact` downloader](/docs/job-specification/artifact).
    14  
    15  ## Task Configuration
    16  
    17  ```hcl
    18  task "webservice" {
    19    driver = "java"
    20  
    21    config {
    22      jar_path    = "local/example.jar"
    23      jvm_options = ["-Xmx2048m", "-Xms256m"]
    24    }
    25  }
    26  ```
    27  
    28  The `java` driver supports the following configuration in the job spec:
    29  
    30  - `class` - (Optional) The name of the class to run. If `jar_path` is specified
    31    and the manifest specifies a main class, this is optional. If shipping classes
    32    rather than a Jar, please specify the class to run and the `class_path`.
    33  
    34  - `class_path` - (Optional) The `class_path` specifies the class path used by
    35    Java to lookup classes and Jars.
    36  
    37  - `jar_path` - (Optional) The path to the downloaded Jar. In most cases this will just be
    38    the name of the Jar. However, if the supplied artifact is an archive that
    39    contains the Jar in a subfolder, the path will need to be the relative path
    40    (`subdir/from_archive/my.jar`).
    41  
    42  - `args` - (Optional) A list of arguments to the Jar's main method. References
    43    to environment variables or any [interpretable Nomad
    44    variables](/docs/runtime/interpolation) will be interpreted before
    45    launching the task.
    46  
    47  - `jvm_options` - (Optional) A list of JVM options to be passed while invoking
    48    java. These options are passed without being validated in any way by Nomad.
    49  
    50  - `pid_mode` - (Optional) Set to `"private"` to enable PID namespace isolation for
    51    this task, or `"host"` to disable isolation. If left unset, the behavior is
    52    determined from the [`default_pid_mode`][default_pid_mode] in plugin configuration.
    53  
    54  !> **Warning:** If set to `"host"`, other processes running as the same user will
    55  be able to access sensitive process information like environment variables.
    56  
    57  - `ipc_mode` - (Optional) Set to `"private"` to enable IPC namespace isolation for
    58    this task, or `"host"` to disable isolation. If left unset, the behavior is
    59    determined from the [`default_ipc_mode`][default_ipc_mode] in plugin configuration.
    60  
    61  !> **Warning:** If set to `"host"`, other processes running as the same user will be
    62  able to make use of IPC features, like sending unexpected POSIX signals.
    63  
    64  - `cap_add` - (Optional) A list of Linux capabilities to enable for the task.
    65    Effective capabilities (computed from `cap_add` and `cap_drop`) must be a subset
    66    of the allowed capabilities configured with [`allow_caps`][allow_caps].
    67  
    68  ```hcl
    69  config {
    70    cap_add = ["net_raw", "sys_time"]
    71  }
    72  ```
    73  
    74  - `cap_drop` - (Optional) A list of Linux capabilities to disable for the task.
    75    Effective capabilities (computed from `cap_add` and `cap_drop`) must be a subset
    76    of the allowed capabilities configured with [`allow_caps`][allow_caps].
    77  
    78  ```hcl
    79  config {
    80    cap_drop = ["all"]
    81    cap_add  = ["chown", "sys_chroot", "mknod"]
    82  }
    83  ```
    84  
    85  ## Examples
    86  
    87  A simple config block to run a Java Jar:
    88  
    89  ```hcl
    90  task "web" {
    91    driver = "java"
    92  
    93    config {
    94      jar_path    = "local/hello.jar"
    95      jvm_options = ["-Xmx2048m", "-Xms256m"]
    96    }
    97  
    98    # Specifying an artifact is required with the "java" driver. This is the
    99    # mechanism to ship the Jar to be run.
   100    artifact {
   101      source = "https://internal.file.server/hello.jar"
   102  
   103      options {
   104        checksum = "md5:123445555555555"
   105      }
   106    }
   107  }
   108  ```
   109  
   110  A simple config block to run a Java class:
   111  
   112  ```hcl
   113  task "web" {
   114    driver = "java"
   115  
   116    config {
   117      class       = "Hello"
   118      class_path  = "${NOMAD_TASK_DIR}"
   119      jvm_options = ["-Xmx2048m", "-Xms256m"]
   120    }
   121  
   122    # Specifying an artifact is required with the "java" driver. This is the
   123    # mechanism to ship the Jar to be run.
   124    artifact {
   125      source = "https://internal.file.server/Hello.class"
   126  
   127      options {
   128        checksum = "md5:123445555555555"
   129      }
   130    }
   131  }
   132  ```
   133  
   134  ## Capabilities
   135  
   136  The `java` driver implements the following [capabilities](/docs/concepts/plugins/task-drivers#capabilities-capabilities-error).
   137  
   138  | Feature              | Implementation                |
   139  | -------------------- | ----------------------------- |
   140  | `nomad alloc signal` | false                         |
   141  | `nomad alloc exec`   | false                         |
   142  | filesystem isolation | none, chroot (only for linux) |
   143  | network isolation    | host, group                   |
   144  | volume mounting      | none, all (only for linux)    |
   145  
   146  ## Plugin Options
   147  
   148  - `default_pid_mode` `(string: optional)` - Defaults to `"private"`. Set to
   149    `"private"` to enable PID namespace isolation for tasks by default, or `"host"` to
   150    disable isolation.
   151  
   152  !> **Warning:** If set to `"host"`, other processes running as the same user will
   153  be able to access sensitive process information like environment variables.
   154  
   155  - `default_ipc_mode` `(string: optional)` - Defaults to `"private"`. Set to
   156    `"private"` to enable IPC namespace isolation for tasks by default,
   157    or `"host"` to disable isolation.
   158  
   159  !> **Warning:** If set to `"host"`, other processes running as the same user will be
   160  able to make use of IPC features, like sending unexpected POSIX signals.
   161  
   162  - `allow_caps` - A list of allowed Linux capabilities. Defaults to
   163  
   164  ```hcl
   165  ["audit_write", "chown", "dac_override", "fowner", "fsetid", "kill", "mknod",
   166   "net_bind_service", "setfcap", "setgid", "setpcap", "setuid", "sys_chroot"]
   167  ```
   168  
   169    which is modeled after the capabilities allowed by [docker by default][docker_caps]
   170    (without [`NET_RAW`][no_net_raw]). Allows the operator to control which capabilities
   171    can be obtained by tasks using [`cap_add`][cap_add] and [`cap_drop`][cap_drop] options.
   172    Supports the value `"all"` as a shortcut for allow-listing all capabilities supported
   173    by the operating system.
   174  
   175  !> **Warning:** Allowing more capabilities beyond the default may lead to
   176  undesirable consequences, including untrusted tasks being able to compromise the
   177  host system.
   178  
   179  ## Client Requirements
   180  
   181  The `java` driver requires Java to be installed and in your system's `$PATH`. On
   182  Linux, Nomad must run as root since it will use `chroot` and `cgroups` which
   183  require root privileges. The task must also specify at least one artifact to
   184  download, as this is the only way to retrieve the Jar being run.
   185  
   186  ## Client Attributes
   187  
   188  The `java` driver will set the following client attributes:
   189  
   190  - `driver.java` - Set to `1` if Java is found on the host node. Nomad determines
   191    this by executing `java -version` on the host and parsing the output
   192  - `driver.java.version` - Version of Java, ex: `1.6.0_65`
   193  - `driver.java.runtime` - Runtime version, ex: `Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)`
   194  - `driver.java.vm` - Virtual Machine information, ex: `Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)`
   195  
   196  Here is an example of using these properties in a job file:
   197  
   198  ```hcl
   199  job "docs" {
   200    # Only run this job where the JVM is higher than version 1.6.0.
   201    constraint {
   202      attribute = "${attr.driver.java.version}"
   203      operator  = ">"
   204      value     = "1.6.0"
   205    }
   206  }
   207  ```
   208  
   209  ## Resource Isolation
   210  
   211  The resource isolation provided varies by the operating system of
   212  the client and the configuration.
   213  
   214  On Linux, Nomad will attempt to use cgroups, namespaces, and chroot
   215  to isolate the resources of a process. If the Nomad agent is not
   216  running as root, many of these mechanisms cannot be used.
   217  
   218  As a baseline, the Java jars will be run inside a Java Virtual Machine,
   219  providing a minimum amount of isolation.
   220  
   221  ### Chroot
   222  
   223  The chroot created on Linux is populated with data in the following
   224  directories from the host machine:
   225  
   226  ```
   227  [
   228    "/bin",
   229    "/etc",
   230    "/lib",
   231    "/lib32",
   232    "/lib64",
   233    "/run/resolvconf",
   234    "/sbin",
   235    "/usr",
   236  ]
   237  ```
   238  
   239  The task's chroot is populated by linking or copying the data from the host into
   240  the chroot. Note that this can take considerable disk space. Since Nomad v0.5.3,
   241  the client manages garbage collection locally which mitigates any issue this may
   242  create.
   243  
   244  This list is configurable through the agent client
   245  [configuration file](/docs/configuration/client#chroot_env).
   246  
   247  [default_pid_mode]: /docs/drivers/java#default_pid_mode
   248  [default_ipc_mode]: /docs/drivers/java#default_ipc_mode
   249  [cap_add]: /docs/drivers/java#cap_add
   250  [cap_drop]: /docs/drivers/java#cap_drop
   251  [no_net_raw]: /docs/upgrade/upgrade-specific#nomad-1-1-0-rc1-1-0-5-0-12-12
   252  [allow_caps]: /docs/drivers/java#allow_caps
   253  [docker_caps]: https://docs.docker.com/engine/reference/run/#runtime-privilege-and-linux-capabilities