Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stable container name for the web service #1091

Closed
miharekar opened this issue Oct 10, 2024 · 5 comments
Closed

Stable container name for the web service #1091

miharekar opened this issue Oct 10, 2024 · 5 comments

Comments

@miharekar
Copy link

Use case: I'd like to connect from Prometheus to my web service without going through the internet.

I can connect to other accessories like [service-name]-cadvisor:9200 but [service-name]-web:80 goes through Kamal Proxy and that one rejects it, because it doesn't recognize the host.

The actual container name of the web service changes with each deploy, so I can't really hardcode it in prometheus' config.yml.

Any other suggestions you might have of how I might achieve that?

@askadityapandey
Copy link

Docker network alias, umm could be a good one?

@djmb
Copy link
Collaborator

djmb commented Oct 10, 2024

We run otel_collector as an accessory and connect prometheus to it. The collector is configured to pick up the app containers using docker discovery.

There's more info here - https://dev.37signals.com/kamal-prometheus/

@dhh dhh closed this as completed Oct 10, 2024
@jonasbark
Copy link

jonasbark commented Oct 29, 2024

I'm really not sure why this ticket was closed, as I've got the same issue and I don't think this is properly covered.

docker network inspect kamal will indeed tell you which endpoint can be used:

...
"f490b65d1ab7622c6a307c50a8a5144db41a55c3343582685c8726f5322de97d": {
                "Name": "cloudconnect-web-efd125f9a55a6f33627761e48ddf907031ee16bb",
...

but as @miharekar noted that name will change with each git commit.

docker network disconnect kamal f490b65d1ab7622c6a307c50a8a5144db41a55c3343582685c8726f5322de97d
docker network connect --alias cloudconnect kamal f490b65d1ab7622c6a307c50a8a5144db41a55c3343582685c8726f5322de97

Will work indeed, but I have not seen this option exposed.

An example post-deploy hook:

CONTAINER_ID=$(kamal server exec -q docker ps --filter "ancestor=ghcr.io/..." --format "{{.ID}}" | tail -n +2)
echo "Container ID: $CONTAINER_ID"
kamal server exec docker network disconnect kamal "$CONTAINER_ID";
kamal server exec docker network connect --alias cloudconnect kamal "$CONTAINER_ID";

@huseyin
Copy link

huseyin commented Oct 29, 2024

I ran into the same issue when setting up Prometheus without otel collector and solved it adding a post-deploy hook that adds an alias on every deploy (something similar to @jonasbark's approach). That said, I don’t think this case is unique to Prometheus. We could be emitting anything through any port with a listener consuming it, and in all those cases, we should be able to connect to the container using a stable hostname. Having an option to add aliases right in the config would indeed be good.

@pjpires
Copy link

pjpires commented Nov 25, 2024

An example post-deploy hook:

CONTAINER_ID=$(kamal server exec -q docker ps --filter "ancestor=ghcr.io/..." --format "{{.ID}}" | tail -n +2)
echo "Container ID: $CONTAINER_ID"
kamal server exec docker network disconnect kamal "$CONTAINER_ID";
kamal server exec docker network connect --alias cloudconnect kamal "$CONTAINER_ID";

@jonasbark this was super useful for me to be able to get an "accessory" (anycable-go) to connect with a service (RPC) running as a "server" (in kamal's terms), thank you very much! 🙏

However, in the end I think I found a simpler solution, so I'll leave it here in case someone finds it useful. Since kamal forwards the options: to the underlying docker run ... command, I was able to do this:

servers:
  anycable-rpc:
    hosts: <%= ENV['HOST'] %>
    options:
      network-alias: my-app-anycable-rpc

accessories:
  anycable-go:
    # ... other configs
    env:
      clear:
        ANYCABLE_RPC_HOST: "my-app-anycable-rpc:50051"

Note: I initially tried the name option instead, it's still forwarded to docker run but would make kamal fail in a subsequent docker container ls command as it is expecting to find the container by a different name.

Edit: fixed the example with the proper hosts: key, as pointed out in #1278 (comment) (thanks!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants