consul+confd实现动态负载均衡
image.pngconsul
docker run -d -p 8400:8400 -p 8500:8500 -p 8600:53/udp
-h cookbook progrium/consul -server
-bootstrap -ui-dir /ui
registrator
docker run -d -v /var/run/docker.sock:/tmp/docker.sock
-h 192.168.33.10 gliderlabs/registrator
-ip 192.168.33.10 consul://192.168.33.10:8500/elb
docker run -d -p 5001:5000 runseb/hostname
docker run -d -p 5002:5000 runseb/hostname
nginx
events {
worker_connections 1024;
}
http {
upstream elb {
server 192.168.33.10:5001;
server 192.168.33.10:5002;
}
server {
listen 80;
location / {
proxy_pass http://elb;
}
}
}
nginx container
docker run -d -p 80:80 -v /home/vagrant/nginx.conf:/etc/nginx/nginx.conf
--name elb nginx
加入confd
sudo mkdir -p /etc/confd/{conf.d,templates}
在/etc/confd/conf.d/config.toml
[template]
src = "config.conf.tmpl"
dest = "/home/vagrant/nginx.conf"
keys = [
"/elb/hostname",
]
reload_cmd = "docker restart elb"
/etc/confd/templates/config.conf.tmpl
events {
worker_connections 1024;
}
http {
upstream elb {
{{range getvs "/elb/hostname/*"}}
server {{.}};
{{end}}
}
server {
listen 80;
location / {
proxy_pass http://elb;
}
}
}
./confd -onetime -backend consul -node 192.168.33.10:8500
docker run -d -p 5003:5000 runseb/hostname
./confd -onetime -backend consul -node 192.168.33.10:8500
./confd -backend consul -interval 5 -node 192.168.33.10:8500
网友评论