github.com/huiliang/nomad@v0.2.1-0.20151124023127-7a8b664699ff/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 applies 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 port "http" {} 47 port "https" {} 48 } 49 ``` 50 51 ### Static Ports 52 53 Static ports bind your job to a specific port on the host they're placed on. 54 Since multiple services cannot share a port, the port must be open in order to 55 place your task. 56 57 ``` 58 task "dnsservice" { 59 port "dns" { 60 static = 53 61 } 62 } 63 ``` 64 65 We recommend _only_ using static ports for [system 66 jobs](/docs/jobspec/schedulers.html) or specialized jobs like load balancers. 67 68 ### Labels and Environment Variables 69 70 The label assigned to the port is used to identify the port in service 71 discovery, and used for the name of the environment variable that indicates 72 which port your application should bind to. For example, we've labeled this 73 port `http`: 74 75 ``` 76 port "http" {} 77 ``` 78 79 When the task is started, it is passed an environment variable named 80 `NOMAD_PORT_http` which indicates the port. 81 82 ``` 83 NOMAD_PORT_http=53423 ./start-command 84 ``` 85 86 ### Mapped Ports 87 88 Some drivers (such as Docker and QEMU) allow you to map ports. A mapped port 89 means that your application can listen on a fixed port (it does not need to 90 read the environment variable) and the dynamic port will be mapped to the port 91 in your container or VM. 92 93 ``` 94 driver = "docker" 95 96 port "http" {} 97 98 config { 99 port_map = { 100 http = 8080 101 } 102 } 103 ``` 104 105 The above example is for the Docker driver. The service is listening on port 106 `8080` inside the container. The driver will automatically map the dynamic port 107 to this service. 108 109 Please refer to the [Docker](/docs/drivers/docker.html) and [QEMU](/docs/drivers/qemu.html) drivers for additional information.