Docker-compose 部署Nginx代理Tomcat集群

来自linux中国网wiki
docker>Evan2019年7月9日 (二) 12:18的版本 →‎see also
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳到导航 跳到搜索

compose install

目录结构

 tree compose_nginx_tomcat/
compose_nginx_tomcat/
|-- docker-compose.yml
|-- mysql
|   |-- conf
|   |   `-- my.cnf
|   `-- data
|-- nginx
|   |-- Dockerfile
|   |-- nginx-1.12.2.tar.gz
|   `-- nginx.conf
|-- tomcat
|   |-- apache-tomcat-8.5.42.tar.gz
|   |-- Dockerfile
|   |-- jdk-8u211-linux-x64.tar.gz
|   `-- server.xml
`-- webapps
    `-- ROOT
        `-- index.jsp

7 directories, 10 files

Nginx Compose

创建DockerCompose项目目录

mkdir compose_nginx_tomcat
cd compose_nginx_tomcat/

mkdir nginx
cd nginx

wget -c https://nginx.org/download/nginx-1.12.2.tar.gz

vi Dockerfile

## 指定镜像
FROM debian:stretch-slim
#FROM centos:6
# 指定管理员
MAINTAINER  linuxsa.org
# 执行命令安装编译库文件
RUN apt update && apt install -y gcc gcc g++ make openssl libssl-dev  libpcre3 libpcre3-dev 
#RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel

# RUN apt-get update && apt-get install -y --no-install-recommends g++ gcc libc6-dev make && rm -rf /var/lib/apt/lists*
# 添加解压nginx包到/tmp目录下
RUN mkdir -p /tmp
ADD nginx-1.12.2.tar.gz /tmp
# 不用什么tar 解压 ,进入目录进行编译安装
RUN cd /tmp/nginx-1.12.2 && ./configure --prefix=/usr/local/nginx --without-http_gzip_module  && make -j 2 && make install

# 删除容器内置配置文件
RUN rm -f /usr/local/nginx/conf/nginx.conf 
# 复制本地配置文件到容器内
COPY nginx.conf /usr/local/nginx/conf
# 声明暴露端口
EXPOSE 80
# 启动容器Nginx服务,指定全局命令daemon off保证服务在前台运行不会关闭
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]


vi nginx.conf #这个要优化一下
user  root; 
worker_processes  auto; 

error_log  logs/error.log  info;

pid        logs/nginx.pid; 


events {
    use epoll; 
}

http {

    include       mime.types;
    default_type  application/octet-stream;

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

    access_log logs/access.log main;
    sendfile        on;
    keepalive_timeout  65;
    
# 代理三台tomcat服务
    upstream www.example.com {
        #ip_hash;
        server tomcat01:8080;
    server tomcat02:8080;
    server tomcat03:8080;
    }

# 动静分离
    server {
        listen 80;
        server_name localhost;

# 动态请求转发给tomcat处理
    location / {
        proxy_pass http://www.example.com;
    }
# 静态资源请求交给nginx处理
        location ~ \.(html|css|js|jpg|png|gif)$ {
            root /opt/webapps/ROOT;
        }
    }
}

创建Mysql Compose


2.1、创建Mysql管理目录

mkdir mysql
mkdir  mysql/data
cd mysql
mkdir conf
cd conf

2.2、创建mysql配置文件

vi my.cnf #要优化 
[mysqld]
user=mysql
port=3306
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
pid-file=/var/run/mysqld/mysqld.pid
log_error=/var/log/mysql/error.log
character_set_server = utf8
max_connections=3600

创建Tomcat Compose


3.1、创建tomcat管理目录与网站目录

mkdir tomcat
mkdir -p webapps/ROOT/
cd tomcat

3.2、下载tomcat、jdk 压缩文件下载到本地
[root@work2 tomcat]# pwd
/root/compose_nginx_tomcat/tomcat
[root@work2 tomcat]# ls 
apache-tomcat-8.5.42.tar.gz  Dockerfile  jdk-8u211-linux-x64.tar.gz  server.xml


3.3 创建Dockerfile文件

vi Dockerfile
FROM debian:stretch-slim
# 指定管理员
MAINTAINER  linuxsa.org
# 解压jdk包到指定目录
ADD jdk-8u211-linux-x64.tar.gz /usr/local
# 安装jdk包到指定目录
ENV JAVA_HOME /usr/local/jdk1.8.0_211
# 解压tomcat包到指定目录
ADD apache-tomcat-8.5.42.tar.gz /usr/local
# 将本地配置文件复制到镜像内
COPY server.xml /usr/local/apache-tomcat-8.5.42/conf

# 指定服务暴露端口
EXPOSE 8080
# 启动tomcat服务
ENTRYPOINT ["/usr/local/apache-tomcat-8.5.42/bin/catalina.sh", "run"]

3.4 创建server.xml配置文件

vi server.xml

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="8005" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->


    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
    -->
    <Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL/TLS HTTP/1.1 Connector on port 8443
         This connector uses the NIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->
    <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />


    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->

        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log" suffix=".txt"
               pattern="%h %l %u %t "%r" %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>

创建docker-compose.yml

4.1、在compose_nginx_tomcat目录下创建docker-compose.yml

vi docker-compose.yml

# 指定服务版本号
version: '3'
# 服务
services:
# 服务名称
  nginx:
# 指定服务容器名字
    hostname: nginx
# 构建
    build:
# 指定目录上下文构建镜像
      context: ./nginx
# 指定dockerfile文件名称
      dockerfile: Dockerfile
# 映射数组级的端口
    ports:
      - 80:80
# 映射服务别名
    links:
      - tomcat01:tomcat01
      - tomcat02:tomcat02
      - tomcat03:tomcat03
# 映射服务数据卷路径
    volumes:
      - ./webapps:/opt/webapps
# 启动依赖,按顺序启动服务
    depends_on:
      - mysql
      - tomcat01
      - tomcat02
      - tomcat03

# 服务名称
  tomcat01:
# 指定服务容器名字
    hostname: tomcat01
# 指定目录上下文构建镜像
    build: ./tomcat
# 映射服务别名
    links:
      - mysql:mysql-db
# 映射服务数据卷路径  可见 有mount 后 只改变母机的不用rebuild 了 方便程序哥作自己的开发环境喽
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps

# 服务名称
  tomcat02:
# 指定服务容器名字
    hostname: tomcat02
# 指定目录上下文构建镜像
    build: ./tomcat
# 映射服务别名
    links:
      - mysql:mysql-db
# 映射服务数据卷路径
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps

# 服务名称
  tomcat03:
# 指定服务容器名字
    hostname: tomcat03
# 指定目录上下文构建镜像
    build: ./tomcat
# 映射服务别名
    links:
      - mysql:mysql-db
# 映射服务数据卷路径
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.5.42/webapps

# 服务名称
  mysql:
# 指定服务容器名字
    hostname: mysql
# 指定服务容器名字
    image: mysql:5.6
# 映射数组级的端口
    ports:
      - 3306:3306
# 映射服务数据卷路径
    volumes:
      - ./mysql/conf:/etc/mysql/conf.d
      - ./mysql/data:/var/lib/mysql
# 指定数据库变量
    environment:
# 设置数据库密码
      MYSQL_ROOT_PASSWORD: 123456
# 添加user用户
      MYSQL_USER: user
# 设置user用户密码
      MYSQL_PASSWORD: user123



web home 编写测试页面

[root@work2 compose_nginx_tomcat]# pwd
/root/compose_nginx_tomcat
tomcat]# mkdir -p webapps/ROOT/
[root@work2 compose_nginx_tomcat]# vi webapps/ROOT/index.jsp

java *********


run 执行dockerCompose 一键部署Nginx代理Tomcat集群

# pwd
/root/compose_nginx_tomcat

# 管理目录下compose_nginx_tomcat 执行该命令 -d 后台运行
docker-compose up -d

测试容器服务

5.1、查看启动状态终端输出

Creating compose_nginx_tomcat_mysql_1 ... done
Creating compose_nginx_tomcat_tomcat03_1 ... done
Creating compose_nginx_tomcat_tomcat02_1 ... done
Creating compose_nginx_tomcat_tomcat01_1 ... done
Creating compose_nginx_tomcat_nginx_1 ... done

#5.2、查看后台运行容器
 docker-compose ps
             Name                            Command               State           Ports         
-------------------------------------------------------------------------------------------------
compose_nginx_tomcat_mysql_1      docker-entrypoint.sh mysqld      Up      0.0.0.0:3306->3306/tcp
compose_nginx_tomcat_nginx_1      /usr/local/nginx/sbin/ngin ...   Up      0.0.0.0:80->80/tcp    
compose_nginx_tomcat_tomcat01_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp              
compose_nginx_tomcat_tomcat02_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp              
compose_nginx_tomcat_tomcat03_1   /usr/local/apache-tomcat-8 ...   Up      8080/tcp    

测试数据库

docker ps 
a00837c4cc53        mysql:5.6                       "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   compose_nginx_tomcat_mysql_1

1.
docker container exec -it a00837c4cc53 /bin/bash

mysql  -uroot -p'123456'

# 2、直接进入数据库,母机安装mysql client 但是不要安装mysql service 
mysql -h192.168.88.22 -uroot -p123456

浏览器测试nginx代理tomcat

打开浏览器 
evan@latop:~/docker$ curl   192.168.88.22
java **




# 1、进入nginx管理界面
docker container exec -it e7c9152c93ac /bin/bash

# 2、查看输出日志测试轮询代理
root@nginx:/# tail /usr/local/nginx/logs/access.log -f
172.19.0.4:8080 192.168.88.4 - - [08/Jul/2019:12:35:24 +0000] "GET / HTTP/1.1" 200 17 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
172.19.0.3:8080 192.168.88.4 - - [08/Jul/2019:12:35:25 +0000] "GET /favicon.ico HTTP/1.1" 404 1085 "http://192.168.88.22/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36" "-"
172.19.0.5:8080 192.168.88.4 - - [08/Jul/2019:12:35:52 +0000] "GET / HTTP/1.0" 200 17 "-" "w3m/0.5.3+git20190105" "-"
172.19.0.4:8080 192.168.88.4 - - [08/Jul/2019:12:35:58 +0000] "GET / HTTP/1.1" 200 17 "-" "curl/7.64.0" "-"

trouble shooting

1.
ERROR: error pulling image configuration: Get https://production.cloudflare.docker.com/registry-v2/docker/registry/v2/blobs/sha256/3e/3ed1080b793fc4a10cab741a04ce090caf1ad2932cbcc679b6587624af9f6157/data?verify=1562587126-TbaoEmAw%2B4kI5NP1AXHRChO0mBM%3D: read tcp 192.168.88.22:54860->104.18.121.25:443: read: connection reset by peer

nameserver 8.8.4.4

修改Docker配置文件/etc/default/docker如下:

DOCKER_OPTS="--registry-mirror=http://aad0405c.m.daocloud.io"
使用service docker restart重启Docker服务即可。

现在使用docker pull拉取镜像真是特别爽,速度很快,再次感谢DaoCloud公司


2. nginx 
E: Unable to locate package gcc
E: Unable to locate package gcc
E: Unable to locate package make
E: Unable to locate package openssl
E: Unable to locate package libssl-dev
E: Package 'libpcre3-dev' has no installation candidate
ERROR: Service 'nginx' failed to build: The command '/bin/sh -c apt install -y gcc gcc g++ make openssl libssl-dev  libpcre3 libpcre3-dev' returned a non-zero code: 100

要加apt update  

apt 可以改源的呢

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

3.
#注意
# 添加解压nginx包到/tmp目录下 自带了解压功能了哦 老是一时忘记
ADD nginx-1.12.1.tar.gz /tmp


debian nginx 
https://github.com/nginxinc/docker-nginx/tree/9a052e07b2c283df9960375ee40be50c5c462a7e/stable/stretch







see also

Docker Compose 一键部署Nginx代理Tomcat集群

docker-compose入门示例:一键部署 Nginx+Tomcat+Mysql

Docker Compose 运行 Tomcat 和Mysql

使用docker-compose定制Javaweb环境:tomcat、jre、mysql、activemq、redis

Docker Compose+nginx实现负载均衡 spring