saltstack搭建LAMP架构案例

摘要:
phpphpinfo();?
LAMP架构案例

1)环境准备,定义file_roots环境

  这个配置会在我们开始安装salt-master的时候,salt-master配置文件中定义:

root@King: ~# grep -A 5 file_roots /etc/salt/master   
# file_roots:
#   base:
#     - /srv/salt/
#   dev:
#     - /srv/salt/dev/services
#     - /srv/salt/dev/states
--
#file_roots:
#  base:
#    - /srv/salt
file_roots:
  base:
    - /home/salt     # 自定义脚本路径

注:以上的默认路径,可以根据实际要求去修改

2)创建对应环境目录

root@King: ~# mkdir /home/salt/{httpd,php,mysql,files}

3)配置文件准备及测试文件准备

[root@salt-master ~]# cp /etc/my.cnf /home/salt/mysql/files/
[root@salt-master ~]# cp /etc/httpd/conf/httpd.conf /home/salt/httpd/files/
[root@salt-master ~]# cp /etc/php.ini  /home/salt/php/files/
[root@salt-master ~]# echo "<h1>LAMP html</h1>" >>/home/salt/files/index.html
[root@salt-master ~]# echo "<?php phpinfo(); ?>" >> /home/salt/files/index.php

4)编写state sls状态文件

#httpd
[root@salt-master ~]# cat /home/salt/httpd/init.sls
apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - httpd-tools

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://httpd/files/httpd.conf
    - user: root
    - group: root
    - mode: 644

apache-service:
  service.running:
    - name: httpd
    - enable: True

#php
[root@salt-master ~]# cat /home/salt/php/init.sls
php-install:
  pkg.installed:
    - pkgs:
      - php
      - php-mysql
      - php-pdo
      - php-cli

php-config:
  file.managed:
    - name: /etc/php.ini
    - source: salt://php/files/php.ini
    - user: root
    - group: root
    - mode: 644

#mysql
[root@salt-master ~]# cat /home/salt/mysql/init.sls
mariadb-install:
  pkg.installed:
    - pkgs:
      - mariadb-server
      - mariadb

mariadb-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://mysql/files/my.cnf
    - user: root
    - group: root
    - mode: 644

mariadb-service:
  service.running:
    - name: mariadb
    - enable: True

5)编写测试文件

#测试文件
[root@salt-master ~]# cat /home/salt/testfile.sls
/var/www/html/index.html:
  file.managed:
    - source: salt://files/index.html

/var/www/html/index.php:
  file.managed:
    - source: salt://files/index.php

6)topfile文件编写(这个也可以不写)

[root@salt-master ~]# cat /home/salt/base/top.sls
prod:
  'salt-minion*':
    - httpd.init
    - php.init
    - mysql.init
    - testfile

7)部署LAMP整体state文件查看

# 目录层级介绍
/home/salt/
    --files
        --index.html
        --index.php
    --httpd
        --init.sls
--files --httpd.conf --mysql --init.sls
--files --my.conf --php --init.sls
--files --php.ini

8)执行topfile

[root@salt-master ~]# salt '*' state.highstate

或者

[root@salt-master ~]# salt '*' state.sls httpd.init
[root@salt-master ~]# salt '*' state.sls mysql.init
[root@salt-master ~]# salt '*' state.sls php.init
[root@salt-master ~]# salt '*' state.sls testfile
state状态依赖

关系说明:
1、require 我依赖某个状态,我依赖谁
2、require_in 我被某个状态依赖,谁依赖我
3、watch 我关注某个状态,当状态发生改变,进行restart或者reload操作
4、watch_in 我被某个状态关注
5、include 我引用谁

修改上面lamp状态间依赖关系

#httpd
[root@salt-master ~]# cat /home/salt/httpd/init.sls
apache-install:
  pkg.installed:
    - pkgs:
      - httpd
      - httpd-tools

apache-config:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://httpd//files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: apache-install   #表示上面apache-install执行成功,才能执行apache-config

apache-service:
  service.running:
    - name: httpd
    - enable: True
    - require:
      - file: apache-config
    - watch:
      - file: apache-config

#php
[root@salt-master ~]# cat /home/salt/php/init.sls
php-install:
  pkg.installed:
    - pkgs:
      - php
      - php-mysql
      - php-pdo
      - php-cli
    - reqiure_in:
      - file: php-config

php-config:
  file.managed:
    - name: /etc/php.ini
    - source: salt://php/files/php.ini
    - user: root
    - group: root
    - mode: 644

#mysql
[root@salt-master ~]# cat /home/salt/mysql/init.sls
mariadb-install:
  pkg.installed:
    - pkgs:
      - mariadb-server
      - mariadb

mariadb-config:
  file.managed:
    - name: /etc/my.cnf
    - source: salt://mysql/files/my.cnf
    - user: root
    - group: root
    - mode: 644
    - require:
      - pkg: mariadb-install

mariadb-service:
  service.running:
    - name: mariadb
    - enable: True
    - reload: True
    - require:
      - file: mariadb-config
    - watch:
      - file: mariadb-config

免责声明:文章转载自《saltstack搭建LAMP架构案例》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇git恢复被修改的文件bad interpreter:No such file or directory 解决方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

php生成RSA公钥私钥方法-OPENSSL

一,环境 windows + php7.2 二,方法实现 $config = array('config' =>'D:phpwwwrootphpextrassslopenssl.cnf',  //安装PHP7会自带这个配置文件'digest_alg' => 'sha256',                 //可以用openssl_get_m...

jQuery jsonp(转载)

来源:https://www.jianshu.com/p/1efe671832e0 其他文章:https://www.cnblogs.com/chiangchou/p/jsonp.html jsonp本身是专为跨域而诞生的。早期开发者面对跨域没什么好办法,突然有人想到,既然引入js文件是不受跨域限制的,可以随意跨域引入,那么,动态引入一个带有你想要的数据...

OB-连接Oceanbase

管理工具 OceanBase 客户端、MySQL 客户端、 OceanBase 开发者中心和 OceanBase 云平台 字符型管理工具 OceanBase 客户端 OceanBase 客户端(OBClient)同时兼容访问 OceanBase 数据库的 MySQL 以及 Oracle 租户 语法 obclient -u[用户名]@[租户名]#[集群名称]...

关于mysql installer 的安装和环境变量配置

MySQL针对不同的用户提供了2中不同的版本: MySQL Community Server:社区版。由MySQL开源社区开发者和爱好者提供技术支持,对开发者开放源代码并提供免费下载。 MySQL Enterprise Server:企业版。包括最全面的高级功能和管理工具,不过对用户收费。 下面讲到的MySQL安装都是以免费开源的社区版为基础。打开My...

MySQL 日期与时间的处理

1.查询当前日期时间:函数有now(),localtime(),current_timestamp(),sysdate()。 mysql> select now(),localtime(),current_timestamp(),sysdate(); +---------------------+---------------------+--...

【MySQL】MySQL层级数据的递归遍历

层级的业务数据在系统中很常见,如组织机构、商品品类等。如果要获取层级数据的全路径,除了缓存起来,就是递归访问的方式了: 将层级数据缓存在redis中,用redis递归获取层级结构。此方法效率高。 在MySQL中做递归遍历,(Oracle有递归的语法支持,而MySQL并没有),需要自己写函数去递归。此方法效率低。 程序运行基于效率要求,一般使用Redis...