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@wyt1 ~]# vim /etc/salt/master
//添加下列内容
file_roots:
  base:
    - /srv/salt/base
  prod:
    - /srv/salt/prod
  test:
    - /srv/salt/test

[root@wyt1 ~]# mkdir -p /srv/salt/{base,prod,test}
[root@wyt1 ~]# tree /srv/salt
/srv/salt
├── base
├── prod
└── test

3 directories, 0 files

[root@wyt1 ~]# systemctl restart salt-master

需要注意:
base是默认的位置,如果file_roots只有一个,则base是必备的且必须叫base,不能改名

2. 用SaltStack配置一个apache实例

在Master上部署sls配置文件并执行

[root@wyt1 ~]# cd /srv/salt/base/
[root@wyt1 base]# mkdir -p web/apache  //创建apache状态描述文件
[root@wyt1 base]# ls
web
[root@wyt1 base]# tree web
web
└── apache

1 directory, 0 files
[root@wyt1 base]# cd web/apache/
[root@wyt1 apache]# ls
[root@wyt1 apache]# vim apache.sls //编辑apache状态描述文件
apache-install:
  pkg.installed:
    - name: httpd

apache-service:
  service.running:
    - name: httpd
    - enable: True
    
[root@wyt1 ~]# salt 'wyt2' state.sls web.apache.apache   //执行状态描述文件
wyt2:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: The following packages were installed/updated: httpd
     Started: 07:18:08.244839
    Duration: 14065.947 ms
     Changes:   
              ----------
              apr:
                  ----------
                  new:
                      1.4.8-5.el7
                  old:
              apr-util:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-93.el7.centos
                  old:
              mailcap:
                  ----------
                  new:
                      2.1.41-2.el7
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 07:18:22.328481
    Duration: 905.463 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2

在Minion上检查

[root@wyt2 ~]# systemctl status httpd //查看httpd是否启动
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2020-08-16 07:18:22 EDT; 14min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11562 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11562 /usr/sbin/httpd -DFOREGROUND
           ├─11571 /usr/sbin/httpd -DFOREGROUND
           ├─11572 /usr/sbin/httpd -DFOREGROUND
           ├─11573 /usr/sbin/httpd -DFOREGROUND
           ├─11574 /usr/sbin/httpd -DFOREGROUND
           └─11575 /usr/sbin/httpd -DFOREGROUND

Aug 16 07:18:22 wyt2 systemd[1]: Starting The Apache HTTP Server...
Aug 16 07:18:22 wyt2 httpd[11562]: AH00558: httpd: Could not reliably determin...ge
Aug 16 07:18:22 wyt2 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@wyt2 ~]# ss -antl //查看端口号
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*   

//由以上内容可知apache确实已部署成功。
执行状态文件的技巧:
先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件

3.用SaltStack配置一个nginx实例

在Master上部署sls配置文件并执行

[root@wyt1 ~]# cd /srv/salt/base/
[root@wyt1 base]#  mkdir -p web/nginx  //创建nginx状态描述文件
[root@wyt1 base]# ls
web
[root@wyt1 base]# tree web
web
└── nginx

1 directory, 0 files

[root@wyt1 base]# cd web/nginx/
[root@wyt1 nginx]# vim nginx.sls    //编辑nginx状态描述文件
nginx-install:
  pkg.installed:
    - name: nginx

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

[root@wyt1 ~]# salt 'wyt2' state.sls web.nginx.nginx  //执行nginx状态描述文件
wyt2:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 09:31:29.640793
    Duration: 11690.435 ms
     Changes:   
              ----------
              nginx:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-all-modules:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-filesystem:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-image-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-perl:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-mail:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-stream:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 09:31:41.346011
    Duration: 912.159 ms
     Changes:   
              ----------
              nginx:
                  True

Summary
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2

在Minion上检查

[root@wyt2 ~]# systemctl status nginx //查看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 Sun 2020-08-16 08:33:42 EDT; 26s ago
  Process: 11911 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 11908 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 11907 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 11914 (nginx)
   CGroup: /system.slice/nginx.service
           ├─11914 nginx: master process /usr/sbin/nginx
           └─11915 nginx: worker process

Aug 16 08:33:42 wyt2 systemd[1]: Starting The nginx HTTP and reverse proxy ser.....
Aug 16 08:33:42 wyt2 nginx[11908]: nginx: the configuration file /etc/nginx/ng...ok
Aug 16 08:33:42 wyt2 nginx[11908]: nginx: configuration file /etc/nginx/nginx....ul
Aug 16 08:33:42 wyt2 systemd[1]: Failed to parse PID from file /run/nginx.pid:...nt
Aug 16 08:33:42 wyt2 systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@wyt2 ~]# ss -antl   //查看nginx端口
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128          :::80                       :::*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*   

在Master上部署sls配置文件并执行

[root@wyt1 ~]# cd /srv/salt/prod/
[root@wyt1 prod]# mkdir -p web/nginx
[root@wyt1 prod]# cd web/nginx/
[root@wyt1 nginx]# cat uninstall.sls  //卸载nginx
nginx-uninstall:
  pkg.removed:
    - name: nginx      
[root@wyt1 nginx]# cd
[root@wyt1 ~]# salt 'wyt2' state.sls web.nginx.uninstall saltenv=prod //执行状态文件,默认是执行base,所以后面加上saltenv=prod
wyt2:
----------
          ID: nginx-uninstall
    Function: pkg.removed
        Name: nginx
      Result: True
     Comment: All targeted packages were removed.
     Started: 10:57:11.013631
    Duration: 3058.655 ms
     Changes:   
              ----------
              nginx:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-all-modules:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-mod-http-image-filter:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-mod-http-perl:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-mod-mail:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7
              nginx-mod-stream:
                  ----------
                  new:
                  old:
                      1:1.16.1-1.el7

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1

//在Minion上检查
[root@wyt2 ~]# systemctl status nginx
Unit nginx.service could not be found.
[root@wyt2 ~]# rpm -qa|grep nginx
[root@wyt2 ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*      

4. 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@wyt1 ~]# cd /srv/salt/base
[root@wyt1 base]# ls
web
[root@wyt1 base]# vim top.sls
[root@wyt1 base]# cat top.sls
base:      //要执行状态文件的环境
  'wyt2':   //要执行状态文件的目标
    - web.nginx.nginx  //要执行的状态文件

[root@wyt1 base]# salt 'wyt2' state.highstate   //使用高级状态来执行
wyt2:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 22:52:22.494403
    Duration: 14245.738 ms
     Changes:   
              ----------
              nginx:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-all-modules:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-image-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-perl:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-http-xslt-filter:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-mail:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
              nginx-mod-stream:
                  ----------
                  new:
                      1:1.16.1-1.el7
                  old:
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 22:52:36.755339
    Duration: 1076.587 ms
     Changes:   
              ----------
              nginx:
                  True

Summary
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2

查看minion的nginx状态

[root@wyt2 ~]# 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 Mon 2020-08-17 22:52:37 EDT; 7min ago
 Main PID: 17806 (nginx)
   CGroup: /system.slice/nginx.service
           ├─17806 nginx: master process /usr/sbin/nginx
           └─17807 nginx: worker process

Aug 17 22:52:37 wyt2 systemd[1]: Starting The nginx HTTP and reverse proxy ser.....
Aug 17 22:52:37 wyt2 nginx[17800]: nginx: the configuration file /etc/nginx/ng...ok
Aug 17 22:52:37 wyt2 nginx[17800]: nginx: configuration file /etc/nginx/nginx....ul
Aug 17 22:52:37 wyt2 systemd[1]: Failed to parse PID from file /run/nginx.pid:...nt
Aug 17 22:52:37 wyt2 systemd[1]: Started The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.
[root@wyt2 ~]# ss -antl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128           *:80                        *:*                  
LISTEN      0      128           *:22                        *:*                  
LISTEN      0      100    127.0.0.1:25                        *:*                  
LISTEN      0      128          :::80                       :::*                  
LISTEN      0      128          :::22                       :::*                  
LISTEN      0      100         ::1:25                       :::*   

注意:
若top file里面的目标是用 * 表示的,要注意的是,top file里面的 * 表示的是所有要执行状态的目标,而 salt ‘*’ state.highstate 里面的 * 表示通知所有机器干活,而是否要干活则是由top file来指定的

高级状态highstate的使用

管理SaltStack时一般最常用的管理操作就是执行高级状态

[root@master ~]# salt '*' state.highstate   //生产环境禁止这样使用salt命令

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

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

//停掉minon上的httpd服务
[root@wyt1 ~]# salt 'wyt2' state.highstate test=True
wyt2:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: Package nginx is already installed.
     Started: 23:06:46.658381
    Duration: 752.664 ms
     Changes:   
----------
          ID: nginx-service
    Function: service.running
        Name: nginx
      Result: None
     Comment: Service nginx is set to start   /将会启动nginx
     Started: 23:06:47.411678
    Duration: 162.636 ms
     Changes:   

Summary
------------
Succeeded: 2 (unchanged=1)
Failed:    0
------------
Total states run:     2

//在minion上查看nginx是否启动
[root@wyt2 ~]# 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: inactive (dead) since Mon 2020-08-17 23:05:05 EDT; 2min 29s ago
 Main PID: 17806 (code=exited, status=0/SUCCESS)

Aug 17 22:52:37 wyt2 systemd[1]: Starting The nginx HTTP and reverse proxy ser.....
Aug 17 22:52:37 wyt2 nginx[17800]: nginx: the configuration file /etc/nginx/ng...ok
Aug 17 22:52:37 wyt2 nginx[17800]: nginx: configuration file /etc/nginx/nginx....ul
Aug 17 22:52:37 wyt2 systemd[1]: Failed to parse PID from file /run/nginx.pid:...nt
Aug 17 22:52:37 wyt2 systemd[1]: Started The nginx HTTP and reverse proxy server.
Aug 17 23:05:05 wyt2 systemd[1]: Stopping The nginx HTTP and reverse proxy ser.....
Aug 17 23:05:05 wyt2 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Hint: Some lines were ellipsized, use -l to show in full.

//由此可见高级状态并没有执行,因为nginx并没有启动
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值