在负载平衡器后面部署Docker容器
我有一个负载平衡器后面的Docker容器部署的问题。
这是我的docker撰写yml文件的内容。
nginx: image: nginx_image links: - node1:node1 - node2:node2 - node3:node3 ports: - "80:80" node1: image: nodeapi_image ports: - "8001" node2: image: nodeapi_image ports: - "8001" node3: image: nodeapi_image ports: - "8001"
在这里我的nginx.conf
worker_processes 4; events { worker_connections 1024; } http { upstream node-app { least_conn; server node1:8001 weight=10 max_fails=3 fail_timeout=30s; server node2:8001 weight=10 max_fails=3 fail_timeout=30s; server node3:8001 weight=10 max_fails=3 fail_timeout=30s; } server { listen 80; listen 443 ssl; # ssl on; ssl_certificate /etc/nginx/ssl/imago.io.chain.crt; ssl_certificate_key /etc/nginx/ssl/imago.io.key; location / { proxy_pass http://node-app; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } }
如果我有一个新的构build映像,我想部署我必须停止节点容器,删除它,并重新创build与新的形象。 这里的问题是,新的容器会得到一个新的IP和nginx容器不知道这个新的IP,所以如果我重新创build负载平衡器后面的3个容器,一旦我重新创build最后一个应用程序将不会再服务,因为所有的IP在nginx机器上的/etc/hosts
和环境variables不再是最新的。
我可以通过SSH进入每个容器,通过从git仓库中取出来更新代码,然后重启进程,但这对我来说似乎是错误的。 什么是正确的方法来做到这一点?
有一个更简单的方法来实现这个,以下面的docker-compose.yml文件为例:
lb: image: tutum/haproxy links: - app ports: - "80:80" app: image: tutum/hello-world
这个docker组成文件描述了两个服务:
- lb:使用tutum / haproxy映像的负载均衡器
- 应用程序:在端口80上侦听的示例webapp
如果你用docker-compose up -d
天真地启动这些服务,最终只有2个容器(负载均衡器和web应用程序)。
但是如果你运行docker-compose scale app=3
然后再次运行docker-compose up -d
,那么你最终会得到4个负载平衡的容器。
这里的关键angular色是tutum/haproxy
泊坞窗图像,它能够发现它所链接的不同容器。
类似的解决scheme是使用Jason Wilder的nginx代理映像,它具有发现新节点的优势。 所以你不必重新启动lb
服务。
lb: image: jwilder/nginx-proxy volumes: - /var/run/docker.sock:/tmp/docker.sock:ro ports: - "80:80" app: image: tutum/hello-world environment: VIRTUAL_HOST: www.mysite.com
VIRTUAL_HOST
环境variables必须设置为parsing为docker主机IP地址的域名。