SaltStack之数据系统

Posted 卑微小胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack之数据系统相关的知识,希望对你有一定的参考价值。

SaltStack之数据系统

文章目录

SaltStack数据系统

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

  • Grains
  • Pillar

SaltStack数据系统组件

SaltStack组件之Grains

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

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

Grains的功能:

  • 收集资产信息

Grains应用场景:

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

模板中进行目标匹配请看:https://docs.saltstack.com/en/latest/topics/pillar/

信息查询实例:

[root@master base]# salt 'minion1' grains.items
minion1:
    ----------
    biosreleasedate:		//bios的时间
        07/22/2020
    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
        - mmx
        - fxsr
        - sse
        - sse2
        - syscall
        - nx
        - mmxext
        - fxsr_opt
        - pdpe1gb
        - rdtscp
        - lm
        - constant_tsc
        - rep_good
        - nopl
        - tsc_reliable
        - nonstop_tsc
        - cpuid
        - extd_apicid
        - pni
        - pclmulqdq
        - ssse3
        - fma
        - cx16
        - sse4_1
        - sse4_2
        - x2apic
        - movbe
        - popcnt
        - aes
        - xsave
        - avx
        - f16c
        - rdrand
        - hypervisor
        - lahf_lm
        - extapic
        - cr8_legacy
        - abm
        - sse4a
        - misalignsse
        - 3dnowprefetch
        - osvw
        - topoext
        - ssbd
        - ibpb
        - vmmcall
        - fsgsbase
        - bmi1
        - avx2
        - smep
        - bmi2
        - rdseed
        - adx
        - smap
        - clflushopt
        - sha_ni
        - xsaveopt
        - xsavec
        - xgetbv1
        - xsaves
        - clzero
        - arat
        - overflow_recov
        - succor
    cpu_model:			//cpu的具体型号
        AMD Ryzen 5 3550H with Radeon Vega Mobile Gfx
    cpuarch:			//cpu架构
        x86_64
    cwd:
        /
    disks:
        - sr0
        - sda
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 192.168.96.2
        ip6_nameservers:
        nameservers:
            - 192.168.96.2
        options:
        search:
            - localdomain
        sortlist:
    domain:
    fqdn:
        minion1
    fqdn_ip4:			//ip地址
        - 192.168.96.133
    fqdn_ip6:
        - fe80::20c:29ff:feff:e083
    fqdns:
        - minion1
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              vmware
    groupname:
        root
    host:			//主机名
        minion1
    hwaddr_interfaces:
        ----------
        ens33:
            00:0c:29:ff:e0:83
        lo:
            00:00:00:00:00:00
    id:				//minion的ID
        minion1
    init:
        systemd
    ip4_gw:
        192.168.96.2
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.96.133
        lo:
            - 127.0.0.1
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens33:
            - fe80::20c:29ff:feff:e083
        lo:
            - ::1
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens33:
            - 192.168.96.133
            - fe80::20c:29ff:feff:e083
        lo:
            - 127.0.0.1
            - ::1
    ipv4:
        - 127.0.0.1
        - 192.168.96.133
    ipv6:
        - ::1
        - fe80::20c:29ff:feff:e083
    kernel:
        Linux
    kernelparams:
        |_
          - BOOT_IMAGE
          - (hd0,msdos1)/vmlinuz-4.18.0-294.el8.x86_64
        |_
          - root
          - /dev/mapper/cs-root
        |_
          - ro
          - None
        |_
          - crashkernel
          - auto
        |_
          - resume
          - /dev/mapper/cs-swap
        |_
          - rd.lvm.lv
          - cs/root
        |_
          - rd.lvm.lv
          - cs/swap
        |_
          - rhgb
          - None
        |_
          - quiet
          - None
    kernelrelease:
        4.18.0-294.el8.x86_64
    kernelversion:
        #1 SMP Mon Mar 15 22:38:42 UTC 2021
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            en_US
        detectedencoding:
            UTF-8
        timezone:
            CST
    localhost:
        minion1
    lsb_distrib_codename:
        CentOS Stream 8
    lsb_distrib_id:
        CentOS Stream
    lsb_distrib_release:
        8
    lvm:
        ----------
        cs:
            - root
            - swap
    machine_id:
        137e45aff9834becbefb338efea9d180
    manufacturer:
        VMware, Inc.
    master:
        192.168.96.129
    mdadm:
    mem_total:
        782
    nodename:
        minion1
    num_cpus:
        1
    num_gpus:
        1
    os:
        CentOS Stream
    os_family:
        RedHat
    osarch:
        x86_64
    oscodename:
        CentOS Stream 8
    osfinger:
        CentOS Stream-8
    osfullname:
        CentOS Stream
    osmajorrelease:
        8
    osrelease:
        8
    osrelease_info:
        - 8
    path:
        /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
    pid:
        10870
    productname:
        VMware Virtual Platform
    ps:
        ps -efHww
    pythonexecutable:
        /usr/bin/python3.6
    pythonpath:
        - /usr/bin
        - /usr/lib64/python36.zip
        - /usr/lib64/python3.6
        - /usr/lib64/python3.6/lib-dynload
        - /usr/lib64/python3.6/site-packages
        - /usr/lib/python3.6/site-packages
    pythonversion:
        - 3
        - 6
        - 8
        - final
        - 0
    saltpath:
        /usr/lib/python3.6/site-packages/salt
    saltversion:
        3003.1
    saltversioninfo:
        - 3003
        - 1
    selinux:
        ----------
        enabled:
            True
        enforced:
            Permissive
    serialnumber:
        VMware-56 4d 45 6c 22 3c dd b2-19 a8 47 ce b5 ff e0 83
    server_id:
        156412900
    shell:
        /bin/sh
    ssds:
    swap_total:
        1023
    systemd:
        ----------
        features:
            +PAM +AUDIT +SELINUX +IMA -APPARMOR +SMACK +SYSVINIT +UTMP +LIBCRYPTSETUP +GCRYPT +GNUTLS +ACL +XZ +LZ4 +SECCOMP +BLKID +ELFUTILS +KMOD +IDN2 -IDN +PCRE2 default-hierarchy=legacy
        version:
            239
    systempath:
        - /usr/local/sbin
        - /usr/local/bin
        - /usr/sbin
        - /usr/bin
    uid:
        0
    username:
        root
    uuid:
        6c454d56-3c22-b2dd-19a8-47ceb5ffe083
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.3.4
#只查询所有的grains的key
[root@master base]# salt 'minion1' grains.ls
minion1:
    - 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
    - kernelparams
    - kernelrelease
    - kernelversion
    - locale_info
    - localhost
    - lsb_distrib_codename
    - lsb_distrib_id
    - lsb_distrib_release
    - lvm
    - 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
    - ssds
    - swap_total
    - systemd
    - systempath
    - uid
    - username
    - uuid
    - virtual
    - zfs_feature_flags
    - zfs_support
    - zmqversion
#查询某个key的值,比如想获取ip地址
[root@master base]# salt '*' grains.get fqdn_ip4
master:
    - 192.168.96.129
minion1:
    - 192.168.96.133
minion22:
    - 192.168.96.128
[root@master base]# salt '*' grains.get ip4_interfaces
master:
    ----------
    ens33:
        - 192.168.96.129
    lo:
        - 127.0.0.1
minion1:
    ----------
    ens33:
        - 192.168.96.133
    lo:
        - 127.0.0.1
minion22:
    ----------
    ens33:
        - 192.168.96.128
    lo:
        - 127.0.0.1
[root@master base]# salt '*' grains.get ip4_interfaces:ens33
master:
    - 192.168.96.129
minion1:
    - 192.168.96.133
minion22:
    - 192.168.96.128

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

#在所有centos系统中执行命令
[root@master base]# salt -G 'os:centOS Stream' cmd.run 'uptime'
master:
     23:03:58 up  5:48,  1 user,  load average: 0.01, 0.02, 0.00
minion1:
     23:03:58 up  5:51,  2 users,  load average: 0.06, 0.02, 0.00
[root@master base]# salt -G 'os:CentOS' cmd.run 'uptime'
minion22:
     23:06:03 up  5:49,  2 users,  load average: 0.00, 0.01, 0.05

在top file里面使用Grains:

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

base:
  'os:Centos':
    - match: grain
    - web.nginx.install

自定义Grains的两种方法:

  • minion配置文件,在配置文件中搜索grains
  • 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@minion1 ~]# vim /etc/salt/grains
[root@minion1 ~]# cat /etc/salt/grains 
test-grains: linux-node1
[root@minion1 ~]# systemctl restart salt-minion
[root@master base]# salt '*' grains.get test-grains
minion1:
    linux-node1
master:
minion22:

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

[root@minion1 ~]# vim /etc/salt/grains
test-grains: linux-node1
aa: test
[root@master base]# salt '*' saltutil.sync_grains
master:
minion1:
minion22:
[root@master base]# salt '*' grains.get aa
minion1:
    test
master:
minion22:

SaltStack组件之Pillar

Pillar也是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 base]# salt '*' pillar.items
minion1:
    ----------
master:
    ----------
minion22:
    ----------

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

[root@master base]# vim /etc/salt/master
pillar_opts: True
#重启master并查看pillar的信息
[root@master base]# systemctl restart salt-master
[root@master base]# salt '*' pillar.items
....此处省略N行
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_floscript:
            /usr/lib/python2.7/site-packages/salt/daemons/flo/worker.flo
        worker_threads:
            5
        zmq_backlog:
            1000
        zmq_filtering:
            False
        zmq_monitor:
            False

pillar自定义数据:
在master的配置文件里找pillar_roots可以看到其存放pillar的位置

[root@master base]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar/base
[root@master base]# mkdir -p /srv/pillar/base
[root@master base]# systemctl restart salt-master
[root@master base]# vim /srv/pillar/base/apache.sls
% if grains['os'] == 'CentOS Stream' %
apache: httpd
% elif grains['os'] == 'Ubuntu' %
apache: apache2
% endif %
#定义top file入口文件
[root@master base]# vim /srv/pillar/base/top.sls
base:
  '*':
    - apache
[root@master base]# salt '*' pillar.items
minion:
.......
#在salt下修改apache的状态文件,引用pillar的数据
[root@master base]# mkdir /srv/salt/base/web/apache/
[root@master base]# vim /srv/salt/base/web/apache/install.sls
apache-install
  pkg.installed:
    - name:  pillar['apache'] 

apache-service:
  service.running:
    - name:  pillar['apache'] 
    - enable: True
#配置ubuntu系统
tom@tom-virtual-machine:~$ sudo curl -fsSL -o /usr/share/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest/salt-archive-keyring.gpg

tom@tom-virtual-machine:~$ echo "deb [signed-by=/usr/share/keyrings/salt-archive-keyring.gpg] https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest focal main" | sudo tee /etc/apt/sources.list.d/salt.list

tom@tom-virtual-machine:~$ sudo apt-get update

tom@tom-virtual-machine:~$ sudo apt-get install salt-minion
tom@tom-virtual-machine:~$ sudo vim /etc/salt/minion
master: 192.168.10.11
tom@tom-virtual-machine:~$ sudo systemctl restart salt-minion

[root@master ~]# salt-key -L
Accepted Keys:
master
minion1
Denied Keys:
Unaccepted Keys:
tom-virtual-machine
[root@master ~]# salt-key -yA
#执行高级状态文件
[root@master ~]# salt '*' state.highstate
master:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 06:36:39.400751
    Duration: 14697.251 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.5-1.el8
                  old:
              httpd:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-filesystem:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  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: 06:36:54.139133
    Duration: 1819.027 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for master
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  16.516 s
minion1:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 06:36:39.313710
    Duration: 22394.335 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.5-1.el8
                  old:
              httpd:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-filesystem:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.37-40.module_el8.5.0+852+0aafc63b
                  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: 06:37:02.053624
    Duration: 6469.225 ms
     Changes:   
              ----------
              httpd:
                  True

Summary for minion1
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  28.864 s
tom-virtual-machine:
----------
          ID: apache-install
    Function: pkg.installed
        Name: apache2
      Result: True
     Comment: The following packages were installed/updated: apache2
     Started: 18:36:39.659617
    Duration: 46710.241 ms
     Changes:   
              ----------
              apache2:
                  ----------
                  new:
                      2.4.41-4ubuntu3.3
                  old:
              apache2-bin:
                  ----------
                  new:
                      2.4.41-4ubuntu3.3
                  old:
              apache2-data:
                  ----------
                  new:
                      2.4.41-4ubuntu3.3
                  old:
              apache2-utils:
                  ----------
                  new:
                      2.4.41-4ubuntu3.3
                  old:
              libapr1:
                  ----------
                  new:
                      1.6.5-1ubuntu1
                  old:
              libaprutil1:
                  ----------
                  new:
                      1.6.1-4ubuntu2
                  old:
              libaprutil1-dbd-sqlite3:
                  ----------
                  new:
                      1.6.1-4ubuntu2
                  old:
              libaprutil1-ldap:
                  ----------
                  new:
                      1.6.1-4ubuntu2
                  old:
              liblua5.2-0:
                  ----------
                  new:
                      5.2.4-1.1build3
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: apache2
      Result: True
     Comment: The service apache2 is already running
     Started: 18:37:26.385690
    Duration: 30.843 ms
     Changes:   

Summary for tom-virtual-machine
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:  46.741 s
#查看状态
[root@master ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-07-06 00:00:55 EDT; 3min 37s ago
     Docs: man:httpd.service(8)
 Main PID: 31186 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 4743)
   Memory: 34.5M
   CGroup: /system.slice/httpd.service
           ├─31186 /usr/sbin/httpd -DFOREGROUND
           ├─31203 /usr/sbin/httpd -DFOREGROUND
           ├─31204 /usr/sbin/httpd -DFOREGROUND
           ├─31205 /usr/sbin/httpd -DFOREGROUND
           └─31206 /usr/sbin/httpd -DFOREGROUND

Jul 06 00:01:54 master systemd[1]: Starting The Apache HTTP Server...
Jul 06 00:01:55 master httpd[31186]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, u>
Jul 06 00:01:55 master systemd[1]: Started The Apache HTTP Server.
Jul 06 00:01:55 master httpd[31186]: Server configured, listening on: port 80

[root@minion1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2021-07-06 00:01:08 EDT; 3min 49s ago
     Docs: man:httpd.service(8)
 Main PID: 33001 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 4741)
   Memory: 29.1M
   CGroup: /system.slice/httpd.service
           ├─33001 /usr/sbin/httpd -DFOREGROUND
           ├─33072 /usr/sbin/httpd -DFOREGROUND
           ├─33073 /usr/sbin/httpd -DFOREGROUND
           ├─33074 /usr/sbin/httpd -DFOREGROUND
           └─33075 /usr/sbin/httpd -DFOREGROUND

Jul 06 00:02:02 minion1 systemd[1]: Starting The Apache HTTP Server...
Jul 06 00:02:08 minion1 httpd[33001]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, >
Jul 06 00:02:08 minion1 systemd[1]: Started The Apache HTTP Server.
Jul 06 00:02:08 minion1 httpd[33001]: Server configured, listening on: port 80

tom@tom-virtual-machine:~$ sudo systemctl status apache2
[sudo] tom 的密码: 
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2021-07-06 00:03:13 CST; 4min 18s ago
       Docs: https://httpd.apache.org/docs/2.4/
   Main PID: 12593 (apache2)
      Tasks: 55 (limit: 2285)
     Memory: 7.1M
     CGroup: /system.slice/apache2.service
             ├─12593 /usr/sbin/apache2 -k start
             ├─12595 /usr/sbin/apache2 -k start
             └─12596 /usr/sbin/apache2 -k start

Grains与Pillar的区别

存储位置类型采集方式应用场景
Grainsminion静态minion启动时采集 可通过刷新避免重启minion服务1.信息查询 2.在命令行下进行目标匹配 3.在top file中进行目标匹配 4.在模板中进行目标匹配
Pillarmaster动态指定,实时生效1.目标匹配 2.敏感数据配置

以上是关于SaltStack之数据系统的主要内容,如果未能解决你的问题,请参考以下文章

SaltStack之数据系统

SaltStack之数据系统

saltstack自动化运维系列②之saltstack的数据系统

SaltStack实战之数据系统-Grains

SaltStack数据系统之GrainsPillar

SaltStack实战之数据系统-Pillar