salt实战系列-安装tomcat

前言:因为我们使用state.sls进行安装,所以你可能需要先熟悉下state.sls的语法
state语法参考

  • 开始先看下salt-master的配置
> tail -10  /etc/salt/master
# salt state执行时候的根目录
file_roots:
  base:
    - /srv/salt/base
# 设置pillar 的根目录
pillar_roots:
  base:
    - /srv/salt/pillar
# master自动接收客户端的验证
auto_accept: True

salt的主目录在 /srv/salt/base;minion端为docker-100;

  • salt-jdk-tomcat 目录结构
> tree /srv/salt/base/
/srv/salt/base/
└── package
    ├── jdk1_8
    │   ├── files
    │   │   └── jdk-8u74-linux-x64.tar.gz
    │   └── init.sls
    └── tomcat7_63
        ├── files
        │   ├── apache-tomcat-7.0.63.tar.gz
        │   ├── catalina.sh
        │   └── server.xml
        └── init.sls

5 directories, 6 files

1. 因为tomcat运行需要jdk,所以先安装jdk环境

> cat /srv/salt/base/package/jdk1_8/init.sls 
jdk-file:
  file.managed:
    - name: /tmp/jdk-8u74-linux-x64.tar.gz
    - source: salt://package/jdk1_8/files/jdk-8u74-linux-x64.tar.gz
    - user: root
    - group: root
    - mode: 755

jdk-install:
  cmd.run:
    - cwd: /tmp
    - names: 
      - tar zxf jdk-8u74-linux-x64.tar.gz -C /usr/local
      - ln -s /usr/local/jdk1.8.0_74 /usr/local/java
      - chown -R root:root /usr/local/java
    - unless: test -d /usr/local/jdk1.8.0_74
    - require:
      - file: jdk-file

jdk-config:
  file.append:
    - name: /etc/profile
    - text:
      - "#############JAVA ENVIRONMNET################"
      - "export JAVA_HOME=/usr/local/java"
      - "export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH"
      - "export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib"

运行看看效果

> salt docker-100 state.sls package.jdk1_8
docker-100:
----------
          ID: jdk-file
    Function: file.managed
        Name: /tmp/jdk-8u74-linux-x64.tar.gz
      Result: True
     Comment: File /tmp/jdk-8u74-linux-x64.tar.gz updated
     Started: 21:12:32.537187
    Duration: 1508.867 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0755
----------
          ID: jdk-install
    Function: cmd.run
        Name: tar zxf jdk-8u74-linux-x64.tar.gz -C /usr/local && ln -s /usr/local/jdk1.8.0_74 /usr/local/java
      Result: True
     Comment: Command "tar zxf jdk-8u74-linux-x64.tar.gz -C /usr/local && ln -s /usr/local/jdk1.8.0_74 /usr/local/java" run
     Started: 21:12:34.047076
    Duration: 4204.087 ms
     Changes:   
              ----------
              pid:
                  10810
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: jdk-install
    Function: cmd.run
        Name: chown -R root:root /usr/local/java
      Result: True
     Comment: unless execution succeeded
     Started: 21:12:38.251407
    Duration: 9.458 ms
     Changes:   
----------
          ID: jdk-config
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: Appended 4 lines
     Started: 21:12:38.261104
    Duration: 5.477 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -78,3 +78,7 @@
                   export PS1="\[\e[35m\]\u\[\e[0m\]\[\e[33m\]@\[\e[0m\]\[\e[34m\]\h\[\e[0m\] \[\e[36m\]\W\n> \[\e[0m\]"
                   
                   
                  +#############JAVA ENVIRONMNET################
                  +export JAVA_HOME=/usr/local/java
                  +export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
                  +export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

Summary
------------
Succeeded: 4 (changed=3)
Failed:    0
------------
Total states run:     4

查看java版本

> java -version
java version "1.8.0_74"
Java(TM) SE Runtime Environment (build 1.8.0_74-b02)
Java HotSpot(TM) 64-Bit Server VM (build 25.74-b02, mixed mode)

2. 编写tomcat.sls文件

>cat /srv/salt/base/package/tomcat7_63/init.sls 
include:
  - package.jdk1_8

tomcat_source:
  file.managed:
    - name: /tmp/apache-tomcat-7.0.63.tar.gz
    - unless: test -e /tmp/apache-tomcat-7.0.63.tar.gz
    - source: salt://package/tomcat7_63/files/apache-tomcat-7.0.63.tar.gz
  cmd.run:
    - names:
      - tar xf /tmp/apache-tomcat-7.0.63.tar.gz -C /usr/local/ && mv /usr/local/apache-tomcat-7.0.63 /usr/local/tomcat7
    - require:
      - file: tomcat_source
    - unless: test -d /usr/local/tomcat7

tomcat-config-file:
  file.managed:
    - name: /usr/local/tomcat7/conf/server.xml
    - source: salt://package/tomcat7_63/files/server.xml
    - template: jinja
    - user: root
    - group: root
    - mode: 644

tomcat-catalina-file:
  file.managed:
    - name: /usr/local/tomcat7/bin/catalina.sh
    - source: salt://package/tomcat7_63/files/catalina.sh
    - template: jinja
    - user: root
    - group: root

include表示关联上面的jdk,安装tomcat前会先去安装jdk,server.xml和catalina.sh分别加了如下配置

  • server.xml
    # 绑定客户端内网ip,并调整tomcat参数,下面只给出修改部分,完整配置得补全
     
  • catalina.sh
    # 根据客户端内存配置动态设置 tomcat运行内存,只给出修改部分,完整配置得补全
#!/bin/sh

{% if grains['mem_total'] > 7000 %}
JAVA_OPTS="-server -Xms4g -Xmx4g -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:NewSize=512m -XX:MaxPermSize=256m -XX:MaxDi
rectMemorySize=1024m -Dfile.encoding=UTF-8"
{% elif 3000 < grains['mem_total'] < 7000 %}
JAVA_OPTS="-server -Xms2g -Xmx2g -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:NewSize=512m -XX:MaxPermSize=256m -XX:MaxDi
rectMemorySize=1024m -Dfile.encoding=UTF-8"
{% elif 1000 < grains['mem_total'] < 3000 %}
JAVA_OPTS="-server -Xms512m -Xmx512m -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:NewSize=256m -XX:MaxPermSize=256m -XX:M
axDirectMemorySize=512m -Dfile.encoding=UTF-8"
{% endif %}

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

执行下

> salt docker-100 state.sls package.tomcat7_63
docker-100:
----------
          ID: jdk-file
    Function: file.managed
        Name: /tmp/jdk-8u74-linux-x64.tar.gz
      Result: True
     Comment: File /tmp/jdk-8u74-linux-x64.tar.gz is in the correct state
     Started: 21:35:56.660322
    Duration: 847.049 ms
     Changes:   
----------
          ID: jdk-install
    Function: cmd.run
        Name: tar zxf jdk-8u74-linux-x64.tar.gz -C /usr/local && ln -s /usr/local/jdk1.8.0_74 /usr/local/java
      Result: True
     Comment: unless execution succeeded
     Started: 21:35:57.508291
    Duration: 6.915 ms
     Changes:   
----------
          ID: jdk-install
    Function: cmd.run
        Name: chown -R root:root /usr/local/java
      Result: True
     Comment: unless execution succeeded
     Started: 21:35:57.515480
    Duration: 5.959 ms
     Changes:   
----------
          ID: jdk-config
    Function: file.append
        Name: /etc/profile
      Result: True
     Comment: File /etc/profile is in correct state
     Started: 21:35:57.521588
    Duration: 5.404 ms
     Changes:   
----------
          ID: tomcat_source
    Function: file.managed
        Name: /tmp/apache-tomcat-7.0.63.tar.gz
      Result: True
     Comment: File /tmp/apache-tomcat-7.0.63.tar.gz updated
     Started: 21:35:57.527137
    Duration: 855.025 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644
----------
          ID: tomcat_source
    Function: cmd.run
        Name: tar xf /tmp/apache-tomcat-7.0.63.tar.gz -C /usr/local/ && mv /usr/local/apache-tomcat-7.0.63 /usr/local/tomcat7
      Result: True
     Comment: Command "tar xf /tmp/apache-tomcat-7.0.63.tar.gz -C /usr/local/ && mv /usr/local/apache-tomcat-7.0.63 /usr/local/tomcat7" run
     Started: 21:35:58.382640
    Duration: 157.25 ms
     Changes:   
              ----------
              pid:
                  11718
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: tomcat-config-file
    Function: file.managed
        Name: /usr/local/tomcat7/conf/server.xml
      Result: True
     Comment: File /usr/local/tomcat7/conf/server.xml updated
     Started: 21:35:58.540145
    Duration: 12.156 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -68,9 +68,16 @@
                            APR (HTTP/AJP) Connector: /docs/apr.html
                            Define a non-SSL HTTP/1.1 Connector on port 8080
                       -->
                  -    
                  +     
                       
                       
                    

你可能感兴趣的:(salt实战系列-安装tomcat)