SaltStack之配置管理
Posted 卑微小胡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SaltStack之配置管理相关的知识,希望对你有一定的参考价值。
SaltStack之配置管理
文章目录
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
...此处省略N行
file_roots:
base:
- /srv/salt/base
test:
- /srv/salt/test
dev:
- /srv/salt/dev
prod:
- /srv/salt/prod
...此处省略N行
[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,不能改名
用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 saltenv=base
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: The following packages were installed/updated: nginx
Started: 22:30:31.964289
Duration: 28599.65 ms
Changes:
----------
dejavu-fonts-common:
----------
new:
2.35-7.el8
old:
......
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: 22:31:00.593519
Duration: 509.77 ms
Changes:
----------
nginx:
True
Summary for minion1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 29.109 s
[root@master nginx]#
在Minion1上检查
[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendo>
Active: active (running) since Mon 2021-07-05 22:31:00 CST; 5min ago
Main PID: 14352 (nginx)
Tasks: 2 (limit: 4767)
Memory: 4.4M
CGroup: /system.slice/nginx.service
├─14352 nginx: master process /usr/sbin/nginx
└─14357 nginx: worker process
Jul 05 22:31:00 minion1 systemd[1]: Starting The nginx HTTP and reverse >
Jul 05 22:31:00 minion1 nginx[14338]: nginx: the configuration file /etc>
Jul 05 22:31:00 minion1 nginx[14338]: nginx: configuration file /etc/ngi>
Jul 05 22:31:00 minion1 systemd[1]: nginx.service: Failed to parse PID f>
Jul 05 22:31:00 minion1 systemd[1]: Started The nginx HTTP and reverse p>
[root@minion1 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
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 [::]:*
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 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 'minion1' state.highstate
minion1:
----------
ID: nginx-install
Function: pkg.installed
Name: nginx
Result: True
Comment: All specified packages are already installed
Started: 22:43:00.368720
Duration: 531.528 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: True
Comment: Service nginx is already enabled, and is running
Started: 22:43:00.902250
Duration: 225.233 ms
Changes:
----------
nginx:
True
Summary for minion1
------------
Succeeded: 2 (changed=1)
Failed: 0
------------
Total states run: 2
Total run time: 756.761 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; vendo>
Active: active (running) since Mon 2021-07-05 22:43:01 CST; 16s ago
Process: 16697 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCES>
Process: 16695 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/>
Process: 16694 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited>
Main PID: 16699 (nginx)
Tasks: 2 (limit: 4767)
Memory: 3.7M
CGroup: /system.slice/nginx.service
├─16699 nginx: master process /usr/sbin/nginx
└─16700 nginx: worker process
Jul 05 22:43:00 minion1 systemd[1]: Starting The nginx HTTP and reverse >
Jul 05 22:43:01 minion1 nginx[16695]: nginx: the configuration file /etc>
Jul 05 22:43:01 minion1 nginx[16695]: nginx: configuration file /etc/ngi>
Jul 05 22:43:01 minion1 systemd[1]: nginx.service: Failed to parse PID f>
Jul 05 22:43:01 minion1 systemd[1]: Started The nginx HTTP and reverse p>
[root@minion1 ~]#
注意:
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而
salt '*' state.highstate
里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的
高级状态highstate的使用
管理SaltStack
时一般最常用的管理操作就是执行高级状态
[root@master ~]# salt '*' state.highstate //生产环境禁止这样使用salt命令
注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。
若在执行高级状态时加上参数test=True
,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作。
#停掉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; vendo>
Active: inactive (dead) since Mon 2021-07-05 22:46:23 CST; 3s ago
Process: 16697 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCES>
Process: 16695 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/>
Process: 16694 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited>
Main PID: 16699 (code=exited, status=0/SUCCESS)
Jul 05 22:43:00 minion1 systemd[1]: Starting The nginx HTTP and reverse >
Jul 05 22:43:01 minion1 nginx[16695]: nginx: the configuration file /etc>
Jul 05 22:43:01 minion1 nginx[16695]: nginx: configuration file /etc/ngi>
Jul 05 22:43:01 minion1 systemd[1]: nginx.service: Failed to parse PID f>
Jul 05 22:43:01 minion1 systemd[1]: Started The nginx HTTP and reverse p>
Jul 05 22:46:23 minion1 systemd[1]: Stopping The nginx HTTP and reverse >
Jul 05 22:46:23 minion1 systemd[1]: nginx.service: Succeeded.
Jul 05 22:46:23 minion1 systemd[1]: Stopped The nginx HTTP and reverse p>
[root@minion1 ~]#
#在master上执行高级状态的测试
[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: 22:47:01.585280
Duration: 494.56 ms
Changes:
----------
ID: nginx-service
Function: service.running
Name: nginx
Result: None
Comment: Service nginx is set to start
Started: 22:47:02.081800
Duration: 60.153 ms
Changes:
Summary for minion1
------------
Succeeded: 2 (unchanged=1)
Failed: 0
------------
Total states run: 2
Total run time: 554.713 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; vendo>
Active: inactive (dead) since Mon 2021-07-05 22:46:23 CST; 1min 1s ago
Process: 16697 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCES>
Process: 16695 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/>
Process: 16694 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited>
Main PID: 16699 (code=exited, status=0/SUCCESS)
Jul 05 22:43:00 minion1 systemd[1]: Starting The nginx HTTP and reverse >
Jul 05 22:43:01 minion1 nginx[16695]: nginx: the configuration file /etc>
Jul 05 22:43:01 minion1 nginx[16695]: nginx: configuration file /etc/ngi>
Jul 05 22:43:01 minion1 systemd[1]: nginx.service: Failed to parse PID f>
Jul 05 22:43:01 minion1 systemd[1]: Started The nginx HTTP and reverse p>
Jul 05 22:46:23 minion1 systemd[1]: Stopping The nginx HTTP and reverse >
Jul 05 22:46:23 minion1 systemd[1]: nginx.service: Succeeded.
Jul 05 22:46:23 minion1 systemd[1]: Stopped The nginx HTTP and reverse p>
[root@minion1 ~]#
#没有启动,因此高级状态并没有执行,只是测试,告诉我们将会做什么
以上是关于SaltStack之配置管理的主要内容,如果未能解决你的问题,请参考以下文章