在性能瓶颈的时候一台容器服务器远远达不到业务要求,在搜查资料后发现 docker 水平扩展刚好能解决部分需求,接下来聊一聊 docker-compose 中的 scale 命令
示例
1docker-compose up --scale web=5 -d
1,启动多容器,这里用nginx镜像实践;
通过scale 启动5个web服务节点(数量可以自行配置),依旧创建 docker-compose.yml 复制以下配置内容,使用traefik作为网关,不了解traefik可以问度娘。
1# ngixn_web.conf
2
3user nginx;
4 worker_processes 1;
5 error_log /var/log/nginx/error.log warn;
6 pid /var/run/nginx.pid;
7 events {
8 worker_connections 1024;
9 }
10
11
12 http {
13 include /etc/nginx/mime.types;
14 default_type application/octet-stream;
15 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
16 '$status $body_bytes_sent "$http_referer" '
17 '"$http_user_agent" "$http_x_forwarded_for"';
18 gzip on;
19
20 server {
21 listen 80;
22 server_name _;
23 server_tokens off;
24 access_log /var/log/nginx/docker-access.log;
25 error_log /var/log/nginx/docker-error.log;
26
27 location / {
28 root /usr/share/nginx/html;
29 index index.html index.htm;
30 }
31
32
33 }
34 access_log off;
35 sendfile on;
36 #tcp_nopush on;
37 keepalive_timeout 65;
38 }
1# docker-compose.yml
2
3version: "3.6"
4
5services:
6
7 web_nginx:
8 image: nginx:1.17.1-alpine
9 restart: always
10 expose:
11 - 80
12 volumes:
13 - ./local_logs:/var/log/nginx
14 - ./nginx_web.conf:/etc/nginx/nginx.conf
15 networks:
16 - traefik
17
18networks:
19 traefik:
20 external: true
1# 创建文件
2mkdir test_local
3
4# cd test_local
5touch nginx.conf && touch docker-compose.yml
6
7# 编辑文件(复制ngixn_web配置内容保存)
8vim ngixn_web.conf
9
10# 编辑文件(复制docker-compose配置内容保存)
11vim docker-compose.yml
12
13#启动容器
14
15docker-compose up --scale web_nginx=5 -d
16
启动成功后的截图 ,docker-compose ps 查看容器状态
启动后如何验证容器是否有效呢? 接下来使用Nginx搭建负载均衡来一起验证吧。
2,使用Nginx 搭建 Web_proxy 服务;
1# nginx_proxy.conf
2
3user nginx;
4worker_processes 1;
5error_log /var/log/nginx/error.log warn;
6pid /var/run/nginx.pid;
7events {
8 worker_connections 1024;
9}
10
11http {
12 include /etc/nginx/mime.types;
13 default_type application/octet-stream;
14 log_format main '$remote_addr - $remote_user [$time_local] "$request" '
15 '$status $body_bytes_sent "$http_referer" '
16 '"$http_user_agent" "$http_x_forwarded_for"';
17 gzip on;
18 # 这里使用水平扩展后的容器作为服务
19 upstream localserver{
20 server test_local_web_nginx_1;
21 server test_local_web_nginx_2;
22 server test_local_web_nginx_3;
23 server test_local_web_nginx_4;
24 server test_local_web_nginx_5;
25 }
26
27 server {
28 listen 80;
29 server_name _;
30 server_tokens off;
31 access_log /var/log/nginx/docker-access.log;
32 error_log /var/log/nginx/docker-error.log;
33
34 location / {
35 proxy_pass http://localserver;
36 }
37
38 }
39 access_log off;
40 sendfile on;
41 #tcp_nopush on;
42 keepalive_timeout 65;
43}
1# docker-compose.yml
2
3version: "3.6"
4
5services:
6
7 web_proxy:
8 container_name: web_proxy
9 image: nginx:1.17.1-alpine
10 restart: always
11 expose:
12 - 80
13 volumes:
14 - ./logs:/var/log/nginx
15 - ./nginx_proxy.conf:/etc/nginx/nginx.conf
16 networks:
17 - traefik
18 labels:
19 - "traefik.enable=true"
20 - "traefik.docker.network=traefik"
21 - "traefik.http.routers.web_proxy.entrypoints=http"
22 - "traefik.http.routers.web_proxy.rule=Host(`web_proxy.halobu.cn`)"
23 - "traefik.http.services.web_proxy-backend.loadbalancer.server.scheme=http"
24 - "traefik.http.services.web_proxy-backend.loadbalancer.server.port=80"
25 logging:
26 driver: "json-file"
27 options:
28 max-size: "10m"
29
30networks:
31 traefik:
32 external: true
1# 创建文件
2mkdir proxy_local
3
4# cd proxy_local
5touch ngixn_proxy.conf && touch docker-compose.yml
6
7# 编辑文件(复制ngixn_proxy配置内容保存)
8vim ngixn_proxy.conf
9
10# 编辑文件(复制docker-compose配置内容保存)
11vim docker-compose.yml
12
13#启动容器
14
15docker-compose up -d
16
启动成功后的截图 ,docker-compose ps 查看容器状态
接下来 访问 web_proxy.halobu.cn,成功截图 (需要绑定hosts)
接下来修改web容器的工作文件,分别进入容器 修改html内容便于区分是否生效
1docker exec -it test_local_web_nginx_1 sh
2docker exec -it test_local_web_nginx_2 sh
3docker exec -it test_local_web_nginx_3 sh
4docker exec -it test_local_web_nginx_4 sh
5docker exec -it test_local_web_nginx_5 sh
6
7vi /usr/share/nginx/html/index.html
8
修改成功后 刷新浏览器会看到这就完成了!
为了方便测试docker-compose这里我创建了不同的工作目录,可合并使用;
1# ngixn_proxy.conf ngixn_web.conf 需要分开创建;
2
3version: "3.6"
4
5 services:
6 # web_proxy
7 web_proxy:
8 container_name: web_proxy
9 image: nginx:1.17.1-alpine
10 restart: always
11 expose:
12 - 80
13 volumes:
14 - ./logs:/var/log/nginx
15 - ./nginx_proxy.conf:/etc/nginx/nginx.conf
16 networks:
17 - traefik
18 labels:
19 - "traefik.enable=true"
20 - "traefik.docker.network=traefik"
21 - "traefik.http.routers.web_proxy.entrypoints=http"
22 - "traefik.http.routers.web_proxy.rule=Host(`web_proxy.halobu.cn`)"
23 - "traefik.http.services.web_proxy-backend.loadbalancer.server.scheme=http"
24 - "traefik.http.services.web_proxy-backend.loadbalancer.server.port=80"
25 logging:
26 driver: "json-file"
27 options:
28 max-size: "10m"
29 #web_nginx
30 web_nginx:
31 image: nginx:1.17.1-alpine
32 restart: always
33 expose:
34 - 80
35 volumes:
36 - ./local_logs:/var/log/nginx
37 - ./nginx_web.conf:/etc/nginx/nginx.conf
38 networks:
39 - traefik
40
41 networks:
42 traefik:
43 external: true
启动(使用合并后的配置文件启动前)
# 分别进入 proxy_local,test_local目录关闭容器避免冲突
docker-compose down
# 启动
docker-compose up --scale web_nginx=5 -d
启动后验证不出意外是相同的效果;
课外,traefik 负载均衡
课外,知识HAProxy;有时间可以操作下
简介:HAProxy提供高可用性、负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费、快速并且可靠的一种解决方案。HAProxy特别适用于那些负载特大的web站点, 这些站点通常又需要会话保持或七层处理。HAProxy运行在当前的硬件上,完全可以支持数以万计的并发连接。并且它的运行模式使得它可以很简单安全的整合进您当前的架构中, 同时可以保护你的web服务器不被暴露到网络上。