saltstack之yum简单部署lnmp

Posted 西红柿圆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了saltstack之yum简单部署lnmp相关的知识,希望对你有一定的参考价值。

今天使用salt简单部署lnmp

部署环境:

node23.com 192.168.0.23 master/minion 部署nginxphp
node24.com 192.168.0.24 minion 部署mysql

首先看一下结构目录:

# tree 
.
├── dev
│   ├── files
│   │   ├── httpd.conf
│   │   └── my.cnf
│   └── lamp.sls
├── mysql
│   ├── my.cnf
│   └── mysql_installed.sls
├── nginx
│   ├── fastcgi_params
│   ├── nginx.conf
│   ├── nginx_installed.sls
│   └── nginx_logrotate_conf
├── php
│   ├── php-fpm.conf
│   ├── php.ini
│   └── php_installed.sls
└── top.sls

5 directories, 13 files

发布目录为/src/salt
入口文件top.sls必须存在,其他的各个应用单独一个目录
top.sls

# cat top.sls 
base:
  \'node23.com\':                 #匹配目标,可写组、正则、或minion
    - nginx.nginx_installed     #表示在那一个目录下的那一个文件。这里是nginx目录下面的nginx_installed.sls
    - php.php_installed
  \'node24.com\':
    - mysql.mysql_installed

nginx_installed.sls:

# cat nginx/nginx_installed.sls 
nginx_installed:                            #id 
  pkg.installed:                            #使用pkg安装模块的installed方法
    - name: nginx                           #包名,如果没有将继承id名称
  file.managed:                             #使用file模块的managed方法管理文件
    - name: /etc/nginx/nginx.conf           #文件名 
    - source: salt://nginx/nginx.conf       #base下那一个文件   salt://表示base环境位置
    - user: root                        
    - group: root
    - mode: 644
    - backup: minion                        #将上一个版本的文件备份。(如果有的话)
    - require:                              #依赖某个服务这里依赖pkg的nginx
      - pkg: nginx_installed
  service.running:                          #使用service服务模块的running方法 
    - name: nginx                           #服务名字
    - enable: True                          #是否启动 True启动 flase 不启动
    - reload: True                          #重新加载服务
    - watch:                                #监控某个状态,这里表示如果监控到nginx.conf发生变化就重新加载服务
      - file: /etc/nginx/nginx.conf
    - require:
      - pkg: nginx_installed

php_installed.sls

# cat php/php_installed.sls 
php_installed:
  pkg.installed:
    - names:
      - php-xcache
      - php-fpm
      - php-common
      - php-mysql
      - php-pdo
  file.managed:
    - name: /etc/php.ini
    - source: salt://php/php.ini
    - user: root
    - group: root
    - mode: 644
    - backup: minion
    - require:
      - pkg: php-fpm
  service.running:
    - name: php-fpm
    - enable: True
    - require:
      - pkg: php-fpm

mysql_installed.sls

# cat mysql/mysql_installed.sls 
mysql_installed:
  pkg.installed:
    - names:
      - mysql
      - mysql-server
      - mysql-devel
    - require_in:
      - file: /etc/my.cnf 
  file.managed:
    - name: /etc/my.cnf
    - source: salt://mysql/my.cnf
    - user: root
    - group: root
    - mode: 644
    - backup: minion
    - watch_in: 
      - service: mysqld
  service.running:
    - name: mysqld
    - enable: True
    - reload: True

测试

# salt \'*\' state.highstate
node23.com:
----------
          ID: nginx_installed
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: Package nginx is already installed.
     Started: 17:39:41.833630
    Duration: 791.416 ms
     Changes:   
----------
          ID: nginx_installed
    Function: file.managed
        Name: /etc/nginx/nginx.conf
      Result: True
     Comment: File /etc/nginx/nginx.conf is in the correct state
     Started: 17:39:42.627688
    Duration: 2.519 ms
     Changes:   
----------
          ID: nginx_installed
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx is already enabled, and is in the desired state
     Started: 17:39:42.630797
    Duration: 30.11 ms
     Changes:   
----------
          ID: php_installed
    Function: pkg.installed
        Name: php-common
      Result: True
     Comment: Package php-common is already installed.
     Started: 17:39:42.661136
    Duration: 0.556 ms
     Changes:   
----------
          ID: php_installed
    Function: pkg.installed
        Name: php-pdo
      Result: True
     Comment: Package php-pdo is already installed.
     Started: 17:39:42.661791
    Duration: 0.268 ms
     Changes:   
----------
          ID: php_installed
    Function: pkg.installed
        Name: php-fpm
      Result: True
     Comment: Package php-fpm is already installed.
     Started: 17:39:42.662119
    Duration: 0.255 ms
     Changes:   
----------
          ID: php_installed
    Function: pkg.installed
        Name: php-mysql
      Result: True
     Comment: Package php-mysql is already installed.
     Started: 17:39:42.662437
    Duration: 0.261 ms
     Changes:   
----------
          ID: php_installed
    Function: pkg.installed
        Name: php-xcache
      Result: True
     Comment: Package php-xcache is already installed.
     Started: 17:39:42.662998
    Duration: 0.299 ms
     Changes:   
----------
          ID: php_installed
    Function: file.managed
        Name: /etc/php.ini
      Result: True
     Comment: File /etc/php.ini is in the correct state
     Started: 17:39:42.663639
    Duration: 2.351 ms
     Changes:   
----------
          ID: php_installed
    Function: service.running
        Name: php-fpm
      Result: True
     Comment: Service php-fpm is already enabled, and is in the desired state
     Started: 17:39:42.666125
    Duration: 29.658 ms
     Changes:   

Summary
-------------
Succeeded: 10
Failed:     0
-------------
Total states run:     10
node24.com:
----------
          ID: mysql_installed
    Function: pkg.installed
        Name: mysql-devel
      Result: True
     Comment: Package mysql-devel is already installed.
     Started: 01:23:27.757810
    Duration: 1097.571 ms
     Changes:   
----------
          ID: mysql_installed
    Function: pkg.installed
        Name: mysql-server
      Result: True
     Comment: Package mysql-server is already installed.
     Started: 01:23:28.855545
    Duration: 0.425 ms
     Changes:   
----------
          ID: mysql_installed
    Function: pkg.installed
        Name: mysql
      Result: True
     Comment: Package mysql is already installed.
     Started: 01:23:28.856036
    Duration: 0.261 ms
     Changes:   
----------
          ID: mysql_installed
    Function: file.managed
        Name: /etc/my.cnf
      Result: True
     Comment: File /etc/my.cnf is in the correct state
     Started: 01:23:28.858598
    Duration: 2.324 ms
     Changes:   
----------
          ID: mysql_installed
    Function: service.running
        Name: mysqld
      Result: True
     Comment: Service mysqld is already enabled, and is in the desired state
     Started: 01:23:28.861334
    Duration: 47.126 ms
     Changes:   

Summary
------------
Succeeded: 5
Failed:    0
------------
Total states run:     5

查看服务状态

# salt \'node24.com\' service.status mysqld
node24.com:
    True

o了服务安装完成,yum只是测试安装,后面我们会进行编译安装

以上是关于saltstack之yum简单部署lnmp的主要内容,如果未能解决你的问题,请参考以下文章

saltstack入门至放弃之salt安装部署

LNMP环境搭建之--YUM仓库

saltstack-gdlwolf自学总结第一篇:saltstack的安装部署(YUM安装)

Linux下部署saltstack

Saltstack(自动化运维环境部署)

SaltStack 之 快速安装