深入浅出Docker技术- 快速构建Nginx Web负载均衡

image.png

环境准备:

    docker/centos7

实例机器:
     192.168.1.102
本地虚拟机

资料准备:

    https://gitee.com/gz-tony/viemall-dubbo/tree/master/viemall-docekr/compose/nginx-tomcat

官网资料:

     https://hub.docker.com/_/nginx/

概括:

  使用三个容器技术,一个容器部署Nginx, 80端口,一个部署WebA,一个部署WebB, Nginx 这项技术不是很懂的可以看我之前的系列文章;这里不细讲!!!!!


方式一:

   采用传统的docker命令,一个一个的容器部署,dockerfile 这里我就不编写,采用官网的docker文件;

    因为docker run 的默认网络是bridge模式, bridge模式是docker的默认网络模式,容器与容器之间是不互通的,那么我们需要容器共享一个 Network Namespace,所以创建的时候使用自定义网络;

 1.新建docker网络

    docker network create viemall

    docker network ls

 2.启动TomcatATomcatB服务:

docker run  -d  -p 8080:8080 --net=viemall --name=viemall-tomcat-a --restart=always  -e TZ="Asia/Shanghai" \
 -v /opt/docker/tomcat/web-a/ROOT:/usr/local/tomcat/webapps/ROOT \
 -v /opt/docker/tomcat/web-a/logs:/usr/local/tomcat/logs \
 tomcat:8.0.51-jre8-slim
 docker run  -d  -p 8081:8080 --net=viemall --name=viemall-tomcat-b --restart=always  -e TZ="Asia/Shanghai" \
 -v /opt/docker/tomcat/web-b/ROOT:/usr/local/tomcat/webapps/ROOT \
 -v /opt/docker/tomcat/web-b/logs:/usr/local/tomcat/logs \
 tomcat:8.0.51-jre8-slim

3.启动Nginx

docker run  -d  -p 80:80 --net=viemall --name=viemall-nginx --restart=always  -e TZ="Asia/Shanghai" \
  -v /opt/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
  -v /opt/docker/nginx/logs:/var/log/nginx \
  -v /opt/docker/nginx/html:/usr/share/nginx/html \
 nginx:1.14-alpine

测试访问:

  http://192.168.1.102/


  image.pngimage.png


方式二: docker compose

   docker composedocker编排服务的一部分,可以让用户在其他平台上快速的安装docker,一键就部署成功了;

  1.安装docker compose

       参考文章:   http://www.dczou.com/viemall/808.html

  2.docker compose官方文档:

        https://docs.docker.com/compose/compose-file/

 3. docker compose文件编写:

version: '3'
services:
  nginx:
    image: nginx:1.14-alpine 
    ports:
      - "80:80"
    container_name: viemall-nginx
    restart: always
    volumes:
      - /opt/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - /opt/docker/nginx/html:/usr/share/nginx/html
      - /opt/docker/nginx/logs:/var/log/nginx
    environment:
      TZ: Asia/Shanghai
    depends_on:
      - viemall-tomcat-A
      - viemall-tomcat-B
    networks:
      - "viemall-net"
  viemall-tomcat-A:
    image: tomcat:8.0.51-jre8-slim
    container_name: viemall-tomcat-a
    ports:
      - "8080:8080"
    restart: always
    volumes:
      - /opt/docker/tomcat/web-a/ROOT:/usr/local/tomcat/webapps/ROOT
      - /opt/docker/tomcat/web-a/logs:/usr/local/tomcat/logs
    environment:
      TZ: Asia/Shanghai
      JAVA_OPTS: -Djava.security.egd=file:/dev/./urandom
    networks:
      - "viemall-net"
  viemall-tomcat-B:
    image: tomcat:8.0.51-jre8-slim
    container_name: viemall-tomcat-b
    ports:
      - "8081:8080"
    restart: always
    volumes:
      - /opt/docker/tomcat/web-b/ROOT:/usr/local/tomcat/webapps/ROOT
      - /opt/docker/tomcat/web-b/logs:/usr/local/tomcat/logs
    environment:
      TZ: Asia/Shanghai
      JAVA_OPTS: -Djava.security.egd=file:/dev/./urandom
    networks:
      - "viemall-net"
networks:
  viemall-net:
    driver: bridge

 4. docker compose 服务启动;

     docker-compose up -d


   查看compose启动的各个容器的状态:

docker-compose ps

   进入某个容器:

docker-compose exec viemall-tomcat-a bash

   退出某个容器

exit

   停止 docker compose启动的容器: 停止运行的容器而不删除它们,它们可以使用命令docker-compose start重新启动起来

docker-compose stop

 卸载docker compose启动的容器:

docker-compose down