查看“Docker-compose 部署Nginx代理Tomcat集群”的源代码
←
Docker-compose 部署Nginx代理Tomcat集群
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
=[http://wiki.linuxchina.net/index.php?title=Docker-compose%E6%95%99%E7%A8%8B#install compose install]= =目录结构= <pre> 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 </pre> =Nginx Compose= ==创建DockerCompose项目目录== <pre> 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; } } } </pre> =创建Mysql Compose= <pre> 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 </pre> =创建Tomcat Compose= <pre> 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"] </pre> ==3.4 创建server.xml配置文件== <pre> 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> </pre> =创建docker-compose.yml= <pre> 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 </pre> ==web home 编写测试页面== <pre> [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 ********* </pre> ==run 执行dockerCompose 一键部署Nginx代理Tomcat集群== <pre> # pwd /root/compose_nginx_tomcat # 管理目录下compose_nginx_tomcat 执行该命令 -d 后台运行 docker-compose up -d </pre> =测试容器服务= <pre> 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 </pre> ==测试数据库== <pre> 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 </pre> ==浏览器测试nginx代理tomcat== <pre> 打开浏览器 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" "-" </pre> =trouble shooting = <pre> 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 </pre> =see also= [https://www.cnblogs.com/xiangsikai/p/9850425.html Docker Compose 一键部署Nginx代理Tomcat集群] [https://www.cnblogs.com/mushou/p/9537846.html docker-compose入门示例:一键部署 Nginx+Tomcat+Mysql] [https://blog.csdn.net/weixin_38589021/article/details/80198390 Docker Compose 运行 Tomcat 和Mysql] [https://gitee.com/wuweixiang/javaweb-compose/blob/master/docker-compose.yml 使用docker-compose定制Javaweb环境:tomcat、jre、mysql、activemq、redis] [https://blog.csdn.net/dandandeshangni/article/details/79771538 Docker Compose+nginx实现负载均衡 spring] [[category:Container]] [[category:compose]]
返回至
Docker-compose 部署Nginx代理Tomcat集群
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
我的导航
关于我
shell
python
ops
linuxchina.net
blog.linuxchina
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息