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参数,下面只给出修改部分,完整配置得补全
     <Connector port="8080" protocol="HTTP/1.1"
                address="{{grains['ipv4'][0]}}"   #绑定本机内网ip
                connectionTimeout="20000"
                maxThreads="1000"
                minSpareThreads="50"
                maxSpareThreads="150"
                minProcessors="100"
                maxProcessors="1000"
                acceptCount="1000"
                redirectPort="8443" />
  • 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
                       -->
                  -    <Connector port="8080" protocol="HTTP/1.1"
                  -               connectionTimeout="20000"
                  -               redirectPort="8443" />
                  +     <Connector port="8080" protocol="HTTP/1.1"
                  +                address="1.1.1.100"
                  +                connectionTimeout="20000"
                  +                maxThreads="1000"
                  +                minSpareThreads="50"
                  +                maxSpareThreads="150"
                  +                minProcessors="100"
                  +                maxProcessors="1000"
                  +                acceptCount="1000"
                  +                redirectPort="8443" />
                       <!-- A "Connector" using the shared thread pool-->
                       <!--
                       <Connector executor="tomcatThreadPool"
              mode:
                  0644
----------
          ID: tomcat-catalina-file
    Function: file.managed
        Name: /usr/local/tomcat7/bin/catalina.sh
      Result: True
     Comment: File /usr/local/tomcat7/bin/catalina.sh updated
     Started: 21:35:58.552409
    Duration: 19.739 ms
     Changes:   
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,4 +1,8 @@
                   #!/bin/sh
                  +
                  +
                  +JAVA_OPTS="-server -Xms512m -Xmx512m -XX:+UseConcMarkSweepGC -XX:+CMSIncrementalMode -XX:NewSize=256m -XX:MaxPermSize=256m -XX:MaxDirectMemorySize=512m -Dfile.encoding=UTF-8" 
                  +
                   
                   # Licensed to the Apache Software Foundation (ASF) under one or more
                   # contributor license agreements.  See the NOTICE file distributed with

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

查看

> ls /usr/local/tomcat7/
bin  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work

我们的tomcat已经被安装到了 /usr/local/tomcat7

Tips
下面主要讲一下 cmd.run 模块:

– name: 这个name是执行脚本和命令的名子

– onlyif: 测试命令,如果执行命令返回为True cmd.run的命令才会被执行
– unless: 正好与onlyif相反,为True不执行 为false才有可能执行
– request: 这个应该很好理解,cmd.run运行,必须以安装了它指定的包为前提的

当然这里面还有一些其他参数,我这里只是用了两三条,下面再列举一些:

– cwd: 执行命令当前目录位置,默认/root
– user: 执行命令用户(root为默认)
– group: 执行命令组(root为默认)
– shell: 执行命令使用的shell
– env: 执行命令的环境设置
– umask: 运行命令时umask设置
– output_loglevel:执行命令输出日志的级别,其中特殊的设置为quiet,那么就不输出日志了
– timeout: 执行命令超时时间


自此,salt安装tomcat完毕

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,482评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,377评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,762评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,273评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,289评论 5 373
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,046评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,351评论 3 400
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,988评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,476评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,948评论 2 324
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,064评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,712评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,261评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,264评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,486评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,511评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,802评论 2 345

推荐阅读更多精彩内容