SaltStack配置管理

7 篇文章 0 订阅
7 篇文章 0 订阅

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 ]# tree /srv/
/srv/

0 directories, 0 files
[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]# touch web/nginx/nginx.sls        #生成一个状态描述文件
[root@master base]# cat web/nginx/nginx.sls          #编辑nginx状态描述文件
nginx-install:
  pkg.installed:
    - name: nginx

nginx-service:
  service.running:
    - name: nginx
    - enable: true

// YAML 配置文件中顶格写的被称作ID,必须全局唯一,不能重复出现
// SaltStack 读 YAML 配置文件时是从上往下读,所以要把先执行的写在前面

[root@master ~]# ls /srv/salt/base/web/nginx/
nginx.sls
[root@master ~]# salt '192.168.25.147' test.ping    #执行状态描述文件之前先测试是否能够ping通受控机
192.168.25.147:
    True

//执行状态描述文件
[root@master ~]# salt '192.168.25.147' state.sls web.nginx.nginx saltenv=base    #如果在基础环境中执行可以不加上saltenv=base
192.168.25.147:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 01:36:27.496463
    Duration: 33858.356 ms
     Changes:   
              ----------
              gd:
                  ----------
                  new:
                      2.2.5-7.el8
                  old:
              libXpm:
                  ----------
                  new:
                      3.5.12-8.el8
                  old:
              libwebp:
                  ----------
                  new:
                      1.0.0-5.el8
                  old:
              nginx:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-all-modules:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-filesystem:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-image-filter:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-perl:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-mail:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              nginx-mod-stream:
                  ----------
                  new:
                      1:1.14.1-9.module_el8.0.0+184+e34fea82
                  old:
              perl-Carp:
                  ----------
                  new:
                      1.42-396.el8
                  old:
              perl-Data-Dumper:
                  ----------
                  new:
                      2.167-399.el8
                  old:
              perl-Digest:
                  ----------
                  new:
                      1.17-395.el8
                  old:
              perl-Digest-MD5:
                  ----------
                  new:
                      2.55-396.el8
                  old:
              perl-Encode:
                  ----------
                  new:
                      4:2.97-3.el8
                  old:
              perl-Errno:
                  ----------
                  new:
                      1.28-420.el8
                  old:
              perl-Exporter:
                  ----------
                  new:
                      5.72-396.el8
                  old:
              perl-File-Path:
                  ----------
                  new:
                      2.15-2.el8
                  old:
              perl-File-Temp:
                  ----------
                  new:
                      0.230.600-1.el8
                  old:
              perl-Getopt-Long:
                  ----------
                  new:
                      1:2.50-4.el8
                  old:
              perl-HTTP-Tiny:
                  ----------
                  new:
                      0.074-1.el8
                  old:
              perl-IO:
                  ----------
                  new:
                      1.38-420.el8
                  old:
              perl-IO-Socket-IP:
                  ----------
                  new:
                      0.39-5.el8
                  old:
              perl-IO-Socket-SSL:
                  ----------
                  new:
                      2.066-4.module_el8.4.0+517+be1595ff
                  old:
              perl-MIME-Base64:
                  ----------
                  new:
                      3.15-396.el8
                  old:
              perl-Mozilla-CA:
                  ----------
                  new:
                      20160104-7.module_el8.3.0+416+dee7bcef
                  old:
              perl-Net-SSLeay:
                  ----------
                  new:
                      1.88-1.module_el8.4.0+517+be1595ff
                  old:
              perl-PathTools:
                  ----------
                  new:
                      3.74-1.el8
                  old:
              perl-Pod-Escapes:
                  ----------
                  new:
                      1:1.07-395.el8
                  old:
              perl-Pod-Perldoc:
                  ----------
                  new:
                      3.28-396.el8
                  old:
              perl-Pod-Simple:
                  ----------
                  new:
                      1:3.35-395.el8
                  old:
              perl-Pod-Usage:
                  ----------
                  new:
                      4:1.69-395.el8
                  old:
              perl-Scalar-List-Utils:
                  ----------
                  new:
                      3:1.49-2.el8
                  old:
              perl-Socket:
                  ----------
                  new:
                      4:2.027-3.el8
                  old:
              perl-Storable:
                  ----------
                  new:
                      1:3.11-3.el8
                  old:
              perl-Term-ANSIColor:
                  ----------
                  new:
                      4.06-396.el8
                  old:
              perl-Term-Cap:
                  ----------
                  new:
                      1.17-395.el8
                  old:
              perl-Text-ParseWords:
                  ----------
                  new:
                      3.30-395.el8
                  old:
              perl-Text-Tabs+Wrap:
                  ----------
                  new:
                      2013.0523-395.el8
                  old:
              perl-Time-Local:
                  ----------
                  new:
                      1:1.280-1.el8
                  old:
              perl-URI:
                  ----------
                  new:
                      1.73-3.el8
                  old:
              perl-Unicode-Normalize:
                  ----------
                  new:
                      1.25-396.el8
                  old:
              perl-constant:
                  ----------
                  new:
                      1.33-396.el8
                  old:
              perl-interpreter:
                  ----------
                  new:
                      4:5.26.3-420.el8
                  old:
              perl-libnet:
                  ----------
                  new:
                      3.11-3.el8
                  old:
              perl-libs:
                  ----------
                  new:
                      4:5.26.3-420.el8
                  old:
              perl-macros:
                  ----------
                  new:
                      4:5.26.3-420.el8
                  old:
              perl-parent:
                  ----------
                  new:
                      1:0.237-1.el8
                  old:
              perl-podlators:
                  ----------
                  new:
                      4.11-1.el8
                  old:
              perl-threads:
                  ----------
                  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: 01:37:01.369156
    Duration: 314.737 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for 192.168.25.147
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  34.173 s

2.2 在Minion上检查

[root@minion ~]# rpm -qa | grep nginx
nginx-mod-stream-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-filesystem-1.14.1-9.module_el8.0.0+184+e34fea82.noarch
nginx-mod-http-perl-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-mod-mail-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-all-modules-1.14.1-9.module_el8.0.0+184+e34fea82.noarch
nginx-mod-http-xslt-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64
nginx-mod-http-image-filter-1.14.1-9.module_el8.0.0+184+e34fea82.x86_64

[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor pr>
   Active: active (running) since Tue 2021-11-02 01:37:01 EDT; 6min ago
 Main PID: 4500 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 12.5M
   CGroup: /system.slice/nginx.service
           ├─4500 nginx: master process /usr/sbin/nginx
           ├─4501 nginx: worker process
           ├─4502 nginx: worker process
           ├─4503 nginx: worker process
           └─4504 nginx: worker process

11月 02 01:37:01 minion systemd[1]: Starting The nginx HTTP and reverse prox>
11月 02 01:37:01 minion nginx[4497]: nginx: the configuration file /etc/ngin>
11月 02 01:37:01 minion nginx[4497]: nginx: configuration file /etc/nginx/ng>
lines 1-16/17 91%

[root@minion ~]# 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               [::]:*     

在这里插入图片描述
由以上内容可知Nginx确实已部署成功

3 top file

3.2 top file介绍

直接通过命令执行sls文件时够自动化吗?答案是否定的,因为我们还要告诉某台主机要执行某个任务,自动化应该是我们让它干活时,它自己就知道哪台主机要干什么活,但是直接通过命令执行sls文件并不能达到这个目的,为了解决这个问题,top file 应运而生。

top file就是一个入口,top file的文件名可通过在 Master的配置文件中搜索top.sls找出,且此文件必须在 base 环境中,默认情况下此文件必须叫top.sls。

top file的作用就是告诉对应的主机要干什么活,比如让web服务器启动web服务,让数据库服务器安装mysql等等。

[root@master ~]# vim /etc/salt/master
# The state system uses a "top" file to tell the minions what environment to
# use and what modules to use. The state_top file is defined relative to the
# root of the base environment as defined in "File Server settings" below.
#state_top: top.sls        #保持默认即可,不用取消注释

top file 实例
在192.168.25.148上部署nginx,在192.168.25.161上部署apache

[root@master base]# pwd
/srv/salt/base
[root@master base]# cat web/apache/install.sls 
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: true

[root@master base]# tree
.
├── top.sls
└── web
    ├── apache
    │   └── install.sls
    └── nginx
        └── install.sls

3 directories, 3 files

[root@master base]# cat web/apache/install.sls 
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: true



[root@master base]# pwd
/srv/salt/base
[root@master base]# cat top.sls 
base:    //要执行状态文件的环境
  '192.168.25.148':    //要执行状态文件的目标
    - web.nginx.install    //要执行的状态文件
  '192.168.25.161':
    - web.apache.install
 
[root@master ~]# salt '*' state.highstate    //使用高级状态来执行
192.168.25.161:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 15:02:54.819649
    Duration: 606.62 ms
     Changes:   
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: The service httpd is already running
     Started: 15:02:55.428882
    Duration: 40.161 ms
     Changes:   

Summary for 192.168.25.161
------------
Succeeded: 2
Failed:    0
------------
Total states run:     2
Total run time: 646.781 ms
192.168.25.148:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 03:02:54.563295
    Duration: 746.56 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 03:02:55.311984
    Duration: 290.321 ms
     Changes:   
              ----------
              nginx:
                  True

Summary for 192.168.25.148
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.037 s


//查看minion的部署服务的状态
[root@localhost ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: active (running) since Tue 2021-11-02 03:02:55 EDT; 1min 43s ago
 Main PID: 8140 (nginx)
    Tasks: 5 (limit: 49290)
   Memory: 8.9M
   CGroup: /system.slice/nginx.service
           ├─8140 nginx: master process /usr/sbin/nginx
           ├─8141 nginx: worker process
           ├─8142 nginx: worker process
           ├─8143 nginx: worker process
           └─8144 nginx: worker process

11月 02 03:02:55 localhost.localdomain systemd[1]: Starting The nginx HTTP and >
11月 02 03:02:55 localhost.localdomain nginx[8137]: nginx: the configuration fi>
11月 02 03:02:55 localhost.localdomain nginx[8137]: nginx: configuration file />
lines 1-16/17 90%

[root@minion2 ~]# 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 二 2021-11-02 14:54:30 CST; 10min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 1854 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─1854 /usr/sbin/httpd -DFOREGROUND
           ├─1867 /usr/sbin/httpd -DFOREGROUND
           ├─1868 /usr/sbin/httpd -DFOREGROUND
           ├─1870 /usr/sbin/httpd -DFOREGROUND
           ├─1871 /usr/sbin/httpd -DFOREGROUND
           └─1873 /usr/sbin/httpd -DFOREGROUND

11月 02 14:54:01 minion2 systemd[1]: Starting The Apache HTTP Server...
11月 02 14:54:20 minion2 httpd[1854]: AH00558: httpd: Could not reliably d...ge
11月 02 14:54:30 minion2 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@master ~]# salt '*' state.highstate   //生产环境禁止这样使用salt命令

注意:
上面让所有人执行高级状态,但实际工作当中,一般不会这么用,工作当中一般都是通知某台或某些台目标主机来执行高级状态,具体是否执行则是由top file来决定的。

若在执行高级状态时加上参数test=True,则它会告诉我们它将会做什么,但是它不会真的去执行这个操作

//停掉minon上的nginx服务
[root@minion ~]# systemctl stop nginx
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: inactive (dead) since Tue 2021-11-02 03:12:14 EDT; 3s ago
 Main PID: 4500 (code=exited, status=0/SUCCESS)

11月 02 01:37:01 minion systemd[1]: Starting The nginx HTTP and reverse proxy s>
11月 02 01:37:01 minion nginx[4497]: nginx: the configuration file /etc/nginx/n>
11月 02 01:37:01 minion nginx[4497]: nginx: configuration file /etc/nginx/nginx>
11月 02 01:37:01 minion systemd[1]: Started The nginx HTTP and reverse proxy se>
11月 02 03:12:14 minion systemd[1]: Stopping The nginx HTTP and reverse proxy s>
11月 02 03:12:14 minion systemd[1]: nginx.service: Succeeded.
11月 02 03:12:14 minion systemd[1]: Stopped The nginx HTTP and reverse proxy se>
lines 1-12/12 (END)

//在master上执行高级状态的测试
[root@master ~]# salt '192.168.25.148' state.highstate test=True
192.168.25.148:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: All specified packages are already installed
     Started: 03:17:50.821450
    Duration: 737.282 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: None
     Comment: Service nginx is set to start     //将会启动nginx
     Started: 03:17:51.560697
    Duration: 45.52 ms
     Changes:   

Summary for 192.168.25.148
------------
Succeeded: 2 (unchanged=1)
Failed:    0
------------
Total states run:     2
Total run time: 782.802 ms

//在minion上查看nginx是否启动
[root@minion ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor prese>
   Active: inactive (dead) since Tue 2021-11-02 03:17:37 EDT; 1min 24s ago
 Main PID: 8140 (code=exited, status=0/SUCCESS)

11月 02 03:02:55 localhost.localdomain systemd[1]: Starting The nginx HTTP and >
11月 02 03:02:55 localhost.localdomain nginx[8137]: nginx: the configuration fi>
11月 02 03:02:55 localhost.localdomain nginx[8137]: nginx: configuration file />
11月 02 03:02:55 localhost.localdomain systemd[1]: Started The nginx HTTP and r>
11月 02 03:17:37 localhost.localdomain systemd[1]: Stopping The nginx HTTP and >
11月 02 03:17:37 localhost.localdomain systemd[1]: nginx.service: Succeeded.
11月 02 03:17:37 localhost.localdomain systemd[1]: Stopped The nginx HTTP and r>
lines 1-12/12 (END)

由此可见高级状态并没有执行,因为nginx并没有启动

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值