1. SaltStack数据系统
SaltStack有两大数据系统,分别是:
- Grains
- Pillar
2. SaltStack数据系统组件
2.1 SaltStack组件之Grains
Grains是SaltStack的一个组件,其存放着minion启动时收集到的信息。
Grains是SaltStack组件中非常重要的组件之一,因为我们在做配置部署的过程中会经常使用它,Grains是SaltStack记录minion的一些静态信息的组件。可简单理解为Grains记录着每台minion的一些常用属性,比如CPU、内存、磁盘、网络信息等。我们可以通过grains.items查看某台minion的所有Grains信息。
- Grains的功能:
收集资产信息
Grains应用场景:
- 信息查询
- 在命令行下进行目标匹配
- 在top file中进行目标匹配
- 在模板中进行目标匹配
模板中进行目标匹配请看:https://docs.saltstack.com/en/latest/topics/pillar/
信息查询实例:
[root@zzl01 ~]# salt '*' grains.items
zzl02:
----------
SSDs:
biosreleasedate:
07/29/2019
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
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- eagerfpu
- pni
- pclmulqdq
- vmx
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- tpr_shadow
- vnmi
- ept
- vpid
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- mpx
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- arat
cpu_model:
Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
cpuarch:
x86_64
cwd:
/
disks:
- sda
- sr0
- dm-0
- dm-1
dns:
----------
domain:
ip4_nameservers:
- 114.114.114.114
- 8.8.8.8
ip6_nameservers:
nameservers:
- 114.114.114.114
- 8.8.8.8
options:
search:
sortlist:
domain:
fqdn:
zzl02
fqdn_ip4:
- 192.168.20.120
- 192.168.122.1
fqdn_ip6:
- fe80::cc4:6cc0:d6df:6335
fqdns:
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
zzl02
hwaddr_interfaces:
----------
ens33:
00:0c:29:90:27:d3
lo:
00:00:00:00:00:00
virbr0:
52:54:00:d2:0d:9f
virbr0-nic:
52:54:00:d2:0d:9f
id:
zzl02
init:
systemd
ip4_gw:
192.168.20.2
ip4_interfaces:
----------
ens33:
- 192.168.20.120
lo:
- 127.0.0.1
virbr0:
- 192.168.122.1
virbr0-nic:
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::cc4:6cc0:d6df:6335
lo:
- ::1
virbr0:
virbr0-nic:
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.20.120
- fe80::cc4:6cc0:d6df:6335
lo:
- 127.0.0.1
- ::1
virbr0:
- 192.168.122.1
virbr0-nic:
ipv4:
- 127.0.0.1
- 192.168.20.120
- 192.168.122.1
ipv6:
- ::1
- fe80::cc4:6cc0:d6df:6335
kernel:
Linux
kernelrelease:
3.10.0-693.el7.x86_64
kernelversion:
#1 SMP Thu Jul 6 19:56:57 EDT 2017
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
zh_CN
detectedencoding:
UTF-8
timezone:
CST
localhost:
zzl02
lsb_distrib_codename:
Red Hat Enterprise Linux Server 7.4 (Maipo)
lsb_distrib_id:
Red Hat Enterprise Linux Server
lsb_distrib_release:
7.4
machine_id:
bf36c002131f4e6ab28bdadc883ec7e8
manufacturer:
VMware, Inc.
master:
192.168.20.99
mdadm:
mem_total:
1823
nodename:
zzl02
num_cpus:
1
num_gpus:
1
os:
RedHat
os_family:
RedHat
osarch:
x86_64
oscodename:
Maipo
osfinger:
Red Hat Enterprise Linux Server-7
osfullname:
Red Hat Enterprise Linux Server
osmajorrelease:
7
osrelease:
7.4
osrelease_info:
- 7
- 4
path:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
pid:
3205
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/lib64/python2.7/site-packages/gtk-2.0
- /usr/lib/python2.7/site-packages
pythonversion:
- 2
- 7
- 5
- final
- 0
saltpath:
/usr/lib/python2.7/site-packages/salt
saltversion:
3000.3
saltversioninfo:
- 3000
- 3
selinux:
----------
enabled:
False
enforced:
Disabled
serialnumber:
VMware-56 4d 1e a5 ee e3 55 59-a3 1d 5d e4 83 90 27 d3
server_id:
1072314615
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:
a51e4d56-e3ee-5955-a31d-5de4839027d3
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.1.4
[root@zzl01 ~]# salt '*' grains.get os
zzl02:
RedHat
//只查询所有的grains的key
[root@zzl01 ~]# salt '*' grains.ls
zzl02:
- 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
- lsb_distrib_release
- 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的值,比如想获取ip地址
[root@zzl01 ~]# salt '*' grains.get fqdn_ip4
zzl02:
- 192.168.20.120
- 192.168.122.1
[root@zzl01 ~]# salt '*' grains.get ip4_interfaces
zzl02:
----------
ens33:
- 192.168.20.120
lo:
- 127.0.0.1
virbr0:
- 192.168.122.1
virbr0-nic:
[root@zzl01 ~]# salt '*' grains.get ip4_interfaces:ens33
zzl02:
- 192.168.20.120
目标匹配实例:
用Grains来匹配minion:
[root@zzl01 ~]# salt -G 'os:RedHat' cmd.run 'uptime'
zzl02:
18:24:15 up 4:19, 2 users, load average: 0.06, 0.09, 0
在top file里面使用Grains:
[root@master ~]# vim /srv/salt/base/top.sls
base:
'os:CentOS':
- match: grain
- web.apache.apache
自定义Grains的两种方法:
- minion配置文件,在配置文件中搜索grains
- 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@zzl01 ~]# vim /etc/salt/grains
test-grains: service
[root@zzl01 ~]# salt-cp '*' /etc/salt/grains /etc/salt/grains
[root@zzl01 ~]# salt '*' grains.get test-grains
zzl02:
service
不重启的情况下自定义Grains:
[root@zzl01 ~]# vim /etc/salt/grains
test-grains: service
zzl: runtime
[root@zzl01 ~]# salt '*' grains.get zzl
zzl02:
[root@zzl01 ~]# salt-cp '*' /etc/salt/grains /etc/salt/grains
zzl02:
----------
/etc/salt/grains:
True
[root@zzl01 ~]# salt '*' saltutil.sync_grains
zzl02:
[root@zzl01 ~]# salt '*' grains.get zzl
zzl02:
runtime
2.2 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@zzl01 ~]# salt '*' pillar.items
zzl02:
----------
默认pillar是没有任何信息的,如果想查看信息,需要在 master 配置文件上把 pillar_opts的注释取消,并将其值设为 True。
[root@zzl01 ~]# vim /etc/salt/master
# master config file that can then be used on minions.
pillar_opts: True
# The pillar_safe_render_error option prevents the master from passing pillar
//重启master(zzl01)并查看pillar的信息
[root@zzl01 ~]# systemctl restart salt-master
[root@zzl01 ~]# 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(zzl01)的配置文件里找pillar_roots可以看到其存放pillar的位置
[root@zzl01 ~]# vim /etc/salt/master
pillar_roots:
base:
- /srv/pillar/base
[root@zzl01 ~]# vim /srv/pillar/base/apache.sls
{% if grains['os'] == 'RedHat' %}
apache: httpd
{% elif grains['os'] == 'CentOS' %}
apache: apache2
{% endif %}
//定义top file入口文件
[root@zzl01 ~]# vim /srv/pillar/base/top.sls
base: //指定环境
'zzl02': //指定主机
- apache //引用apache.sls或apache/init.sls
//这个top.sls文件的意思表示的是zzl02这台主机的base环境能够访问到apache这个pillar
[root@zzl01 ~]# salt '*' pillar.items
zzl02:
----------
apache:
httpd
//在salt下修改apache的状态文件,引用pillar的数据
[root@zzl01 ~]# 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@zzl01 ~]# salt '*' state.highstate
zzl02:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:11:38.982474
Duration: 1093.78 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 20:11:40.077067
Duration: 372.96 ms
Changes:
----------
httpd:
True
Summary for zzl02
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.467 s
2.3 Grains与Pillar的区别
组件 | 存储位置 | 类型 | 采集方式 | 应用场景 |
---|---|---|---|---|
Grains | minion | 静态 | minion启动时采集可通过刷新避免重启minion服务 | 1.信息查询 2.在命令行下进行目标匹配 3.在top file中进行目标匹配 4.在模板中进行目标匹配 |
Pillar | master | 动态 | 指定,实时生效 | 1.目标匹配 2.敏感数据配置 |