前言:因为我们使用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完毕