天天看點

docker實作tomcat+jdk+msm

Docker容器

 docker版本      : 1.7.1

 系統    版本     :CentOS release 6.8 

 tomcat版本    :apache-tomcat-7.0.57

 memcache版本:yum安裝

Docker 容器在運作裡有些步驟出錯會産生<none>之類的無效鏡像,産生之後直接運作腳本

删除

[root@1xx tom]#cat kong.sh

#!/bin/bash

#

for I in `docker images | grep"<none>" | awk -F" " '{print $3}'`;do

     echo"none key name $I"

     dockerstop $I

     docker  rmi -f $I

     echo$keys

done

docker images

[root@1xx tom]#cat Dockerfile

FROM centos                                                   #基礎鏡像來自于centos

MAINTAINER xiong <[email protected]>  #作者資訊

#安裝ifconfig 類型工具

RUN yum -y install net-tools                                    

#ADD複制本地檔案并自動解壓至目錄下

ADD apache-tomcat-7.0.57.tar.gz/usr/local/ 

# 運作軟連結指令

RUN ln -sv /usr/local/apache-tomcat-7.0.57/usr/local/tomcat

# 定義tomcat環境變量值

ENV CATALINA_HOME /usr/local/tomcat

ENV PATH $PATH:$CATALINA_HOME/bin

#複制jdk檔案到root目錄并使用rpm指令安裝

COPY jdk-7u79-linux-x64.rpm /root/

RUN rpm -ivh  /root/jdk-7u79-linux-x64.rpm

#定義java環境變量

ENV JAVA_HOME=/usr/java/latest

ENV PATH $PATH:$JAVA_HOME/bin

# 當機器運作裡自動啟動,此處可省略

RUN echo"/usr/local/tomcat/catalina.sh start" >> /etc/rc.d/rc.local

# 産先配置好server.xml檔案然後直接複制到conf檔案目錄下

COPY server.xml /usr/local/tomcat/conf/

# 複制msm檔案至lib檔案下

COPY mem/* /usr/local/tomcat/lib/

# 運作啟動catalina.sh腳本使用run 腳本在運作之後啟動并列印debug資訊在使用ctrl+p ctrl+q 運作容器,否則腳本運作後之後腳本會退出

CMD["catalina.sh","run"]

memcached session manager

https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

下載下傳需要注意版本資訊      并且需要注意的是版本号需要對應一緻否則有可能報錯

javolution-5.4.3.1.jar          

memcached-session-manager-tc7-2.1.1.jar            

spymemcached-2.11.1.jar

memcached-session-manager-2.1.1.jar 

msm-javolution-serializer-2.1.1.jar                        #也可以選擇其它

 [root@1xxtom]#cat del.sh

docker rm -f tom{1..9}

docker rm -f mem{1..9}

docker rmi xiong

docker build -t xiong .

[root@1xx memcached]# cat Dockerfile

         FROMcentos                                                          #基礎鏡像來自于centos

RUN yum -yinstall memcached net-tools        #安裝mem以及ifconfig 類型工具

         #運作memcached指令,别使用daemon 腳本在運作完之後容器會自動退出

CMD["/usr/bin/memcached","-p","11211","-m","64","-c","1024","-u","root"]

         [root@1xxmemcached]# cat del.sh

docker rm -f mem1

docker rmi mem

docker build -t mem .

配置完基礎鏡像運作容器,為友善我這裡也定義了腳本

[root@1xx tom]#cat docktest.sh

# docker 運作容器名稱tom2 端口本地92 容器8080 資料卷本地xx/xx/tom2/ 容器/web/apps 基礎鏡像

docker run -it--name tom2 -p 92:8080 -v /root/tom/tom2:/web/apps/ xiong

docker run -it--name tom3 -p 93:8080 -v /root/tom/tom3:/web/apps/ xiong

#docker 運作容器名稱m2m1 端口本地201 遠端11211 基礎鏡像

docker run -it--name mem1 -p 201:11211 mem

docker run -it--name mem2 -p 202:11211 mem

docker ps

# 測試容器memcached是否運作成功 本地運作telnet

<a href="https://s1.51cto.com/wyfs02/M02/8D/A5/wKioL1ilFLDBMnTXAAAo4BUoDcc821.png-wh_500x0-wm_3-wmp_4-s_174397445.png" target="_blank"></a>

容器tom2号 session資訊

<a href="https://s3.51cto.com/wyfs02/M01/8D/A7/wKiom1ilFJrwV4zbAAC3niGsd7c947.png-wh_500x0-wm_3-wmp_4-s_1465351516.png" target="_blank"></a>

JSESSIONID=A4B7390062C98E719D1CBF5C198F835D-n2

<a href="https://s5.51cto.com/wyfs02/M01/8D/A7/wKiom1ilFH3jTqDmAACpMmwzn3k170.png-wh_500x0-wm_3-wmp_4-s_1845582411.png" target="_blank"></a>

Session資訊一緻說明成功

Service.xml檔案内容

[root@1xx tom]# cat server.xml

&lt;?xmlversion='1.0' encoding='utf-8'?&gt;

&lt;!--

  Licensed to the Apache Software Foundation(ASF) under one or more

  contributor license agreements.  See the NOTICE file distributed with

  thiswork for additional information regarding copyright ownership.

  The ASF licenses this file to You under theApache License, Version 2.0

  (the "License"); you may not usethis 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 agreedto in writing, software

  distributed under the License is distributedon an "AS IS" BASIS,

  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.

  See the License for the specific languagegoverning permissions and

  limitations under the License.

--&gt;

&lt;!--Note:  A "Server" is not itselfa "Container", so you may not

     define subcomponents such as"Valves" at this level.

     Documentation at /docs/config/server.html

 --&gt;

&lt;Serverport="8005" shutdown="SHUTDOWN"&gt;

  &lt;ListenerclassName="org.apache.catalina.startup.VersionLoggerListener" /&gt;

  &lt;!-- Security listener. Documentation at/docs/config/listeners.html

  &lt;ListenerclassName="org.apache.catalina.security.SecurityListener" /&gt;

  --&gt;

  &lt;!--APR library loader. Documentation at/docs/apr.html --&gt;

  &lt;ListenerclassName="org.apache.catalina.core.AprLifecycleListener"SSLEngine="on" /&gt;

  &lt;!--Initialize Jasper prior to webapps areloaded. Documentation at /docs/jasper-howto.html --&gt;

  &lt;ListenerclassName="org.apache.catalina.core.JasperListener" /&gt;

  &lt;!-- Prevent memory leaks due to use ofparticular java/javax APIs--&gt;

  &lt;Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener"/&gt;

  &lt;ListenerclassName="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener"/&gt;

  &lt;ListenerclassName="org.apache.catalina.core.ThreadLocalLeakPreventionListener"/&gt;

  &lt;!-- Global JNDI resources

       Documentation at/docs/jndi-resources-howto.html

  &lt;GlobalNamingResources&gt;

    &lt;!-- Editable user database that canalso be used by

         UserDatabaseRealm to authenticateusers

    --&gt;

    &lt;Resource name="UserDatabase"auth="Container"

             type="org.apache.catalina.UserDatabase"

              description="User databasethat can be updated and saved"

             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

              pathname="conf/tomcat-users.xml"/&gt;

  &lt;/GlobalNamingResources&gt;

  &lt;!-- A "Service" is a collectionof one or more "Connectors" that share

       a single "Container"Note:  A "Service" is notitself a "Container",

       so you may not define subcomponents suchas "Valves" at this level.

       Documentation at/docs/config/service.html

   --&gt;

  &lt;Service name="Catalina"&gt;

    &lt;!--The connectors can use a sharedexecutor, you can define one or more named thread pools--&gt;

    &lt;!--

    &lt;Executorname="tomcatThreadPool" namePrefix="catalina-exec-"

        maxThreads="150"minSpareThreads="4"/&gt;

    &lt;!-- A "Connector" representsan endpoint by which requests are received

         and responses are returned.Documentation at :

         Java HTTP Connector: /docs/config/http.html(blocking &amp; non-blocking)

         Java AJP  Connector: /docs/config/ajp.html

         APR (HTTP/AJP) Connector:/docs/apr.html

         Define a non-SSL HTTP/1.1 Connector onport 8080

    &lt;Connector port="8080"protocol="HTTP/1.1"

               connectionTimeout="20000"

               redirectPort="8443"/&gt;

    &lt;!-- A "Connector" using theshared thread pool--&gt;

    &lt;Connectorexecutor="tomcatThreadPool"

               port="8080"protocol="HTTP/1.1"

               connectionTimeout="20000"

    &lt;!-- Define a SSL HTTP/1.1 Connector onport 8443

         This connector uses the BIOimplementation that requires the JSSE

         style configuration. When using theAPR/native implementation, the

         OpenSSL style configuration isrequired as described in the APR/native

         documentation --&gt;

    &lt;Connector port="8443"protocol="org.apache.coyote.http11.Http11Protocol"

               maxThreads="150"SSLEnabled="true" scheme="https" secure="true"

               clientAuth="false"sslProtocol="TLS" /&gt;

    &lt;!-- Define an AJP 1.3 Connector on port8009 --&gt;

    &lt;Connector port="8009"protocol="AJP/1.3" redirectPort="8443" /&gt;

    &lt;!-- An Engine represents the entrypoint (within Catalina) that processes

         every request.  The Engine implementation for Tomcat standalone

         analyzes the HTTP headers includedwith the request, and passes them

         on to the appropriate Host (virtualhost).

         Documentation at/docs/config/engine.html --&gt;

    &lt;!-- You should set jvmRoute to supportload-balancing via AJP ie :

    &lt;Engine name="Catalina"defaultHost="localhost" jvmRoute="jvm1"&gt;

    &lt;Engine name="Catalina" defaultHost="localhost"&gt;

      &lt;!--For clustering, please take a lookat documentation at:

          /docs/cluster-howto.html  (simple how to)

          /docs/config/cluster.html (referencedocumentation) --&gt;

      &lt;!--

      &lt;Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/&gt;

      --&gt;

      &lt;!-- Use the LockOutRealm to preventattempts to guess user passwords

           via a brute-force attack --&gt;

      &lt;RealmclassName="org.apache.catalina.realm.LockOutRealm"&gt;

        &lt;!-- This Realm uses theUserDatabase configured in the global JNDI

             resources under the key"UserDatabase".  Any edits

             that are performed against thisUserDatabase are immediately

             available for use by theRealm.  --&gt;

        &lt;Realm className="org.apache.catalina.realm.UserDatabaseRealm"

              resourceName="UserDatabase"/&gt;

      &lt;/Realm&gt;

        &lt;Host name="localhost"appBase="/web/apps" unpackWARs="true"autoDeploy="false"&gt;

        &lt;Context path=""docBase="/web/apps/" reloadable="true" &gt;

            &lt;ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"

              memcachedNodes="n1:192.168.8.101:201,n2:192.168.8.101:202"

              failoverNodes="n1"

              storageKeyPrefix="context"

              requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

              transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"

                /&gt;

    &lt;/Context&gt;

        &lt;ValveclassName="org.apache.catalina.valves.AccessLogValve"directory="/web/logs"

               prefix="app_access_log"suffix=".txt"

               pattern="%h %l %u %t&amp;quot;%r&amp;quot; %s %b" /&gt;

        &lt;/Host&gt;

    &lt;/Engine&gt;

  &lt;/Service&gt;

&lt;/Server&gt;

Tomcat資料卷中 tomcat 目錄 index.jsp

[root@1xx tom]# cat tom2/index.jsp

&lt;%@ page language="java" %&gt;

&lt;%@ page import="java.util.*"%&gt;

&lt;html&gt;

       &lt;head&gt;

                &lt;title&gt;tom2&lt;/title&gt;

       &lt;/head&gt;

       &lt;body&gt;

                &lt;% out.println("Hello,tom2."); %&gt;

       &lt;/body&gt;

&lt;/html&gt;

     本文轉自812374156 51CTO部落格,原文連結:http://blog.51cto.com/xiong51/1898356,如需轉載請自行聯系原作者