上一篇讲解了如何批量部署nginx,这篇说一说如何批量部署tomcat,其实方式上都差不多,tomcat多了个java必须配置,以及需要多个实例一起部署。方式方法上可能不是最优方案,功能上是都可以实现基本需求的。


环境介绍:

Centos 6.5

tomcat 7.0.68

java 1.7.0_80

实例3个,site.aaa.com|boss.aaa.com|mobile.aaa.com

minion:test


1.修改主入口文件,如何你看不懂其中没标注的一些行,请参考Saltstack批量编译部署nginx(多模块)

vim /srv/salt/top.sls

base:
  'test':
    - tomcat.init

2.创建tomcat目录

mkdir -p /srv/salt/tomcat/files

mkdir -p /srv/salt/soft/

cd /srv/salt/tomcat && tree

3.编辑初始化文件init.sls

vim /srv/salt/tomcat/init.sls

include:
  - tomcat.install

4.编辑安装部署文件install.sls

vim /srv/salt/tomcat/install.sls

include:
   - soft.jdk        #包含jdk的安装
   - soft.start-stop        #包含启动停止脚本
tomcta-install:
  file.managed:
    - name: /root/apache-tomcat-7.0.68.tar.gz
    - source: salt://tomcat/files/apache-tomcat-7.0.68.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: mkdir/data/ && cd /root && tar zxf apache-tomcat-7.0.68.tar.gz && mv apache-tomcat-7.0.68 /data/
    - unless: test -d /data/apache-tomcat-7.0.68/

tomcat-main-config:        #复制自定义的catalina.sh文件
  file.managed:
    - name: /data/apache-tomcat-7.0.68/bin/catalina.sh
    - source: salt://tomcat/files/catalina.sh

tomcat-site-config:        #复制自定义的server.xml文件
  cmd.run:        #创建实例启动所需的三个文件夹
    - name: mkdir -p /data/site.aaa.com/{logs,bin,conf}
  file.managed:
    - name: /data/site.aaa.com/conf/server.xml
    - source: salt://tomcat/files/site/server.xml

tomcat-mobile-config:
  cmd.run:
    - name: mkdir -p /data/mobile.aaa.com/{logs,bin,conf}
  file.managed:
    - name: /data/mobile.aaa.com/conf/server.xml
    - source: salt://tomcat/files/mobile/server.xml
    
tomcat-boss-config:
  cmd.run:
    - name: mkdir -p /data/boss.aaa.com/{logs,bin,conf}
  file.managed:
    - name: /data/boss.aaa.com/conf/server.xml
    - source: salt://tomcat/files/boss/server.xml

4.编辑jdk配置文件

vim /srv/salt/soft/jdk.sls

jdk-installl:
  file.managed:
    - name: /root/jdk-7u80-linux-x64.tar.gz
    - source: salt://tomcat/files/jdk-7u80-linux-x64.tar.gz
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: cd /root/ && tar zxf jdk-7u80-linux-x64.tar.gz && mv jdk1.7.0_80 /usr/local/jdk
    - unless: test -d /usr/local/jdk
    - requrie:
      - file: jdk-install

jdk-config:
  file.append:        #在文件最后一行添加文字
    - name: /etc/profile
    - text:
      - export JAVA_HOME=/usr/local/jdk
      - export PATH=${PATH}:${JAVA_HOME}/bin
      - export CLASSPATH=${CLASSPATH}:${JAVA_HOME}/lib
  cmd.run:
    - name: source /etc/profile        #让添加的文字生效
    - require:
      - file: jdk-config

5.编辑启动停止脚本文件,这个脚本是我自己写的俩个启动和停止的方法,各位可以根据自己的需要改编

vim /srv/salt/soft/start-stop.sls

tomcat-site-start:
  file.managed:
    - name: /data/site.aaa.com/bin/start.sh
    - source: salt://tomcat/files/site/start.sh
tomcat-site-stop:
  file.managed:
    - name: /data/site.aaa.com/bin/stop.sh
    - source: salt://tomcat/files/site/stop.sh

tomcat-boss-start:
  file.managed:
    - name: /data/boss.aaa.com/bin/start.sh
    - source: salt://tomcat/files/boss/start.sh
tomcat-boss-stop:
  file.managed:
    - name: /data/boss.aaa.com/bin/stop.sh
    - source: salt://tomcat/files/boss/stop.sh

tomcat-mobile-start:
  file.managed:
    - name: /data/mobile.aaa.com/bin/start.sh
    - source: salt://tomcat/files/mobile/start.sh
tomcat-mobile-stop:
  file.managed:
    - name: /data/mobile.aaa.com/bin/stop.sh
    - source: salt://tomcat/files/mobile/stop.sh

tomcat-project-chmod:
  cmd.run:        #给脚本相应的执行权限
    - name: chmod +x /data/{m,kstore,boss}.aaa.com/bin/*.sh

注:start.sh

CATALINA_HOME="/data/apache-tomcat-7.0.68"
CATALINA_BASE="/data/site.aaa.com"
export CATALINA_BASE CATALINA_HOME
echo "$CATALINA_HOME"
echo $PATH
$CATALINA_HOME/bin/catalina.sh start

stop.sh

CATALINA_HOME="/data/apache-tomcat-7.0.68"
CATALINA_BASE="/data/site.aaa.com"
export CATALINA_BASE CATALINA_HOME
echo "$CATALINA_HOME"
echo $PATH
$CATALINA_HOME/bin/catalina.sh stop

6.执行同步推送

salt 'test' state.highstate

观察是否全部为success,如果有fail,查看具体失败原因,解决问题后可继续推送