SaltStack数据系统


1. 概述


什么是数据系统?

  • 数据系统(data system)是指由数据获取、数据存储、数据更新、数据流通和数据挖掘5部分组成的按照不同的层次分布式存储而成的系统。这样,我们就可以快速地、完整地、形象地、变尺度地了解各种宏观和微观的情况,并充分发挥这些数据的作用。

SaltStack主要的数据系统有哪些?

  • Grains
  • Pillar

2. Grains数据系统


Grains 是 SaltStack 的一个组件,其存放着 minion 启动时收集到的信息。

当 minion 启动后会收集自身的状态信息即 grains 信息,grains 信息是静态的,存储在 minion 端,如操作系统版本,内核版本,CPU,内存,硬盘,设备型号等。这些信息可以作为 master 端的匹配目标

2.1 查询Grains信息

可以通过grains.items命令查看某台 minion 的所有 Grains 信息:

[root@node01 ~]# salt '*' grains.items 
node02:
    ----------
    SSDs:
    biosreleasedate:
        02/27/2020
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
...

也可以通过grains.ls命令只查询所有的grains的key

[root@node01 ~]# salt '*' grains.ls 
node02:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - disks
......

也可以通过grains.get xx命令获取某个key的值

[root@node01 ~]# salt '*' grains.get ipv4
node02:
    - 127.0.0.1
    - 172.16.78.128

2.2 自定义Grains信息

1. 在minion端minion配置文件中搜索grains段,自定义grains信息

[root@minion ~]# vim /etc/salt/minion
...
grains:
  roles:
    - apache
[root@minion ~]# systemctl restart salt-minion
[root@master ~]# salt '*' grains.get roles    
minion:
    - apache

这种方式不推荐使用,因为minion端很多的时候配置起来很麻烦,并且配置文件我们一般不会去修改


2. 在master端定义/etc/salt/grains文件,需要执行命令推送到minion端

[root@master ~]# vim /etc/salt/grains
test: abc
[root@master ~]# salt-cp '*' /etc/salt/grains /etc/salt/grains 
minion:
    ----------
    /etc/salt/grains:
        True
[root@master ~]# salt '*' saltutil.sync_grains  //刷新grains
[root@master ~]# salt '*' grains.get test
minion:
    abc

推荐使用这种方式,因为只用在master端执行操作,并且不用修改配置文件


2.3 匹配实例

注:node02为CentOS系统,node06为RedHat系统

1. 以命令的方式用 Grains 来匹配 minion

[root@node01 ~]# salt -G 'os:RedHat' cmd.run 'hostname' 
node06:
    node06

2. 在top file里面使用 Grains 来匹配 minion:

[root@node01 ~]# vim /srv/salt/base/top.sls
base:
  'os:RedHat':
    - match: grain
    - web.apache.apache
[root@node01 ~]# salt '*' state.highstate
node02:              `//因为node02是CentOS系统,所以失败是正常的`
----------
          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 node02
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
node06:               `//可以看到node06因为是redhat系统,所以执行成功了`
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 21:20:29.806678
    Duration: 5244.735 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.4.8-5.el7
                  old:
              apr-util:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-server
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 21:20:35.057458
    Duration: 20203.292 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for node06
------------
Succeeded: 2 (changed=2)    
Failed:    0
------------
Total states run:     2
Total run time:  25.448 s
ERROR: Minions returned with non-zero exit code

3. Pillar数据系统


Pillar是动态的,Pillar存储在master端,提供给minion端。

Pillar主要记录一些加密信息, 可以确保这些敏感数据不被其他minion看到。

Pillar在SaltStack中主要作用是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的存储格式跟Grains类似,都是YAML格式。

在master配置文件中可以定义Pillar的工作目录

[root@node01 ~]# vim /etc/salt/master
...
#pillar_roots:
#  base:
#    - /srv/pillar

默认Base环境下Pillar的工作目录在/srv/pillar目录下。若你想定义多个环境不同的Pillar工作目录,只需要修改此处配置文件即可。

Pillar的特点:

  • 可以给指定的minion定义它需要的数据
  • 只有指定的人才能看到定义的数据
  • 在master配置文件里设置

3.1 查询Pillar信息

[root@node01 ~]# salt '*' pillar.items
node06:
    ----------
node02:
    ----------

默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为 True。

[root@node01 ~]# vim /etc/salt/master
pillar_opts: True
[root@node01 ~]# systemctl restart salt-master
[root@node01 ~]# salt '*' pillar.items
...
winrepo_remotes_ng:
            - https://github.com/saltstack/salt-winrepo-ng.git
        winrepo_ssl_verify:
            True
        winrepo_user:
        worker_threads:
            5
        zmq_backlog:
            1000
        zmq_filtering:
            False
        zmq_monitor:
            False

我们一般都是将pillar_opts设置为False,自定义pillar来使用


3.2 自定义Pillar信息

修改配置文件

[root@node01 ~]# vim /etc/salt/master
...
pillar_roots:
  base:
    - /srv/pillar/base

创建pillar工作目录,并重启服务

[root@node01 ~]# mkdir -p /srv/pillar/base
[root@node01 ~]# systemctl restart salt-master

自定义pillar信息

[root@node01 ~]# cd /srv/pillar/base/
[root@node01 base]# vim install.sls
{% if grains['os'] == 'CentOS' %}
install: httpd
{% elif grains['os'] == 'RedHat' %}
install: vsftpd
{% endif %}

//如果是centos系统则安装httpd,如果是redhat系统则安装vsftpd

定义top file入口文件

[root@node01 base]# vim top.sls
base:
  '*':
    - install

查看pillar信息

[root@node01 ~]# salt '*' pillar.items
node06:
    ----------
    install:
        vsftpd
node02:
    ----------
    install:
        httpd

修改install的状态文件,引用pillar的数据

[root@node01 ~]# cd /srv/salt/base/web
[root@node01 web]# vim install.sls
install:
  pkg.installed:
    - name: {{ pillar['install'] }}
[root@node01 web]# cd ..
[root@node01 base]# vim top.sls
base:
  '*':
    - web.install

执行高级状态文件

[root@node01 ~]# salt '*' state.highstate
node06:
----------
          ID: install
    Function: pkg.installed
        Name: vsftpd
      Result: True
     Comment: The following packages were installed/updated: vsftpd
     Started: 22:32:41.112172
    Duration: 4078.651 ms
     Changes:   
              ----------
              vsftpd:
                  ----------
                  new:
                      3.0.2-27.el7
                  old:

Summary for node06
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   4.079 s
node02:
----------
          ID: install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 17:43:29.056817
    Duration: 5495.067 ms
     Changes:   
              ----------
              httpd:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:

Summary for node02
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
Total run time:   5.495 s

验证

[root@node02 ~]# rpm -qa |grep httpd
httpd-2.4.6-93.el7.centos.x86_64
httpd-tools-2.4.6-93.el7.centos.x86_64
[root@node06 ~]# rpm -qa |grep vsftpd
vsftpd-3.0.2-27.el7.x86_64

4. Grains和Pillar的区别


存储位置类型采集方式应用场景
Grainsminion静态minion启动时采集,
可通过salt '*' saltutil.sync_grains
刷新避免重启minion服务
1.信息查询
2.在命令行下进行目标匹配
3.在top file中进行目标匹配
4.在模板中进行目标匹配
Pillarmaster动态指定,实时生效1.目标匹配
2.敏感数据配置
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值