github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/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 an environment variable named 83 `NOMAD_ADDR_http` which indicates a combination of the interface IP and port. 84 85 ``` 86 NOMAD_ADDR_http=127.0.0.1:53423 ./start-command 87 ``` 88 89 ### Mapped Ports <a id="mapped_ports"></a> 90 91 Some drivers (such as Docker and QEMU) allow you to map ports. A mapped port 92 means that your application can listen on a fixed port (it does not need to 93 read the environment variable) and the dynamic port will be mapped to the port 94 in your container or VM. 95 96 ``` 97 driver = "docker" 98 99 port "http" {} 100 101 config { 102 port_map = { 103 http = 8080 104 } 105 } 106 ``` 107 108 The above example is for the Docker driver. The service is listening on port 109 `8080` inside the container. The driver will automatically map the dynamic port 110 to this service. 111 112 When the task is started, it is passed an additional environment variable named 113 `NOMAD_HOST_PORT_http` which indicates the host port that the http service is 114 bound to. 115 116 Please refer to the [Docker](/docs/drivers/docker.html) and [QEMU](/docs/drivers/qemu.html) drivers for additional information.