Written in the front, k8s clusters have been set up. Please refer to the previous article for specific steps.
Write the Dockerfile file to create a public image. Each time tomcat deploys, it directly uses the image without build ing the image every time.
#At first, I wanted to use tomcat's official image, but I had no choice but to use diban 8 for the official image system, because I need to install cronolog software for log cutting. #google and Baidu didn't find out how to use apt get to install, but they had to use the source code to install, but the installation process needs the support of gcc and make. #If you don't use diban 8 for apt get update, you can't install it. However, due to the backwardness of the system, the official or 163 or Tsinghua sources can't support it. At last, the image of ubuntu 14 is used. FROM ubuntu:14.04.5 #tomcat installation directory ENV TOMCAT_HOME /usr/local/tomcat #This is the directory where the server.xml files are placed. This directory needs to be hyperlinked to the server.xml under the tomcat/conf folder. Why? #Because I want to use configmap to mount the configuration file. If I mount it directly to the conf directory, then all other files in the directory will be gone. So we mount the newly created directory when closing, and then make a hyperlink map. ENV SERVER_XML /configfile #Configure the jdk environment ENV JAVA_HOME /opt/jdk1.7.0_21 ENV JRE_HOME $JAVA_HOME/jre ENV JAVA_BIN $JAVA_HOME/bin ENV CLASSPATH $CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib ENV PATH $TOMCAT_HOME/bin:$JAVA_BIN:$JAVA_HOME/jre/bin:$PATH:$HOME/bin #Add jdk compression package to / opt directory ADD jdk-7u21-linux-x64.tar.gz /opt ADD apache-tomcat-7.0.77.tar.gz /usr/local RUN ADD ./catalina.sh $TOMCAT_HOME/bin/ ADD ./cronolog-1.6.2.tar.gz /opt/ #This is the tomcat startup script written by myself. #The first line is to start tomcat using SH / usr/local/tomcat/bin/startup.sh. #The container requires that there must be a container for the process executed by the foreground to not exit, so in the second line, you can just set a log at tail - f. ADD ./start.sh $TOMCAT_HOME/bin/ WORKDIR /opt/cronolog-1.6.2 RUN mv /usr/local/apache-tomcat-7.0.77 /usr/local/tomcat && chmod a+x /usr/local/tomcat/bin/*.sh && \ apt-get update -y && apt-get install gcc make -y && \ ./configure && make && make install && chmod a+x $TOMCAT_HOME/bin/start.sh $TOMCAT_HOME/bin/catalina.sh && \ rm -rf $TOMCAT_HOME/webapps/* && rm -rf $TOMCAT_HOME/conf/server.xml && mkdir $SERVER_XML && \ touch $SERVER_XML/server.xml && ln -s $SERVER_XML/server.xml $TOMCAT_HOME/conf/server.xml EXPOSE 8080 ENTRYPOINT ["/bin/sh", "/usr/local/tomcat/bin/start.sh"]
The required configuration files are as follows:
start.sh
#!/bin/sh sh /usr/local/tomcat/bin/startup.sh tail -f /usr/local/tomcat/bin/catalina.sh
catalina.sh
################Above and original tomcat Consistent default profile################ 421 #touch "$CATALINA_OUT" 422 if [ "$1" = "-security" ] ; then 423 if [ $have_tty -eq 1 ]; then 424 echo "Using Security Manager" 425 fi 426 shift 427 eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 428 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 429 -classpath "\"$CLASSPATH\"" \ 430 -Djava.security.manager \ 431 -Djava.security.policy=="\"$CATALINA_BASE/conf/catalina.policy\"" \ 432 -Dcatalina.base="\"$CATALINA_BASE\"" \ 433 -Dcatalina.home="\"$CATALINA_HOME\"" \ 434 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 435 org.apache.catalina.startup.Bootstrap "$@" start \ 436 >> "$CATALINA_OUT" 2>&1 "&" 437 438 else 439 eval $_NOHUP "\"$_RUNJAVA\"" "\"$LOGGING_CONFIG\"" $LOGGING_MANAGER $JAVA_OPTS $CATALINA_OPTS \ 440 -D$ENDORSED_PROP="\"$JAVA_ENDORSED_DIRS\"" \ 441 -classpath "\"$CLASSPATH\"" \ 442 -Dcatalina.base="\"$CATALINA_BASE\"" \ 443 -Dcatalina.home="\"$CATALINA_HOME\"" \ 444 -Djava.io.tmpdir="\"$CATALINA_TMPDIR\"" \ 445 org.apache.catalina.startup.Bootstrap "$@" start 2>&1 | /usr/local/sbin/cronolog "$CATALINA_BASE"/logs/catalina.%Y-%m-%d.out >> /dev/null & 446 447 fi ################The following and the original tomcat Consistent default profile################
Next, build the image directly.
Create a configmap server.xml file to hold tomcat.
kubectl "create" configmap "cm server.xml -- from file =" absolute path of server.xml "
Writing yaml file of k8s
apiVersion: apps/v1 kind: Deployment metadata: name: dubbo-admin labels: app: dubbo-admin spec: replicas: 1 selector: matchLabels: app: dubbo-admin template: metadata: labels: app: dubbo-admin spec: #Where need to be explained carefully, here we create three volume s to store tomcat's log, java project code and a tomcat configuration file (server.xml) in the form of configmap. volumes: - name: "code-war" hostPath: path: "/Disk/data/tomcat/dubbo-admin/code/" - name: "tomcat-log" hostPath: path: "/Disk/data/tomcat/dubbo-admin/log/" - name: "serverxml" configMap: name: tomcat-dubbo-admin-server.xml items: - key: server.xml path: server.xml containers: - name: dubbo-admin #The container uses the image just built image: fushuitong/tomcat:jdk7u21-tomcat7.0.77-cronolog1.6.2 ports: - containerPort: 8080 #Mount three volume s. The mount directory is as follows volumeMounts: - name: code-war mountPath: /usr/local/tomcat/webapps - name: tomcat-log mountPath: /usr/local/tomcat/logs - name: serverxml mountPath: /configfile
Create the Deployment directly.
Create a service and trace the Tomcat port to the node for external access. If there is nginx in front of tomcat, it can be mapped to the clusterIP directly. Use nginx for forwarding.
Changed yaml file of service
apiVersion: v1 kind: Service metadata: name: dubbo-admin spec: ports: - port: 8080 nodePort: 32333 targetPort: dubbo-admin protocol: TCP selector: app: dubbo-admin type: NodePort
Finally, the corresponding tomca project can be accessed by accessing the 32333 port of the ip of any node.