cabrinoob / rancher-registrator

A registrator-like made for Consul & Rancher
15 stars 16 forks source link

Failure when using Rancher's random port mapping #10

Open davidnortonjr opened 7 years ago

davidnortonjr commented 7 years ago

With newer versions of Rancher (we are running 1.5.1), using a port mapping such as 80/tcp causes Rancher to choose a random port for each container. This is actually very helpful, but it causes the following exception

Fri May 12 2017 14:33:43 GMT+0000 (UTC) - container start pipeline-sample-aws-app-1 (image : ----)
TypeError: Cannot read property 'split' of undefined
    at /usr/src/app/index.js:224:50
    at Array.forEach (native)
    at /usr/src/app/index.js:222:38
    at checkForPortMapping (/usr/src/app/index.js:216:12)
    at process._tickCallback (internal/process/next_tick.js:103:7)
undefined

It looks like it can't split 80/tcp.

Do you think there's any way to get the actual exposed public port from the Rancher metadata rather than from the stack definition? This is what the docker-compose looks like, after clicking View Config on the Rancher UI.

version: '2'
services:
  app:
    image: nginx:latest
    dns:
    - 172.17.0.1:8600
    ports:
    - 80/tcp
cabrinoob commented 7 years ago

Can you tell me how do you declare your service in rancher? Via UI or via a docker-compose.yml file? Because, when I create a new stack using a docker-compose file with a service which have the 80/tcp port mapping (so, rancher will choose a public port for us), I cannot reproduce your bug. I'am running rancher v1.6.0

edited: And adding a new service via the rancher UI without public port is working fine too. No error message from rancher-registrator and my service appears in consul..

as you can see, in consul I can retrieve my 2 test services with the dynamic ports (58817 and 62171) : image

Can you try to upgrade to the rancher 1.6.0 ?

pascalbayer commented 7 years ago

This still happens sometimes in Rancher 1.6.6

2.8.2017 18:57:59Wed Aug 02 2017 16:57:59 GMT+0000 (UTC) - container start node-node-1 (image : nginx:latest)
2.8.2017 18:57:59TypeError: Cannot read property 'split' of undefined
2.8.2017 18:57:59    at /usr/src/app/index.js:224:50
2.8.2017 18:57:59    at Array.forEach (native)
2.8.2017 18:57:59    at /usr/src/app/index.js:222:38
2.8.2017 18:57:59    at checkForPortMapping (/usr/src/app/index.js:216:12)
2.8.2017 18:57:59    at process._tickCallback (internal/process/next_tick.js:103:7)
2.8.2017 18:57:59undefined

but works most of the times, so I don't know how to exactly reproduce the error

2.8.2017 18:58:53Wed Aug 02 2017 16:58:53 GMT+0000 (UTC) - container stop node-node-1 (image : nginx)
2.8.2017 18:58:53[]
2.8.2017 18:58:56Wed Aug 02 2017 16:58:56 GMT+0000 (UTC) - container start node-node-1 (image : nginx:latest)
2.8.2017 18:58:56No port mappings for node-node-1
2.8.2017 18:58:56undefined

However I've the same issue that the randomly assigned ports aren't detected by rancher-registrator. Nginx docker container exposes port 80 by defaut also I set the following config:

version: '2'
services:
  node:
    image: nginx:latest
    environment:
      SERVICE_80_NAME: nginx
    stdin_open: true
    tty: true
    ports:
    - 80/tcp
    labels:
      io.rancher.container.pull_image: always

First of all it doesn't detect the port and second doesn't change the service name in consul correctly, but that might be a chained error. If I can support with any additional information, please let me know. Great work by the way 👍

davidnortonjr commented 7 years ago

I think what happened was, I first created the stack via rancher-compose without any exposed ports. Then I updated the docker-compose.yml with a port as above (- 80/tcp). Rancher in this case kept it as just 80/tcp without assigning a fixed public port - and each subsequent launch of a container gets a random port.