2016-10-28 00:36:26 +00:00
---
2020-02-06 23:45:31 +00:00
layout: docs
page_title: network Stanza - Job Specification
sidebar_title: network
2016-10-28 00:36:26 +00:00
description: |-
2020-07-23 13:18:59 +00:00
The "network" stanza specifies the networking requirements for the task group,
2020-09-30 13:48:40 +00:00
including networking mode and port allocations.
2016-10-28 00:36:26 +00:00
---
# `network` Stanza
2020-09-30 13:48:40 +00:00
<Placement groups={[['job', 'group', 'network']]} />
2016-10-28 00:36:26 +00:00
2020-07-23 13:18:59 +00:00
The `network` stanza specifies the networking requirements for the task group,
including the network mode and port allocations. When scheduling jobs in
2016-10-28 00:36:26 +00:00
Nomad they are provisioned across your fleet of machines along with other jobs
and services. Because you don't know in advance what host your job will be
provisioned on, Nomad will provide your tasks with network configuration when
they start up.
2020-07-23 13:18:59 +00:00
When the `network` stanza is defined with `bridge` as the networking mode,
2020-03-12 22:05:34 +00:00
all tasks in the task group share the same network namespace. This is a prerequisite for
2020-03-20 21:00:59 +00:00
[Consul Connect](/docs/integrations/consul-connect). Tasks running within a
2020-03-12 22:05:34 +00:00
network namespace are not visible to applications outside the namespace on the same host.
This allows [Connect][] enabled applications to bind only to localhost within the shared network stack,
and use the proxy for ingress and egress traffic.
2019-09-09 03:39:30 +00:00
2016-10-28 00:36:26 +00:00
Note that this document only applies to services that want to _listen_ on a
port. Batch jobs or services that only make outbound connections do not need to
allocate ports, since they will use any available interface to make an outbound
connection.
```hcl
job "docs" {
group "example" {
2020-07-23 13:18:59 +00:00
network {
port "http" {}
port "https" {}
port "lb" {
static = 8889
2016-10-28 00:36:26 +00:00
}
}
}
}
```
## `network` Parameters
2020-08-28 16:38:30 +00:00
- `mbits` <code>([*deprecated*](/docs/upgrade/upgrade-specific#nomad-0-12-0) int: 10)</code> - Specifies the bandwidth required in MBits.
2020-09-16 15:06:35 +00:00
2017-03-15 23:08:22 +00:00
- `port` <code>([Port](#port-parameters): nil)</code> - Specifies a TCP/UDP port
2016-10-28 00:36:26 +00:00
allocation and can be used to specify both dynamic ports and reserved ports.
2020-02-06 23:45:31 +00:00
- `mode` `(string: "host")` - Mode of the network. The following modes are available:
2019-09-09 03:57:55 +00:00
2020-02-19 16:06:18 +00:00
- `none` - Task group will have an isolated network without any network interfaces.
- `bridge` - Task group will have an isolated network namespace with an interface
that is bridged with the host. Note that bridge networking is only
currently supported for the `docker`, `exec`, `raw_exec`, and `java` task
drivers.
- `host` - Each task will join the host network namespace and a shared network
namespace is not created. This matches the current behavior in Nomad 0.9.
2020-07-08 19:45:04 +00:00
- `cni/<cni network name>` - Task group will have an isolated network namespace
with the network configured by CNI.
2019-09-09 03:39:30 +00:00
2020-09-30 13:48:40 +00:00
- `dns` <code>([DNSConfig](#dns-parameters): nil)</code> - Sets the DNS configuration
for the allocations. By default all DNS configuration is inherited from the client host.
DNS configuration is only supported on Linux clients at this time.
2020-04-28 03:11:06 +00:00
2016-10-28 00:36:26 +00:00
### `port` Parameters
2020-07-08 19:45:04 +00:00
- `static` `(int: nil)` - Specifies the static TCP/UDP port to allocate. If omitted, a
dynamic port is chosen. We **do not recommend** using static ports, except
2016-10-28 00:36:26 +00:00
for `system` or specialized jobs like load balancers.
2019-10-18 22:42:28 +00:00
- `to` `(string:nil)` - Applicable when using "bridge" mode to configure port
2020-07-08 19:45:04 +00:00
to map to inside the task's network namespace. `-1` sets the mapped port equal to the
dynamic port allocated by the scheduler. The `NOMAD_PORT_<label>` environment variable
will contain the `to` value.
- `host_network` `(string:nil)` - Designates the host network name to use when allocating
the port. When port mapping the host port will only forward traffic to the matched host
network address.
2016-10-28 00:36:26 +00:00
The label assigned to the port is used to identify the port in service
discovery, and used in the name of the environment variable that indicates
which port your application should bind to. For example:
```hcl
port "foo" {}
```
When the task starts, it will be passed the following environment variables:
- <tt>NOMAD_IP_foo</tt> - The IP to bind on for the given port label.
- <tt>NOMAD_PORT_foo</tt> - The port value for the given port label.
2020-02-06 23:45:31 +00:00
- <tt>NOMAD_ADDR_foo</tt> - A combined
<tt>ip:port</tt> that can be used for convenience.
2016-10-28 00:36:26 +00:00
The label of the port is just text - it has no special meaning to Nomad.
2020-04-28 03:11:06 +00:00
## `dns` Parameters
- `servers` `(array<string>: nil)` - Sets the DNS nameservers the allocation uses for name resolution.
- `searches` `(array<string>: nil)` - Sets the search list for hostname lookup
- `options` `(array<string>: nil)` - Sets internal resolver variables.
2016-10-28 00:36:26 +00:00
## `network` Examples
The following examples only show the `network` stanzas. Remember that the
2016-10-31 00:41:03 +00:00
`network` stanza is only valid in the placements listed above.
2016-10-28 00:36:26 +00:00
### Dynamic Ports
This example specifies a dynamic port allocation for the port labeled "http".
2017-09-22 08:24:05 +00:00
Dynamic ports are allocated in a range from `20000` to `32000`.
2016-10-28 00:36:26 +00:00
Most services run in your cluster should use dynamic ports. This means that the
port will be allocated dynamically by the scheduler, and your service will have
to read an environment variable to know which port to bind to at startup.
```hcl
2020-07-23 13:18:59 +00:00
group "example" {
network {
port "http" {}
port "https" {}
2016-10-28 00:36:26 +00:00
}
}
```
```hcl
network {
port "http" {}
}
```
### Static Ports
This example specifies a static port allocation for the port labeled "lb". Static
ports bind your job to a specific port on the host they' are placed on. Since
multiple services cannot share a port, the port must be open in order to place
your task.
```hcl
network {
port "lb" {
static = 6539
}
}
```
### Mapped Ports
Some drivers (such as [Docker][docker-driver] and [QEMU][qemu-driver]) allow you
to map ports. A mapped port means that your application can listen on a fixed
port (it does not need to read the environment variable) and the dynamic port
will be mapped to the port in your container or virtual machine.
```hcl
2020-07-23 13:18:59 +00:00
group "app" {
network {
2020-10-02 12:39:48 +00:00
port "http" {
to = 8080
}
2016-10-28 00:36:26 +00:00
}
2020-07-23 13:18:59 +00:00
task "example" {
driver = "docker"
config {
2020-10-02 12:39:48 +00:00
ports = ["http"]
2016-10-28 00:36:26 +00:00
}
}
}
```
The above example is for the Docker driver. The service is listening on port
`8080` inside the container. The driver will automatically map the dynamic port
to this service.
When the task is started, it is passed an additional environment variable named
`NOMAD_HOST_PORT_http` which indicates the host port that the HTTP service is
bound to.
2019-09-09 03:39:30 +00:00
### Bridge Mode
2020-07-23 13:18:59 +00:00
Bridge mode allows compatible tasks to share a networking stack and interfaces. Nomad
can then do port mapping without relying on individual task drivers to implement port
mapping configuration.
2019-09-09 03:39:30 +00:00
The following example is a group level network stanza that uses bridge mode
and port mapping.
```hcl
network {
mode = "bridge"
port "http" {
static = 9002
to = 9002
}
}
```
2016-10-28 00:36:26 +00:00
2020-04-28 03:11:06 +00:00
### DNS
The following example configures the allocation to use Google's DNS resolvers 8.8.8.8 and 8.8.4.4.
```hcl
network {
dns {
servers = ["8.8.8.8", "8.8.4.4"]
}
}
```
2020-07-08 19:45:04 +00:00
### Container Network Interface (CNI)
Nomad supports CNI by fingerprinting each node for [CNI network configurations](https://github.com/containernetworking/cni/blob/v0.8.0/SPEC.md#network-configuration).
These are associated to the node by the `name` field of the CNI configuration.
The `name` can then be used when setting the network `mode` field in the form of `cni/<name>`.
As an example if the following CNI configuration was on a node the proceeding network stanza could be used.
```json
{
"cniVersion": "0.3.1",
"name": "mynet",
"plugins": [
{
"type": "ptp",
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "172.16.30.0/24",
"routes": [
{
"dst": "0.0.0.0/0"
}
]
}
},
{
"type": "portmap",
2020-09-30 13:48:40 +00:00
"capabilities": { "portMappings": true }
2020-07-08 19:45:04 +00:00
}
]
}
```
2020-09-30 13:48:40 +00:00
2020-07-08 19:45:04 +00:00
```hcl
network {
mode = "cni/mynet"
port "http" {
to = 8080
}
}
```
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.
### Host Networks
2020-07-23 13:18:59 +00:00
In some cases a port should only be allocated to a specific interface or address on the host.
The `host_network` field of a port will constrain port allocation to a single named host network.
2020-07-08 19:45:04 +00:00
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.
If not set the "default" host network is used which is commonly the address with a default route associated with it.
2020-07-23 13:18:59 +00:00
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.
2020-09-30 13:48:40 +00:00
\*Host networks does not currently support task based mapped ports such as the Docker driver's `port_map` configuration.
2020-07-08 19:45:04 +00:00
```hcl
network {
mode = "bridge"
2020-09-30 13:48:40 +00:00
2020-07-08 19:45:04 +00:00
# define a port to use for public https traffic
port "https" {
static = 443
to = 8080
host_network = "public"
}
# define a port that is only exposed to private traffic
port "admin" {
to = 9000
host_network = "private"
}
}
```
2019-10-18 22:42:28 +00:00
### Limitations
2020-02-06 23:45:31 +00:00
- Only one `network` stanza can be specified, when it is defined at the task group level.
- Only the `NOMAD_PORT_<label>` and `NOMAD_HOST_PORT_<label>` environment
2019-10-18 22:42:28 +00:00
variables are set for group network ports.
2020-02-06 23:45:31 +00:00
[docker-driver]: /docs/drivers/docker 'Nomad Docker Driver'
[qemu-driver]: /docs/drivers/qemu 'Nomad QEMU Driver'
[connect]: /docs/job-specification/connect 'Nomad Consul Connect Integration'