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@node01-Linux ~]# vim /etc/salt/master
...
file_roots:
base:
- /srv/salt/base
prod:
- /srv/salt/prod
...
[root@node01-Linux ~]# mkdir -p /srv/salt/base
[root@node01-Linux ~]# tree /srv/salt/
/srv/salt/
└── base
[root@node01-Linux ~]# systemctl restart salt-master
需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名
2. 用SaltStack配置一个apache实例
2.1 在Master上部署sls配置文件并执行
[root@node01-Linux ~]# cd /srv/salt/base/
[root@node01-Linux base]# mkdir -p web/apache
[root@node01-Linux base]# tree web
web
└── apache
1 directory, 0 files
[root@node01-Linux base]# cd web/apache/
[root@node01-Linux apache]# vim install.sls
apache-install:
pkg.installed:
- name: httpd
apache-service:
service.running:
- name: httpd
- enable: True
// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面
#执行状态描述文件
[root@node01-Linux base]# salt 'node02*' state.sls web.apache.install
node02-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 19:16:44.385272
Duration: 28265.877 ms
Changes:
----------
httpd:
----------
new:
2.4.6-93.el7.centos
old:
httpd-tools:
----------
new:
2.4.6-93.el7.centos
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 19:17:12.678412
Duration: 4096.884 ms
Changes:
----------
httpd:
True
Summary for node02-linux.example.com
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 32.363 s
2.2 在Minion上检查
[root@node02-linux ~]# 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 2020-08-18 10:16:08 CST; 2min 6s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 13561 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─13561 /usr/sbin/httpd -DFOREGROUND
├─13568 /usr/sbin/httpd -DFOREGROUND
├─13569 /usr/sbin/httpd -DFOREGROUND
├─13570 /usr/sbin/httpd -DFOREGROUND
├─13571 /usr/sbin/httpd -DFOREGROUND
└─13572 /usr/sbin/httpd -DFOREGROUND
Aug 18 10:15:47 node02-linux.example.com systemd[1]: Starting The A...
Aug 18 10:16:08 node02-linux.example.com systemd[1]: Started The Ap...
Hint: Some lines were ellipsized, use -l to show in full.
由以上内容可知apache确实已部署成功。
卸载
[root@node01-Linux ~]# cd /srv/salt/
[root@node01-Linux salt]# ls
base
[root@node01-Linux salt]# mkdir -p prod/web/apache
[root@node01-Linux salt]# tree
.
├── base
│ └── web
│ └── nginx
│ └── install.sls
└── prod
└── web
└── apache
6 directories, 1 file
[root@node01-Linux salt]# pwd
/srv/salt
[root@node01-Linux salt]# cd prod/web/apache/
[root@node01-Linux apache]# cat uninstall.sls
apache-uninstall:
pkg.removed:
- name: httpd
[root@node01-linux salt]# pwd
/srv/salt
[root@node01-linux salt]# tree
.
├── base
│ └── web
│ └── apache
│ └── install.sls
└── prod
└── web
└── apache
└── uninstall.sls
6 directories, 2 files
[root@node01-linux ~]# salt 'node02*' state.sls web.apache.uninstall saltenv=prod
node02-linux.example.com:
----------
ID: apache-uninstall
Function: pkg.removed
Name: httpd
Result: True
Comment: All specified packages are already absent
Started: 22:55:14.096511
Duration: 519.164 ms
Changes:
Summary for node02-linux.example.com
------------
Succeeded: 1
Failed: 0
------------
Total states run: 1
Total run time: 519.164 ms
#被控制端minion验证
[root@node02-linux ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-93.el7.centos.x86_64
执行状态文件的技巧:
先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件
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@node01-linux base]# pwd
/srv/salt/base
[root@node01-linux base]# ls
web
[root@node01-linux base]# vim top.sls
base: //要执行状态文件的环境
'node02*': //要执行状态文件的目标
- web.apache.install //要执行的状态文件
#使用高级状态来执行
[root@node01-linux base]# salt '*' state.highstate
node07-linux.example.com:
----------
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 node07-linux.example.com
------------
Succeeded: 0
Failed: 1
------------
Total states run: 1
Total run time: 0.000 ms
node02-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: True
Comment: The following packages were installed/updated: httpd
Started: 23:58:17.173064
Duration: 11928.814 ms
Changes:
----------
httpd:
----------
new:
2.4.6-93.el7.centos
old:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: True
Comment: Service httpd has been enabled, and is running
Started: 23:58:29.129135
Duration: 494.611 ms
Changes:
----------
httpd:
True
Summary for node02-linux.example.com
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 12.423 s
ERROR: Minions returned with non-zero exit code
#查看minion的httpd
[root@node02-linux ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-93.el7.centos.x86_64
httpd-2.4.6-93.el7.centos.x86_64
[root@node02-linux ~]# 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 Wed 2020-08-19 23:58:29 CST; 2min 38s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 2410 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─2410 /usr/sbin/httpd -DFOREGROUND
├─2417 /usr/sbin/httpd -DFOREGROUND
├─2418 /usr/sbin/httpd -DFOREGROUND
├─2419 /usr/sbin/httpd -DFOREGROUND
├─2420 /usr/sbin/httpd -DFOREGROUND
└─2421 /usr/sbin/httpd -DFOREGROUND
Aug 19 23:58:29 node02-linux.example.com systemd[1]: Starting The Apache...
Aug 19 23:58:29 node02-linux.example.com systemd[1]: Started The Apache ...
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@node01-linux ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
#检查minion有没有安装httpd
[root@node02-linux ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-93.el7.centos.x86_64
#在master上执行高级状态的测试
[root@node01-linux base]# salt 'node02*' state.highstate test=True
node02-linux.example.com:
----------
ID: apache-install
Function: pkg.installed
Name: httpd
Result: None
Comment: The following packages would be installed/updated: httpd
Started: 00:12:45.259321
Duration: 554.327 ms
Changes:
----------
ID: apache-service
Function: service.running
Name: httpd
Result: None
Comment: Service httpd not present; if created in this state run, it would have been started
Started: 00:12:45.814438
Duration: 25.575 ms
Changes:
Summary for node02-linux.example.com
------------
Succeeded: 2 (unchanged=2)
Failed: 0
------------
Total states run: 2
Total run time: 579.902 ms
#在minion上查看httpd是否安装
[root@node02-linux ~]# rpm -qa|grep httpd
httpd-tools-2.4.6-93.el7.centos.x86_64
//由此可见高级状态并没有执行,因为httpd并没有安装