docker容器化快速部署在日常开发中已经得到普遍使用,下面我们以nginx+tomcat为例来进行说明,如何在不同的系统环境中来快速将单节点应用做水平拓展和负载均衡。

单台服务器上应用做水平拓展、负载均衡。

  • 核心命令 docker-compose --scale

首先,编写docker应用编排文件

我们先编写tomcat的docker-compose.yml,内容如下:

version: '3.8'
services:     
  tomcat:
    hostname: tomcat
    image: tomcat:alpine
    container_name: "tomcat"
    deploy:
      resources:
        limits:
          cpus: '0.80'
          memory: 200M
        reservations: 
          memory: 100M
    ports:
      - "7080:8080"
    environment:
      - JAVA_OPTS=-server -Xms128m -Xmx128m -Djava.security.egd=file:/dev/./urandom
    restart: on-failure
    logging:
      driver: 'json-file'
      options:
        max-size: '5m'
        max-file: '1'

将此文件上传到安装了docker-compose的服务器上,并执行

docker-compose --compatibility up -d

docker ps

这里对容器的cpu、mem资源做了限制,需要加上 --compatibility,此时我们访问 http://{ip}:7080便会出现tomcat页面。此页面是tomcat页面。
执行docker ps命令查看服务
在这里插入图片描述

其次,加入nginx来做服务代理

tomcat、nginx完整的docker-compose.yml内容如下:

version: '3.8'
services:     
  nginx:
    image: nginx:alpine
    container_name: "nginx"
    deploy:
      resources:
        limits:
          cpus: '0.80'
          memory: 50M
        reservations: 
          memory: 20M
    ports:
      - "7080:80"
    restart: on-failure
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/conf.d:/etc/nginx/conf.d
    logging:
      driver: 'json-file'
      options:
        max-size: '5m'
        max-file: '1'  
    depends_on:
      - tomcat
     
  tomcat:
    hostname: tomcat
    image: tomcat:alpine
    container_name: "tomcat"
    deploy:
      resources:
        limits:
          cpus: '0.80'
          memory: 200M
        reservations: 
          memory: 100M
    environment:
      - JAVA_OPTS=-server -Xms128m -Xmx128m -Djava.security.egd=file:/dev/./urandom
    restart: on-failure
    logging:
      driver: 'json-file'
      options:
        max-size: '5m'
        max-file: '1' 

这里使用了nginx做代理,故无需暴露tomcat 服务端口。
将此文件覆盖起前面的文件,同时在同级目录创建如下2个nginx文件:
{docker-compose.yml目录}/nginx/nginx.conf


user  nginx;
worker_processes  4;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

{docker-compose.yml目录}/nginx/conf.d/00fly.conf

server {
    listen 80;
    
    location / {
        proxy_pass   http://tomcat:8080;
        client_max_body_size   5m;
    }

    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        root   /usr/share/nginx/html;
    }
} 

仔细观察
proxy_pass http://tomcat:8080;
这边的tomcat 必须与 docker-compose.yml的tomcat 服务命名一致。
同样执行

docker-compose --compatibility up -d

docker ps

我们会看到,docker有2个服务:
在这里插入图片描述
此时访问 http://{ip}:7080便会出现tomcat页面。
注意此时的页面实际是由nginx代理的tomcat 服务返回的页面。

最后,修改docker-compose文件,去掉多实例部署会导致冲突的container_name、ports 节点内容

删除掉下图所示行:
在这里插入图片描述
使用下面的命令来启动服务:

docker-compose --compatibility up -d --scale tomcat=5

会看到如下输出:
在这里插入图片描述
访问 http://{ip}:7080便会出现tomcat页面。

下面可以尝试运行如下命令:

docker-compose --compatibility up -d --scale tomcat=3
docker ps

docker-compose --compatibility up -d --scale tomcat=0
docker ps

之后访问http://{ip}:7080查看页面!

多个不同service水平扩容

示例 docker-compose.yml 文件

version: '3.8'
services:
  tomcat:
    hostname: tomcat
    image: tomcat:alpine
    deploy:
      resources:
        limits:
          cpus: '0.80'
          memory: 100M
        reservations:
          memory: 80M
    ports:
      - 8080
    environment:
      - JAVA_OPTS=-server -Xms128m -Xmx128m -Djava.security.egd=file:/dev/./urandom
    restart: on-failure
    logging:
      driver: 'json-file'
      options:
        max-size: '5m'
        max-file: '1'
  tomcat_node:
    hostname: tomcat_node
    image: tomcat:alpine
    deploy:
      resources:
        limits:
          cpus: '0.80'
          memory: 100M
        reservations:
          memory: 80M
    ports:
      - 8080
    environment:
      - JAVA_OPTS=-server -Xms128m -Xmx128m -Djava.security.egd=file:/dev/./urandom
    restart: on-failure
    logging:
      driver: 'json-file'
      options:
        max-size: '5m'
        max-file: '1'

启动脚本

docker-compose --compatibility up -d --scale tomcat=2 --scale tomcat_node=2

多台服务器之间的服务做水平拓展、负载均衡

  • 多台服务器之间的服务做水平拓展、负载均衡需要结合docker swarm来实现,参考资料较多。不再详细描述。仅提供编排文件 docker-stack.yml 和 脚本供感兴趣的童鞋研究!
version: '3'
services:
    nginx:
        image: nginx:alpine
        deploy:
          resources:
            limits:
              cpus: '0.80'
              memory: 50M
            reservations: 
              memory: 20M
        ports:
            - "10080:80"
        volumes:
             - ./nginx/nginx.conf:/etc/nginx/nginx.conf
             - ./nginx/conf.d:/etc/nginx/conf.d  
        logging:
            driver: 'json-file'
            options:
                max-size: '5m'
                max-file: '1'
        depends_on:
           - tomcat
    tomcat:
        image: tomcat:alpine
        deploy:
          replicas: 2
          resources:
            limits:
              cpus: '0.80'
              memory: 200M
            reservations: 
              memory: 100M
          update_config:
            parallelism: 2
            delay: 10s
          restart_policy:
            condition: on-failure
        environment:
            - JAVA_OPTS=-server -Xms128m -Xmx128m -Djava.security.egd=file:/dev/./urandom
        restart: on-failure
        logging:
            driver: 'json-file'
            options:
                max-size: '5m'
                max-file: '1' 

启动脚本

docker stack deploy -c docker-stack.yml stack

-完-

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐