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
/srv
└── salt
├── base
├── dev
├── prod
└── test
//重启服务
[root@master ~]# systemctl restart salt-master
注意点:
- base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
用SaltStack配置一个apache实例
在Master上部署sls配置文件并执行
[root@master ~]# cd /srv/salt/base/
[root@master base]# mkdir -p web/apache
[root@master base]# cd web/apache/
[root@master apache]# touch install.sls
[root@master apache]# vim apache.sls //创建一个状态文件
[root@localhost base]# tree
.
└── web
└── apache
└── apache.sls
[root@master apache]# cat apache.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
//测试需要执行状态文件的主机是否能正常通信
[root@master apache]# salt 'node1' test.ping
node1:
True
//执行状态描述文件
[root@master apache]# salt 'node1' state.sls web.apache.install saltenv=base //此处如果环境是base,可以不需要写saltenv=base,但是如果不是base环境,需要加上saltenv
node1:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211112135221953927
ERROR: Minions returned with non-zero exit code
在Minion上检查
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabl>
Active: active (running) since Fri 2021-11-12 21:52:57 CST; >
Docs: man:httpd.service(8)
Main PID: 11544 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 5770)
Memory: 25.4M
CGroup: /system.slice/httpd.service
├─11544 /usr/sbin/httpd -DFOREGROUND
├─11971 /usr/sbin/httpd -DFOREGROUND
├─11972 /usr/sbin/httpd -DFOREGROUND
├─11973 /usr/sbin/httpd -DFOREGROUND
└─11974 /usr/sbin/httpd -DFOREGROUND
Nov 12 21:52:42 node1 systemd[1]: Starting The Apache HTTP Serv>
Nov 12 21:52:57 node1 httpd[11544]: AH00558: httpd: Could not r>
Nov 12 21:52:57 node1 systemd[1]: Started The Apache HTTP Serve>
Nov 12 21:53:07 node1 httpd[11544]: Server configured, listenin>
执行状态文件的技巧:
- 先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
top file
直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。
top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。
top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。
top file 实例两台主机分别安装httpd和nginx:
环境说明:
| 主机 | IP | 需安装的应用 |
|---|---|---|
| master | 192.168.218.132 | salt-master salt-minion |
| node1 | 192.168.218.132 | salt-minion |
| node2 | 192.168.218.132 | salt-minion |
[root@master ~]# salt-key -L
Accepted Keys:
master
node1
node2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
//环境结构
[root@master ~]# cd /srv/salt/base/web/
[root@master web]# ls
apache nginx
[root@master web]# tree
.
├── apache
│ └── install.sls
└── nginx
└── install.sls
//编写状态文件
[root@master web]# cat apache/install.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
[root@master web]# cat nginx/install.sls
nginx-install:
pkg.installed:
- name: nginx
nginx-service:
service.running:
- name: nginx
- enable: True
//编写top file
[root@master base]# pwd
/srv/salt/base
[root@master base]# ls
web
[root@master base]# vim top.sls
[root@master base]# cat top.sls
base:
'node1':
- web.nginx.install
'node2':
- web.apache.install
//执行top file
[root@master base]# salt '*' state.highstate saltenv=base
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
node1:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211102144322280180
node2:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211102144322280180
ERROR: Minions returned with non-zero exit code
//查看minion的httpd状态
[root@node1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:80 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:80 [::]:*
LISTEN 0 128 [::]:22 [::]:*
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
注意:
- 若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而 salt ‘*’ state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
注意:
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
//停掉minion上的httpd服务
[root@node2 ~]# systemctl stop httpd
//通知node2主机执行高级状态
[root@master base]# salt 'node2' state.highstate saltenv=base
node2:
Minion did not return. [No response]
The minions may not have all finished running and any remaining minions will return upon completion. To look up the return data for this job later, run the following command:
salt-run jobs.lookup_jid 20211102145745388034
ERROR: Minions returned with non-zero exit code
[root@master base]# salt-run jobs.lookup_jid 20211102145745388034
node2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 22:57:47.999334
Duration: 570.261 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd is already enabled, and is running
Started: 22:57:48.571253
Duration: 15185.955 ms
Changes:
----------
httpd:
True
Summary for node2
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 15.756 s
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:22 [::]:*
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
//停掉minon上的httpd服务
[root@node2 ~]# systemctl stop httpd
//在master上执行高级状态的测试
[root@master base]# salt 'node2' state.highstate saltenv=base test=true
node2:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 23:10:00.807242
Duration: 582.195 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd is set to start
Started: 23:10:01.391406
Duration: 48.108 ms
Changes:
Summary for node2
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 630.303 ms
//在minion上查看httpd是否启动
[root@node2 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
//由此可见高级状态并没有执行,因为httpd并没有启动
本文介绍如何使用 SaltStack 进行高级状态管理,包括 YAML 配置、topfile 的作用及其配置方法,以及如何利用这些配置实现自动化的服务安装与启动。
399

被折叠的 条评论
为什么被折叠?



