It was announced on cf-dev mailing list and demo’d in CF Summit 2016…yes, TCP Routing is now available on Cloud Foundry. It will allow developers to bring non-http work loads to Cloud Foundry. It will open up the platform and its benefits to all kind of new applications!
I would like to give readers a quick tour of TCP Routing feature in Cloud Foundry. TCP routes in Cloud Foundry are based on reserving external/frontend port of TCP router group for an application. Any traffic coming on that port will be forwarded to one or more instances of that application (using round-robin load balancing policy). TCP router groups are collection of one or more TCP routers (HAProxy) that are identically configured. This is done for high availability of TCP routing layer. Also, this concept of router groups allows for adding multiple router groups of different types to allow different routing functionality to be added to Cloud Foundry in future. Currently there is only one router group of type tcp. But this could be easily changed to add multiple tcp router groups thereby allowing different applications to reserve same port on different router groups.
At Cloud Foundry we strive to keep the developer experience simple and similar to existing UX. With that as our guiding principle we developed TCP routing user experience. So lets take a look at developer experience of TCP routing.
To create a TCP route you should first create a tcp domain. Creation of tcp domain is very similar to http domains, you just need to specify router group when creating tcp domain as follows:
cf create-shared-domain tcp.example.com --router-group default-tcp
You can see that while creating a tcp shared domain you just need to specify tcp router group. You can get all the router groups available in your deployment by running following command:
Getting router groups as admin ...
There is only one router group available today with routing deployment and that is named default-tcp.
Now you are ready to create and map tcp routes to your application. Of course, here we are assuming that dns entries for above created tcp domain will resolve to load balancer in front of tcp router groups or if tcp routers have public IP addresses then it will resolve to tcp routers. This has to be done by the operator/administrator of your Cloud Foundry deployment.
Now lets push an application, create a tcp route and map it to the application and lets do it all in one command…yes one command!
cf push mqttbroker --docker-image toke/mosquitto -d tcp.example.com --random-route
There are many things going on here. Lets break it down:
- A docker image toke/mosquitto is being downloaded from docker hub.
- It is staged (a droplet is created).
- A random TCP route is created. This is achieved by specifying tcp domain through -d tcp.example.com option and –random-route option. As explained above TCP route consists of reserving an external port on tcp router group, by specifying random-route option we let system choose the free port from available ports on router-group and reserve that port for creating a tcp route.
- This TCP route is mapped to the application mqttbroker.
- Staged application is now deployed and run.
- Application is now ready to receive the traffic on tcp route that is mapped to the application.
With above command we have pushed a MQTT broker (mosquitto) as CF App! We can now give the tcp route mapped to this MQTT broker to publishers and subscribers to publish and subscribe MQTT messages.
Like http routes, you can create and map tcp route independent of cf push. This is how you can do it:
cf create-route SPACE DOMAIN (--port PORT | --random-port)
e.g. cf create-route s1 tcp.example.com --port 8883
e.g. cf create-route s1 tcp.example.com --random-port
The above command will allow you to create a tcp route. Notice that it allows you to let system choose a port for you (–random-port) or you can specify the port of your choice (–port 8883). Of course, if you specify the port of your choice and if it is unavailable then route creation will fail and you will get an error message indicating that port is already reserved and not available. Once tcp route is created you can map it to application, again very similar to how you will map http routes. Note that if the tcp route being mapped is not present then it will be created (again very much like http routes), so you don’t need to create tcp route before hand if you have the application to bind to. The only time you will need to create a tcp route is if you need to reserve a port and use it later to bind to an app.
cf map-route APP_NAME DOMAIN (--port PORT | --random-port) e.g. cf map-route myapp tcp.example.com --port 8883 e.g. cf map-route myapp tcp.example.com --random-port
This is command will map the tcp route to app and create the tcp route if one doesn’t exist.
That’s it…your apps that need non-http protocol support can now happily run on Cloud Foundry. Now all the great benefits that web apps got from Cloud Foundry are available to non-http apps as well.
As always, we love feedback–please share your experiences in the comments!