SaltStack之配置管理
1. YAML语言
YAML是一种直观的能够被电脑识别的数据序列化格式,是一个可读性高并且容易被人类阅读,容易和脚本语言交互,用来表达资料序列的编程语言。
它类似于标准通用标记语言的子集XML的数据描述语言,语法比XML简单很多。
YAML语言的格式如下:
house:
family:
name: Doe
parents:
- John
- Jane
children:
- Paul
- Mark
- Simone
address:
number: 34
street: Main Street
city: Nowheretown
zipcode: 12345
YAML的基本规则:
使用缩进来表示层级关系,每层2个空格,禁止使用TAB键
当冒号不是处于最后时,冒号后面必须有一个空格
用 - 表示列表,- 的后面必须有一个空格
用 # 表示注释
YAML配置文件要放到SaltStack让我们放的位置,可以在SaltStack的 Master 配置文件中查找file_roots即可看到。
[root@master ~]# vim /etc/salt/master
...
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
...
[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
4 directories, 0 files
[root@master ~]# systemctl restart salt-master
需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
2. 用SaltStack配置一个nginx实例
2.1 在Master上部署sls配置文件并执行
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
[root@master base]# mkdir -p web/nginx
[root@master base]# tree web
web
└── nginx
1 directory, 0 files
[root@master base]# cd web/nginx/
[root@master nginx]# touch nginx.sls
[root@master nginx]# ll
总用量 4
-rw-r--r--. 1 root root 247 8月 17 20:36 nginx.sls
[root@master nginx]# cat nginx.sls
nginx-install:
pkg.installed:
- name: nainx
// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
[root@master ~]# ls /srv/salt/base/web/nginx/
nginx.sls
//执行状态描述文件
[root@master ~]# salt 'minion' state.sls web.nginx.nginx saltenv=base
minion:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 22:01:39.359415
Duration: 26451.423 ms
Changes:
----------
centos-indexhtml:
----------
new:
7-9.el7.centos
old:
...省略...
nginx-mod-stream:
----------
new:
1:1.16.1-1.el7
old:
Summary for 192.168.152.168
------------
Succeeded: 1 (changed=1)
Failed: 0
------------
Total states run:
Total run time: 23.220 s
3. top file
3.1 top file介绍
直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。
top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。
top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。
top file 实例:
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
web
[root@master base]# vim top.sls
base: //要执行状态文件的环境
'minion': //要执行状态文件的目标
- web.apache.apache //要执行的状态文件
//停止minion的httpd
[root@minion ~]# systemctl stop httpd
[root@master ~]# 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
minion:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:03:41.543406
Duration: 2369.267 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 20:03:43.914117
Duration: 6379.735 ms
Changes:
----------
httpd:
True
Summary for minion
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 8.749 s
ERROR: Minions returned with non-zero exit code
//查看minion的httpd状态
[root@minion ~]# 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 二 2020-08-18 20:03:50 PDT; 8min ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 123926 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 125041 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
Tasks: 6
CGroup: /system.slice/httpd.service
├─125041 /usr/sbin/httpd -DFOREGROUND
├─125240 /usr/sbin/httpd -DFOREGROUND
├─125241 /usr/sbin/httpd -DFOREGROUND
├─125242 /usr/sbin/httpd -DFOREGROUND
├─125243 /usr/sbin/httpd -DFOREGROUND
└─125244 /usr/sbin/httpd -DFOREGROUND
8月 18 20:03:44 minion systemd[1]: Starting The Apache HTTP Server...
8月 18 20:03:49 minion httpd[125041]: AH00558: httpd: Could not reliably determine the server's fully qualifie...ssage
8月 18 20:03:50 minion systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
注意:
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而 salt ‘*’ state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
3.2高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@minion ~]# systemctl stop httpd
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 二 2020-08-18 20:12:51 PDT; 10s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 5219 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 125041 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 125041 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
8月 18 20:03:44 minion systemd[1]: Starting The Apache HTTP Server...
8月 18 20:03:49 minion httpd[125041]: AH00558: httpd: Could not reliably determine the server's fully qualifie...ssage
8月 18 20:03:50 minion systemd[1]: Started The Apache HTTP Server.
8月 18 20:12:50 minion systemd[1]: Stopping The Apache HTTP Server...
8月 18 20:12:51 minion systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
//在master上执行高级状态的测试
[root@master ~]# salt 'minion' state.highstate test=True
minion:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 20:15:09.183083
Duration: 2990.095 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 20:15:12.176415
Duration: 108.873 ms
Changes:
Summary for minion
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 3.099 s
//在minion上查看httpd是否启动
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: inactive (dead) since 二 2020-08-18 20:12:51 PDT; 4min 16s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 5219 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Process: 125041 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
Main PID: 125041 (code=exited, status=0/SUCCESS)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
8月 18 20:03:44 minion systemd[1]: Starting The Apache HTTP Server...
8月 18 20:03:49 minion httpd[125041]: AH00558: httpd: Could not reliably determine the server's fully qualifie...ssage
8月 18 20:03:50 minion systemd[1]: Started The Apache HTTP Server.
8月 18 20:12:50 minion systemd[1]: Stopping The Apache HTTP Server...
8月 18 20:12:51 minion systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
//由此可见高级状态并没有执行,因为httpd并没有启动