欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SaltStack之lamt架构状态文件(上)

程序员文章站 2022-03-16 13:48:09
SaltStack之lamt架构状态文件srv文件部署安装环境创建网络源目录创建网络源安装目录编写网络源安装sls文件编写安装LAMT软件sls文件Apache创建Apache目录编写Apache安装sls文件编写Apache安装脚本编写Apache service文件MySQL创建MySQL目录编写MySQL安装sls文件编写Mysql安装脚本编写MySQL service 文件编写MySQL修改密码脚本Tomcat创建Tomcat目录编写Tomcat安装sls文件编写Tomcat service文件LA...



下篇备份

部署lamt的/srv目录结构

[root@master files]# tree -d /srv/.
/srv/.
├── pillar
│   └── base
└── salt
    └── base
        ├── minion
        │   └── files
        ├── modules
        │   ├── app
        │   │   └── tomcat
        │   │       └── files
        │   ├── common
        │   ├── database
        │   │   └── mariadb
        │   │       └── files
        │   ├── web
        │   │   └── apache
        │   │       └── files
        │   └── yum
        ├── project
        │   └── files
        └── start
            └── start_files

22 directories

[root@master ~]# tree /srv/
/srv/
├── pillar
│   └── base
│       ├── lamt_150.sls
│       ├── lamt_245.sls
│       └── top.sls
└── salt
    └── base
        ├── minion
        │   ├── files
        │   │   ├── CentOS-Base.repo
        │   │   ├── epel.repo
        │   │   ├── minion
        │   │   └── salt-repo-latest-2.el7.noarch.rpm
        │   ├── install.sls
        │   └── minion_ssh.sh
        ├── modules
        │   ├── app
        │   │   └── tomcat
        │   │       ├── files
        │   │       │   ├── apache-tomcat-9.0.37.tar.gz
        │   │       │   ├── setenv.sh
        │   │       │   └── tomcat.service
        │   │       └── install.sls
        │   ├── common
        │   │   ├── init-install.sls
        │   │   └── yum.sls
        │   ├── database
        │   │   └── mariadb
        │   │       ├── files
        │   │       │   ├── mariadb.service
        │   │       │   ├── mariadb.sh
        │   │       │   ├── mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
        │   │       │   └── mysql_passwd.sh
        │   │       ├── install.sls
        │   │       └── mysql_passwd.sls
        │   ├── web
        │   │   └── apache
        │   │       ├── files
        │   │       │   ├── apache_install.sh
        │   │       │   ├── apr-1.7.0.tar.bz2
        │   │       │   ├── apr-util-1.6.1.tar.bz2
        │   │       │   ├── httpd-2.4.43.tar.bz2
        │   │       │   ├── httpd.conf
        │   │       │   └── httpd.service
        │   │       └── install.sls
        │   └── yum
        │       ├── CentOS-Base.repo
        │       └── epel.repo
        ├── project
        │   ├── files
        │   │   ├── httpd-vhosts.conf
        │   │   ├── index.html
        │   │   └── lamt.conf
        │   └── lamt.sls
        ├── start
        │   ├── lamt_start.sls
        │   └── start_files
        │       ├── httpd_start.sls
        │       ├── mysql_start.sls
        │       └── tomcat_start.sls
        └── top.sls

22 directories, 39 files



1. 部署安装环境

1.1 创建网络源目录

[root@master ~]# mkdir -p /srv/salt/base/modules/yum/

[root@master yum]# ls
CentOS-Base.repo  epel.repo

1.2 创建网络源安装目录

[root@master ~]# mkdir -p /srv/salt/base/modules/common/

[root@master common]# ls
init-install.sls  yum.sls

1.3 编写网络源安装sls文件

[root@master common]# vim yum.sls 
/etc/yum.repos.d/CentOS-Base.repo:
  file.managed:
    - source: salt://modules/yum/CentOS-Base.repo
    - user: root
    - group: root
    - mode: 0644
  
/etc/yum.repos.d/epel.repo:
  file.managed:
    - source: salt://modules/yum/epel.repo
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: yum clean all && yum makecache fast

1.4 编写安装LAMT软件sls文件

[root@master common]# vim init-install.sls 

init-install:
  pkg.installed:
    - pkgs:
      - ncurses-devel 
      - openssl-devel 
      - openssl 
      - cmake 
      - mariadb-devel
      - vim-minimal
      - pcre-devel 
      - expat-devel 
      - libtool 
      - gcc 
      - gcc-c++
      - wget
      - bzip2
      - java-1.8.0-openjdk 
      - java-1.8.0-openjdk-devel 
  cmd.run:
    - name: yum groups mark install "Development Tools"


2. Apache

2.1 创建Apache目录

[root@master ~]# mkdir -p /srv/salt/base/modules/web/apache/files/

[root@master ~]# cd /srv/salt/base/modules/web/apache/

2.2 编写Apache安装sls文件

[root@master apache]# vim install.sls 

apache-user:
  user.present:
    - name: apache
    - system: True
    - shell: /sbin/nologin
    - createhome: False


/usr/src/apr-1.7.0.tar.bz2:
  file.managed:
    - source: salt://modules/web/apache/files/apr-1.7.0.tar.bz2
    - user: root
    - group: root
    - mode: 0644


/usr/src/apr-util-1.6.1.tar.bz2:
  file.managed:
    - source: salt://modules/web/apache/files/apr-util-1.6.1.tar.bz2
    - user: root
    - group: root
    - mode: 0644


/usr/src/httpd-2.4.43.tar.bz2:
  file.managed:
    - source: salt://modules/web/apache/files/httpd-2.4.43.tar.bz2
    - user: root
    - group: root
    - mode: 0644


unpack_httpd_packages:
  cmd.run:
    - name: cd /usr/src/ && tar xf apr-1.7.0.tar.bz2 -C /usr/local && tar xf apr-util-1.6.1.tar.bz2 -C /usr/local && tar xf httpd-2.4.43.tar.bz2 -C /usr/local && sed -i '/$RM "$cfgfile"/d' /usr/local/apr-1.7.0/configure
    - require:
      - file: /usr/src/httpd-2.4.43.tar.bz2
    - unless: test -d /usr/local/apr-1.7.0


/tmp/apache_install.sh:
  file.managed:
    - source: salt://modules/web/apache/files/apache_install.sh
    - user: root
    - group: root
    - mode: 755
  cmd.run:
    - name: /bin/bash /tmp/apache_install.sh
    - require:
      - cmd: unpack_httpd_packages    
    - unless: test -d /usr/local/apache 



/usr/lib/systemd/system/httpd.service:
  file.managed:
    - source: salt://modules/web/apache/files/httpd.service
    - user: root
    - group: root
    - mode: 644
  cmd.run:
    - name: systemctl daemon-reload
    - require:
      - cmd: /tmp/apache_install.sh


Apache所需软件以及安装脚本

[root@master apache]# cd files/
[root@master files]# ls
apache_install.sh  apr-util-1.6.1.tar.bz2  httpd.conf
apr-1.7.0.tar.bz2  httpd-2.4.43.tar.bz2    httpd.service

2.3 编写Apache安装脚本

[root@master files]# vim apache_install.sh 
cd /usr/local/apr-1.7.0 && \
./configure --prefix=/usr/local/apr &> /dev/null && \
make -j $(nproc) &> /dev/null && make install &> /dev/null && \
cd /usr/local/apr-util-1.6.1 && \
./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr &> /dev/null && \
make -j $(nproc) &> /dev/null && make install &> /dev/null  && \
cd /usr/local/httpd-2.4.43 && \
./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork &> /dev/null && \
make -j $(nproc) &> /dev/null && make install &> /dev/null 

2.4 编写Apache service文件

[root@master files]# vim httpd.service 
[Unit]
Description=Start httpd
[Service]
Type=simple
EnvironmentFile=/etc/httpd24/httpd.conf
ExecStart=/usr/local/apache/bin/httpd -k start -DFOREGROUND
ExecReload=/usr/local/apache/bin/httpd -k graceful
ExecStop=/bin/kill -WINCH ${MAINPID}
[Install]
WantedBy=multi-user.target


3. MySQL

3.1 创建MySQL目录

[root@master ~]# mkdir -p /srv/salt/base/modules/database/mariadb/files/

[root@master ~]# cd /srv/salt/base/modules/database/mariadb/

3.2 编写MySQL安装sls文件

[root@master mariadb]# vim install.sls 

mysql-user:
  user.present:
    - name: mysql
    - system: True
    - shell: /sbin/nologin
    - createhome: False


/usr/src/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz:
  file.managed:
    - source: salt://modules/database/mariadb/files/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
    - user: root
    - group: root
    - mode: 0644


unpack_packages:
  cmd.run:
    - name: cd /usr/src/ && tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local
    - require:
      - file: /usr/src/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
    - unless: test -d /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64


/usr/local/mysql:
  file.symlink:
    - target: /usr/local/mysql-5.7.30-linux-glibc2.12-x86_64
    - require:
      - cmd: unpack_packages


/opt/mysql-database:
  file.directory:
    - user: mysql
    - group: mysql
    - mode: 0755
    - makedirs: True
    - unless: test -d /opt/mysql-database


mysql-environment:
  cmd.run:
    - name: echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh  


/tmp/mariadb.sh:
  file.managed:
    - source: salt://modules/database/mariadb/files/mariadb.sh
    - user: root
    - group: root
    - mode: 0755
  cmd.run:
    - name: /bin/bash /tmp/mariadb.sh



/usr/lib/systemd/system/mariadb.service:
  file.managed:
    - source: salt://modules/database/mariadb/files/mariadb.service
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: systemctl daemon-reload


安装Mysql所需软件以及安装脚本

[root@master mariadb]# cd files/
[root@master files]# ls
mariadb.service  mariadb.sh  mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz  mysql_passwd.sh

3.3 编写MySQL安装脚本

[root@master files]# vim mariadb.sh 

#!/bin/bash
data=/opt/mysql-database

if [ `ls $data | wc -l` -eq 0 ];then
/usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=$data &> /dev/null 
fi


cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = $data
socket = /tmp/mysql.sock
port = 3306
pid-file = $data/mysql.pid
user = mysql
skip-name-resolve
EOF

3.4 编写MySQL service 文件

[root@master files]# vim mariadb.service 

[Unit]
Description=MySQL Server
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
TimeoutSec=0
PermissionsStartOnly=true
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --daemonize
LimitNOFILE = 65535
Restart=on-failure
RestartSec=3
RestartPreventExitStatus=1
PrivateTmp=false

3.5 编写MySQL修改密码脚本

[root@master files]# cat mysql_passwd.sh 
basedir=/usr/local
password=1
echo "mysql密码为1"
$basedir/mysql/bin/mysql  -e "set password=password('$password');" &> /dev/null


4. Tomcat

4.1 创建Tomcat目录

[root@master ~]# mkdir -p /srv/salt/base/modules/app/tomcat/files/

[root@master ~]# cd /srv/salt/base/modules/app/tomcat

4.2 编写Tomcat安装sls文件

[root@master tomcat]# vim install.sls 

/usr/src/apache-tomcat-9.0.37.tar.gz:
  file.managed:
    - source: salt://modules/app/tomcat/files/apache-tomcat-9.0.37.tar.gz
    - user: root
    - group: root
    - mode: 0644



unpack_tomcat_packages:
  cmd.run:
    - name : cd /usr/src && tar xf apache-tomcat-9.0.37.tar.gz -C /usr/local
    - require:
      - file: /usr/src/apache-tomcat-9.0.37.tar.gz 
    - unless: test -d /usr/local/apache-tomcat-9.0.37


/usr/local/tomcat:
  file.symlink:
    - target: /usr/local/apache-tomcat-9.0.37


/usr/local/tomcat/bin/setenv.sh:
  file.managed:
    - source: salt://modules/app/tomcat/files/setenv.sh
    - user: root
    - group: root
    - mode: 0644
    - require:
      - file: /usr/local/tomcat


/usr/lib/systemd/system/tomcat.service:
  file.managed:
    - source: salt://modules/app/tomcat/files/tomcat.service
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: systemctl daemon-reload
    - require:
      - file: /usr/local/tomcat/bin/setenv.sh


安装Tomcat所需软件以及安装脚本

[root@master tomcat]# cd files/
[root@master files]# ls
apache-tomcat-9.0.37.tar.gz  setenv.sh  tomcat.service

4.3 编写Tomcat service文件

[root@master files]# vim setenv.sh 
#add tomcat pid
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#add java opts
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

[root@master files]# vim tomcat.service 
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/usr/local/tomcat/tomcat.pid
ExecStart=/usr/local/tomcat/bin/startup.sh 
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target


5. 部署LAMT架构

5.1 编写Apache主配置文件

  • 注释监听端口
  • 取消注释- -LoadModule proxy_module modules/mod_proxy.so
  • 取消注释- -LoadModule proxy_http_module modules/mod_proxy_http.so
  • 取消注释- -指定虚拟主机目录
[root@master ~]# cd /srv/salt/base/modules/web/apache/files/
[root@master files]# vim httpd.conf 
.....
#Listen 80
.....
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
......
Include /etc/httpd24/project/*.conf

5.2 创建LAMT项目目录

[root@master ~]# mkdir -p /srv/salt/base/project/files/

[root@master ~]# cd /srv/salt/base/project/

[root@master project]# ls
files  lamt.sls

5.3 编写LAMT部署sls文件

[root@master project]# vim lamt.sls 
include:
  - modules.web.apache.install
  - modules.database.mariadb.install
  - modules.app.tomcat.install

/etc/httpd24/httpd.conf:
  file.managed:
    - source: salt://modules/web/apache/files/httpd.conf
    - user: root
    - group: root
    - mode: 0644
    - template: jinja


/etc/httpd24/project:
  file.directory:
    - user: root
    - group: root
    - mode: 755
    - makedirs: True


/etc/httpd24/project/lamt.conf:
  file.managed:
    - source: salt://project/files/lamt.conf
    - user: root
    - group: root
    - mode: 0644
    - template: jinja


部署lamt所需文件

[root@master project]# cd files/
[root@master files]# ls
httpd-vhosts.conf  index.html  lamt.conf

5.4 编写LAMT虚拟主机文件

[root@master files]# vim lamt.conf 
# Virtual Hosts
#
# Required modules: mod_log_config

# If you want to maintain multiple domains/hostnames on your
# machine you can setup VirtualHost containers for them. Most configurations
# use only name-based virtual hosts so the server doesn't need to worry about
# IP addresses. This is indicated by the asterisks in the directives below.
#
# Please see the documentation at 
# <URL:http://httpd.apache.org/docs/2.4/vhosts/>
# for further details before you try to setup virtual hosts.
#
# You may use the command line option '-S' to verify your virtual host
# configuration.

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#

listen {{ pillar['APACHE_PORT'] }} 
<VirtualHost *:{{ pillar['APACHE_PORT'] }}>
DocumentRoot "/usr/local/apache/htdocs/"
    ServerName lamt.com
    ProxyPass /  http://{{ pillar['Tomcat_IP'] }}:8080/    
    ProxyPassReverse /  http://{{ pillar['Tomcat_IP'] }}:8080/
    <Directory "/usr/local/apache/project/">
        Options none
        AllowOverride none
        Require all granted
    </Directory>
</VirtualHost>

5.5 创建pillar目录

[root@master ~]# mkdir -p /srv/pillar/base/

[root@master ~]# cd /srv/pillar/base/

[root@master base]# ls
lamt_150.sls  lamt_245.sls  top.sls

5.6 设置变量

//如果想在部署一台lamt,需要在创建一个sls文件
[root@master base]# vim lamt_150.sls 
APACHE_PORT: 80
Tomcat_IP: 192.168.30.150

5.7 编写pillar top.sls

//添加进top.sls

[root@master base]# vim top.sls 
base:              
  '192.168.30.245':                   
    - lamt_245
  'zyy180':
    - lamt_150


5.8 编写启动lamt-sls文件

[root@master common]# mkdir -p /srv/salt/base/start/start_files/

[root@master common]# cd /srv/salt/base/start/

[root@master start]# ls
lamt_start.sls  start_files

[root@master start]# vim lamt_start.sls 
include:
  - start.start_files.httpd_start
  - start.start_files.mysql_start
  - start.start_files.tomcat_start


启动lamt所需文件

[root@master start]# cd start_files/
[root@master start_files]# ls
httpd_start.sls  mysql_start.sls  tomcat_start.sls

[root@master start_files]# vim httpd_start.sls 
httpd-service:
  service.running:
    - name: httpd
    - enable: True


[root@master start_files]# vim mysql_start.sls 
mariadb.service:
  service.running:
    - name: mariadb
    - enable: True


[root@master start_files]# vim tomcat_start.sls 
tomcat-service:
  service.running:
    - name: tomcat
    - enable: True

5.9 编写top.sls

[root@master ~]# cd /srv/salt/base/


[root@master base]# cat top.sls 
base:
  '*':
    - modules.common.yum
    - modules.common.init-install
    - project.lamt
    - start.lamt_start
    - modules.database.mariadb.mysql_passwd


6. 扩展:编写salt-ssh脚本

[root@master ~]# mkdir -p /srv/salt/base/minion/files/

[root@master ~]# cd /srv/salt/base/minion/

[root@master minion]# ls
files  install.sls  minion_ssh.sh

[root@master minion]# vim minion_ssh.sh 

#!/bin/bash

echo "正在安装salt-ssh" 
yum -y install salt-ssh &> /dev/null
echo "salt-ssh安装完成!"

read -p "请输入minion的类型:" ID
read -p "请输入minion的IP:" IP
read -p "请输入minion的passwd:" passwd

cat /etc/salt/roster | grep $IP &> /dev/null
if [ $? -ne 0 ];then
cat >> /etc/salt/roster << EOF
$ID:
  host: $IP
  user: root
  passwd: $passwd
EOF
fi

echo 'StrictHostKeyChecking no' > /root/.ssh/config

[root@master minion]# vim install.sls 

/etc/yum.repos.d/CentOS-Base.repo:
  file.managed:
    - source: salt://minion/files/CentOS-Base.repo
    - user: root
    - group: root
    - mode: 0644

/etc/yum.repos.d/epel.repo:
  file.managed:
    - source: salt://minion/files/epel.repo
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: yum clean all && yum makecache fast



/tmp/salt-repo-latest-2.el7.noarch.rpm:
  file.managed:
    - source: salt://minion/files/salt-repo-latest-2.el7.noarch.rpm
    - user: root
    - group: root
    - mode: 0644
  cmd.run:
    - name: yum -y install /tmp/salt-repo-latest-2.el7.noarch.rpm


minion_install:
  pkg.installed:
    - name: salt-minion



/etc/salt/minion:
  file.managed:
    - source: salt://minion/files/minion
    - user: root
    - group: root
    - mode: 0644


salt-minion:
  service.running:
    - name: salt-minion
    - enable: True
    - watch:
      - file: /etc/salt/minion

[root@master minion]# cd files/

[root@master files]# ls
CentOS-Base.repo  epel.repo  minion  salt-repo-latest-2.el7.noarch.rpm

[root@master files]# vim minion 
......
master: 192.168.30.150    //因此想改master端,改一下IP即可




本文地址:https://blog.csdn.net/zyy130988/article/details/108214300