使用SaltStack自动化部署LNMP

文章目录

    • 一、文件以树状图格式显示
    • 二、修改master配置文件
    • 三、nginx安装
    • 四、mysql安装
    • 五、php安装
    • 六、编写lnmp的部署文件
    • 七、web页面访问

环境说明:

主机 IP 服务
master 192.168.91.135 salt-matser
node1 192.168.91.137 salt-minion

一、文件以树状图格式显示

[root@master prod]# tree lnmp/
lnmp/
|-- files
|   |-- index.php
|   |-- my.cnf
|   `-- nginx.conf
|-- install.sls
|-- mysql.sls
`-- nginx.sls

1 directory, 6 files


[root@master prod]# tree modules/
modules/
|-- application
|   `-- php
|       |-- files
|       |   |-- install.sh
|       |   |-- oniguruma-devel-6.8.2-2.el8.x86_64.rpm
|       |   |-- php-7.4.24.tar.xz
|       |   |-- php-fpm
|       |   |-- php-fpm.conf
|       |   |-- php-fpm.service
|       |   `-- www.conf
|       `-- install.sls
|-- database
|   `-- mysql
|       |-- files
|       |   |-- install.sh
|       |   |-- my.cnf
|       |   |-- mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
|       |   |-- mysql.server
|       |   `-- mysqld.service
|       `-- install.sls
`-- web
    |-- apache
    |   |-- config.sls
    |   |-- files
    |   |   |-- apr-1.7.0.tar.gz
    |   |   |-- apr-util-1.6.1.tar.gz
    |   |   |-- httpd-2.4.51.tar.gz
    |   |   |-- httpd.conf
    |   |   |-- httpd.service
    |   |   |-- index.php
    |   |   `-- install.sh
    |   `-- install.sls
    `-- nginx
        |-- files
        |   |-- install.sh
        |   |-- nginx-1.20.1.tar.gz
        |   `-- nginx.service
        `-- install.sls

11 directories, 27 files

二、修改master配置文件

[root@master prod]# vim /etc/salt/master
# highstate format, and is generally just key/value pairs.
pillar_roots:
  base:
    - /srv/pillar/base
  prod:						// 添加这两行
    - /srv/pillar/prod
#
#ext_pillar:

// 创建目录
[root@master prod]# cd /srv/pillar/
[root@master pillar]# mkdir prod
[root@master pillar]# systemctl restart salt-master.service 

// 变量存放位置
[root@master pillar]# tree prod/
prod/
|-- mysql.sls
|-- nginx.sls
|-- php.sls
`-- top.sls

0 directories, 4 files


三、nginx安装

[root@master pillar]# cd
[root@master ~]# cat /srv/salt/prod/modules/web/nginx/install.sls 
"Development Tools:
  pkg.group_installed

usradd.nginx:
  user.present:
    - name: nginx
    - shell: /sbin/nologin
    - system: true
    - createhome: false

nginx-depend-packages:
  pkg.installed:
    - pkgs:
      - pcre-devel
      - openssl
      - openssl-devel
      - gd-devel
      - gcc
      - gcc-c++

copy_nginx:
  file.managed:
    - names:
      - /usr/src/nginx-1.20.1.tar.gz
        - source: salt://modules/web/nginx/files/nginx-1.20.1.tar.gz
      - /usr/lib/systemd/system/nginx.service:
        - source: salt://modules/web/nginx/files/nginx.service
        - template: jinjia

install_nginx:
  cmd.script:
    - name: salt://modules/web/nginx/files/install.sh {
     {
      pillar['php_dir'] }}
    - unless: test -d {
     {
      pillar['php_dir'] }}

四、mysql安装

[root@master ~]# cat /srv/salt/prod/modules/database/mysql/install.sls 
ncurses-compat-libs:
  pkg.installed

create-mysql-user:
  user.present:
    - name: mysql
    - system: true
    - createhome: false
    - shell: /sbin/nologin
   
create-datadir:
  file.directory:
    - name: /opt/data
    - user: mysql
    - group: mysql
    - mode: '0755'

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

install_mysql:
  cmd.script:
    - name: salt://modules/database/mysql/files/install.sh
    - unless: test -d /usr/local/mysql

trasfer-files:
  file.managed:
    - names:
      - /usr/local/mysql/support-files/mysql.server:
        - source: salt://modules/database/mysql/files/mysql.server
        - user: root
        - group: root
        - mode: '0644'
      - {
     {
      pillar['mysql_dir'] }}/support-files/mysql.server:
        - source: salt://modules/database/mysql/files/mysqld.service
        - user: mysql
        - group: mysql
        - mode: '0755'
        - template: jinja
    - require:
      - cmd: install_mysql  

五、php安装

[root@master ~]# cat /srv/salt/prod/modules/application/php/install.sls 
/usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm:
  file.managed:
    - source: salt://modules/application/php/files/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
    - user: root
    - group: root
    - mode: '0644'
  cmd.run:
    - name: yum -y install /usr/src/oniguruma-devel-6.8.2-2.el8.x86_64.rpm
    - unless: rpm -q oniguruma-devel

depend-package-install:
  pkg.installed:
    - pkgs:
      - libxml2
      - libxml2-devel
      - openssl
      - openssl-devel
      - bzip2
      - bzip2-devel
      - libcurl
      - libcurl-devel
      - libicu-devel
      - libjpeg-turbo
      - libjpeg-turbo-devel
      - libpng
      - libpng-devel
      - openldap-devel
      - pcre-devel
      - freetype
      - freetype-devel
      - gmp
      - gmp-devel
      - libmcrypt
      - libmcrypt-devel
      - readline
      - readline-devel
      - libxslt
      - libxslt-devel
      - mhash
      - mhash-devel
      - php-mysqlnd
      - libsqlite3x-devel
      - libzip-devel

/usr/src/php-7.4.24.tar.xz:
  file.managed:
    - source: salt://modules/application/php/files/php-7.4.24.tar.xz 
    - user: root
    - group: root
    - mode: '0644'

install_php:
  cmd.script:
    - name: salt://modules/application/php/files/install.sh {
     {
      pillar['php_dir'] }}
    - unless: test -d /usr/local/php

copy_config:
  file.managed:
    - names: 
       - /etc/init.d/php-fpm:
         - source: salt://modules/application/php/files/php-fpm
         - user: root
         - group: root
         - mode: '0755'
       - {
     {
      pillar['php_dir'] }}/etc/php-fpm.conf:
         - source: salt://modules/application/php/files/php-fpm.conf
         - user: root
         - group: root
         - mode: '0644'
       - {
     {
      pillar['php_dir'] }}/etc/php-fpm.d/www.conf:
         - source: salt://modules/application/php/files/www.conf
         - user: root
         - group: root
         - mode: '0644'
       - /usr/lib/systemd/system/php-fpm.service:
         - source: salt://modules/application/php/files/php-fpm.service
         - user: root
         - group: root
         - mode: '0644' 
    - require:
      - cmd: install_php

php-fpm.service:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: install_php
      - file: copy_config
    - watch:
      - file: copy_config  

六、编写lnmp的部署文件

nginx

[root@master ~]# cat /srv/salt/prod/lnmp/nginx.sls 
include:
  - modules.web.nginx.install

copy_nginx_files:
  file.managed:
    - names:
      - {
     {
      pillar['php_dir'] }}/conf/nginx.conf:
        - source: salt://lnmp/files/nginx.conf
        - user: root
        - group: root
        - mode: '0644'
      - {
     {
      pillar['php_dir'] }}/html/index.php:
        - source: salt://lnmp/files/index.php
      - require:
        - cmd: install_nginx

nginx:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: install_nginx
    - watch:
      - file: copy_nginx_files

mysql

[root@master ~]# cat /srv/salt/prod/lnmp/mysql.sls 
include:
  - modules.database.mysql.install

copy_lnmp_mysql_file:
  file.managed:
    - user: root
    - group: root
    - mode: '0644'
    - names:
      - /etc/my.cnf:
        - source: salt://lnmp/files/my.cnf
      - require:
        - cmd: install_mysql

mysqld:
  service.running:
    - enable: true
    - reload: true
    - require:
      - cmd: install_mysql
      - file: trasfer-files
    - watch:
      - file: copy_lnmp_mysql_file

set_mysql_password:
  cmd.run:
    - name: {
     {
      pillar['mysql_dir'] }}/bin/mysql -e "set password=password('123456');"
    - require:
      - service: mysqld
    - unless: {
     {
      pillar['mysql_dir'] }}/bin/mysql -uroot -p123 -e "exit"

lnmp一键部署

[root@master ~]# cat /srv/salt/prod/lnmp/install.sls 
include:
  - lnmp.nginx
  - lnmp.mysql
  - modules.application.php.install

查看端口号

[root@node1 ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process                                                     
LISTEN 0      128        127.0.0.1:9000        0.0.0.0:*                                                                
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*                                                                
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*                                                                
LISTEN 0      80                 *:3306              *:*                                                                
LISTEN 0      128             [::]:22             [::]:* 

七、web页面访问

使用SaltStack自动化部署LNMP_第1张图片

你可能感兴趣的:(运维,linux,saltstack,lnmp,自动化工具)