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
dev:
- /srv/salt/dev/services
- /srv/salt/dev/states
prod:
- /srv/salt/prod/services
- /srv/salt/prod/states
# 省略
[root@master ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
7 directories, 3 files
[root@master ~]# systemctl restart salt-master
需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
SaltStack配置apache实例
在Master上配置SLS文件(状态描述文件)
[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
top.sls web
[root@master base]# rm -rf *
[root@master base]# mkdir -p web/apache
[root@master base]# tree
.
└── web
└── apache
2 directories, 0 files
[root@master base]# cd web/apache
[root@master apache]# vim apache.sls
[root@master apache]# cat apache.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
[root@master base]# tree
.
└── web
└── apache
└── apache.sls
2 directories, 1 file
//执行状态文件前,先用test.ping模块ping一下,防止没连接执行失败
[root@master base]# salt 'node1' test.ping
node1:
True //状态为True,可以执行
//执行
[root@master base]# salt 'node1' state.sls web.apache.apache saltenv=base
node1:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: All specified packages are already installed
Started: 18:42:00.257054
Duration: 1704.971 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: The service httpd is already running
Started: 18:42:01.966728
Duration: 65.123 ms
Changes:
Summary for node1
------------
Succeeded: 2
Failed: 0
------------
Total states run: 2
Total run time: 1.770 s
注意:
YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
在node1上查看
[root@node1 ~]# 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 Tue 2021-11-02 18:16:48 CST; 26min ago
Docs: man:httpd.service(8)
Main PID: 1117 (httpd)
Status: "Running, listening on: port 80"
Tasks: 213 (limit: 23788)
Memory: 42.5M
CGroup: /system.slice/httpd.service
├─1117 /usr/sbin/httpd -DFOREGROUND
├─1270 /usr/sbin/httpd -DFOREGROUND
├─1271 /usr/sbin/httpd -DFOREGROUND
├─1272 /usr/sbin/httpd -DFOREGROUND
└─1274 /usr/sbin/httpd -DFOREGROUND
11月 02 18:16:48 node1 systemd[1]: Starting The Apache HTTP Server...
11月 02 18:16:48 node1 httpd[1117]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.159.14. Set the 'ServerName' directive globally to supp>
11月 02 18:16:48 node1 systemd[1]: Started The Apache HTTP Server.
11月 02 18:16:49 node1 httpd[1117]: Server configured, listening on: port 80
由以上内容可知apache确实已部署成功。
执行状态文件的技巧:
- 先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
top file
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 base]# mkdir -p web/nginx/
[root@master base]# tree
.
└── web
├── apache
│ └── apache.sls
└── nginx
3 directories, 1 file
[root@master base]# cd web/nginx/
[root@master nginx]# touch nginx.sls
[root@master nginx]# vim nginx.sls
[root@master nginx]# cat nginx.sls
nginx-install:
pkg.installed:
- name: nginx
nginx-service:
service.running:
- name: nginx
- enable: True
//返回base下并创建top.sls文件
[root@master base]# vim top.sls
[root@master base]# cat top.sls
base: //要执行状态文件的环境
'node2': //要执行状态文件的目标(这里用主机名或者IP地址都可以)
- web.nginx.nginx //要执行的状态文件
//停止node2上的nginx
[root@master base]# systemctl stop nginx
//执行top.sls
[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
node1:
----------
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 node1
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
node2:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 18:57:31.711529
Duration: 1907.269 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx is already enabled, and is running
Started: 18:57:33.628350
Duration: 176.28 ms
Changes:
----------
nginx:
True
Summary for node2
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 2.084 s
//在node2上查看
[root@node2 ~]# 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 Tue 2021-11-02 18:57:33 CST; 6min ago
Process: 75951 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Process: 75950 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
Process: 75948 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
Main PID: 75953 (nginx)
Tasks: 5 (limit: 23788)
Memory: 8.9M
CGroup: /system.slice/nginx.service
├─75953 nginx: master process /usr/sbin/nginx
├─75954 nginx: worker process
├─75955 nginx: worker process
├─75956 nginx: worker process
└─75957 nginx: worker process
11月 02 18:57:33 node2 systemd[1]: Starting The nginx HTTP and reverse proxy server...
11月 02 18:57:33 node2 nginx[75950]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
11月 02 18:57:33 node2 nginx[75950]: nginx: configuration file /etc/nginx/nginx.conf test is successful
11月 02 18:57:33 node2 systemd[1]: Started The nginx HTTP and reverse proxy server.
执行结束之后,发现node1也执行此操作
注意:
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而 salt '*' state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
高级状态highstate的使用
管理SaltStack时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
如果使用的是'*'来进行执行操作的话,则会使所有节点进行执行此命令,容易出事
//先停掉node2上的nginx服务
[root@node2 ~]# systemctl stop nginx
//再次使用高级状态来执行top.sls文件
[root@master base]# salt 'node2' state.highstate //将'*'换为节点名称
node2:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 19:06:21.369127
Duration: 1224.644 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx is already enabled, and is running
Started: 19:06:22.600049
Duration: 178.347 ms
Changes:
----------
nginx:
True
Summary for node2
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 1.403 s
评论区