Saltstack配置管理

YAML语言

YAML是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。

它类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。

YAML的基本规则:

  • 使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
  • 当冒号不是处于最后时,冒号后面必须有一个空格
  • 用 - 表示列表,-
  • 的后面必须有一个空格 用 # 表示注释
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到
[root@master salt]# vim master
#file_roots:
#  base:
#    - /srv/salt
#
file_roots:
  base:
    - /srv/salt/base
  test:
    - /srv/salt/test
  dev:
    - /srv/salt/dev
  prod:
    - /srv/salt/prod
[root@master salt]# ls /srv/
[root@master salt]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master salt]# tree /srv/
/srv/
`-- salt
    |-- base
    |-- dev
    |-- prod
    `-- test

5 directories, 0 files

修改配置之后重启生效
[root@master salt]# systemctl restart salt-master

需要注意: base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名

apache实例

创建目录结构
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
[root@master base]# mkdir web/{nginx,apache} -p
[root@master base]# tree
.
`-- web
    |-- apache
    `-- nginx

3 directories, 0 files

配置文件 
注意: YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
[root@master base]# vim web/apache/apache.sls
[root@master base]# cat web/apache/apache.sls
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: true
[root@master base]# tree 
.
`-- web
    |-- apache
    |   `-- apache.sls
    `-- nginx

3 directories, 1 file

在受控主机上执行apache.sls状态文件
[root@master base]# salt 'minion2' state.sls web.apache.apache saltenv=base
minion2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 14:30:19.078114
    Duration: 8995.341 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.6.3-11.el8
                  old:
              apr-util:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-bdb:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              apr-util-openssl:
                  ----------
                  new:
                      1.6.1-6.el8
                  old:
              centos-logos-httpd:
                  ----------
                  new:
                      85.8-1.el8
                  old:
              httpd:
                  ----------
                  new:
                      2.4.37-39.module_el8.4.0+950+0577e6ac.1
                  old:
              httpd-filesystem:
                  ----------
                  new:
                      2.4.37-39.module_el8.4.0+950+0577e6ac.1
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.37-39.module_el8.4.0+950+0577e6ac.1
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.48-3.el8
                  old:
              mod_http2:
                  ----------
                  new:
                      1.15.7-3.module_el8.4.0+778+c970deab
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 14:30:28.085891
    Duration: 1277.562 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion2
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  10.273 s

检查受控主机
[root@minion2 ~]# rpm -qa|grep httpd
httpd-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64
httpd-tools-2.4.37-39.module_el8.4.0+950+0577e6ac.1.x86_64
centos-logos-httpd-85.8-1.el8.noarch
httpd-filesystem-2.4.37-39.module_el8.4.0+950+0577e6ac.1.noarch
[root@minion2 ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                     *:80                    *:*                
LISTEN   0        128                  [::]:22                 [::]:*                
[root@minion2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: di>
   Active: active (running) since Tue 2021-11-02 14:30:29 EDT; 1min 35s ago
     Docs: man:httpd.service(8)
 Main PID: 35825 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 49298)
   Memory: 39.2M
   CGroup: /system.slice/httpd.service
           ├─35825 /usr/sbin/httpd -DFOREGROUND
           ├─35894 /usr/sbin/httpd -DFOREGROUND
           ├─35895 /usr/sbin/httpd -DFOREGROUND
           ├─35896 /usr/sbin/httpd -DFOREGROUND
           └─35897 /usr/sbin/httpd -DFOREGROUND

Nov 02 14:30:28 minion2 systemd[1]: Starting The Apache HTTP Server...
Nov 02 14:30:29 minion2 httpd[35825]: AH00558: httpd: Could not reliably determine t>
Nov 02 14:30:29 minion2 systemd[1]: Started The Apache HTTP Server.
Nov 02 14:30:30 minion2 httpd[35825]: Server configured, listening on: port 80

top file

介绍

直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。

top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。

top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。

top file:定义文件入口

  • 一对多

实例

[root@minion1 ~]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
[root@minion1 ~]# yum list all|grep nginx
Failed to set locale, defaulting to C.UTF-8
nginx.x86_64                                           1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-all-modules.noarch                               1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-filesystem.noarch                                1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-image-filter.x86_64                     1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-perl.x86_64                             1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-xslt-filter.x86_64                      1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-mail.x86_64                                  1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-stream.x86_64                                1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
pcp-pmda-nginx.x86_64                                  5.2.5-6.el8_4                                     appstream        

[root@minion2 ~]# cat /etc/redhat-release
CentOS Linux release 8.4.2105
[root@minion2 ~]# yum list all|grep nginx
Failed to set locale, defaulting to C.UTF-8
nginx.x86_64                                           1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-all-modules.noarch                               1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-filesystem.noarch                                1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-image-filter.x86_64                     1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-perl.x86_64                             1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-http-xslt-filter.x86_64                      1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-mail.x86_64                                  1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
nginx-mod-stream.x86_64                                1:1.14.1-9.module_el8.0.0+184+e34fea82            appstream        
pcp-pmda-nginx.x86_64                                  5.2.5-6.el8_4                                     appstream        

[root@master base]# tree 
.
`-- web
    |-- apache
    |   `-- install.sls
    `-- nginx

3 directories, 1 file
[root@master base]# vim web/nginx/install.sls
[root@master base]# cat web/nginx/install.sls 
nginx-install:
  pkg.installed:
    - name: nginx

nginx-service:
  service.running:
    - name: nginx
    - enable: true
[root@master base]# tree 
.
`-- web
    |-- apache
    |   `-- install.sls
    `-- nginx
        `-- install.sls

3 directories, 2 files
[root@master base]# ls
web
[root@master base]# vim top.sls
[root@master base]# cat top.sls 
base:
  'minion1':
    - web.nginx.install
  'minion2':
    - web.apache.install
[root@master base]# salt '*' state.highstate saltenv=base               //第二次执行查看结果
master:                         //master显示红色报错是没有问题的 因为top file没有指定master做事
----------  
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or master_tops data matches found. Please see master log for details.
     Changes:   

Summary for master
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
minion2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 14:39:40.666205
    Duration: 686.242 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 14:39:41.354053
    Duration: 50.114 ms
     Changes:   

Summary for minion2
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 736.356 ms
minion1:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 14:39:40.674417
    Duration: 734.172 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 14:39:41.410945
    Duration: 52.55 ms
     Changes:   

Summary for minion1
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 786.722 ms
ERROR: Minions returned with non-zero exit code

查看
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: di>
   Active: active (running) since Tue 2021-11-02 14:39:32 EDT; 3min 33s ago
 Main PID: 51795 (nginx)
    Tasks: 5 (limit: 49298)
   Memory: 7.7M
   CGroup: /system.slice/nginx.service
           ├─51795 nginx: master process /usr/sbin/nginx
           ├─51796 nginx: worker process
           ├─51797 nginx: worker process
           ├─51798 nginx: worker process
           └─51799 nginx: worker process

Nov 02 14:39:32 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server>
Nov 02 14:39:32 minion1 nginx[51792]: nginx: the configuration file /etc/nginx/nginx>
Nov 02 14:39:32 minion1 nginx[51792]: nginx: configuration file /etc/nginx/nginx.con>
Nov 02 14:39:32 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@minion1 ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
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        128                  [::]:80                 [::]:*                
LISTEN   0        128                  [::]:22                 [::]:*                

[root@minion2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: di>
   Active: active (running) since Tue 2021-11-02 14:30:29 EDT; 12min ago
     Docs: man:httpd.service(8)
 Main PID: 35825 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 49298)
   Memory: 39.2M
   CGroup: /system.slice/httpd.service
           ├─35825 /usr/sbin/httpd -DFOREGROUND
           ├─35894 /usr/sbin/httpd -DFOREGROUND
           ├─35895 /usr/sbin/httpd -DFOREGROUND
           ├─35896 /usr/sbin/httpd -DFOREGROUND
           └─35897 /usr/sbin/httpd -DFOREGROUND

Nov 02 14:30:28 minion2 systemd[1]: Starting The Apache HTTP Server...
Nov 02 14:30:29 minion2 httpd[35825]: AH00558: httpd: Could not reliably determine t>
Nov 02 14:30:29 minion2 systemd[1]: Started The Apache HTTP Server.
Nov 02 14:30:30 minion2 httpd[35825]: Server configured, listening on: port 80
[root@minion2 ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                     *:80                    *:*                
LISTEN   0        128                  [::]:22                 [::]:*                

高级状态highstate的使用

管理SaltStack时一般最常用的管理操作就是执行高级状态

[root@master ~]# salt '*' state.highstate   //生产环境禁止这样使用salt命令

注意:

上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的

若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作

停掉minon上的httpd服务
[root@minion2 ~]# systemctl stop httpd
[root@minion2 ~]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                  [::]:22                 [::]:*                

在master上执行高级状态的测试
[root@master base]# salt 'minion2' state.highstate test=true
minion2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 14:47:31.930294
    Duration: 593.979 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd is set to start
     Started: 14:47:32.525839
    Duration: 40.901 ms
     Changes:   

Summary for minion2
------------
Succeeded: 2 (unchanged=1)
Failed:    0
------------
Total states run:     2
Total run time: 634.880 ms

在minion上查看httpd是否启动
[root@minion2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: di>
   Active: inactive (dead) since Tue 2021-11-02 14:46:41 EDT; 1min 42s ago
     Docs: man:httpd.service(8)
  Process: 35825 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, statu>
 Main PID: 35825 (code=exited, status=0/SUCCESS)
   Status: "Running, listening on: port 80"

Nov 02 14:30:28 minion2 systemd[1]: Starting The Apache HTTP Server...
Nov 02 14:30:29 minion2 httpd[35825]: AH00558: httpd: Could not reliably determine t>
Nov 02 14:30:29 minion2 systemd[1]: Started The Apache HTTP Server.
Nov 02 14:30:30 minion2 httpd[35825]: Server configured, listening on: port 80
Nov 02 14:46:40 minion2 systemd[1]: Stopping The Apache HTTP Server...
Nov 02 14:46:41 minion2 systemd[1]: httpd.service: Succeeded.
Nov 02 14:46:41 minion2 systemd[1]: Stopped The Apache HTTP Server.

由此看来高级状态并没有执行因为httpd并没有启动
上一篇:2021-01-06 《赤壁賦》蘇軾


下一篇:C&C++学习心法06函数