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

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

目录结构

[[email protected] ~]# tree compose_nginx_tomcat/
compose_nginx_tomcat/
├── docker-compose.yml
├── mysql
│   ├── conf
│   │   └── my.cnf
│   └── data
├── nginx
│   ├── Dockerfile
│   ├── nginx-1.12.1.tar.gz
│   └── nginx.conf
├── tomcat
│   ├── apache-tomcat-8.0.46.tar.gz
│   ├── Dockerfile
│   ├── jdk-8u181-linux-x64.tar.gz
│   └── server.xml
└── webapps
    └── ROOT
        └── index.jsp

7 directories, 10 files


一、创建Nginx Compose

1、创建DockerCompose项目目录

mkdir compose_nginx_tomcat
cd compose_nginx_tomcat/

1.2、创建nginx管理目录

mkdir nginx
cd nginx

1.3、将nginx源码包下载到本地

  • Nginx-1.12.1
  • 下载地址:https://pan.baidu.com/s/1IAdODW63jbpwbQX992coYg
  • 密码:p89j

1.4、创建Dockerfile文件

vim Dockerfile

# 指定镜像
FROM centos:6
# 指定管理员
MAINTAINER xiangsikai
# 执行命令安装编译库文件
RUN yum install -y gcc gcc-c++ make openssl-devel pcre-devel
# 添加解压nginx包到/tmp目录下
ADD nginx-1.12.1.tar.gz /tmp
# 进入目录进行编译安装
RUN cd /tmp/nginx-1.12.1 && ./configure --prefix=/usr/local/nginx && 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;"]

1.5、创建nginx.conf配置文件

vim 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;
        }
    }
}

nginx配置文件



二、创建Mysql Compose

2.1、创建Mysql管理目录

mkdir mysql
cd mysql
mkdir conf
cd conf

2.2、创建mysql配置文件

vim 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

mysql配置文件



三、创建Tomcat Compose

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

mkdir tomcat
mkdir -p webapps/ROOT/
cd tomcat

3.2、下载tomcat、jdk 压缩文件下载到本地

  • apache-tomcat-8.0.46
  • 下载地址:https://pan.baidu.com/s/1MuSGn3S1wILUfKZmemUu8g
  • 密码:xydu
  • jdk-8u181-linux-x64
  • 下载地址:https://pan.baidu.com/s/1eJPPR1cun09u4Uks0800hg
  • 密码:gjvv

3.3 创建Dockerfile文件

vim Dockerfile

# 指定镜像
FROM centos:6
# 指定管理员
MAINTAINER xiangsikai
# 解压jdk包到指定目录
ADD jdk-8u181-linux-x64.tar.gz /usr/local
# 安装jdk包到指定目录
ENV JAVA_HOME /usr/local/jdk1.8.0_181
# 解压tomcat包到指定目录
ADD apache-tomcat-8.0.46.tar.gz /usr/local
# 将本地配置文件复制到镜像内
COPY server.xml /usr/local/apache-tomcat-8.0.46/conf

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

3.4 创建server.xml配置文件

vim 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 &quot;%r&quot; %s %b" />

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

tomcat配置文件



四、创建docker-compose.yml

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

vim 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
# 映射服务数据卷路径
    volumes:
      - ./webapps:/usr/local/apache-tomcat-8.0.46/webapps

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

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

4.2、编写测试页面

vim webapps/ROOT/index.jsp

java ...........

4.3、执行dockerCompose 一键部署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   docker-               Up      0.0.0.0:3306->3306/
_mysql_1               entrypoint.sh                 tcp
                       mysqld
compose_nginx_tomcat   /usr/local/nginx/sb   Up      0.0.0.0:80->80/tcp
_nginx_1               in/ngin ...
compose_nginx_tomcat   /usr/local/apache-    Up      8080/tcp
_tomcat01_1            tomcat-8 ...
compose_nginx_tomcat   /usr/local/apache-    Up      8080/tcp
_tomcat02_1            tomcat-8 ...
compose_nginx_tomcat   /usr/local/apache-    Up      8080/tcp
_tomcat03_1            tomcat-8 ...

5.3、测试数据库

# 1、进入数据库容器
docker container exec -it c764f337ffad /bin/bash

# 2、进入数据库
mysql -h192.168.1.77 -uroot -p123456

# 3、查看创建用户user
mysql> select user,host from mysql.user;

+------+-----------+
| user | host |
+------+-----------+
| root | % |
| user | % |
| root | localhost |
+------+-----------+
3 rows in set (0.00 sec)

5.4、浏览器测试nginx代理tomcat

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

# 2、查看输出日志测试轮询代理
[[email protected] /]# tail /usr/local/nginx/logs/access.log -f
172.20.0.4:8080 192.168.1.2 - - [25/Oct/2018:07:34:07 +0000] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"
172.20.0.3:8080 192.168.1.2 - - [25/Oct/2018:07:34:07 +0000] "GET /favicon.ico HTTP/1.1" 404 1016 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"
172.20.0.5:8080 192.168.1.2 - - [25/Oct/2018:07:34:31 +0000] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"
172.20.0.4:8080 192.168.1.2 - - [25/Oct/2018:07:34:35 +0000] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"
172.20.0.3:8080 192.168.1.2 - - [25/Oct/2018:07:34:40 +0000] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"
172.20.0.5:8080 192.168.1.2 - - [25/Oct/2018:07:34:41 +0000] "GET / HTTP/1.1" 200 10 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:62.0) Gecko/20100101 Firefox/62.0" "-"

原文地址:https://www.cnblogs.com/xiangsikai/p/9850425.html

时间: 2025-01-01 21:40:29

Docker Compose 一键部署Nginx代理Tomcat集群的相关文章

Docker部署Nginx、Tomcat集群

Tomcat集群由多个tomcat组成,使用Nginx的好处是可以实现负载均衡和动静分离.使用Docker的好处是~~~我们不需要复杂的配置,只需要执行简单的命令就能拉取已有的官方docker镜像,再通过一条命令就能运行我们的实例了.对于多个Tomcat,我们使用相同的镜像,然后使用简单的命令,就能创建不同的tomcat container实例. 1.拉去镜像 $ docker pull tomcat:8-jre8-alpine $ docker pull nginx:latest $ dock

使用nginx反向代理tomcat集群

一.反向代理的概念 正向代理是一个位于客户端和目标服务器之间的代理服务器(中间服务器).为了从原始服务器取得内容,客户端向代理服务器发送一个请求,并且指定目标服务器,之后代理向目标服务器转交并且将获得的内容返回给客户端.正向代理的情况下客户端必须要进行一些特别的设置才能使用. 反向代理正好相反.对于客户端来说,反向代理就好像目标服务器.并且客户端不需要进行任何设置.客户端向反向代理发送请求,接着反向代理判断请求走向何处,并将请求转交给客户端,使得这些内容就好似他自己一样,一次客户端并不会感知到反

nginx整合tomcat集群并做session共享----测试案例

最近出于好奇心,研究了一下tomcat集群配置,并整合nginx,实现负载均衡,session共享,写篇记录,防止遗忘.---------菜鸡的自我修炼. 说明:博主采用一个web项目同时部署到两台tomcat下,(tomcat-A,tomca-B),使用nginx做反向代理,按照设置的权值,将请求分发到后台的tomcatA/tomcat-B,并且实现session共享. 配置好本地域名指向:修改host文件:添加 127.0.0.1  www.domain.com.cn 新建项目:tiny-d

nginx代理mogilefs集群实现

nginx代理mogilefs集群实现 一.实验拓扑 二.实验环境 三.实验步骤 1.节点部署 192.168.0.3 node1 [Nginx,Tracker,Storage,Mariadb] 192.168.0.4 node2 [Tracker,Storage] 192.168.0.5 node3 [Tracker,Storage] 2.初始化工作 配置好三台服务器IP,hosts文件等网络环境,时间同步以及以下的rpm包安装 注:以下操作在三个节点都执行 #下载rpm包(附件有提供) Mo

nginx+双tomcat集群负载均衡(一台机器)

nginx简介 Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日.其将源代码以类BSD许可证的形式发布,因它的稳定性.丰富的功能集.示例配置文件和低系统资源的消耗而闻名. 下面是采用一台机器nginx+双tomcat集群负载均衡方案.当更新项目时可以停止

Nginx+Memcached+Tomcat集群配置

1.   Nginx Nginx是通过将多个Web Server绑定到同一个IP地址下,以实现多个WebServer间的负载均衡,降低单个Web Server的负荷,以提高整体的性能与稳定性. 安装和配置Nginx的简单方式如下: 1)      下载并解压Nginx到本地目录:http://nginx.org/en/download.html 2)      对Nginx的配置主要是对它的配置文件/conf/nginx.conf的修改.如下链接是nginx.conf配置文件各个属性的详细说明:

nginx+memcached+tomcat集群 session共享完整版

nginx+memcached+tomcat集群 session共享完整版 集群环境 1.nginx版本 nginx-1.6.2.tar.gz 2.jdk 版本 jdk-7u21-linux-x64.tar.gz 3.tomcat 版本  7.0.29 4.memcached 版本 memcached-1.4.22.tar.gz 5. CentOS 6.5 系统采用一台服务做测试 一.nginx安装 安装依赖包 yum -y install gcc gcc-c++ 1.安装pcre库 tar z

nginx反向代理tomcat集群实现动静分离

我们都知道,nginx作为一个轻量级的web服务器,其在高并发下处理静态页面的优越性能是tomcat这样的web容器所无法媲美的,tomcat更倾向于处理动态文件,所以一个web应用可以通过nginx反向代理来实现动静分离,静态文件由nginx处理,动态文件由tomcat处理. 环境: hadoop0.updb.com    192.168.0.100    nginx server hadoop2.updb.com    192.168.0.102    tomcat server hadoo

nginx反向代理tomcat集群达到负载均衡,同时使用proxy_cache做web缓存

Nginx最早是作为一款优秀的反向代理软件,以高并发下的卓越性能被越来越多的用户所用户,国内最早研究nginx的是张宴,该大牛的博客地址:http://zyan.cc/ .但是随着需求的发展,nginx的功能已经不再单单是反向代理,现在已经更倾向作为web容器. Nginx从0.7.48版本开始,支持了类似Squid的缓存功能.Nginx的Web缓存服务主要由proxy_cache相关指令集和fastcgi_cache相关指令集构成,前者用于反向代理时,对后端内容源服务器进行缓存,后者主要用于对