saltstack 数据系统!!!

saltstack 数据系统

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

  1. Grains
  2. Pillar

SaltStack数据系统组件

Grains

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

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

Grains的功能:
1. 收集资产信息
Grains应用场景:

  1. 信息查询
  2. 在命令行下进行目标匹配
  3. 在top file中进行目标匹配
  4. 在模板中进行目标匹配

实例!!

[root@master ~]# salt 'laolin' grains.items
laolin:
    ----------
    SSDs:
    biosreleasedate:
        02/27/2020
    biosversion:
        6.00
    cpu_flags:
        - fpu
        - vme
        - de
        - pse
        - tsc
        - msr
        - pae
        - mce
        - cx8
        - apic
        - sep
        - mtrr
        - pge
        - mca
        - cmov
        - pat
        - pse36
        - clflush
        - mmx
        - fxsr
        - sse
        - sse2
        - ht
        - 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
        - cmp_legacy
        - extapic
        - cr8_legacy
        - abm
        - sse4a
        - misalignsse
        - 3dnowprefetch
        - osvw
        - ssbd
        - ibpb
        - vmmcall
        - fsgsbase
        - bmi1
        - avx2
        - smep
        - bmi2
        - rdseed
        - adx
        - smap
        - clflushopt
        - sha_ni
        - xsaveopt
        - xsavec
        - xsaves
        - clzero
        - arat
        - overflow_recov
        - succor
    cpu_model:
        AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
    cpuarch:
        x86_64
    cwd:
        /
    disks:
        - dm-1
        - sr0
        - dm-0
        - sda
    dns:
        ----------
        domain:
        ip4_nameservers:
            - 192.168.160.2
        ip6_nameservers:
        nameservers:
            - 192.168.160.2
        options:
        search:
            - localdomain
        sortlist:
    domain:
    fqdn:
        laolin
    fqdn_ip4:
        - 192.168.160.129
        - 192.168.122.1
    fqdn_ip6:
        - fe80::db73:fc88:cc7a:b105
    fqdns:
    gid:
        0
    gpus:
        |_
          ----------
          model:
              SVGA II Adapter
          vendor:
              vmware
    groupname:
        root
    host:
        laolin
    hwaddr_interfaces:
        ----------
        ens33:
            00:0c:29:73:03:4c
        lo:
            00:00:00:00:00:00
        virbr0:
            52:54:00:47:8a:31
        virbr0-nic:
            52:54:00:47:8a:31
    id:
        laolin
    init:
        systemd
    ip4_gw:
        192.168.160.2
    ip4_interfaces:
        ----------
        ens33:
            - 192.168.160.129
        lo:
            - 127.0.0.1
        virbr0:
            - 192.168.122.1
        virbr0-nic:
    ip6_gw:
        False
    ip6_interfaces:
        ----------
        ens33:
            - fe80::db73:fc88:cc7a:b105
        lo:
            - ::1
        virbr0:
        virbr0-nic:
    ip_gw:
        True
    ip_interfaces:
        ----------
        ens33:
            - 192.168.160.129
            - fe80::db73:fc88:cc7a:b105
        lo:
            - 127.0.0.1
            - ::1
        virbr0:
            - 192.168.122.1
        virbr0-nic:
    ipv4:
        - 127.0.0.1
        - 192.168.122.1
        - 192.168.160.129
    ipv6:
        - ::1
        - fe80::db73:fc88:cc7a:b105
    kernel:
        Linux
    kernelrelease:
        4.18.0-257.el8.x86_64
    kernelversion:
        #1 SMP Thu Dec 3 22:16:23 UTC 2020
    locale_info:
        ----------
        defaultencoding:
            UTF-8
        defaultlanguage:
            zh_CN
        detectedencoding:
            UTF-8
        timezone:
            EDT
    localhost:
        laolin
    lsb_distrib_codename:
        CentOS Stream 8
    lsb_distrib_id:
        CentOS Stream
    lsb_distrib_release:
        8
    machine_id:
        24c6019118c24407b74cc45f1ff184f0
    manufacturer:
        VMware, Inc.
    master:
        192.168.160.131
    mdadm:
    mem_total:
        3708
    nodename:
        laolin
    num_cpus:
        2
    num_gpus:
        1
    os:
        CentOS Stream
    os_family:
        CentOS Stream
    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:
        93034
    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:
        3000.9
    saltversioninfo:
        - 3000
        - 9
    selinux:
        ----------
        enabled:
            True
        enforced:
            Enforcing
    serialnumber:
        VMware-56 4d 21 6c 76 40 ae 6b-06 66 93 70 b2 73 03 4c
    server_id:
        134728941
    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 +IDN2 -IDN +PCRE2 default-hierarchy=legacy
        version:
            239
    uid:
        0
    username:
        root
    uuid:
        6c214d56-4076-6bae-0666-9370b273034c
    virtual:
        VMware
    zfs_feature_flags:
        False
    zfs_support:
        False
    zmqversion:
        4.3.1
[root@master ~]# salt '*' grains.get fqdn_ip4
master:
    - 192.168.160.131
laolin:
    - 192.168.160.129
    - 192.168.122.1
[root@master ~]# salt '*' grains.get ip4_interfaces
laolin:
    ----------
    ens33:
        - 192.168.160.129
    lo:
        - 127.0.0.1
    virbr0:
        - 192.168.122.1
    virbr0-nic:
master:
    ----------
    ens33:
        - 192.168.160.131
    lo:
        - 127.0.0.1
[root@master ~]# salt '*' grains.get ip4_interfaces:ens33
laolin:
    - 192.168.160.129
master:
    - 192.168.160.131

用Grains来匹配minion

[root@master ~]# salt -G 'os:CentOS Stream' cmd.run 'uptime'
master:
     22:29:10 up 43 min,  3 users,  load average: 0.50, 0.27, 0.35
laolin:
     22:29:10 up 58 min,  3 users,  load average: 0.36, 0.40, 0.37
自定义grains的方法!

minion配置文件,在配置文件中搜索grains
在/etc/salt下生成一个grains文件,在此文件中定义

[root@laolin ~]# vim /etc/salt/grains
[root@laolin ~]# cat /etc/salt/grains
test-grains: linux-node1
[root@laolin ~]# systemctl restart salt-minion
[root@master ~]# salt '*' grains.get test-grains
laolin:
    linux-node1
master:
 
# 不重启自定义grains!!
[root@laolin ~]# vim /etc/salt/grains
[root@laolin ~]# cat /etc/salt/grains
test-grains: linux-node1
aa: test
[root@laolin ~]# systemctl restart salt-minion
[root@master ~]# salt '*' saltutil.sync_grains
laolin:
master:

[root@master ~]# salt '*' grains.get aa
master:
    test
minion1:

SaltStack组件之Pillar!

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

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

Pillar的特点:

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

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

[root@master ~]#  vim /etc/salt/master
# master config file that can then be used on minions.
pillar_opts: True

[root@master ~]# salt '*' pillar.items
master:
    ----------
    master:
        ----------
        __cli:
            salt-master
        __role:
            master
        allow_minion_key_revoke:
            True
        archive_jobs:
            False
        auth_events:
            True
        auth_mode:
            1
        auto_accept:
            False
        azurefs_update_interval:
            60
        cache:
            localfs
        cache_sreqs:
            True
        cachedir:
            /var/cache/salt/master
        clean_dynamic_modules:
            True
        cli_summary:
        ......
            5
        zmq_backlog:
            1000
        zmq_filtering:
            False
        zmq_monitor:
            False

[root@master ~]# vim /etc/salt/master
pillar_roots:
  base:
    - /srv/pillar
[root@master ~]# mkdir -p /srv/pillar/base
[root@master ~]# systemctl restart salt-master
[root@master ~]# vim /srv/pillar/base/apache.sls
[root@master ~]# cat /srv/pillar/base/apache.sls
{% if grains['os'] == 'CentOS Stream' %}
apache: httpd
{% elif grains['os'] == 'Ubuntu' %}
apache: apache2
{% endif %}

[root@master ~]# vim /srv/pillar/base/top.sls
[root@master ~]# cat /srv/pillar/base/top.sls
base:
  'laolin':
    - apache

[root@master ~]# salt '*' pillar.items
minion:
    ----------
    apache:
        httpd

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

[root@master ~]# mkdir /srv/salt/base/web/apache/
[root@master ~]# 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系统
ljj@czh-virtual-machine:~$ sudo apt-get install curl

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
laolin
ljj-virtual-machine
Denied Keys:
Unaccepted Keys:
tom-virtual-machine

[root@master ~]# salt 'ljj-virtual-machine' state.highstate
ljj-virtual-machine:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 11:59:29.632336
    Duration: 31734.892 ms
     Changes:   
              ----------
              libnginx-mod-http-image-filter:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              libnginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              libnginx-mod-mail:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              libnginx-mod-stream:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              nginx:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              nginx-common:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
              nginx-core:
                  ----------
                  new:
                      1.18.0-0ubuntu1.2
                  old:
----------
          ID: nginx-sevice
    Function: service.running
        Name: nginx
      Result: True
     Comment: The service nginx is already running
     Started: 12:00:01.384897
    Duration: 31.48 ms
     Changes:   

Summary for czh-virtual-machine
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:  30.699 s

laolin:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: False
     Comment: State 'pkg.installed' was not found in SLS 'web.nginx.install'
              Reason: 'pkg' __virtual__ returned False
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: False
     Comment: The named service nginx is not available
     Started: 22:19:30.228337
    Duration: 35.679 ms
     Changes:   

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

根本出不来,一直试,一直都是这个样子!!!!

上一篇:SaltStack数据系统


下一篇:X86汇编语言从实模式到保护模式14:用户程序编程接口及其实现