Saltsatck数据系统组件: Grains和 pillar

1. Saltsatck数据系统组件: Grains和 pillar

SaltStack有两大数据系统,分别是:Grains和Pillar

Grainspillar提供了一种允许在minion中使用用户自定义变量的方法。模板则为这些变量提供在minion上创建文件的更高级用法。

Grains定义在指定的minion上,而Piller则定义在master上。它们都可以通过静态或动态的方式进行定义,但是Grains通常用于提供不常修改的数据,也就是静态数据,而Piller更倾向于动态数据。

2. grains

grains介绍及模板中进行目标匹配:https://docs.saltstack.com/en/latest/topics/grains/

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

GrainsSaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,GrainsSaltStack记录minion的一些静态信息的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。

Grains的功能:

  • 收集资产信息

Grains应用场景:

  • 信息查询
  • 在命令行下进行目标匹配
  • 在top file中进行目标匹配
  • 在模板中进行目标匹配

grains会在minion进程启动时进行加载,并缓存在内存中。这样salt-minion进程就无需每次操作都检索系统来获取grains,极大地提升了minion的性能。这对salt来说时必要的,毕竟从设计之初就是为了快速执行任务,而不是每次执行都等待很长时间。

2.1 查看grains数据

可以通过grains.items方法来查看minion有哪些grains数据:

salt myminion grains.items

[root@master ~]# salt '*' grains.items
minion:
    ----------
    SSDs:
    biosreleasedate:
        07/29/2019
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
......省略.....
    uid:
        0
    username:
        root
    uuid:
        64cb4d56-25df-192a-2e7e-dea64f261efc
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.1.4

只查询所有的grains的key

[root@master ~]# salt '*' grains.ls
minion:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - fqdns
    - gid

查询某个key的值

[root@master ~]# salt '*' grains.get fqdn_ip4
minion:
    - 192.168.32.135
[root@master ~]# salt '*' grains.get os_family
minion:
    RedHat
[root@master ~]# salt '*' grains.get ip4_interfaces
minion:
    ----------
    ens33:
        - 192.168.32.135
    lo:
        - 127.0.0.1

2.2 目标匹配

Grains来匹配minion

在所有centos系统中执行命令
[root@master ~]# salt -G 'os:CentOS' cmd.run 'uptime'
minion-2:
     22:24:25 up  1:58,  1 user,  load average: 0.00, 0.01, 0.05
minion-1:
     10:24:25 up  1:58,  1 user,  load average: 0.26, 0.12, 0.07

在top file里面使用Grains:

[root@master ~]# vim /srv/salt/base/top.sls
base:
  'os:CentOS':
    - match: grain
    - web.apache.install

2.3 自定义Grains:

  • minion配置文件,在配置文件中搜索grains
  • 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@minion ~]# vim /etc/salt/minion
grains:
  foo:
    - bus
    
[root@master ~]# salt '*' saltutil.sync_grains
minion-1:
minion-2:
[root@master ~]# salt '*' grains.get foo
minion-1:
minion-2:
    - bus    

这种方法很久了,在现在依然可用,但不建议使用

现在更常用的方法是将自定义的静态grains存放在一个叫做grains的文件中(路径为/etc/salt/grains)

这样做的好处是:

  • Grains 独立存储,易于在本地查找
  • Grains 能通过Grain执行模块进行修改
[root@master ~]# vim /etc/salt/grains
[root@master ~]# cat /etc/salt/grains
number: 114
[root@master ~]# salt '*' saltutil.sync_grains
minion-1:
minion-2:
[root@master ~]# salt '*' grains.get number
minion-1:
    114
minion-2:

3. pillar

illar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式。

在Master配置文件中有一段Pillar settings选项专门定义Pillar相关的一些参数:

#pillar_roots:
#  base:
#    - /srv/pillar

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

Pillar的特点:

  • 可以给指定的minion定义它需要的数据
  • 只有指定的人才能看到定义的数据
  • 在master配置文件里设置
//查看pillar的信息
[root@master ~]# salt '*' pillar.items
minion-2:
    ----------
minion-1:
    ----------

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

[root@master ~]# vim /etc/salt/master
pillar_opts: Ture

#重启master并查看pillar的信息
[root@master ~]# systemctl restart salt-master
[root@master ~]# salt '*' pillar.items
.......
        winrepo_user:
        worker_threads:
            5
        zmq_backlog:
            1000
        zmq_filtering:
            False
        zmq_monitor:
            False

3.1 pillar自定义数据:

在master的配置文件里找pillar_roots可以看到其存放pillar的位置

[root@master ~]# vim /etc/salt/master
......
#####         Pillar settings        #####
##########################################
# Salt Pillars allow for the building of global data that can be made selectively
# available to different minions based on minion grain filtering. The Salt
# Pillar is laid out in the same fashion as the file server, with environments,
# a top file and sls files. However, pillar data does not need to be in the
# highstate format, and is generally just key/value pairs.
pillar_roots:
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod

[root@localhost ~]# mkdir -p /srv/pillar/{base,prod}
[root@localhost pillar]# tree
.
├── base
└── prod

2 directories, 0 files


[root@master ~]# salt '*' grains.get fqdn
minion-1:
    minion-1
minion-2:
    minion-2



[root@master ~]# systemctl restart salt-master
[root@master pillar]# vim /srv/pillar/base/apache.sls
[root@master pillar]# cat /srv/pillar/base/apache.sls
{% if grains['fqdn'] == 'minion-1' %}
apache: httpd
{% elif grains['fqdn'] == 'minion-2' %}
apache: nginx
{% endif %}

[root@master salt]# salt '*' saltutil.refresh_pillar
minion-1:
    True
minion-2:
    True
[root@master salt]# salt '*' pillar.item apache
minion-1:
    ----------
    apache:
        httpd
minion-2:
    ----------
    apache:
        nginx






#定义top file入口文件
[root@master ~]# vim /srv/pillar/base/top.sls
base:
  '*':
    - apache

#这个top.sls文件的意思表示的是所有主机的base环境能够访问到apache这个pillar
[root@master ~]# salt '*' pillar.items
minion-1:
    ----------
    apache:
        httpd
minion-2:
    ----------
    apache:
        nginx

#在salt下修改apache的状态文件,引用pillar的数据
[root@master ~]# vim /srv/salt/base/web/apache/apache.sls
apache-install:
  pkg.installed:
    - name: {{ pillar['apache'] }}

apache-service:
  service.running:
    - name: {{ pillar['apache'] }}
    - enable: True






[root@localhost ~]# tree /srv/
/srv/
├── pillar
│   ├── base
│   │   ├── apache.sls
│   │   └── top.sls
│   └── prod
└── salt
    └── base
        ├── top.sls
        └── web
            └── apache
                └── install.sls

7 directories, 4 files
[root@localhost ~]# cat /srv/salt/base/top.sls 
base:
  '*':
    - web.apache.install



#执行高级状态文件
[root@master base]# salt '*' state.highstate
minion-1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 22:31:57.697039
    Duration: 7968.647 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-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 22:32:05.692740
    Duration: 399.347 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion-1
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:   8.368 s
minion-2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 10:31:57.006091
    Duration: 8868.752 ms
     Changes:   
              ----------
              centos-indexhtml:
                  ----------
                  new:
                      7-9.el7.centos
                  old:
              dejavu-fonts-common:
                  ----------
                  new:
                      2.33-6.el7
                  old:
              dejavu-sans-fonts:
                  ----------
                  new:
                      2.33-6.el7
                  old:
              fontconfig:
                  ----------
                  new:
                      2.13.0-4.3.el7
                  old:
              fontpackages-filesystem:
                  ----------
                  new:
                      1.44-8.el7
                  old:
              gd:
                  ----------
                  new:
                      2.0.35-26.el7
                  old:
              gperftools-libs:
                  ----------
                  new:
                      2.6.1-1.el7
                  old:
              libX11:
                  ----------
                  new:
                      1.6.7-2.el7
                  old:
              libX11-common:
                  ----------
                  new:
                      1.6.7-2.el7
                  old:
              libXau:
                  ----------
                  new:
                      1.0.8-2.1.el7
                  old:
              libXpm:
                  ----------
                  new:
                      3.5.12-1.el7
                  old:
              libjpeg-turbo:
                  ----------
                  new:
                      1.2.90-8.el7
                  old:
              libxcb:
                  ----------
                  new:
                      1.13-1.el7
                  old:
              nginx:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-all-modules:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-filesystem:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-image-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-perl:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-mail:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-stream:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 10:32:05.884002
    Duration: 254.402 ms
     Changes:   
              ----------
              nginx:
                  True

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





[root@nimion-1 ~]# rpm -qa | egrep "nginx|httpd"
httpd-tools-2.4.6-93.el7.centos.x86_64
httpd-2.4.6-93.el7.centos.x86_64

[root@nimion-2 ~]# rpm -qa | egrep "nginx|httpd"
httpd-tools-2.4.6-93.el7.centos.x86_64
nginx-mod-mail-1.16.1-1.el7.x86_64
nginx-all-modules-1.16.1-1.el7.noarch
nginx-filesystem-1.16.1-1.el7.noarch
nginx-mod-http-perl-1.16.1-1.el7.x86_64
nginx-mod-http-xslt-filter-1.16.1-1.el7.x86_64
nginx-mod-http-image-filter-1.16.1-1.el7.x86_64
nginx-mod-stream-1.16.1-1.el7.x86_64
nginx-1.16.1-1.el7.x86_64

4. Grains与Pillar的区别

posted @ 2020-08-21 10:41  EverEternity  阅读(245)  评论(0编辑  收藏  举报