用SaltStack配置一个nginx实例
在Master上部署sls配置文件并执行
[root@master ~]# mkdir -p /srv/salt/base
[root@master ~]# cd /srv/salt/base/
[root@master base]# mkdir -p web/nginx
[root@master base]# cd web/nginx/
[root@master nginx]# touch install.sls
[root@master nginx]# vim install.sls
nginx-install:
pkg.installed:
- name: nginx
nginx-service:
service.running:
- name: nginx
- enable: True
[root@master nginx]# salt 'minion1' state.sls web.nginx.install
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 09:47:55.421400
Duration: 18955.246 ms
Changes:
----------
gd:
----------
new:
2.2.5-7.el8
old:
libXpm:
----------
new:
3.5.12-8.el8
old:
libwebp:
----------
new:
1.0.0-5.el8
old:
nginx:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-all-modules:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-filesystem:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-image-filter:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-perl:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-http-xslt-filter:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-mail:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
nginx-mod-stream:
----------
new:
1:1.14.1-9.module_el8.0.0+184+e34fea82
old:
perl-Carp:
----------
new:
1.42-396.el8
old:
perl-Data-Dumper:
----------
new:
2.167-399.el8
old:
perl-Digest:
----------
new:
1.17-395.el8
old:
perl-Digest-MD5:
----------
new:
2.55-396.el8
old:
perl-Encode:
----------
new:
4:2.97-3.el8
old:
perl-Errno:
----------
new:
1.28-420.el8
old:
perl-Exporter:
----------
new:
5.72-396.el8
old:
perl-File-Path:
----------
new:
2.15-2.el8
old:
perl-File-Temp:
----------
new:
0.230.600-1.el8
old:
perl-Getopt-Long:
----------
new:
1:2.50-4.el8
old:
perl-HTTP-Tiny:
----------
new:
0.074-1.el8
old:
perl-IO:
----------
new:
1.38-420.el8
old:
perl-IO-Socket-IP:
----------
new:
0.39-5.el8
old:
perl-IO-Socket-SSL:
----------
new:
2.066-4.module_el8.4.0+517+be1595ff
old:
perl-MIME-Base64:
----------
new:
3.15-396.el8
old:
perl-Mozilla-CA:
----------
new:
20160104-7.module_el8.3.0+416+dee7bcef
old:
perl-Net-SSLeay:
----------
new:
1.88-1.module_el8.4.0+517+be1595ff
old:
perl-PathTools:
----------
new:
3.74-1.el8
old:
perl-Pod-Escapes:
----------
new:
1:1.07-395.el8
old:
perl-Pod-Perldoc:
----------
new:
3.28-396.el8
old:
perl-Pod-Simple:
----------
new:
1:3.35-395.el8
old:
perl-Pod-Usage:
----------
new:
4:1.69-395.el8
old:
perl-Scalar-List-Utils:
----------
new:
3:1.49-2.el8
old:
perl-Socket:
----------
new:
4:2.027-3.el8
old:
perl-Storable:
----------
new:
1:3.11-3.el8
old:
perl-Term-ANSIColor:
----------
new:
4.06-396.el8
old:
perl-Term-Cap:
----------
new:
1.17-395.el8
old:
perl-Text-ParseWords:
----------
new:
3.30-395.el8
old:
perl-Text-Tabs+Wrap:
----------
new:
2013.0523-395.el8
old:
perl-Time-Local:
----------
new:
1:1.280-1.el8
old:
perl-URI:
----------
new:
1.73-3.el8
old:
perl-Unicode-Normalize:
----------
new:
1.25-396.el8
old:
perl-constant:
----------
new:
1.33-396.el8
old:
perl-interpreter:
----------
new:
4:5.26.3-420.el8
old:
perl-libnet:
----------
new:
3.11-3.el8
old:
perl-libs:
----------
new:
4:5.26.3-420.el8
old:
perl-macros:
----------
new:
4:5.26.3-420.el8
old:
perl-parent:
----------
new:
1:0.237-1.el8
old:
perl-podlators:
----------
new:
4.11-1.el8
old:
perl-threads:
----------
new:
1:2.21-2.el8
old:
perl-threads-shared:
----------
new:
1.58-2.el8
old:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx has been enabled, and is running
Started: 09:48:14.426582
Duration: 934.742 ms
Changes:
----------
nginx:
True
Summary for minion1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 19.890 s
在minion1上检查
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-07-04 09:48:15 EDT; 1min 3s ago
Main PID: 25935 (nginx)
Tasks: 2 (limit: 4741)
Memory: 8.9M
CGroup: /system.slice/nginx.service
├─25935 nginx: master process /usr/sbin/nginx
└─25936 nginx: worker process
Jul 04 09:48:14 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 09:48:15 minion1 nginx[25125]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 09:48:15 minion1 nginx[25125]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 09:48:15 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 09:48:15 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
top file 实例:
[root@master nginx]# cd /srv/salt/base/
[root@master base]# vim top.sls
base:
'minion1':
- web.nginx.install
#停止minion1的nginx
[root@minion1 ~]# systemctl stop nginx
[root@master base]# salt '*' state.highstate
[root@master base]# salt '*' state.highstate
master:
----------
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 master
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 09:56:06.242460
Duration: 742.896 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx is already enabled, and is running
Started: 09:56:06.986551
Duration: 167.029 ms
Changes:
----------
nginx:
True
Summary for minion1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 909.925 ms
ERROR: Minions returned with non-zero exit code
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-07-04 09:56:07 EDT; 18s ago
Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 27548 (nginx)
Tasks: 2 (limit: 4741)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─27548 nginx: master process /usr/sbin/nginx
└─27549 nginx: worker process
Jul 04 09:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 09:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 09:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 09:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 09:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
高级状态highstate的使用
#停掉minion1上的nginx1服务
[root@minion1 ~]# systemctl stop nginx
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sun 2021-07-04 09:58:28 EDT; 13s ago
Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 27548 (code=exited, status=0/SUCCESS)
Jul 04 09:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 09:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 09:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 09:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 09:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Jul 04 09:58:28 minion1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Jul 04 09:58:28 minion1 systemd[1]: nginx.service: Succeeded.
Jul 04 09:58:28 minion1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
[root@master base]# salt 'minion1' state.highstate test=True
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 03:00:09.129614
Duration: 745.065 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: None
Comment: Service nginx is set to start
Started: 03:00:09.875923
Duration: 40.279 ms
Changes:
Summary for minion1
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 785.344 ms
#在minion1上查看nginx是否启动
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sun 2021-07-04 09:58:28 EDT; 2min 16s ago
Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 27548 (code=exited, status=0/SUCCESS)
Jul 04 09:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 09:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 09:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 09:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 09:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Jul 04 09:58:28 minion1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Jul 04 09:58:28 minion1 systemd[1]: nginx.service: Succeeded.
Jul 04 09:58:28 minion1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
SaltStack数据系统
信息查询实例:
[root@master base]# salt 'minion1' grains.items
minion1:
----------
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
- ss
- syscall
- nx
- pdpe1gb
- rdtscp
- lm
- constant_tsc
- arch_perfmon
- nopl
- xtopology
- tsc_reliable
- nonstop_tsc
- cpuid
- pni
- pclmulqdq
- ssse3
- fma
- cx16
- pcid
- sse4_1
- sse4_2
- x2apic
- movbe
- popcnt
- tsc_deadline_timer
- aes
- xsave
- avx
- f16c
- rdrand
- hypervisor
- lahf_lm
- abm
- 3dnowprefetch
- cpuid_fault
- invpcid_single
- pti
- ssbd
- ibrs
- ibpb
- stibp
- fsgsbase
- tsc_adjust
- bmi1
- avx2
- smep
- bmi2
- invpcid
- rdseed
- adx
- smap
- clflushopt
- xsaveopt
- xsavec
- xsaves
- arat
- md_clear
- flush_l1d
- arch_capabilities
cpu_model:
Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz
cpuarch:
x86_64
cwd:
/
disks:
- sr0
- sda
dns:
----------
domain:
ip4_nameservers:
- 114.114.114.114
ip6_nameservers:
nameservers:
- 114.114.114.114
options:
search:
sortlist:
domain:
fqdn:
minion1
fqdn_ip4:
- 192.168.11.120
fqdn_ip6:
- fe80::20c:29ff:fe56:e00d
fqdns:
- minion1
gid:
0
gpus:
|_
----------
model:
SVGA II Adapter
vendor:
vmware
groupname:
root
host:
minion1
hwaddr_interfaces:
----------
ens33:
00:0c:29:56:e0:0d
lo:
00:00:00:00:00:00
id:
minion1
init:
systemd
ip4_gw:
192.168.11.2
ip4_interfaces:
----------
ens33:
- 192.168.11.120
lo:
- 127.0.0.1
ip6_gw:
False
ip6_interfaces:
----------
ens33:
- fe80::20c:29ff:fe56:e00d
lo:
- ::1
ip_gw:
True
ip_interfaces:
----------
ens33:
- 192.168.11.120
- fe80::20c:29ff:fe56:e00d
lo:
- 127.0.0.1
- ::1
ipv4:
- 127.0.0.1
- 192.168.11.120
ipv6:
- ::1
- fe80::20c:29ff:fe56:e00d
kernel:
Linux
kernelparams:
|_
- BOOT_IMAGE
- (hd0,msdos1)/vmlinuz-4.18.0-257.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-257.el8.x86_64
kernelversion:
#1 SMP Thu Dec 3 22:16:23 UTC 2020
locale_info:
----------
defaultencoding:
UTF-8
defaultlanguage:
en_US
detectedencoding:
UTF-8
timezone:
EDT
localhost:
minion1
lsb_distrib_codename:
CentOS Stream 8
lsb_distrib_id:
CentOS Stream
lsb_distrib_release:
8
lvm:
----------
cs:
- root
- swap
machine_id:
77fbdf25f89147e5ab6034dbcacc11ed
manufacturer:
VMware, Inc.
master:
192.168.11.11
mdadm:
mem_total:
780
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:
24800
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 d0 7e ac 30 4e 61-c2 f3 a4 df 51 56 e0 0d
server_id:
156412900
shell:
/bin/sh
ssds:
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
systempath:
- /usr/local/sbin
- /usr/local/bin
- /usr/sbin
- /usr/bin
uid:
0
username:
root
uuid:
7ed04d56-30ac-614e-c2f3-a4df5156e00d
virtual:
VMware
zfs_feature_flags:
False
zfs_support:
False
zmqversion:
4.3.4
[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
[root@master base]# salt '*' grains.get fqdn_ip4
minion1:
- 192.168.11.120
master:
- 192.168.11.11
[root@master base]# salt '*' grains.get ip4_interfaces
minion1:
----------
ens33:
- 192.168.11.120
lo:
- 127.0.0.1
master:
----------
eth0:
- 192.168.11.11
lo:
- 127.0.0.1
[root@master base]# salt '*' grains.get ip4_interfaces:ens33
master:
minion1:
- 192.168.11.120
在top file里面使用Grains:
[root@master base]# vim /srv/salt/base/top.sls
base:
'os:CentOS Stream':
- match: grain
- web.nginx.install
#在minion1上停掉nginx
[root@minion1 ~]# systemctl stop nginx
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: inactive (dead) since Sun 2021-07-04 09:58:28 EDT; 34min ago
Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 27548 (code=exited, status=0/SUCCESS)
Jul 04 09:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 09:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 09:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 09:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 09:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Jul 04 09:58:28 minion1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Jul 04 09:58:28 minion1 systemd[1]: nginx.service: Succeeded.
Jul 04 09:58:28 minion1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
#在master上执行
[root@master base]# salt 'minion1' state.highstate
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 10:34:16.218945
Duration: 806.932 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx is already enabled, and is running
Started: 10:34:17.027175
Duration: 159.837 ms
Changes:
----------
nginx:
True
Summary for minion1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 966.769 ms
#查看nginx状态
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2021-07-04 10:34:17 EDT; 4s ago
Process: 27948 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 27946 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 27945 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 27950 (nginx)
Tasks: 2 (limit: 4741)
Memory: 4.1M
CGroup: /system.slice/nginx.service
├─27950 nginx: master process /usr/sbin/nginx
└─27951 nginx: worker process
Jul 04 10:34:17 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Jul 04 10:34:17 minion1 nginx[27946]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Jul 04 10:34:17 minion1 nginx[27946]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Jul 04 10:34:17 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Jul 04 10:34:17 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
自定义Grains的两种方法:
- minion配置文件,在配置文件中搜索grains
- 在/etc/salt下生成一个grains文件,在此文件中定义(推荐方式)
[root@minion1 ~]# vim /etc/salt/grains
test-grains: linux-node1
[root@minion1 ~]# systemctl restart salt-minion
[root@master ~]# salt '*' grains.get test-grains
minion1:
master:
linux-node1
不重启的情况下自定义grains:
[root@minion1 ~]# vim /etc/salt/grains
test-grains: linux-node1
aa: test
[root@master ~]# salt '*' saltutil.sync_grains
master:
minion1:
[root@master ~]# salt '*' grains.get aa
master:
test
minion1:
SaltStack组件之Pillar
Pillar也是SaltStack组件中非常重要的组件之一,是数据管理中心,经常配置states在大规模的配置管理工作中使用它。Pillar在SaltStack中主要的作用就是存储和定义配置管理中需要的一些数据,比如软件版本号、用户名密码等信息,它的定义存储格式与Grains类似,都是YAML格式
Pillar的特点:
- 可以给指定的minion定义它需要的数据
- 只有指定的人才能看到定义的数据
- 在master配置文件里设置
[root@master ~]# salt '*' pillar.items
minion1:
----------
master:
----------
默认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 ~]# systemctl restart salt-master
[root@master ~]# salt '*' pillar.items
.....
winrepo_cachefile:
winrepo.p
winrepo_dir:
/srv/salt/win/repo
winrepo_dir_ng:
/srv/salt/win/repo-ng
winrepo_fallback:
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@master ~]# vim /etc/salt/master
pillar_roots:
base:
- /srv/pillar/base
[root@master ~]# mkdir -p /srv/pillar/base
[root@master ~]# systemctl restart salt-master
[root@master ~]# vim /srv/pillar/base/apache.sls
{% if grains['os'] == 'CentOS Stream' %}
apache: httpd
{% elif grains['os'] == 'Ubuntu' %}
apache: apache2
{% endif %}
#定义top file入口文件
[root@master ~]# vim /srv/pillar/base/top.sls
base:
'*':
- apache
[root@master ~]# salt '*' pillar.items
minion1:
----------
apache:
httpd
#state
[root@master ~]# vim /srv/salt/base/top.sls
base:
'*':
- web.apache.install
#在salt下修改apache的状态文件,引用pillar的数据
[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系统
one@one-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
one@one-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
one@one-virtual-machine:~$ sudo apt-get update
one@one-virtual-machine:~$ sudo apt-get install salt-minion
one@one-virtual-machine:~$ sudo vim /etc/salt/minion
master: 192.168.11.11
one@one-virtual-machine:~$ sudo systemctl restart salt-minion
[root@master ~]# salt-key -L
Accepted Keys:
master
minion1
Denied Keys:
Unaccepted Keys:
one-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
one-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 one-virtual-machine
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 46.741 s
#这里可以看到他会根据操作系统的不同安装apache
查看状态
[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-04 06:36: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 04 06:36:54 master systemd[1]: Starting The Apache HTTP Server...
Jul 04 06:36:55 master httpd[31186]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, u>
Jul 04 06:36:55 master systemd[1]: Started The Apache HTTP Server.
Jul 04 06:36: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-04 06:37: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 04 06:37:02 minion1 systemd[1]: Starting The Apache HTTP Server...
Jul 04 06:37:08 minion1 httpd[33001]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, >
Jul 04 06:37:08 minion1 systemd[1]: Started The Apache HTTP Server.
Jul 04 06:37:08 minion1 httpd[33001]: Server configured, listening on: port 80
one@one-virtual-machine:~$ sudo systemctl status apache2
[sudo] one 的密码:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2021-07-04 18:37: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
7月 04 18:37:13 one-virtual-machine systemd[1]: Starting The Apache HTTP Server...
7月 04 18:37:13 one-virtual-machine apachectl[12592]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName'>
7月 04 18:37:13 one-virtual-machine systemd[1]: Started The Apache HTTP Server.