首页 > 代码库 > Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架

Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架

安装所需软件

Docker

Docker-compose

   

配置docker-compose.yml文件内容如下:

#load balancer will automatically update the config using consul-template

lb:

image: yeasy/nginx-consul-template:latest

hostname: lb

volumes:

- /usr/soft/consul/logapi.conf:/etc/consul-templates/nginx.conf

links:

- consulserver:consul

ports:

- "80:80"

   

consulserver:

image: gliderlabs/consul-server:latest

hostname: consulserver

ports:

- "8300:8300"

- "8301:8301"

- "8301:8301/udp"

- "192.168.190.131:8302:8302"

- "192.168.190.131:8302:8302/udp"

- "192.168.190.131:8400:8400"

- "192.168.190.131:8500:8500"

- "53:53/udp"

- "53:53"

command: -data-dir /tmp/consul -bootstrap -advertise 192.168.190.131

   

registrator:

image: gliderlabs/registrator:latest

hostname: registrator

links:

- consulserver:consul

volumes:

- "/var/run/docker.sock:/tmp/docker.sock"

command: consul://consul:8500

   

logapi:

image: logapi-1-0-app

environment:

SERVICE_80_NAME: http

SERVICE_NAME: logapi

SERVICE_TAGS: backend

ports:

- "60000"

   

Nginx模板文件 /usr/soft/consul/logapi.conf内容如下:logapi名称与docker-compose.yml文件中logapi节点下的SERVICE_NAME名称相同

   

upstream app {

{{range service "logapi"}}server {{.Address}}:{{.Port}} max_fails=3 fail_timeout=60 weight=1;

{{else}}server 127.0.0.1:65535; # force a 502{{end}}

}

   

server {

listen 80 default_server;

   

location / {

proxy_pass http://app;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

}

   

转到docker-compose.yml文件所在目录

技术分享

运行sudo docker-compose up

   

技术分享

   

查看运行状态

技术分享

   

防火墙开启相应端口

32860端口为logapi-1-0-app容器对应的端口号

sudo firewall-cmd --zone=public --add-port=8500/tcp --permanent

sudo firewall-cmd --zone=public --add-port=32861-33000/tcp --permanent

sudo firewall-cmd --reload

   

查看网站内容返回如下:

curl http://192.168.190.131

{"status":1,"message":"","data":{"hostName":"fdbd2c30b6c4","ip":["172.17.0.2","172.17.0.2","172.17.0.2"]}}

   

开启2个站点

sudo docker-compose scale logapi=2

   

技术分享

   

再次访问站点内容

技术分享

   

技术分享

   

Centos 7 Docker、docker-compose、Registrator、Consul、Consul Template和Nginx实现高可扩展的Web框架