github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/website/content/docs/job-specification/network.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: network Stanza - Job Specification
     4  sidebar_title: network
     5  description: |-
     6    The "network" stanza specifies the networking requirements for the task group,
     7    including networking mode and port allocations.
     8  ---
     9  
    10  # `network` Stanza
    11  
    12  <Placement groups={[['job', 'group', 'network']]} />
    13  
    14  The `network` stanza specifies the networking requirements for the task group,
    15  including the network mode and port allocations. When scheduling jobs in
    16  Nomad they are provisioned across your fleet of machines along with other jobs
    17  and services. Because you don't know in advance what host your job will be
    18  provisioned on, Nomad will provide your tasks with network configuration when
    19  they start up.
    20  
    21  When the `network` stanza is defined with `bridge` as the networking mode,
    22  all tasks in the task group share the same network namespace. This is a prerequisite for
    23  [Consul Connect](/docs/integrations/consul-connect). Tasks running within a
    24  network namespace are not visible to applications outside the namespace on the same host.
    25  This allows [Connect][]-enabled applications to bind only to localhost within the shared network stack,
    26  and use the proxy for ingress and egress traffic.
    27  
    28  Note that this document only applies to services that want to _listen_ on a
    29  port. Batch jobs or services that only make outbound connections do not need to
    30  allocate ports, since they will use any available interface to make an outbound
    31  connection.
    32  
    33  ```hcl
    34  job "docs" {
    35    group "example" {
    36      network {
    37        port "http" {}
    38        port "https" {}
    39        port "lb" {
    40          static = 8889
    41        }
    42      }
    43    }
    44  }
    45  ```
    46  
    47  ## `network` Parameters
    48  
    49  - `mbits` <code>([_deprecated_](/docs/upgrade/upgrade-specific#nomad-0-12-0) int: 10)</code> - Specifies the bandwidth required in MBits.
    50  
    51  - `port` <code>([Port](#port-parameters): nil)</code> - Specifies a TCP/UDP port
    52    allocation and can be used to specify both dynamic ports and reserved ports.
    53  
    54  - `mode` `(string: "host")` - Mode of the network. The following modes are available:
    55  
    56    - `none` - Task group will have an isolated network without any network interfaces.
    57    - `bridge` - Task group will have an isolated network namespace with an interface
    58      that is bridged with the host. Note that bridge networking is only
    59      currently supported for the `docker`, `exec`, `raw_exec`, and `java` task
    60      drivers.
    61    - `host` - Each task will join the host network namespace and a shared network
    62      namespace is not created. This matches the current behavior in Nomad 0.9.
    63    - `cni/<cni network name>` - Task group will have an isolated network namespace
    64      with the network configured by CNI.
    65  
    66  - `dns` <code>([DNSConfig](#dns-parameters): nil)</code> - Sets the DNS configuration
    67    for the allocations. By default all DNS configuration is inherited from the client host.
    68    DNS configuration is only supported on Linux clients at this time.
    69  
    70  ### `port` Parameters
    71  
    72  - `static` `(int: nil)` - Specifies the static TCP/UDP port to allocate. If omitted, a
    73    dynamic port is chosen. We **do not recommend** using static ports, except
    74    for `system` or specialized jobs like load balancers.
    75  - `to` `(string:nil)` - Applicable when using "bridge" mode to configure port
    76    to map to inside the task's network namespace. `-1` sets the mapped port equal to the
    77    dynamic port allocated by the scheduler. The `NOMAD_PORT_<label>` environment variable
    78    will contain the `to` value.
    79  - `host_network` `(string:nil)` - Designates the host network name to use when allocating
    80    the port. When port mapping the host port will only forward traffic to the matched host
    81    network address.
    82  
    83  The label assigned to the port is used to identify the port in service
    84  discovery, and used in the name of the environment variable that indicates
    85  which port your application should bind to. For example:
    86  
    87  ```hcl
    88  port "foo" {}
    89  ```
    90  
    91  When the task starts, it will be passed the following environment variables:
    92  
    93  - <tt>NOMAD_IP_foo</tt> - The IP to bind on for the given port label.
    94  - <tt>NOMAD_PORT_foo</tt> - The port value for the given port label.
    95  - <tt>NOMAD_ADDR_foo</tt> - A combined
    96    <tt>ip:port</tt> that can be used for convenience.
    97  
    98  The label of the port is just text - it has no special meaning to Nomad.
    99  
   100  ## `dns` Parameters
   101  
   102  - `servers` `(array<string>: nil)` - Sets the DNS nameservers the allocation uses for name resolution.
   103  - `searches` `(array<string>: nil)` - Sets the search list for hostname lookup
   104  - `options` `(array<string>: nil)` - Sets internal resolver variables.
   105  
   106  ## `network` Examples
   107  
   108  The following examples only show the `network` stanzas. Remember that the
   109  `network` stanza is only valid in the placements listed above.
   110  
   111  ### Dynamic Ports
   112  
   113  This example specifies a dynamic port allocation for the port labeled "http".
   114  Dynamic ports are allocated in a range from `20000` to `32000`.
   115  
   116  Most services run in your cluster should use dynamic ports. This means that the
   117  port will be allocated dynamically by the scheduler, and your service will have
   118  to read an environment variable to know which port to bind to at startup.
   119  
   120  ```hcl
   121  group "example" {
   122    network {
   123      port "http" {}
   124      port "https" {}
   125    }
   126  }
   127  ```
   128  
   129  ```hcl
   130  network {
   131    port "http" {}
   132  }
   133  ```
   134  
   135  ### Static Ports
   136  
   137  This example specifies a static port allocation for the port labeled "lb". Static
   138  ports bind your job to a specific port on the host they' are placed on. Since
   139  multiple services cannot share a port, the port must be open in order to place
   140  your task.
   141  
   142  ```hcl
   143  network {
   144    port "lb" {
   145      static = 6539
   146    }
   147  }
   148  ```
   149  
   150  ### Mapped Ports
   151  
   152  Some drivers (such as [Docker][docker-driver] and [QEMU][qemu-driver]) allow you
   153  to map ports. A mapped port means that your application can listen on a fixed
   154  port (it does not need to read the environment variable) and the dynamic port
   155  will be mapped to the port in your container or virtual machine.
   156  
   157  ```hcl
   158  group "app" {
   159    network {
   160      port "http" {
   161        to = 8080
   162      }
   163    }
   164  
   165    task "example" {
   166      driver = "docker"
   167  
   168      config {
   169        ports = ["http"]
   170      }
   171    }
   172  }
   173  ```
   174  
   175  The above example is for the Docker driver. The service is listening on port
   176  `8080` inside the container. The driver will automatically map the dynamic port
   177  to this service.
   178  
   179  When the task is started, it is passed an additional environment variable named
   180  `NOMAD_HOST_PORT_http` which indicates the host port that the HTTP service is
   181  bound to.
   182  
   183  ### Bridge Mode
   184  
   185  Bridge mode allows compatible tasks to share a networking stack and interfaces. Nomad
   186  can then do port mapping without relying on individual task drivers to implement port
   187  mapping configuration.
   188  
   189  The following example is a group level network stanza that uses bridge mode
   190  and port mapping.
   191  
   192  ```hcl
   193  network {
   194    mode = "bridge"
   195    port "http" {
   196      static = 9002
   197      to     = 9002
   198    }
   199  }
   200  ```
   201  
   202  ### DNS
   203  
   204  The following example configures the allocation to use Google's DNS resolvers 8.8.8.8 and 8.8.4.4.
   205  
   206  ```hcl
   207  network {
   208    dns {
   209      servers = ["8.8.8.8", "8.8.4.4"]
   210    }
   211  }
   212  ```
   213  
   214  ### Container Network Interface (CNI)
   215  
   216  Nomad supports CNI by fingerprinting each node for [CNI network configurations](https://github.com/containernetworking/cni/blob/v0.8.0/SPEC.md#network-configuration).
   217  These are associated to the node by the `name` field of the CNI configuration.
   218  The `name` can then be used when setting the network `mode` field in the form of `cni/<name>`.
   219  
   220  As an example if the following CNI configuration was on a node the proceeding network stanza could be used.
   221  
   222  ```json
   223  {
   224    "cniVersion": "0.3.1",
   225    "name": "mynet",
   226    "plugins": [
   227      {
   228        "type": "ptp",
   229        "ipMasq": true,
   230        "ipam": {
   231          "type": "host-local",
   232          "subnet": "172.16.30.0/24",
   233          "routes": [
   234            {
   235              "dst": "0.0.0.0/0"
   236            }
   237          ]
   238        }
   239      },
   240      {
   241        "type": "portmap",
   242        "capabilities": { "portMappings": true }
   243      }
   244    ]
   245  }
   246  ```
   247  
   248  ```hcl
   249  network {
   250    mode = "cni/mynet"
   251    port "http" {
   252      to = 8080
   253    }
   254  }
   255  ```
   256  
   257  The Nomad client will build the correct [capabilities arguments](https://github.com/containernetworking/cni/blob/v0.8.0/CONVENTIONS.md#well-known-capabilities) for the portmap plugin based on the defined port stanzas.
   258  
   259  ### Host Networks
   260  
   261  In some cases a port should only be allocated to a specific interface or address on the host.
   262  The `host_network` field of a port will constrain port allocation to a single named host network.
   263  If `host_network` is set for a port, Nomad will schedule the allocations on a node which has defined a `host_network` with the given name.
   264  If not set the "default" host network is used which is commonly the address with a default route associated with it.
   265  
   266  When Nomad does port mapping for ports with a defined `host_network`, the port mapping rule will use the host address as the destination address.
   267  
   268  ~> Note: `host_network` does not currently support task-based mapped ports such as the Docker driver's `port_map` configuration.
   269  
   270  ```hcl
   271  network {
   272    mode = "bridge"
   273  
   274    # define a port to use for public https traffic
   275    port "https" {
   276      static       = 443
   277      to           = 8080
   278      host_network = "public"
   279    }
   280    # define a port that is only exposed to private traffic
   281    port "admin" {
   282      to           = 9000
   283      host_network = "private"
   284    }
   285  }
   286  ```
   287  
   288  ### Limitations
   289  
   290  - Only one `network` stanza can be specified, when it is defined at the task group level.
   291  - Only the `NOMAD_PORT_<label>` and `NOMAD_HOST_PORT_<label>` environment
   292    variables are set for group network ports.
   293  
   294  [docker-driver]: /docs/drivers/docker 'Nomad Docker Driver'
   295  [qemu-driver]: /docs/drivers/qemu 'Nomad QEMU Driver'
   296  [connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration'