Docker Compose 一鍵部署Nginx代理Tomcat集羣css
目錄結構html
[root@localhost ~]# 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
1、建立Nginx Composejava
一、建立DockerCompose項目目錄mysql
mkdir compose_nginx_tomcat
cd compose_nginx_tomcat/
1.二、建立nginx管理目錄linux
mkdir nginx
cd nginx
1.三、將nginx源碼包下載到本地nginx
1.四、建立Dockerfile文件c++
vim Dockerfileweb
# 指定鏡像 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.五、建立nginx.conf配置文件sql
vim nginx.confdocker
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; } } }
2、建立Mysql Compose
2.一、建立Mysql管理目錄
mkdir mysql
cd mysql
mkdir conf
cd conf
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
3、建立Tomcat Compose
3.一、建立tomcat管理目錄與網站目錄
mkdir tomcat mkdir -p webapps/ROOT/ cd tomcat
3.二、下載tomcat、jdk 壓縮文件下載到本地
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 "%r" %s %b" /> </Host> </Engine> </Service> </Server>
4、建立docker-compose.yml
4.一、在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.二、編寫測試頁面
vim webapps/ROOT/index.jsp
java ...........
4.三、執行dockerCompose 一鍵部署Nginx代理Tomcat集羣
# 管理目錄下compose_nginx_tomcat 執行該命令 -d 後臺運行 docker-compose up -d
5、測試容器服務
5.一、查看啓動狀態終端輸出
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.二、查看後臺運行容器
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.三、測試數據庫
# 一、進入數據庫容器 docker container exec -it c764f337ffad /bin/bash # 二、進入數據庫 mysql -h192.168.1.77 -uroot -p123456 # 三、查看建立用戶user mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | % | | user | % | | root | localhost | +------+-----------+ 3 rows in set (0.00 sec)
5.四、瀏覽器測試nginx代理tomcat
# 一、進入nginx管理界面 docker container exec -it c764f337ffad /bin/bash # 二、查看輸出日誌測試輪詢代理 [root@nginx /]# 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" "-"