github.com/ryanslade/nomad@v0.2.4-0.20160128061903-fc95782f2089/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 ## IP Address 22 23 Hosts in Nomad may have multiple network interfaces attached to them. This 24 allows you to have a higher density of services, or bind to interfaces on 25 different subnets (for example public vs. private subnets). 26 27 Each task will receive port allocations on a single interface. The IP is passed 28 to your job via the `NOMAD_IP` environment variable. 29 30 ## Ports 31 32 In addition to allocating an interface, Nomad can allocate static or dynamic 33 ports to your task. 34 35 ### Dynamic Ports 36 37 Dynamic ports are allocated in a range from `20000` to `60000`. 38 39 Most services run in your cluster should use dynamic ports. This means that the 40 port will be allocated dynamically by the scheduler, and your service will have 41 to read an environment variable (see below) to know which port to bind to at 42 startup. 43 44 ``` 45 task "webservice" { 46 ... 47 resources { 48 ... 49 network { 50 port "http" {} 51 port "https" {} 52 } 53 } 54 } 55 ``` 56 57 ### Static Ports 58 59 Static ports bind your job to a specific port on the host they're placed on. 60 Since multiple services cannot share a port, the port must be open in order to 61 place your task. 62 63 ``` 64 task "dnsservice" { 65 ... 66 resources { 67 ... 68 network { 69 port "dns" { 70 static = 53 71 } 72 } 73 } 74 } 75 ``` 76 77 We recommend _only_ using static ports for [system 78 jobs](/docs/jobspec/schedulers.html) or specialized jobs like load balancers. 79 80 ### Labels and Environment Variables 81 82 The label assigned to the port is used to identify the port in service 83 discovery, and used for the name of the environment variable that indicates 84 which port your application should bind to. For example, we've labeled this 85 port `http`: 86 87 ``` 88 port "http" {} 89 ``` 90 91 When the task is started, it is passed an environment variable named 92 `NOMAD_PORT_http` which indicates the port. 93 94 ``` 95 NOMAD_PORT_http=53423 ./start-command 96 ``` 97 98 ### Mapped Ports 99 100 Some drivers (such as Docker and QEMU) allow you to map ports. A mapped port 101 means that your application can listen on a fixed port (it does not need to 102 read the environment variable) and the dynamic port will be mapped to the port 103 in your container or VM. 104 105 ``` 106 driver = "docker" 107 108 port "http" {} 109 110 config { 111 port_map = { 112 http = 8080 113 } 114 } 115 ``` 116 117 The above example is for the Docker driver. The service is listening on port 118 `8080` inside the container. The driver will automatically map the dynamic port 119 to this service. 120 121 Please refer to the [Docker](/docs/drivers/docker.html) and [QEMU](/docs/drivers/qemu.html) drivers for additional information.