github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/website/source/docs/jobspec/networking.html.md (about) 1 --- 2 layout: "docs" 3 page_title: "Nomad Networking" 4 sidebar_current: "docs-jobspec-networking" 5 description: |- 6 Learn how to configure networking and ports for Nomad tasks. 7 --- 8 9 # Networking 10 11 When scheduling jobs in Nomad they are provisioned across your fleet of 12 machines along with other jobs and services. Because you don't know in advance 13 what host your job will be provisioned on, Nomad will provide your task with 14 network configuration when they start up. 15 16 Note that this document only applies to services that want to _listen_ 17 on a port. Batch jobs or services that only make outbound connections do not 18 need to allocate ports, since they will use any available interface to make an 19 outbound connection. 20 21 ## Ports 22 23 In addition to allocating an interface, Nomad can allocate static or dynamic 24 ports to your task. 25 26 ### Dynamic Ports 27 28 Dynamic ports are allocated in a range from `20000` to `60000`. 29 30 Most services run in your cluster should use dynamic ports. This means that the 31 port will be allocated dynamically by the scheduler, and your service will have 32 to read an environment variable (see below) to know which port to bind to at 33 startup. 34 35 ``` 36 task "webservice" { 37 ... 38 resources { 39 ... 40 network { 41 port "http" {} 42 port "https" {} 43 } 44 } 45 } 46 ``` 47 48 ### Static Ports 49 50 Static ports bind your job to a specific port on the host they're placed on. 51 Since multiple services cannot share a port, the port must be open in order to 52 place your task. 53 54 ``` 55 task "dnsservice" { 56 ... 57 resources { 58 ... 59 network { 60 port "dns" { 61 static = 53 62 } 63 } 64 } 65 } 66 ``` 67 68 We recommend _only_ using static ports for [system 69 jobs](/docs/jobspec/schedulers.html) or specialized jobs like load balancers. 70 71 ### Labels and Environment Variables 72 73 The label assigned to the port is used to identify the port in service 74 discovery, and used for the name of the environment variable that indicates 75 which port your application should bind to. For example, we've labeled this 76 port `http`: 77 78 ``` 79 port "http" {} 80 ``` 81 82 When the task is started, it is passed the following environment variables: 83 84 * `NOMAD_IP_http` - The IP to bind on for the given port label. 85 86 * `NOMAD_PORT_http` - The port value for the given port label. 87 88 * `NOMAD_ADDR_http` - A combined `IP:Port` that can be used for convenience. 89 90 ### Mapped Ports <a id="mapped_ports"></a> 91 92 Some drivers (such as Docker and QEMU) allow you to map ports. A mapped port 93 means that your application can listen on a fixed port (it does not need to 94 read the environment variable) and the dynamic port will be mapped to the port 95 in your container or VM. 96 97 ``` 98 driver = "docker" 99 100 port "http" {} 101 102 config { 103 port_map = { 104 http = 8080 105 } 106 } 107 ``` 108 109 The above example is for the Docker driver. The service is listening on port 110 `8080` inside the container. The driver will automatically map the dynamic port 111 to this service. 112 113 When the task is started, it is passed an additional environment variable named 114 `NOMAD_HOST_PORT_http` which indicates the host port that the http service is 115 bound to. 116 117 Please refer to the [Docker](/docs/drivers/docker.html) and [QEMU](/docs/drivers/qemu.html) drivers for additional information.