Caddy api to remove reverse proxy hosts

Hi,

I am using Caddy 2 on Windows Server 2019. I love the power of Caddy!

However, I have a question. I am a DevOps guy and using Caddy to load balance an application. In my release process, I essentially want to drain a node from Caddy and the nrelease to the server and restart it.

The caddy API is not granular in that it can remove/add nodes. Is the only way to do this to supply an updated config file? So essentially, I use c# or powershell to modify the config file (remove the node), save it, and then upload that?

Thanks

1 Like

You can actually amend the array without having the POST full new config, just PATCH the respective path. Your API request can traverse the config JSON to make amendments or GET specific values. See the doc about PATCH, PUT, and DELETE here:

1 Like

ah yes, that’d be easier! Also, caddy can use an adapter to convert caddyfile to json right?

My last question, regarding the load balancer. When I remove a node from the reverse proxy config, are connections then moved to the remaining nodes?

The Caddyfile config format is an adapter to JSON. Caddy always adapts your Caddyfile to JSON when this when you run it with a Caddyfile as input.

You can use the caddy adapt command to explicitly convert from Caddyfile to JSON.

Not exactly. Any config changes trigger a full graceful reload in Caddy. See this page in the docs for details: Architecture — Caddy Documentation

If you have any long-running connections (like websockets, or long-polling/SSE, or downloads that take a long time) then the reload will hang until these are done (in the case of websockets, I don’t think it’ll ever be “done” so it may hang indefinitely) unless you set the grace_period config option which is a timeout after which Caddy will explicitly cancel any remaining connections to allow the shutdown of the old config to complete.

Currently, yes that’s the only way.

But we’re working on a rewrite of the reverse_proxy module to add support for “dynamic upstream” modules. This might make it possible to implement a module that can provide an admin API endpoint to add/remove nodes. We won’t have plans for such a module to be included in Caddy’s standard distribution yet, but it might come later. A third party plugin is possible.

Thanks.

On the reloading methodology, that is ok. The apps I am using don’t use web sockets, but anything is possible.

I have built a cicd process for my caddy files, so my build server does a validate on them and then they get pushed to my deployment tool and dropped onto a folder. I don’t want to put ip addresses in source control so I may use variable substitution for the ip addresses, and then my deployment tool can put in the hosts. I may go this way with editing the file as opposed to using the API.

1 Like

This topic was automatically closed after 30 days. New replies are no longer allowed.