This demon do following operation in order:
-
Get list of running containers and identify app containers using specified regex pattern
-
Generate loadbalancer(nginx) config file with found app containers as backends(upstream servers)
-
Listen to docker events for any 'start' or 'stop' events.
If any app container is created or stopped, it then add or remove this entry respectively from load balancer configuration, update configuration file and restart nginx . Before updating config file, it takes backup of old configuration file
- golang (preferrably ~ 1.3)
#Steps to build
git clone project
go build main.go
mv main backendUpdater
D:\tmp\go\tryOne>main --help
Usage:
-bkp_dir="": file backup directory(Optional)
-config="": Configuration file path(*required) eg. /etc/nginx/sites-enabled/default
-dockerAddr="0.0.0.0:4243": docker bind addresss(Optional)
-heartbeat=30s: heartbeat interval for containers check. eg 30s , 5m, 30m (Optional)
-host="127.0.0.1": hostname or IP to attach to containers(optional)
-service="": Service Pattern to track(*required)
-since=1412355983: docker events from whence(optional)
-template="loadbalancer.conf": template file(required)
- bkp_dir : Directory to store old config files
- dockerAdd : Address to listen for docker remote api
- exportfile : Path to export config file (usually sites-enabled folder)
- hearteat : Interval to wait before updating file
- host : IP Address to attach in backend server list . PORT will be populated from container public port information
- service : Regex service pattern to find app containers
- since : To track events since XXX
- template : load balancer config template
./main -dockerAddr http://199.127.219.76:4243 -service "dailyreport[0-9]*.stackexpress.com" -config "/etc/nginx/sites-enabled/loadbalancer.conf" -heartbeat 30s
A simple template for init scripts that provide the start, stop, restart and status commands.
Copy init to /etc/init.d and rename it to something meaningful. Then edit the script and enter that name after Provides: (between ### BEGIN INIT INFO and ### END INIT INFO).
Now set the following three variables in the script:
The working directory of your process.
The user that should execute the command.
The command line to start the process.
Here's an example for an app called backendUpdater
... Provides: backendUpdater_init
...
dir="/opt/apps/backendUpdater"
user="root"
cmd='/var/apps/backendUpdater/main -dockerAddr http://127.0.0.1:4243 -service "dailyReport[0-9]*.stackepress.com" -config "default" '
# copy Init script to /etc/init.d/
cp init /etc/init.d/backendUpdater_init
Starts the app.
/etc/init.d/backendUpdater_init start
Stops the app.
/etc/init.d/backendUpdater_init stop
Restarts the app.
/etc/init.d/backendUpdater_init restart
Tells you whether the app is running. Exits with 0 if it is and 1 otherwise.
/etc/init.d/backendUpdater_init status
By default, standard output goes to /var/log/scriptname.log and
error output to /var/log/scriptname.err. If you're not happy with
that, change the variables stdout_log
and stderr_log
.
Goals before writing code
-
Keep update( config file) and restart(nginx ) operation to minimum
i.e If many docker events are triggered in short interval, then update only after populating results from all events. This update interval is customizale with
heartbeat
command arguments.- It'll wait for specified
heartbeat
interval. In this all docker events will be queued. After that each event is processed - Summary of all queued event is populated into Final Updated Container list
- If app container list is changed, then only loadbalancer config file is updated and nginx restarted, else nothing happened
- It'll wait for specified
- Backends healthcheck
- Remove unhealthy backends