前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >salt实现自动化部署项目

salt实现自动化部署项目

作者头像
嘻哈记
发布2020-11-24 10:34:59
1.5K0
发布2020-11-24 10:34:59
举报
文章被收录于专栏:运维学习交流运维学习交流

1.在salt-master上设置配置文件

代码语言:javascript
复制
[root@zabbix project]# cat /etc/salt/master |grep -Ev '^#|^$'
interface: 0.0.0.0
file_roots:
  base:
    - /srv/salt/base
pillar_roots:
  base:
    - /srv/salt/pillar

[root@zabbix project]# pwd
/srv/salt/base/project

2.project目录是我们需要创建自动部署项目所需的文件目录,如下

代码语言:javascript
复制
[root@zabbix project]# ll
总用量 16676
drwxr-xr-x 3 root root     159 4月  24 17:11 file
-rw-r--r-- 1 root root      32 3月  21 2019 init.sls
-rw-r--r-- 1 root root    1906 5月  24 2019 project.sls
drwxr-xr-x 3 root root      34 4月  24 17:11 test
-rw-r--r-- 1 root root 8528017 3月  21 2019 tomcat-9.0.11.zip
-rw-r--r-- 1 root root 8528017 4月  24 17:04 tomcat-9.0.11.zip.20200424
-rw-r--r-- 1 root root     401 5月  24 2019 user.sls
  1. 我们都知道pillar设置是需要一个总入口文件,这个项目的总入口文件是:init.sls
代码语言:javascript
复制
[root@zabbix project]# cat init.sls
include:
  - .user
  - .project

既然有总入口这个init.sls文件,如上的配置文件可以看到还应该有user和project的子配置文件,具体内容如下:

代码语言:javascript
复制
[root@zabbix project]# cat user.sls
#项目用户
create_user:
  user.present:
    - name: {{ pillar['user'] }}
    - home: /home/{{ pillar['user'] }}
    - hash_password: True
    - password: "123456"
#环境变量
bashrc:
  file.managed:
    - name: /home/{{ pillar['user'] }}/.bashrc
    - source: salt://project/file/bashrc  
    - template: jinja
    - user: {{ pillar['user'] }}
    - group: {{ pillar['user'] }}
    - mode: 644




[root@zabbix project]# cat project.sls
#项目配置

{% if pillar['type'] == 'tomcat'  %}
#安装tomcat
  install_timcat:
    archive.extracted:
      - name: /home/{{ pillar['user'] }}/
      - source: salt://project/file/tomcat-9.0.11.zip
      - use_cmd_unzip: True
#配置tomcat
  config_tomcat:
    file.managed:
      - name: /home/{{ pillar['user'] }}/apache-tomcat-9.0.11/conf/server.xml
      - source: salt://project/file/server.xml
      - template: jinja
#软链接
  symlink_tomcat:
    file.symlink:
      - name: /home/{{ pillar['user'] }}/tomcat
      - target: /home/{{ pillar['user'] }}/apache-tomcat-9.0.11
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
#创建webapps
  create_webapps:
      file.directory:
        - name: /home/{{ pillar['user'] }}/webapps
        - user: {{ pillar['user'] }}
        - group:  {{ pillar['user'] }}
        - mode: 755
        - makedirs: True
#启动脚本
  start_shell:
    file.managed:
      - name: /home/{{ pillar['user'] }}/publish.sh
      - source: salt://project/file/publish.sh
      - template: jinja
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755

{% elif pillar['type'] == 'spring' %}

#启动脚本
  spring_shell:
    file.managed:
      - name: /home/{{ pillar['user'] }}/publish.sh
      - source: salt://project/file/start.sh
      - template: jinja
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
#日志目录
  log_dir:
    file.directory:
      - name: /home/{{ pillar['user'] }}/logs
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
      - makedirs: True

{% endif %}
#权限属组
  chown_and_chmod:
    file.directory:
      - name: /home/{{ pillar['user'] }}
      - user: {{ pillar['user'] }}
      - group: {{ pillar['user'] }}
      - mode: 755
      - recurse:
        - user
        - group
     #   - mode
  1. 上面的子配中设计的文件(bashrc、server.xml、publish.sh、start.sh)配置如下
代码语言:javascript
复制
[root@zabbix file]# cat bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

export JAVA_HOME=/usr/java/jdk1.8.0_181
export MVN_HOME=/opt/apache-maven-3.5.2
export PATH=$JAVA_HOME/bin:$PATH:$MVN_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tool.jar
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=/opt/mongodb/mongodb-linux-x86_64-3.2.3/bin:$PATH

TZ='Asia/Shanghai'
代码语言:javascript
复制
[root@zabbix file]# cat server.xml
<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- Note:  A "Server" is not itself a "Container", so you may not
     define subcomponents such as "Valves" at this level.
     Documentation at /docs/config/server.html
 -->
<Server port="-1" shutdown="SHUTDOWN">
  <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
  <!-- Security listener. Documentation at /docs/config/listeners.html
  <Listener className="org.apache.catalina.security.SecurityListener" />
  -->
  <!--APR library loader. Documentation at /docs/apr.html -->
  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html -->

  <!-- Prevent memory leaks due to use of particular java/javax APIs-->
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
  <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

  <!-- Global JNDI resources
       Documentation at /docs/jndi-resources-howto.html
  -->
  <GlobalNamingResources>
    <!-- Editable user database that can also be used by
         UserDatabaseRealm to authenticate users
    -->
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <!-- A "Service" is a collection of one or more "Connectors" that share
       a single "Container" Note:  A "Service" is not itself a "Container",
       so you may not define subcomponents such as "Valves" at this level.
       Documentation at /docs/config/service.html
   -->
  <Service name="Catalina">

    <!--The connectors can use a shared executor, you can define one or more named thread pools-->
    <!--
    <Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
        maxThreads="150" minSpareThreads="4"/>
    -->

    <Connector port="{{ pillar['port'] }}" address="0.0.0.0" protocol="HTTP/1.1" URIEncoding="UTF-8" relaxedQueryChars="[,]"/>
    <!-- A "Connector" represents an endpoint by which requests are received
         and responses are returned. Documentation at :
         Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
         Java AJP  Connector: /docs/config/ajp.html
         APR (HTTP/AJP) Connector: /docs/apr.html
         Define a non-SSL HTTP/1.1 Connector on port 8080
    -->
    <!-- A "Connector" using the shared thread pool-->
    <!--
    <Connector executor="tomcatThreadPool"
               port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
    -->
    <!-- Define a SSL HTTP/1.1 Connector on port 8443
         This connector uses the BIO implementation that requires the JSSE
         style configuration. When using the APR/native implementation, the
         OpenSSL style configuration is required as described in the APR/native
         documentation -->
    <!--
    <Connector port="8443" protocol="org.apache.coyote.http11.Http11Protocol"
               maxThreads="150" SSLEnabled="true" scheme="https" secure="true"
               clientAuth="false" sslProtocol="TLS" />
    -->

    <!-- Define an AJP 1.3 Connector on port 8009 -->

    <!-- An Engine represents the entry point (within Catalina) that processes
         every request.  The Engine implementation for Tomcat stand alone
         analyzes the HTTP headers included with the request, and passes them
         on to the appropriate Host (virtual host).
         Documentation at /docs/config/engine.html -->

    <!-- You should set jvmRoute to support load-balancing via AJP ie :
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
    -->
    <Engine name="Catalina" defaultHost="localhost">

      <!--For clustering, please take a look at documentation at:
          /docs/cluster-howto.html  (simple how to)
          /docs/config/cluster.html (reference documentation) -->
      <!--
      <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
      -->

      <!-- Use the LockOutRealm to prevent attempts to guess user passwords
           via a brute-force attack -->
      <Realm className="org.apache.catalina.realm.LockOutRealm">
        <!-- This Realm uses the UserDatabase configured in the global JNDI
             resources under the key "UserDatabase".  Any edits
             that are performed against this UserDatabase are immediately
             available for use by the Realm.  -->
        <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
               resourceName="UserDatabase"/>
      </Realm>

      <Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">

        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->
        <Context path="" docBase="/home/{{ pillar['user'] }}/webapps/{{ pillar['user'] }}"></Context>
        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />

      </Host>
    </Engine>
  </Service>
</Server>
代码语言:javascript
复制
[root@zabbix file]# cat publish.sh
#!/bin/bash
NAME={{ pillar['user'] }}
WAR_NAME={{ pillar['pkg'] }}
rm -rf /home/$NAME/webapps/$NAME/*
unzip -d /home/$NAME/webapps/$NAME /home/$NAME/$WAR_NAME
ps -ef | grep  $NAME  |grep java | grep -v grep | awk '{print $2}' |xargs kill -9
echo "/home/$NAME/tomcat/bin/startup.sh"
sleep 3s
/home/$NAME/tomcat/bin/startup.sh
代码语言:javascript
复制
[root@zabbix file]# cat start.sh
#!/bin/bash
PROJECT_HOME=/home/{{ pillar['user'] }}
PORT={{ pillar['port'] }}
JAR_NAME={{ pillar['pkg'] }}
ps -ef|grep $JAR_NAME|grep java|grep -v grep|awk '{print $2}'|xargs kill -9
nohup java -Dserver.port=$PORT -Xms256m -Xmx256m -verbose:gc -XX:+PrintGCDetails -XX:NativeMemoryTracking=detail -XX:+PrintGCTimeStamps -XX:+UseParallelGC -XX:+AggressiveOpts -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -XX:+AlwaysPreTouch -Dlog4j.shutdownHookEnabled=false -XX:+DisableExplicitGC -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=256m -Dfile.encoding=UTF-8 -jar $PROJECT_HOME/$JAR_NAME > nohup.out 2>&1 &
  1. 自动创建项目命令如下:

v1代表是服务器,pillar后面的参数是属于key-value形式的参数,user代表项目启动用户,type指定是激spring项目还是tomcat项目,port代表端口,pkg代表包名

代码语言:javascript
复制
salt 'v1' state.sls project pillar='{"user": "elastic-job", "type":"spring","port":8086,"pkg":"elastic-job.jar"}'

salt 'v5' state.sls project pillar='{"user": "gateway-kunlai", "type":"tomcat","port":8065,"pkg":"gateway-kunlai.war"}
  1. 整体目录结构如下:
代码语言:javascript
复制
[root@zabbix base]# tree project/
project/
├── file
│   ├── apache-tomcat-9.0.11
│   │   ├── bin
│   │   │   ├── bootstrap.jar
│   │   │   ├── catalina.sh
│   │   │   ├── catalina-tasks.xml
│   │   │   ├── ciphers.sh
│   │   │   ├── commons-daemon.jar
│   │   │   ├── commons-daemon-native.tar.gz
│   │   │   ├── configtest.sh
│   │   │   ├── daemon.sh
│   │   │   ├── digest.sh
│   │   │   ├── makebase.sh
│   │   │   ├── setclasspath.sh
│   │   │   ├── shutdown.sh
│   │   │   ├── startup.sh
│   │   │   ├── tomcat-juli.jar
│   │   │   ├── tomcat-native.tar.gz
│   │   │   ├── tool-wrapper.sh
│   │   │   └── version.sh
│   │   ├── BUILDING.txt
│   │   ├── conf
│   │   │   ├── Catalina
│   │   │   │   └── localhost
│   │   │   ├── catalina.policy
│   │   │   ├── catalina.properties
│   │   │   ├── context.xml
│   │   │   ├── jaspic-providers.xml
│   │   │   ├── jaspic-providers.xsd
│   │   │   ├── logging.properties
│   │   │   ├── server.xml
│   │   │   ├── tomcat-users.xml
│   │   │   ├── tomcat-users.xsd
│   │   │   └── web.xml
│   │   ├── CONTRIBUTING.md
│   │   ├── lib
│   │   │   ├── annotations-api.jar
│   │   │   ├── catalina-ant.jar
│   │   │   ├── catalina-ha.jar
│   │   │   ├── catalina.jar
│   │   │   ├── catalina-storeconfig.jar
│   │   │   ├── catalina-tribes.jar
│   │   │   ├── ecj-4.7.3a.jar
│   │   │   ├── el-api.jar
│   │   │   ├── jasper-el.jar
│   │   │   ├── jasper.jar
│   │   │   ├── jaspic-api.jar
│   │   │   ├── jsp-api.jar
│   │   │   ├── servlet-api.jar
│   │   │   ├── tomcat-api.jar
│   │   │   ├── tomcat-coyote.jar
│   │   │   ├── tomcat-dbcp.jar
│   │   │   ├── tomcat-i18n-es.jar
│   │   │   ├── tomcat-i18n-fr.jar
│   │   │   ├── tomcat-i18n-ja.jar
│   │   │   ├── tomcat-i18n-ru.jar
│   │   │   ├── tomcat-jdbc.jar
│   │   │   ├── tomcat-jni.jar
│   │   │   ├── tomcat-util.jar
│   │   │   ├── tomcat-util-scan.jar
│   │   │   ├── tomcat-websocket.jar
│   │   │   └── websocket-api.jar
│   │   ├── LICENSE
│   │   ├── logs
│   │   ├── NOTICE
│   │   ├── README.md
│   │   ├── RELEASE-NOTES
│   │   ├── RUNNING.txt
│   │   ├── temp
│   │   ├── webapps
│   │   └── work
│   ├── bashrc
│   ├── publish.sh
│   ├── server.xml
│   ├── start.sh
│   ├── tomcat-9.0.11.zip
│   └── tomcat-9.0.11.zip.20200424
├── init.sls
├── project.sls
├── test
│   └── apache-tomcat-9.0.11
│       ├── bin
│       │   ├── bootstrap.jar
│       │   ├── catalina.sh
│       │   ├── catalina-tasks.xml
│       │   ├── ciphers.sh
│       │   ├── commons-daemon.jar
│       │   ├── commons-daemon-native.tar.gz
│       │   ├── configtest.sh
│       │   ├── daemon.sh
│       │   ├── digest.sh
│       │   ├── makebase.sh
│       │   ├── setclasspath.sh
│       │   ├── shutdown.sh
│       │   ├── startup.sh
│       │   ├── tomcat-juli.jar
│       │   ├── tomcat-native.tar.gz
│       │   ├── tool-wrapper.sh
│       │   └── version.sh
│       ├── BUILDING.txt
│       ├── conf
│       │   ├── Catalina
│       │   │   └── localhost
│       │   ├── catalina.policy
│       │   ├── catalina.properties
│       │   ├── context.xml
│       │   ├── jaspic-providers.xml
│       │   ├── jaspic-providers.xsd
│       │   ├── logging.properties
│       │   ├── server.xml
│       │   ├── tomcat-users.xml
│       │   ├── tomcat-users.xsd
│       │   └── web.xml
│       ├── CONTRIBUTING.md
│       ├── lib
│       │   ├── annotations-api.jar
│       │   ├── catalina-ant.jar
│       │   ├── catalina-ha.jar
│       │   ├── catalina.jar
│       │   ├── catalina-storeconfig.jar
│       │   ├── catalina-tribes.jar
│       │   ├── ecj-4.7.3a.jar
│       │   ├── el-api.jar
│       │   ├── jasper-el.jar
│       │   ├── jasper.jar
│       │   ├── jaspic-api.jar
│       │   ├── jsp-api.jar
│       │   ├── servlet-api.jar
│       │   ├── tomcat-api.jar
│       │   ├── tomcat-coyote.jar
│       │   ├── tomcat-dbcp.jar
│       │   ├── tomcat-i18n-es.jar
│       │   ├── tomcat-i18n-fr.jar
│       │   ├── tomcat-i18n-ja.jar
│       │   ├── tomcat-i18n-ru.jar
│       │   ├── tomcat-jdbc.jar
│       │   ├── tomcat-jni.jar
│       │   ├── tomcat-util.jar
│       │   ├── tomcat-util-scan.jar
│       │   ├── tomcat-websocket.jar
│       │   └── websocket-api.jar
│       ├── LICENSE
│       ├── logs
│       ├── NOTICE
│       ├── README.md
│       ├── RELEASE-NOTES
│       ├── RUNNING.txt
│       ├── temp
│       ├── webapps
│       └── work
├── tomcat-9.0.11.zip
├── tomcat-9.0.11.zip.20200424
└── user.sls

22 directories, 131 files
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-05-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档