SaltStack之数据系统

1. SaltStack数据系统

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

  • Grains
  • Pillar

2. SaltStack数据系统组件

2.1 SaltStack组件之Grains

什么是grains:
是salt的一个组件,负责minion第一次启动的时候采集的静态数据,可以用在salt的模块和其他组件中。每次的minion启动(重启)的时候都会采集。

应用场景:
1.在state中使用 用于配置管理模块
2.在target中使用,用来匹配minion,例如匹配操作系统使用-G的选项
3.用于信息查询,grains保存收集到的客户端的详细信息

信息查询实例:

//列出所有grains的key和value
[root@xian base]# salt '*' grains.items
yeqixian:
    ----------
    SSDs:
    biosreleasedate:        //bios的时间
        09/30/2014
    biosversion:            //bios的版本
        6.00
    cpu_flags:              //cpu相关的属性
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - dts
        - mmx
        - fxsr
        - sse
        - sse2
        - ss
        - syscall
        - nx
        - rdtscp
        - lm
        - constant_tsc
        - arch_perfmon
        - pebs
        - bts
        - nopl
        - xtopology
        - tsc_reliable
        - nonstop_tsc
        - aperfmperf
        - pni
        - pclmulqdq
        - ssse3
        - cx16
        - pcid
        - sse4_1
        - sse4_2
        - x2apic
        - popcnt
        - tsc_deadline_timer
        - aes
        - xsave
        - avx
        - f16c
        - rdrand
        - hypervisor
        - lahf_lm
        - fsgsbase
        - tsc_adjust
        - smep
        - dtherm
        - ida
        - arat
        - pln
        - pts
    cpu_model:          //cpu的具体型号
        Intel(R) Xeon(R) CPU E5-2630 v2 @ 2.60GHz
    cpuarch:            //cpu架构
        x86_64
    disks:
        - sda
        - sr0
        - dm-0
        - dm-1
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 223.5.5.5
            - 223.6.6.6
        ip6_nameservers:
        nameservers:
            - 223.5.5.5
            - 223.6.6.6
        options:
        search:
        sortlist:
    domain:
    fqdn:
        minion
    fqdn_ip4:       //ip地址
        - 192.168.69.202
    fqdn_ip6:
        - fe80::6860:c429:1c06:1639
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              unknown
    groupname:
        root
    host:       //主机名
        minion
    hwaddr_interfaces:
        ----------
        ens32:
            00:0c:29:d8:05:54
        lo:
            00:00:00:00:00:00
    id:         //minion的ID
        192.168.69.202
    init:
        systemd
    ip4_gw:
        192.168.69.1
    ip4_interfaces:
        ----------
        ens32:
            - 192.168.69.202
        lo:
            - 127.0.0.1
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens32:
            - fe80::6860:c429:1c06:1639
        lo:
            - ::1
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens32:
            - 192.168.69.202
            - fe80::6860:c429:1c06:1639
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 127.0.0.1
        - 192.168.69.202
    ipv6:
        - ::1
        - fe80::6860:c429:1c06:1639
    kernel:
        Linux
    kernelrelease:
        3.10.0-693.el7.x86_64
    kernelversion:
        #1 SMP Tue Aug 22 21:09:27 UTC 2017
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            en_US
        detectedencoding:
            UTF-8
    localhost:
        minion
    lsb_distrib_codename:
        CentOS Linux 7 (Core)
    lsb_distrib_id:
        CentOS Linux
    machine_id:
        59745bbcb85b42a1a8441ffed7b54cba
    manufacturer:
        VMware, Inc.
    master:
        192.168.69.201
    mdadm:
    mem_total:
        1823
    nodename:
        localhost.localdomain
    num_cpus:
        8
    num_gpus:
        1
    os:
        CentOS
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        CentOS Linux 7 (Core)
    osfinger:
        CentOS Linux-7
    osfullname:
        CentOS Linux
    osmajorrelease:
        7
    osrelease:
        7.4.1708
    osrelease_info:
        - 7
        - 4
        - 1708
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    pid:
        9407
    productname:
        VMware Virtual Platform
    ps:
        ps -efHww
    pythonexecutable:
        /usr/bin/python
    pythonpath:
        - /usr/bin
        - /usr/lib64/python27.zip
        - /usr/lib64/python2.7
        - /usr/lib64/python2.7/plat-linux2
        - /usr/lib64/python2.7/lib-tk
        - /usr/lib64/python2.7/lib-old
        - /usr/lib64/python2.7/lib-dynload
        - /usr/lib64/python2.7/site-packages
        - /usr/lib/python2.7/site-packages
    pythonversion:
        - 2
        - 7
        - 5
        - final
        - 0
    saltpath:
        /usr/lib/python2.7/site-packages/salt
    saltversion:
        2018.3.3
    saltversioninfo:
        - 2018
        - 3
        - 3
        - 0
    selinux:
        ----------
        enabled:
            True
        enforced:
            Permissive
    serialnumber:
        VMware-56 4d b9 cc 33 9d ad d5-08 1d 49 ac bd d8 05 54
    server_id:
        1679846262
    shell:
        /bin/sh
    swap_total:
        2047
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 -SECCOMP +BLKID +ELFUTILS +KMOD +IDN
        version:
            219
    uid:
        0
    username:
        root
    uuid:
        564db9cc-339d-add5-081d-49acbdd80554
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.1.4

//只查询所有的grains的key
[root@xian base]# salt '*' grains.ls
yeqixian:
    - SSDs
    - biosreleasedate
    - biosversion
    - cpu_flags
    - cpu_model
    - cpuarch
    - cwd
    - disks
    - dns
    - domain
    - fqdn
    - fqdn_ip4
    - fqdn_ip6
    - fqdns
    - gid
    - gpus
    - groupname
    - host
    - hwaddr_interfaces
    - id
    - init
    - ip4_gw
    - ip4_interfaces
    - ip6_gw
    - ip6_interfaces
    - ip_gw
    - ip_interfaces
    - ipv4
    - ipv6
    - kernel
    - kernelrelease
    - kernelversion
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - machine_id
    - manufacturer
    - master
    - mdadm
    - mem_total
    - nodename
    - num_cpus
    - num_gpus
    - os
    - os_family
    - osarch
    - oscodename
    - osfinger
    - osfullname
    - osmajorrelease
    - osrelease
    - osrelease_info
    - path
    - pid
    - productname
    - ps
    - pythonexecutable
    - pythonpath
    - pythonversion
    - saltpath
    - saltversion
    - saltversioninfo
    - selinux
    - serialnumber
    - server_id
    - shell
    - swap_total
    - systemd
    - uid
    - username
    - uuid
    - virtual
    - zfs_feature_flags
    - zfs_support
    - zmqversion

//查询某个key的值,比如想获取安装系统
[root@xian base]# salt '*' grains.get os
yeqixian:
    CentOS

[root@xian base]# salt '*' grains.get ip6_interfaces
yeqixian:
    ----------
    ens33:
        - fd15:4ba5:5a2b:1008:b916:4746:8489:abda
        - fe80::a960:a184:96b9:c8e1
    lo:
        - ::1
[root@xian base]# salt '*' grains.get ip6_interfaces:ens33
yeqixian:
    - fd15:4ba5:5a2b:1008:b916:4746:8489:abda
    - fe80::a960:a184:96b9:c8e1

目标匹配实例:
用Grains来匹配minion:

//在主机名叫xian的主机中执行命令
[root@xian base]# salt -G 'os:CentOS' cmd.run 'uptime'(在所有centos系统中执行命令)
yeqixian:
     03:22:20 up 44 min,  1 user,  load average: 0.00, 0.01, 0.05
xian:
     19:22:21 up 44 min,  1 user,  load average: 0.00, 0.02, 0.05
[root@xian base]# salt -G 'host:xian' cmd.run 'uptime'
xian:
     19:24:22 up 46 min,  1 user,  load average: 0.00, 0.01, 0.05

在top file里面使用Grains:

[root@xian base]# vim top.sls
base:
  'fqdn_ip4:192.168.116.145':
    - match: grain
    - web.nginx.nginx

[root@xian base]# salt '*' state.highstate
xian:
----------
          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 xian
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
yeqixian:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 17:53:03.853765
    Duration: 11842.51 ms
     Changes:   
              ----------
              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: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 17:53:15.707955
    Duration: 230.69 ms
     Changes:   
              ----------
              nginx:
                  True

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


自定义Grains的两种方法:

minion配置文件,在配置文件中搜索grains(不推荐)

[root@yeqixian ~]# vim /etc/salt/minion
#grains:
#  roles:
#    - webserver
#    - memcache
去掉#号,roles下面跟服务类型
用salt -G '*' grains.get role查看


在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@xian ~]# vim /etc/salt/grains
roles: webserver
[root@xian ~]# salt-cp '*' /etc/salt/grains /etc/salt/grains
xian:
    ----------
    /etc/salt/grains:
        True
yeqixian:
    ----------
    /etc/salt/grains:
        True

[root@xian ~]# systemctl restart salt-minion
[root@xian ~]# salt '*' grains.get role
xian:
yeqixian:

不重启的情况下自定义Grains:

[root@xian ~]# vim /etc/salt/grains
roles: webserver
[root@xian ~]# salt-cp '*' /etc/salt/grains /etc/salt/grains
xian:
    ----------
    /etc/salt/grains:
        True
yeqixian:
    ----------
    /etc/salt/grains:
        True

[root@xian ~]# salt '*' saltutil.sync_grains
xian:
yeqixian:
[root@xian ~]# salt '*' grains.get role
xian:
yeqixian:

2.2 SaltStack组件之Pillar

  pillar也是Saltstack最重要的组件之一,其作用是定义与被控主机相关的任何数据,定义好的数据可以被其他组件使用,如模板、state、API等。在pillar中定义的数据与不同业务特性的被控主机相关联,这样不同被控主机只能看到自己匹配的数据,因此pillar安全性很高,适用于一些比较敏感的数据,这也是区别于grains最关键的一点,如定义不同业务组主机的用户ID、组ID、读写权限、程序包等信息,定义的规范是采用python字典形式,即键/值,最上层的键一般为主机的ID或组名称,它的定义存储格式与Grains类似,都是YAML格式。

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

[root@xian ~]# vim /etc/salt/master
#pillar_roots:
#  base:
#    - /srv/pillar
#pillar_roots:
#  base:
#    - /srv/pillar
pillar_roots:
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod
  test:
    - /srv/pillar/test
[root@xian ~]# mkdir -p /srv/salt/pillar/{base,prod,test}
[root@xian ~]# tree /srv/salt/pillar/
/srv/salt/pillar/
├── base
├── prod
└── test

3 directories, 0 files

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

Pillar的特点:

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

//查看pillar的信息
[root@xian ~]# salt '*' pillar.items
xian:
    ----------
yeqixian:
    ----------

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

[root@xian ~]# vim /etc/salt/master
#pillar_opts: False
pillar_opts: True

//重启master并查看pillar的信息

[root@xian ~]# systemctl restart salt-master
[root@xian ~]# salt '*' pillar.items
....此处省略N行
        winrepo_dir_ng:
            /srv/salt/win/repo-ng
        winrepo_insecure_auth:
            False
        winrepo_passphrase:
        winrepo_password:
        winrepo_privkey:
        winrepo_pubkey:
        winrepo_refspecs:
            - +refs/heads/*:refs/remotes/origin/*
            - +refs/tags/*:refs/tags/*
        winrepo_remotes:
            - https://github.com/saltstack/salt-winrepo.git
        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自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置

[root@xian ~]# vim /etc/salt/master
#pillar_roots:
#  base:
#    - /srv/pillar
#pillar_roots:
#  base:
#    - /srv/pillar
pillar_roots:
  base:
    - /srv/pillar/base
  prod:
    - /srv/pillar/prod
  test:
    - /srv/pillar/test
[root@xian ~]# mkdir -p /srv/pillar/{base,prod,test}
[root@xian ~]# tree /srv/pillar/
/srv/salt/pillar/
├── base
├── prod
└── test

3 directories, 0 files
[root@xian ~]# systemctl restart salt-master

[root@xian ~]# vim /srv/pillar/base/nginx.sls
{% if grains['host'] == 'yeqixian' %}
nginx: nginx
{% elif grains['host'] == 'xian' %}
nginx: nginx2
{% endif %}

//定义top file入口文件

[root@xian ~]# vim /srv/pillar/base/top.sls
base:       //指定环境
  '*':     //指定目标
    - nginx            //引用nginx.sls或nginx/init.sls

//这个top.sls文件的意思表示的是主机的base环境能够访问到nginx这个pillar

[root@xian base]# salt '*' pillar.items
yeqixian:
    ----------
    nginx:
        nginx
xian:
    ----------
    nginx:
        nginx2

//在salt下修改apache的状态文件,引用pillar的数据

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

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

//执行高级状态文件

[root@xian base]# salt 'yeqixian' state.highstate
yeqixian:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 19:01:51.371212
    Duration: 8959.773 ms
     Changes:   
              ----------
              nginx:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-all-modules:
                  ----------
                  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: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 19:02:00.337647
    Duration: 217.732 ms
     Changes:   
              ----------
              nginx:
                  True

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

2.3 Grains与Pillar的区别

名称存储位置数据类型数据采集更新方式应用
grainsminion端静态数据minion启动时收集,也可以使用saltutil.sync_grains进行刷新存储minion基本数据。比如用于匹配minion,自身数据可以用来做资产管理cmdb等。
pillarmaster端动态数据在master端定义,指定给对应的minion。可以使用saltutil.refresh_pillar进行刷新存储master指定数据,只用指定的minion可以看到。用于敏感数据保存。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值