利用saltstack 部署lnmp环境(yum版本)

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

例行吐槽:有年月没更新博客了,最近太浮躁了,以前一起工作的小伙伴都找到新的东家了,薪资条那叫一个长,不开森了,都不带我[j_0004.gif]

#######################分隔线####################

一、简介

  saltstack 是一个新基础设施管理工具,可以看做是强化的Func+弱化puppet的组合,间接的反映出了saltstack的两大功能:远程执行命令与配置管理,

  saltstack是使用python开发的,非常简单易用和轻量级的管理工具,由master和minion构成,通过ZeroMQ进行通信


二、安装

  安装时需要epel源的支持,请自行安装与当系统匹配的epel源

  *:安装依赖包 

1
yum  install  python-jinja2 -y

  1、安装master端

1
yum -y  install  salt-master enablerepr=epel-testing

  2、安装minion端

1
yum -y  install  salt-minion enablerepr=epel-testing


三、配置环境

  a、master

    1、修改本地绑定地址

1
sed  -ie  's/^#.*interface:.*/\  interface:  192.168.2.65/g'  /etc/salt/master

     注:或是此处写上主机名,并绑定/etc/hosts文件

    2、自动接收所有minion的请求

1
sed  -ie  's@^#\(auto_accept: \)False@\  \1true@g'  /etc/salt/master

  b、minion

   1、指向master

1
sed  -ie  's@^#.*master:.*@\  master:  salt@g'  /etc/salt/minion

注:由于saltstack配置文件所限,当启用每一个配置参数时对格式有严格要求,书写时请注意


四、环境测试

  1、分别在master/minion中启动服务

1
2
3
#service salt-master start
 
#service salt-minion start

   说明:

     saltstack的master监听于4505,minion监听于4506;

     可以将master与minion部署同一台服务器上(没有什么意义[哈哈~])。

 2、salt 测试

1
2
3
4
5
[root@openapi php-fpm] # salt "*" test.ping
192.168.2.36-CentOS.backup. test .backend:
     True
192.168.2.30-centos.public. test :
     True

说明:salt的其它命令可用salt --help很简单,在此不多介绍

五、salt 常用的正则表达式(部分)

  a、E:正则匹配

wKioL1W4_f7i-jLPAACz8jBS0LA203.jpg

可以在每一个句后面加一个 -l debug来显示命令具体执行过程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@openapi self_userd] # salt -E '(backend81)' test.ping
backend81:
     True
[root@openapi self_userd] # salt -E '(backend81)' test.ping -l debug
[DEBUG   ] Reading configuration from  /etc/salt/master
[DEBUG   ] Guessing ID. The  id  can be explicitly  in  set  /etc/salt/minion
[INFO    ] Found minion  id  from generate_minion_id(): openapi. test .dns.com.cn
[DEBUG   ] Missing configuration  file : ~/.saltrc
[DEBUG   ] Configuration  file  path:  /etc/salt/master
[DEBUG   ] Reading configuration from  /etc/salt/master
[DEBUG   ] Guessing ID. The  id  can be explicitly  in  set  /etc/salt/minion
[INFO    ] Found minion  id  from generate_minion_id(): openapi. test .dns.com.cn
[DEBUG   ] Missing configuration  file : ~/.saltrc
[DEBUG   ] MasterEvent PUB socket URI: ipc: ///var/run/salt/master/master_event_pub .ipc
[DEBUG   ] MasterEvent PULL socket URI: ipc: ///var/run/salt/master/master_event_pull .ipc
[DEBUG   ] LazyLoaded local_cache.get_load
[DEBUG   ] get_iter_returns  for  jid 20150727212822728009 sent to  set ([ 'backend81' ]) will timeout at 21:28:27.732956
[DEBUG   ] jid 20150727212822728009  return  from backend81
backend81:
     True
[DEBUG   ] jid 20150727212822728009 found all minions  set ([ 'backend81' ])

  b、-L :命令行里面一般是以列表的形式来指定对象的。

wKioL1W4_wXxyIZXAACFo_8-OSc489.jpg

  c、-G:这个参数很强大,会根据默认的grain的结果来过滤。(grains也可以自己定义)

wKiom1W4_X6xGjf0AACqkBNK2kM670.jpg

wKioL1W4_4Oiz6lcAACmVYdhGlY139.jpg

  d、-N:这个参数是基于分组的,前提是你得先分好组。(分组可以定义于主匹配文件/etc/salt/master中,也可以定义于/etc/salt/master.d/*.conf)

wKioL1W5ADKxFDpLAAHISTdTPI8672.jpg

然后可以这样使用

wKiom1W4_mOzPyZWAABgST-EzN4712.jpg

  e、-C :表示tagger可是一个复合语句

1
2
3
[root@openapi salt] # salt -C 'G@virtual:physical and E@backend81' test.ping 
backend81:
     True

 f、-b :一次操作多少台,也可以使使用百分比来操作(--batch-size)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
[root@openapi salt] # salt "*" -b 3 grains.item os
#salt "*" --batch-size 25%  grains.item os
 
36 Detected  for  this batch run
backend81 Detected  for  this batch run
backend84 Detected  for  this batch run
zabbix.server.dns.com.cn Detected  for  this batch run
backend83 Detected  for  this batch run
webdata.backup Detected  for  this batch run
  
Executing run on [ 'zabbix.server.dns.com.cn' 'webdata.backup' 'backend84' ]
  
webdata.backup:
     ----------
     os:
         CentOS
backend84:
     ----------
     os:
         CentOS
zabbix.server.dns.com.cn:
     ----------
     os:
         CentOS
  
Executing run on [ 'backend83' 'backend81' '36' ]
  
backend81:
     ----------
     os:
         CentOS
backend83:
     ----------
     os:
         CentOS
36:
     ----------
     os:
         CentOS

注:

   更多的模块使用说明可以使用

1
salt \* sys.doc | grep  <模块名称>

  salt 的每一个子命令都可以用-d来查看具体的用法

六、自定义grains

 1、grains的优先级

    grains可以保持在minion端、通过master端下发等多个方式来分发。但不同的方法有不同的优先级的:

      a. /etc/salt/grains

      b. /etc/salt/minion

      c./srv/salt/_grains/  master端_grains目录下

   优先级顺序依次为存在在minion端/etc/salt/minion配置文件中的同名grains会覆盖/etc/salt/grains文件中的值,而通过master端_grains目录下grains文件下发的值可以会覆盖minion端的所有同名值。比较拗口,总之记得,通过master下发的grains优先级是最高的可,/etc/salt/minion次之,/etc/salt/grains最低(core grains不大懂,就不讨论了,这个比/etc/salt/grains还低)

 2、自定义grains

  注:

    a、首先要在master端的主匹配文件中开启file_roots

1
2
3
  file_roots:
     base:
       /srv/salt

    b、如果没有以上目录,创建即可(此处使用_grains目录)

1
2
[root@openapi _grains] # pwd
/srv/salt/_grains

    c、自定义grains字典

1
2
3
4
5
6
7
8
9
10
11
[root@openapi _grains] # cat ports.py 
#!/usr/bin/env python 
#coding:utf8
 
def  open_moutil_port ():
     grains = {}
     grains[ 'web_port' ] = '80'
     grains[ 'mysql_path' ] = '/data/mysql'
     grains[ 'mysqld_port' ] = '3307'
     grains[ 'max_open_file' ] = '65535'
     return  grains

   d、同步grains并重新加载grains(grains是“静态”的,是每一个minion接入到master自动提交的)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@openapi _grains] # salt "*" saltutil.sync_all
192.168.2.36-CentOS.backup. test .backend:
     ----------
     beacons:
     grains:
     modules:
     outputters:
     renderers:
     returners:
     states:
     utils:
192.168.2.30-centos.public. test :
     ----------
     beacons:
     grains:
     modules:
     outputters:
     renderers:
     returners:
     states:
     utils:
[root@openapi _grains] # salt '*' sys.reload_modules
192.168.2.36-CentOS.backup. test .backend:
     True
192.168.2.30-centos.public. test :
     True

    e、查询自定义的grains

1
2
3
4
5
6
7
8
9
[root@openapi _grains] # salt "*" grains.item web_port
192.168.2.36-CentOS.backup. test .backend:
     ----------
     web_port:
         80
192.168.2.30-centos.public. test :
     ----------
     web_port:
         80

七、LNMP环境

  1、目录结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@openapi salt] # tree
.
├── _grains
│   └── ports.py
├── lnmp.sls
├── mysql
│   ├── installed.sls
│   └── my.cnf
├── nginx
│   ├── default.conf
│   └── installed.sls
├── php-fpm
│   ├── index.php
│   └── installed.sls
└──  top .sls

  a、top文件(此文件必须存在,它定义了各minion的执行规划)

1
2
3
4
5
6
7
[root@openapi salt] # cat top.sls 
base:
   '192.168.2.30-centos.public.test' #此处可以写组、正则、或minion;匹配目标
     - nginx.installed  #这表示到那一个目录下的那一个文件 ,此部分表示在30安装nginx,php-fpm
     - php-fpm.installed 
   '192.168.2.36-CentOS.backup.test.backend' :
     - mysql.installed  #在2.36上安装mysql-server

  b、nginx目录下的文件 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@openapi nginx] # ll
total 8
-rw-r--r-- 1 sysadmin sysadmin 1102 Jul 29 21:50 default.conf 
#nginx 默认的配置文件,只改了root目录为/var/www/html,其它地方与/etc/nginx/conf.d/default完全一样
-rw-r--r-- 1 root     root      360 Jul 29 23:23 installed.sls 
#操作文件 
[root@openapi nginx] # cat installed.sls 
nginx:  #id 
  pkg.installed:  #使用pkg安装包,installed表示要安装完成,也可以指定版本来保证所有环境使用同一版本软件包
   - name: nginx  #包名,如果没有将继承id名称
  service.running:  #服务状态
   enable : True  #是否启动
   - reload: True  #重新载入
   watch :   #监控文件
      file /etc/nginx/conf .d /default .conf  #如果此文件发生变化将文件重新装载到服务中
   - require:  #依赖
      - pkg: nginx  #包名
 
  file .managed:  #文件操作
    - name:  /etc/nginx/conf .d /default .conf  #文件名
    source : salt: //nginx/default .conf  #base下那一个文件
    - user: nginx  #用户
    - group: root  #组
    - mode: 644    #权限
    - backup: minion  #将上一个版本的文件备份。(如果有的话)
    - require:   #依赖
       - pkg: nginx

  c、mysql目录下的文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
[root@openapi mysql] # ll
total 8
-rw-r--r-- 1 root     root     434 Jul 29 23:07 installed.sls  #操作文件
-rw-r--r-- 1 sysadmin sysadmin 775 Jul 29 22:26 my.cnf  #默认的匹配文件
[root@openapi mysql] # cat installed.sls 
mysql-server:
  pkg.installed:
   - name: mysql-server
  service.running:
   - name: mysqld
   enable : True
   - reload: True
   watch :
      file /etc/my .cnf
   - require:
      - pkg: mysql-server
  file .managed:
   - name:  /etc/my .cnf
   source : salt: //mysql/my .cnf
   - user: mysql
   - group: mysql
   - mode: 644
   - backup: minion
 
mysql:
   pkg.installed:
    - name: mysql
    
myslq-devel:
   pkg.installed:
    - name: mysql-devel

  d、php-fpm目录下的文件 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@openapi php-fpm] # ll
total 8
-rw-r--r-- 1 root root  22 Jul 29 22:35 index.php
-rw-r--r-- 1 root root 351 Jul 29 23:34 installed.sls
[root@openapi php-fpm] # cat index.php  #php测试页面
<?php
    phpinfo()
?>
[root@openapi php-fpm] # cat installed.sls 
php-xcache:
   pkg.installed:
    - name: php-xcache
 
php-fpm:
   pkg.installed:
    - name: php-fpm
   service.running:
    enable : Ture
    - require:
       - pkg: php-fpm
 
   file .managed:
     - name:  /var/www/html/index .php
     source : salt: //php-fpm/index .php
     - user: nginx
     - group: root
     - mode: 644
     - require:
        - pkg: php-fpm

注:在php-fpm中的installed.sls如果需要的模块不是php-fpm依赖的必需定义到phf-fpm之前,如果先安装完成php-fpm后安装所需要的php模块,还要重新reloadphp-fpm服务才能生效

  e、定义顺序文件

   lnmp环境一般要先行安装数据库-->web服务-->php-fpm所以要定义一下两台不同服务执行执行顺序。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@openapi salt] # pwd
/srv/salt
[root@openapi salt] # cat lnmp.sls 
db_setup:  #id  
  salt.state:  #状态
   - tgt:  '192.168.2.36-CentOS.backup.test.backend'  #目标与base定义的一样
   - highstate: True 
 
web_setup:
  salt.state:
   - tgt:  '192.168.2.30-centos.public.test'
   - highstate: True
   - require:  #依赖
      - salt: db_setup

  2、执行操作

1
salt-run state.orchestrate lnmp

 注: 也可以使用如下命令测试 

1
salt-run state.orchestrate lnmp  test =Ture

剩下说等待操作完成了

  3、最络执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
[root@openapi nginx] # salt-run state.orchestrate lnmp
openapi. test .dns.com.cn_master:
----------
           ID: db_setup
     Function: salt.state
       Result: True
      Comment: States ran successfully. No changes made to 192.168.2.36-CentOS.backup. test .backend.
      Started: 23:27:11.401701
     Duration: 3533.27 ms
      Changes:   
----------
           ID: web_setup
     Function: salt.state
       Result: True
      Comment: States ran successfully. Updating 192.168.2.30-centos.public. test .
      Started: 23:27:14.936140
     Duration: 46763.368 ms
      Changes:   
               192.168.2.30-centos.public. test :
               ----------
                         ID: nginx
                   Function: pkg.installed
                     Result: True
                    Comment: Package nginx is already installed.
                    Started: 23:29:08.840872
                   Duration: 971.023 ms
                    Changes:   
               ----------
                         ID: nginx
                   Function:  file .managed
                       Name:  /etc/nginx/conf .d /default .conf
                     Result: True
                    Comment: File  /etc/nginx/conf .d /default .conf is  in  the correct state
                    Started: 23:29:09.816842
                   Duration: 5.82 ms
                    Changes:   
               ----------
                         ID: nginx
                   Function: service.running
                     Result: True
                    Comment: Service nginx is already enabled, and is  in  the desired state
                    Started: 23:29:09.822994
                   Duration: 273.52 ms
                    Changes:   
               ----------
                         ID: php-fpm
                   Function: pkg.installed
                     Result: True
                    Comment: The following packages were installed /updated : php-fpm
                    Started: 23:29:10.097200
                   Duration: 26085.157 ms
                    Changes:   
                             ----------
                             php-fpm:
                                 ----------
                                 new:
                                     5.3.3-46.el6_6
                                 old:
                                     
               ----------
                         ID: php-fpm
                   Function: service.running
                     Result: True
                    Comment: Started Service php-fpm
                    Started: 23:29:36.192137
                   Duration: 301.652 ms
                    Changes:   
                             ----------
                             php-fpm:
                                 True
               ----------
                         ID: php-fpm
                   Function:  file .managed
                       Name:  /var/www/html/index .php
                     Result: True
                    Comment: File  /var/www/html/index .php updated
                    Started: 23:29:36.499508
                   Duration: 23.542 ms
                    Changes:   
                             ----------
                             diff :
                                 New  file
                             mode:
                                 0644
                             user:
                                 nginx
               ----------
                         ID: php-xcache
                   Function: pkg.installed
                     Result: True
                    Comment: The following packages were installed /updated : php-xcache
                    Started: 23:29:36.527960
                   Duration: 16844.534 ms
                    Changes:   
                             ----------
                             php-xcache:
                                 ----------
                                 new:
                                     3.0.4-1.el6
                                 old:
                                     
               
               Summary
               ------------
               Succeeded: 7 (changed=4)
               Failed:    0
               ------------
               Total states run:     7
  
Summary
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2

温馨提示:在卸载软件包时,千万不要用yum、yum、yum.

    a、查看服务状态

1
2
3
4
5
6
7
8
9
[root@openapi salt] # salt "192.168.2.36-CentOS.backup.test.backend" service.status mysqld
192.168.2.36-CentOS.backup. test .backend:
     Ture
[root@openapi salt] # salt "192.168.2.30-centos.public.test" service.status nginx
192.168.2.30-centos.public. test :
     Ture
[root@openapi salt] # salt "192.168.2.30-centos.public.test" service.status php-fpm
192.168.2.30-centos.public. test :
     Ture

    b、测试php

wKioL1W5DhvQV1jxAALdWRa3lw4194.jpg#########################到此lnmp部署完成###############################

ps:

   其它了服务也是同理,是重要的是注意操作文件的格式及服务执行的先后顺序。

   下次更新来个源码的及pillar的使用!回家,碎觉~










本文转自 jinlinger 51CTO博客,原文链接:http://blog.51cto.com/essun/1679860,如需转载请自行联系原作者
目录
相关文章
|
3月前
|
缓存 Linux Shell
RHEL7部署http应用配置共享yum源
RHEL7部署http应用配置共享yum源
77 0
|
4月前
|
关系型数据库 应用服务中间件 nginx
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
基于Docker的LNMP环境微服务搭建
|
7月前
|
应用服务中间件 PHP nginx
基于Anolis OS 3快速搭建LNMP环境制作KodBox
本教程介绍如何搭建LNMP环境,其中本实验的LNMP分别代表Anolis OS 3、Nginx、Mariadb和PHP。
133 0
|
3月前
|
API PHP 数据库
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
Docker六脉神剑(四) 使用Docker-Compose进行服务编排搭建lnmp环境
28 0
|
4月前
|
Java Linux Docker
Linux【上手 01】MobaXterm+系统版本查看+hostname修改+hosts修改+yum源配置+jdk安装+docker部署使用(不断完善)
Linux【上手 01】MobaXterm+系统版本查看+hostname修改+hosts修改+yum源配置+jdk安装+docker部署使用(不断完善)
51 0
|
4月前
|
关系型数据库 MySQL 应用服务中间件
小白带你部署LNMP分布式部署
小白带你部署LNMP分布式部署
67 0
|
4月前
|
Linux 编译器 开发工具
Linux基础环境开发工具的使用(yum,vim,gcc,g++)(下)
Linux基础环境开发工具的使用(yum,vim,gcc,g++)
|
4月前
|
Linux 开发工具 C语言
Linux基础环境开发工具的使用(yum,vim,gcc,g++)(中)
Linux基础环境开发工具的使用(yum,vim,gcc,g++)
|
4月前
|
Linux 程序员 编译器
Linux基础环境开发工具的使用(yum,vim,gcc,g++)(上)
Linux基础环境开发工具的使用(yum,vim,gcc,g++)
|
6月前
|
关系型数据库 MySQL Linux
Linux环境下LNMP架构实战案例
Linux环境下LNMP架构实战案例