I’m working on a way to automate the creation of new hosts on demand. So if someone wants to run
a container or group of containers, but there are no resources available, I’d
like to automatically create a new host, then honor their request. Also, if I have a host is sitting around idle for too long,
I’d like to automatically terminate it and free up the resources.
I don’t know how practical this will be, but I’d like to know if Rancher has any hooks in place to facilitate this sort of thing.
There are no hooks for these, but deleting hosts that have no containers could be done through the API and would be relatively straightforward I would think.
- Poll
GET /v1/hosts
- Follow the
instances
link on each host to get the containers - If there are no containers (you’ll want to ignore ones with
"systemContainer": "NetworkAgent"
), note the time somewhere. - Rinse, lather, repeat. If there have been no containers for long enough, call
DELETE
on thephysicalHost
link for the host (assuming these hosts were created through Rancher/docker-machine.)
Detecting when to add is more difficult… There’s not anything obvious to detect why scheduling a service container fails, or if adding a host would improve the situation. e.g. there could be no host with port 80 available so adding another host might address that, or there might be a global service that uses port 80 and it will still be unavailable if you add one.