Last active
August 29, 2015 14:12
-
-
Save huangsam/d3d9a9cb457d831bc92a to your computer and use it in GitHub Desktop.
Load balance Docker containers in a Vagrant VM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# lb-run.sh: Run load balancer on three app instances | |
# kill necessary containers | |
if [ "x$1" == "xkill" ] ; then | |
docker rm -f etcd app1 app2 app3 proxy | |
exit 0 | |
fi | |
# run etcd server | |
docker run -d -p 4001:4001 -p 7001:7001 \ | |
-v /var/etcd/:/data \ | |
--name etcd microbox/etcd \ | |
-name etcd | |
# run three stateless apps | |
docker run -d -p 5000 --name app1 training/webapp | |
docker run -d -p 5000 --name app2 training/webapp | |
docker run -d -p 5000 --name app3 training/webapp | |
# set etcd keys | |
etcdctl set app1 "$(docker inspect -f '{{.NetworkSettings.IPAddress}}' app1)" | |
etcdctl set app2 "$(docker inspect -f '{{.NetworkSettings.IPAddress}}' app2)" | |
etcdctl set app3 "$(docker inspect -f '{{.NetworkSettings.IPAddress}}' app3)" | |
# generate haproxy override config | |
confd -onetime | |
# run haproxy with override config | |
docker run -d -p 80:80 -v ~/override:/haproxy-override --name proxy dockerfile/haproxy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# lb-setup.sh: The setup required for load balancer | |
# install etcdctl | |
wget -O /tmp/etcd.tar.gz https://github.com/coreos/etcd/releases/download/v0.4.6/etcd-v0.4.6-linux-amd64.tar.gz | |
tar -xf /tmp/etcd.tar.gz -C /tmp | |
sudo mv /tmp/etcd*/etcdctl /usr/bin | |
rm -rf /tmp/* | |
# install confd | |
wget -O /tmp/confd https://github.com/kelseyhightower/confd/releases/download/v0.7.1/confd-0.7.1-linux-amd64 | |
chmod 755 /tmp/confd | |
sudo mv /tmp/confd /bin/confd | |
# setup confd directories | |
sudo mkdir -p /etc/confd/{conf.d,templates} | |
sudo chown -R vagrant:vagrant /etc/confd | |
# create confd config | |
cat > /etc/confd/conf.d/haproxy.toml <<eof | |
[template] | |
src = "haproxy.cfg.tmpl" | |
dest = "/home/vagrant/override/haproxy.cfg" | |
keys = [ | |
"/", | |
] | |
eof | |
# create confd template | |
cat > /etc/confd/templates/haproxy.cfg.tmpl <<eof | |
frontend train | |
timeout client 10s | |
bind *:80 | |
default_backend train-backend | |
backend train-backend | |
timeout connect 10s | |
timeout server 1m | |
balance roundrobin | |
{{range gets "/*"}}server {{base .Key}} {{.Value}}:5000 check | |
{{end}} | |
eof | |
# setup haproxy directory | |
mkdir -p ~/override |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment