Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CloudFoundry application opening two ports

I have a CF application that opens two ports. AFAIK CF can create routing on only for one of them - to the one that is located in VCAP_APP_PORT or PORT. How can I create some route to the second port? I don't mind having separate name directing to other port.

like image 953
kkonrad Avatar asked Aug 18 '15 13:08

kkonrad


4 Answers

As stated in some other comments it is now possible in CF to use multiple ports for your application. There is a chapter in the CF documentation which describes how to do it. I followed the instructions and still had some trouble to fully understand it, that's why I provide a step by step guide here with some explanations (replace all variables in [] with the actual values):

  1. Configure your application to listen on multiple ports. In my case I configured a spring boot app to listen on port 8080 for HTTPS requests and on port 8081 for HTTP requests (used for calls to actuator endpoints like health/prometheus as described here). This means that I have configured one TCP route and one HTTP route in CF and mapped those routes to the CF app.
  2. Get the [APP_GUID] of the CF app which should be reachable on multiple ports: cf app [APP_NAME] --guid
  3. Add the ports (e.g. 8080, 8081) to the CF app: cf curl /v2/apps/[APP_GUID] -X PUT -d '{"ports": [8080, 8081]}'
  4. Now the route (e.g. in this case the HTTP route) which points to the CF app must also be adjusted so that it points to the correct CF app port. First you need to get the route information, you can do it with cf curl /v2/routes?q=host:[HOST_NAME] or with cf curl /v2/apps/[APP_GUID]/routes and save the guid of the route that points to your app ([ROUTE_GUID]).
  5. For this particular route you have to adjust the route mappings. Each CF route can have multiple route mappings. You can show current route mappings for a route with this command: cf curl /v2/routes/[ROUTE_GUID]/route_mappings. With cf curl /v2/route_mappings -X POST -d '{"app_guid": "[APP_GUID]", "route_guid": "[ROUTE_GUID]", "app_port": 8081}' you add a mapping to a route (e.g. here to 8081).
  6. The route has now two mappings, one pointing to 8080 and one pointing to 8081. If you want the route to only point to one of the ports (e.g. 8081) you have to delete the mapping with the port you do not want to have. Run cf curl /v2/routes/[ROUTE_GUID]/route_mappings to show all route mappings. Then extract the guid of the route mapping that should be deleted (e.g. the one to port 8080). Finally, run cf curl /v2/route_mappings/[GUID_ROUTE_MAPPING] -X DELETE to delete the route mapping you do not need.

Now your CF app should be reachable on another port than 8080 when the newly configured route is used.

like image 193
maxeh Avatar answered Oct 22 '22 15:10

maxeh


Currently an application on Cloud Foundry is not able to have two ports mapped into its container environment. As part of the new Diego runtime, multiple port mappings has been exposed, but is not currently available through the API.

Depending on what you need, you could take a look at Lattice, which uses the Diego runtime. Some documentation can be found here.

like image 26
crhino Avatar answered Oct 22 '22 13:10

crhino


Cloud Foundry will route TCP/WebSocket traffic coming from 80/443 to the one assigned port. Your application can not listen to any other port.

https://docs.cloudfoundry.org/devguide/deploy-apps/prepare-to-deploy.html#ports

You can either create multiple url mappings, or have two applications that communicate with each other using a messaging or database service.

like image 2
Ram Vennam Avatar answered Oct 22 '22 14:10

Ram Vennam


Resurrecting an old question, but this is supported in Cloud Foundry now. Support was added around April 2019. Check your version to see if it supports this.

The general process is:

  • Use cf cli to update your app to list all the ports it listens on
  • Update each route to the app with the specific port that the route should use. If you have two ports, you'll need two or more routes one port per route.
  • Restart the app

Right now you have to use cf curl to manually update these records. Instructions can be found here: https://docs.cloudfoundry.org/devguide/custom-ports.html. Hopefully future cf cli versions make this easier.

like image 1
Daniel Mikusa Avatar answered Oct 22 '22 15:10

Daniel Mikusa