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

   

   

再次访问站点内容

   

   

转载于:https://www.cnblogs.com/liuyuhua/p/5944976.html

评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值