码迷,mamicode.com
首页 > 其他好文 > 详细

SaltStack之配置管理

时间:2020-07-27 10:00:07      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:全局   ellipsize   部署   问题   min   bin   通用   alt   sim   

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
...此处省略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,不能改名

    2. 用SaltStack配置一个apache实例

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

[root@master ~]# cd /srv/salt/base/
[root@master base]# ls
[root@master base]# mkdir -p web/apache
[root@master base]# tree web
web
└── apache

1 directory, 0 files

[root@master base]# cd web/apache/
[root@master apache]# touch apache.sls      //生成一个状态描述文件
[root@master apache]# ll
总用量 0
-rw-r--r--. 1 root root 0 2月  19 19:25 apache.sls

[root@master apache]# vim apache.sls
apache-install:
  pkg.installed:
    - name: httpd

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

[root@master ~]# ls /srv/salt/base/web/apache/
apache.sls
//执行状态描述文件
[root@master ~]# salt ‘192.168.69.202‘ state.sls web.apache.apache saltenv=base
192.168.69.202:
----------
      ID: apache-install
Function: pkg.installed
    Name: httpd
  Result: True
 Comment: The following packages were installed/updated: httpd
 Started: 20:01:54.224738
Duration: 46294.423 ms
 Changes:
          ----------
          apr:
              ----------
              new:
                  1.4.8-3.el7_4.1
              old:
          apr-util:
              ----------
              new:
                  1.5.2-6.el7
              old:
          httpd:
              ----------
              new:
                  2.4.6-88.el7.centos
              old:
          httpd-tools:
              ----------
              new:
                  2.4.6-88.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: 20:02:42.295530
Duration: 6926.009 ms
 Changes:
          ----------
          httpd:
              True
Summary for 192.168.69.202
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time:  53.220 s

2.2 在Minion上检查

[root@minion ~]# 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 二 2019-02-19 20:02:48 CST; 6min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 84162 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─84162 /usr/sbin/httpd -DFOREGROUND
           ├─84168 /usr/sbin/httpd -DFOREGROUND
           ├─84169 /usr/sbin/httpd -DFOREGROUND
           ├─84170 /usr/sbin/httpd -DFOREGROUND
           ├─84171 /usr/sbin/httpd -DFOREGROUND
           └─84172 /usr/sbin/httpd -DFOREGROUND

2月 19 20:02:42 minion systemd[1]: Starting The Apache HTTP Server...
2月 19 20:02:43 minion httpd[84162]: AH00558: httpd: Could not reliably determine the server‘s fully qualifie...essage
2月 19 20:02:48 minion systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

由以上内容可知apache确实已部署成功。

执行状态文件的技巧:

  • 先用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@master ~]# cd /srv/salt/base/
[root@master base]# ls
web
[root@master base]# vim top.sls
base:   //要执行状态文件的环境
  ‘192.168.69.202‘:     //要执行状态文件的目标
    - web.apache.apache   //要执行的状态文件

//停止minion的httpd
[root@minion ~]# systemctl stop httpd

[root@master base]# cd
[root@master ~]# salt ‘*‘ state.highstate   //使用高级状态来执行
192.168.69.201:
----------
          ID: states
    Function: no.None
      Result: False
     Comment: No Top file or master_tops data matches found.    //在top file里没找到它要干啥
     Changes:

Summary for 192.168.69.201
------------
Succeeded: 0
Failed:    1
------------
Total states run:     1
Total run time:   0.000 ms
192.168.69.202:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 14:39:39.373195
    Duration: 1207.756 ms
     Changes:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd is already enabled, and is running
     Started: 14:39:40.582298
    Duration: 636.999 ms
     Changes:
              ----------
              httpd:
                  True

Summary for 192.168.69.202
------------
Succeeded: 2 (changed=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.845 s
ERROR: Minions returned with non-zero exit code

//查看minion的httpd状态
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since 三 2019-02-20 14:39:41 CST; 1min 10s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 11071 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
 Main PID: 11138 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11138 /usr/sbin/httpd -DFOREGROUND
           ├─11139 /usr/sbin/httpd -DFOREGROUND
           ├─11140 /usr/sbin/httpd -DFOREGROUND
           ├─11141 /usr/sbin/httpd -DFOREGROUND
           ├─11143 /usr/sbin/httpd -DFOREGROUND
           └─11144 /usr/sbin/httpd -DFOREGROUND

2月 20 14:39:40 minion systemd[1]: Starting The Apache HTTP Server...
2月 20 14:39:41 minion httpd[11138]: AH00558: httpd: Could not reliably determine the server‘s fully ...ssage
2月 20 14:39:41 minion 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上的httpd服务
[root@minion ~]# systemctl stop httpd
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-02-20 14:58:04 CST; 22s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 12401 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 11138 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 11138 (code=exited, status=0/SUCCESS)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"

2月 20 14:39:40 minion systemd[1]: Starting The Apache HTTP Server...
2月 20 14:39:41 minion httpd[11138]: AH00558: httpd: Could not reliably determine the server‘s fully ...ssage
2月 20 14:39:41 minion systemd[1]: Started The Apache HTTP Server.
2月 20 14:58:03 minion systemd[1]: Stopping The Apache HTTP Server...
2月 20 14:58:04 minion systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

//在master上执行高级状态的测试
[root@master ~]# salt ‘192.168.69.202‘ state.highstate test=True
192.168.69.202:
----------
          ID: apache-install
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: All specified packages are already installed
     Started: 14:58:50.450451
    Duration: 1055.942 ms
     Changes:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: None
     Comment: Service httpd is set to start     //将会启动httpd
     Started: 14:58:51.508031
    Duration: 57.505 ms
     Changes:

Summary for 192.168.69.202
------------
Succeeded: 2 (unchanged=1)
Failed:    0
------------
Total states run:     2
Total run time:   1.113 s

//在minion上查看httpd是否启动
[root@minion ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 三 2019-02-20 14:58:04 CST; 2min 27s ago
     Docs: man:httpd(8)
           man:apachectl(8)
  Process: 12401 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
  Process: 11138 ExecStart=/usr/sbin/httpd $OPTIONS -DFOREGROUND (code=exited, status=0/SUCCESS)
 Main PID: 11138 (code=exited, status=0/SUCCESS)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"

2月 20 14:39:40 minion systemd[1]: Starting The Apache HTTP Server...
2月 20 14:39:41 minion httpd[11138]: AH00558: httpd: Could not reliably determine the server‘s fully ...ssage
2月 20 14:39:41 minion systemd[1]: Started The Apache HTTP Server.
2月 20 14:58:03 minion systemd[1]: Stopping The Apache HTTP Server...
2月 20 14:58:04 minion systemd[1]: Stopped The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

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

SaltStack之配置管理

标签:全局   ellipsize   部署   问题   min   bin   通用   alt   sim   

原文地址:https://blog.51cto.com/14736606/2513473

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!