Docker Compose 部署 nginx 代理 Tomcat 集群

Docker Compose 部署 nginx 代理 Tomcat 集群

  • 备注:nginx 目录下的 nginx-1.12.2.tar.gz 文件 和 tomcat 目录下的 apache-tomcat-8.5.40.tar.gz 和 jdk-8u211-linux-x64.tar.gz 文件 需要自己下载

1、简介

  • 使用Docker镜像部署Nginx代理的多个Tomcat集群:
    • 使用 Dockerfile 构建 nginx 镜像
    • 使用 Dockerfile 构建 tomcat 镜像
    • mysql 镜像使用 docker hub 镜像
1、结构
├── docker-compose.yaml
├── etc
│   └── localtime
├── mysql
│   ├── conf
│   │   └── my.cnf
│   └── mysqldb
├── nginx
│   ├── conf.d
│   │   └── test.conf
│   ├── Dockerfile
│   ├── logs
│   │   ├── error.log
│   │   ├── test_access.log
│   │   └── test_error.log
│   ├── nginx-1.12.2.tar.gz
│   └── nginx.conf
├── tomcat
│   ├── apache-tomcat-8.5.42.tar.gz
│   ├── Dockerfile
│   ├── jdk-8u201-linux-x64.tar.gz
│   └── server.xml
└── webapps
    └── ROOT
        └── index.jsp

[root@docker01 ~]# mkdir -p nginx-tomcat/{etc,mysql,nginx,tomcat,webapps}
  • webapps 用于放置tomcat项目使用

  • mysqldb 数据将会存放在 mysql/mysqldb中

2、部署和构建

localtime


[root@compose ~]# cd /root/nginx-tomcat/e
[root@compose etc]# cp /etc/localtime .
my.cnf
[root@compose nginx-tomcat]# cd mysql/
[root@compose mysql]# ls
conf  mysqldb
[root@compose mysql]# cd conf/
[root@compose conf]# cat my.cnf 
[client]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set = utf8
[mysql]
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set = utf8
[mysqld]
port = 3306
basedir = /var/lib/mysql
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
character-set-server = utf8
skip-external-locking
slow-query-log = on
long_query_time = 1
slow_query_log_file = /var/lib/mysql/slow.log

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER

log-error=/var/lib/mysql/mysql.log
pid-file=/var/lib/mysql/mysql.pid
Nginx Dockerfile
FROM centos:7
MAINTAINER bigberg
RUN yum -y install pcre-devel openssl-devel net-tools gcc gcc-c++ zlib zlib-devel \
    make openssl
ADD nginx-1.12.2.tar.gz /tmp/
RUN cd /tmp/nginx-1.12.2 \
    && ./configure --prefix=/usr/local/nginx \
     --with-http_ssl_module \
     --with-http_gzip_static_module \
     --with-http_realip_module \
    && make && make install
RUN mkdir -p /usr/local/nginx/logs \
    && groupadd -g 1001 nginx \
    && useradd -g 1001 -u 1001 -s /sbin/nologin -M nginx

CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
nginx.conf
user  nginx;
worker_processes  1;

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

events {
    worker_connections  1024;
}

http {
    include       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;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain  text/javascript application/x-javascript text/css  text/xml image/jpg  application/xml  image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable        "Dalvik\.";
    gzip_disable "MSIE [1-6].(?!.*SV1)";

    include /usr/local/nginx/conf.d/*.conf;
}

[root@compose nginx]# cd conf.d/
[root@compose conf.d]# ls
test.conf
[root@compose conf.d]# cat test.conf 
upstream example {
   server tomcat01:8080;
   server tomcat02:8080;
   server tomcat03:8080;
}

server {
    listen  80;
    # listen  443 ssl;
    server_name  localhost;

    access_log  logs/test_access.log main;
    error_log   logs/test_error.log ;
    
    location / {
      proxy_pass http://example;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
    }
    
    location ~ \.(html|css|js|jpg|png|gif)$ {
       root /opt/webapps/ROOT;
    }
}
Tomcat Dockerfile
FROM centos:7
MAINTAINER bigberg

ADD jdk-8u201-linux-x64.tar.gz /usr/local
ENV JAVA_HOME /usr/local/jdk1.8.0_201
ADD apache-tomcat-8.5.42.tar.gz /usr/local
COPY server.xml /usr/local/apache-tomcat-8.5.42/conf/server.xml

EXPOSE 8080
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.42/bin/catalina.sh", "run"]
[root@compose tomcat]# cat server.xml 




  
  
  
  
  
  
  
  

  
  
    
    
  

  
  

    
    


    
    
    
    
    
    
    
    

    
    


    

    
    

      
      

      
      
        
        
      

      

        
        

        
        

      
    
  


3、docker-compose.yml
[root@compose nginx-tomcat]# cat docker-compose.yaml 
version: '3'
services:
  # web-server  
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    ports:
      - "80:80"
      - "443:443"
    links:
      - tomcat01:tomcat01
      - tomcat02:tomcat02
      - tomcat03:tomcat03
    volumes:
      - ./webapps:/opt/webapps
      - ./nginx/logs:/usr/local/nginx/logs
      - ./etc/localtime:/etc/localtime
      - ./nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro
      - ./nginx/conf.d/:/usr/local/nginx/conf.d/:ro
    depends_on:
      - mysql
      - tomcat01
      - tomcat02
      - tomcat03
  # tomcat
  tomcat01:
    hostname: tomcat01
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps
      - ./etc/localtime:/etc/localtime
    depends_on:
      - mysql
  tomcat02:
    hostname: tomcat02
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps
      - ./etc/localtime:/etc/localtime

  tomcat03:
    hostname: tomcat03
    build: ./tomcat
    links:
      -  mysql:mysql
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps
      - ./etc/localtime:/etc/localtime
    depends_on:
      - mysql
  # mysql
  mysql:
    hostname: mysql
    image: mysql:5.7
    ports:
     - "3307:3306"
    volumes:
      - ./mysql/conf/my.cnf:/etc/mysql/conf.d/my.cnf
      - ./mysql/mysqldb:/var/lib/mysql
      - ./etc/localtime:/etc/localtime:ro
    environment:
      - MYSQL_ROOT_PASSWORD=1234.com
4、编写index.jsp文件
[root@compose ~]# mkdir webapps/ROOT
[root@compose ~]# vim webapps/ROOT/index.jsp
nginx-tomcat cluster
5、构建
[root@compose ~]# docker-compose up
6、查看
docker-compose ps
         Name                       Command               State                    Ports                 
----------------------------------------------------------------------------------------------------------
nginxtomcat_mysql_1      docker-entrypoint.sh mysqld      Up      0.0.0.0:3307->3306/tcp                 
nginxtomcat_nginx_1      /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:443->443/tcp, 0.0.0.0:80->80/tcp
nginxtomcat_tomcat01_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp                               
nginxtomcat_tomcat02_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp                               
nginxtomcat_tomcat03_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-iKOo31G2-1603109456203)(assets/821560-20180306180111369-220329479-1573107950532.png)]

  • 备注:nginx 目录下的 nginx-1.12.2.tar.gz 文件 和 tomcat 目录下的 apache-tomcat-8.5.40.tar.gz 和 jdk-8u211-linux-x64.tar.gz 文件 需要自己下载

你可能感兴趣的:(实验+实战,nginx,tomcat,centos,docker,负载均衡)