This topic describes how routes and domains work in Pivotal Application Service, and how developers and administrators configure routes and domains for their applications using the Cloud Foundry Command Line Interface (cf CLI).

For more information about routing capabilities in PAS, see the HTTP Routing topic.

Routes

The PAS Gorouter routes requests to applications by associating an app with an address, known as a route. We call this association a mapping. Use the cf CLI cf map-route command to associate an app and route.

The routing tier compares each request with a list of all the routes mapped to apps and attempts to find the best match. For example, the Gorouter would make the following matches for the two routes myapp.shared-domain.example.com and myapp.shared-domain.example.com/products:

Request

Matched Route

http://myapp.shared-domain.example.com

myapp.shared-domain.example.com

http://myapp.shared-domain.example.com/contact

myapp.shared-domain.example.com

http://myapp.shared-domain.example.com/products

myapp.shared-domain.example.com/products

http://myapp.shared-domain.example.com/products/123

myapp.shared-domain.example.com/products

http://products.shared-domain.example.com

No match; 404

The Gorouter does not use a route to match requests until the route is mapped to an app. In the above example, products.shared-domain.example.com may have been created as a route in Cloud Foundry, but until it is mapped to an app, requests for the route receive a 404.

The routing tier knows the location of instances for apps mapped to routes. Once the routing tier determines a route as the best match for a request, it makes a load-balancing calculation using a round-robin algorithm, and forwards the request to an instance of the mapped app.

Developers can map many apps to a single route, resulting in load-balanced requests for the route across all instances of all mapped apps. This approach enables the blue/green zero-downtime deployment strategy. Developers can also map an individual app to multiple routes, enabling access to the app from many URLs. The number of routes that can be mapped to each app is approximately 1000 (128 KB).

Routes belong to a space, and developers can only map apps to a route in the same space.

Note: Routes are globally unique. Developers in one space cannot create a route with the same URL as developers in another space, regardless of which orgs control these spaces.

HTTP vs. TCP Routes

Note: By default, PAS only supports routing of HTTP requests to applications.

Routes are considered HTTP if they are created from HTTP domains, and TCP if they are created from TCP domains. See HTTP vs. TCP Shared Domains.

HTTP routes include a domain, an optional hostname, and an optional context path. shared-domain.example.com, myapp.shared-domain.example.com, and myapp.shared-domain.example.com/products are all examples of HTTP routes. Applications should listen to the localhost port defined by the $PORT environment variable, which is 8080 on Diego. As an example, requests to myapp.shared-domain.example.com would be routed to the application container at localhost:8080.

Requests to HTTP routes must be sent to ports 80 or 443.

Ports cannot be reserved for HTTP routes.

TCP routes include a domain and a route port. A route port is the port clients make requests to. This is not the same port as what an application pushed to Cloud Foundry listens on. tcp.shared-domain.example.com:60000 is an example of a TCP route. Just as for HTTP routes, applications should listen to the localhost port defined by the $PORT environment variable, which is 8080 on Diego. As an example, requests to tcp.shared-domain.example.com:60000 would be routed to the application container at localhost:8080.

Once a port is reserved for a route, it cannot be reserved for another route.

Hostname and path are not supported for TCP routes.

Internal Routes

Note: This feature is currently experimental.

Applications can communicate without leaving the platform on the container network using internal routes. You can create an internal route using the cf map-route command with an internal domain.

By default, apps cannot communicate with each other on the container network. To allow apps to communicate with each other you must create a network policy. For more information, see add-network-policy in the Cloud Foundry CLI Reference Guide.

Create a Route

When a developer creates a route using the cf CLI, PAS determines whether the route is an HTTP or a TCP route based on the domain. To create a HTTP route, a developer must choose an HTTP domain. To create a TCP route, a developer must choose a TCP domain.

Domains in PAS provide a namespace from which to create routes. To list available domains for a targeted organization, use the cf domains command. For more information about domains, see the Domains section.

The following sections describe how developers can create HTTP and TCP routes for different use cases.

Create an HTTP Route with Hostname

In PAS, a hostname is the label that indicates a subdomain of the domain associated with the route. Given a domain shared-domain.example.com, a developer can create the route myapp.shared-domain.example.com in space my-space by specifying the hostname myapp with the cf create-route command as shown in this example:

If a client sends a request to http://app.foo.shared-domain.example.com by accident, attempting to reach myapp.foo.shared-domain.example.com, PAS routes the request to the app mapped to the route *.foo.shared-domain.example.com.

Create an HTTP Route with a Path

Developers can use paths to route requests for the same hostname and domain to different apps.

A developer can create three routes using the same hostname and domain in the space my-space with the following commands:

If the developer maps the first route with path products to the products app, the second route with path orders to the orders app, and the last route to the storefront app. After this, the following occurs:

PAS routes requests to http://store.shared-domain.example.com/products to the products app.

PAS routes requests to http://store.shared-domain.example.com/orders to the orders app.

PAS routes requests to http://store.shared-domain.example.com to the storefront app.

PAS attempts to match routes with a path first, and then attempts to match host and domain.

Note: Routes with the same domain and hostname but different paths can only be created in the same space. Private domains do not have this limitation.

Note: PAS does not route requests for context paths to the root context of an application. Applications must serve requests on the context path.

Create a TCP Route with a Port

A developer can create a TCP route for tcp.shared-domain.example.com on an arbitrary port with the following command. If the clients of the app can accommodate addressing an arbitrary port, then developers should use the --random-port to instruct PAS to pick a port for your route.

In this example, PAS routes requests to tcp.shared-domain.example.com:60034 to apps mapped to this route.

To request a specific port, a developer can use the --port flag, so long as the port is not reserved for another space. The following command creates a TCP route for tcp.shared-domain.example.com on port 60035:

Note: Changes to route mappings are executed asynchronously. On startup, an application will be accessible at its route within a few seconds. Similarly, upon mapping a new route to a running app, the app will be accessible at this route within a few seconds of the CLI exiting successfully.

Manually Map a Route

Given the following routes and applications:

Route

Application

store.shared-domain.example.com/products

products

store.shared-domain.example.com/orders

orders

store.shared-domain.example.com

storefront

tcp.shared-domain.example.com:60000

tcp-app

The following commands map the above routes to their respective apps. Developers use hostname, domain, and path to uniquely identify a route to map their apps to.

Map a Route with Application Push

Developers can map a route to their app with the cf push command.

If a domain or hostname is not specified, then a route will be created using the app name and the default shared domain (see Shared Domains). The following command pushes the app myapp, creating the route myapp.shared-domain.example.com from the default shared domain shared-domain.example.com. If the route has not already been created in another space this command also maps it to the app.

$ cf push myapp

To customize the route during push, specify the domain using the -d flag and the hostname with the --hostname flag. The following command creates the foo.private-domain.example.com route for myapp:

$ cf push myapp -d private-domain.example.com --hostname foo

To map a TCP route during push, specify a TCP domain and request a random port using --random-route. To specify a port, push the app without a route, then create and map the route manually by following the steps in the Create a TCP Route with a Port section.

$ cf push tcp-app -d tcp.shared-domain.example.com --random-route

Map a Route Using Application Manifest

Developers can map a route to their app with a manifest by editing the route attribute to specify the host, domain, port and/or path components of the route. For more information, see the Deploying with Application Manifests topic.

Routing multiple apps to the same route may cause undesirable behavior in some situations by routing incoming requests randomly to one of the apps on the shared route.

See the Routing Conflict section of the Troubleshooting Application Deployment and Health topic for more information about troubleshooting this problem.

Map Multiple Routes to One App

You can have multiple routes to an app, but those routes cannot have different context paths.

The following routes are valid for a single app:

Route 1

Route 2

myapp.example.com

myapp.apps.cf.example.com

myapp.example.com/foo

myapp.apps.cf.example.com/foo

The following routes are not valid for a single app:

Route 1

Route 2

myapp.example.com/foo

myapp.apps.cf.example.com/bar

myapp.apps.cf.example.com/foo

myapp.example.com/bar

Map an Internal Route to an App

Note: This feature is currently experimental.

You can map an internal route to any app. This internal route allows your app to communicate with other apps without leaving the platform. Once mapped, this route becomes available to all other apps on the platform.

This example creates a foo.apps.internal internal route for myapp:

$ cf map-route myapp apps.internal --hostname foo

Unmap a Route

Developers can remove a route from an app using the cf unmap-route command. The route remains reserved for later use in the space where it was created until the route is deleted.

To unmap an HTTP route from an app, identify the route using the hostname, domain, and path:

If the cf CLI cannot find the instance the format is incorrect, a 404 status code is returned.

Domains

Note: The term domain in this topic differs from its common use and is specific to Cloud Foundry. Likewise, shared domain and private domain refer to resources with specific meaning in Cloud Foundry. The use of domain name, root domain, and subdomain refers to DNS records.

Domains indicate to a developer that requests for any route created from the domain will be routed to PAS. This requires DNS to be configured out-of-band to resolve the domain name to the IP address of a load balancer configured to forward requests to the CF routers. For more information about configuring DNS, see the DNS for Domains section.

List Domains for an Org

When creating a route, developers will select from domains available to them. Use the cf domains command to view a list of available domains for the targeted org:

This example displays three available domains: a shared HTTP domain shared-domain.example.com, a shared TCP domain tcp.shared-domain.example.com, and a private domain private-domain.example.com. See Shared Domains and Private Domains.

HTTP vs. TCP Domains

HTTP domains indicate to a developer that only requests using the HTTP protocol will be routed to applications mapped to routes created from the domain. Routing for HTTP domains is layer 7 and offers features like custom hostnames, sticky sessions, and TLS termination.

TCP domains indicate to a developer that requests over any TCP protocol, including HTTP, will be routed to applications mapped to routes created from the domain. Routing for TCP domains is layer 4 and protocol agnostic, so many features available to HTTP routing are not available for TCP routing. TCP domains are defined as being associated with the TCP Router Group. The TCP Router Group defines the range of ports available to be reserved with TCP Routes. Currently, only Shared Domains can be TCP.

Note: By default, PAS only supports routing of HTTP requests to applications.

Shared Domains

Admins manage shared domains, which are available to users in all orgs of a PAS deployment. An admin can offer multiple shared domains to users. For example, an admin may offer developers the choice of creating routes for their apps from shared-domain.example.com and cf.some-company.com.

There is not technically a default shared domain. If a developer pushes an app without specifying a domain (see Map a Route with Application Push), a route will be created for it from the first shared domain created in the system. All other operations involving route require the domain be specified (see Routes).

Shared domains are HTTP by default, but can be configured to be TCP when associated with the TCP Router Group.

Create a Shared Domain

Admins can create an HTTP shared domain with the cf create-shared-domain command:

$ cf create-shared-domain shared-domain.example.com

To create a TCP shared domain, first discover the name of the TCP Router Group.

Delete a Shared Domain

Admins can delete a shared domain from PAS with the cf delete-shared-domain command:

$ cf delete-shared-domain example.com

Internal Domain

Note: This feature is currently experimental.

The internal domain is a special type of shared domain used for app communication internal to the platform. When you enable service discovery, the internal domain apps.internal becomes provided for your use.

Note: The internal domain apps.internal cannot be deleted. At this time, no other internal domains can be created.

Private Domains

Org managers can add private domains, or custom domains, and give members of the org permission to create routes for privately registered domain names. Private domains can be shared with other orgs, enabling users of those orgs to create routes from the domain.

Private domains can be HTTP or HTTPS only. TCP Routing is supported for shared domains only.

Create a Private Domain

Org managers can create a private domain with the following command:

$ cf create-domain my-org private-domain.example.com

Org managers can create a private domain for a subdomain with the following command:

$ cf create-domain my-org foo.private-domain.example.com

Sharing a Private Domain with One or More Orgs

Org managers can grant or revoke access to a private domain to other orgs if they have permissions for these orgs with the following commands:

Mapping Multiple Subdomains to Your Custom Domain

Use a wildcard CNAME record to point all of the subdomains in your custom domain to shared-domain.example.com.

Each separately configured subdomain has priority over the wildcard configuration.

The following table provides some example wildcard CNAME record mappings.

Record Set in Custom Domain

Type

Target in PAS

*.yourcustomdomain.com.

CNAME

*.shared-domain.example.com

*.yourcustomdomain.com.

CNAME

*.myapp.shared-domain.example.com

If you use a wildcard as the subdomain name, then your DNS provider can route from *.YOURCUSTOMDOMAIN to any of the following:

*.shared-domain.example.com

foo.myapp.shared-domain.example.com

bar.foo.myapp.shared-domain.example.com

Configuring DNS for Your Registered Root Domain

To use your root domain (for example, example.com) for apps on PAS you can either use custom DNS record types like ALIAS and ANAME, if your DNS provider offers them, or subdomain redirection.

Note: Root domains are also called zone apex domains.

If your DNS provider supports using an ALIAS or ANAME record, configure your root domain with your DNS provider to point at a shared domain in PAS.

Record

Name

Target

Note

ALIAS or ANAME

empty or @

private-domain.example.com.

Refer to your DNS provider documentation to determine whether to use an empty or @ value for the Name entry.

If your DNS provider does not support ANAME or ALIAS records you can use subdomain redirection, also known as domain forwarding, to redirect requests for your root domain to a subdomain configured as a CNAME.