SaltStack配置管理

本文介绍了如何使用YAML语言在SaltStack中进行配置管理,包括基础规则、配置文件位置、部署nginx实例和利用topfile实现自动化任务调度。重点讲解了如何通过topfile指定目标主机执行状态文件和高级状态highstate的使用方式。

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
……
file_roots:
  base:
    - /srv/salt/base
  test:
    - /srv/salt/text
  dev:
    - /srv/salt/dev
  prod:
    - /srv/salt/prod
……
[root@master ~]# mkdir -p /srv/salt/{base,test,dev,prod}
[root@master ~]# tree /srv/salt/
/srv/salt/
├── base
├── dev
├── prod
└── test
[root@master ~]# systemctl restart salt-master //由于改的是master下的文件所有需要重新启动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]# vim install.sls 
[root@master nginx]# cat install.sls 
nginx-install:
  pkg.installed:
    - name: nginx

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

[root@master nginx]# tree /srv/salt/
/srv/salt/
├── base
│   └── web
│       └── nginx
│           └── install.sls
├── dev
├── prod
└── test
[root@master php]# salt '*' state.sls web.nginx.install
minion1:
----------
          ID: nginx-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 02:58:44.402825
    Duration: 140150.359 ms
……
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
Total run time: 140.431 s

查看

[root@minion1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; ven>
   Active: active (running) since Mon 2021-07-19 03:01:04 EDT; 8min ago
  Process: 69251 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCE>
  Process: 69249 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0>
  Process: 69247 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exite>
 Main PID: 69252 (nginx)
    Tasks: 5 (limit: 4743)
   Memory: 17.9M
   CGroup: /system.slice/nginx.service
           ├─69252 nginx: master process /usr/sbin/nginx
           ├─69253 nginx: worker process
           ├─69254 nginx: worker process
           ├─69255 nginx: worker process
           └─69256 nginx: worker process

在这里插入图片描述

top file

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

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

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

    [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 '*' state.highstate
    [root@master base]# salt '*' state.highstate
    master:
    ----------
              ID: states
        Function: no.None
          Result: False
         Comment: No Top file or master_tops data matches found. Please see master log for details. #这里是正常的,因为top file里没有指定他要做什么事
         Changes:   

    Summary for master
    ------------
    Succeeded: 0
    Failed:    1
    ------------
    Total states run:     1
    Total run time:   0.000 ms
    minion1:
    ----------
              ID: nginx-install
        Function: pkg.installed
            Name: nginx
          Result: True
         Comment: All specified packages are already installed
         Started: 02:56:06.242460
        Duration: 742.896 ms
         Changes:   
    ----------
              ID: nginx-service
        Function: service.running
            Name: nginx
          Result: True
         Comment: Service nginx is already enabled, and is running
         Started: 02:56:06.986551
        Duration: 167.029 ms
         Changes:   
                  ----------
                  nginx:
                      True

    Summary for minion1
    ------------
    Succeeded: 2 (changed=1)
    Failed:    0
    ------------
    Total states run:     2
    Total run time: 909.925 ms
    ERROR: Minions returned with non-zero exit code
    #查看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; vendor preset: disabled)
       Active: active (running) since Sun 2021-07-04 02:56:07 EDT; 18s ago
      Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 27548 (nginx)
        Tasks: 2 (limit: 4741)
       Memory: 4.1M
       CGroup: /system.slice/nginx.service
               ├─27548 nginx: master process /usr/sbin/nginx
               └─27549 nginx: worker process

    Jul 04 02:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Jul 04 02:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
    Jul 04 02:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.

注意:
若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; vendor preset: disabled)
       Active: inactive (dead) since Sun 2021-07-04 02:58:28 EDT; 13s ago
      Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 27548 (code=exited, status=0/SUCCESS)

    Jul 04 02:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Jul 04 02:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
    Jul 04 02:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
    Jul 04 02:58:28 minion1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
    Jul 04 02:58:28 minion1 systemd[1]: nginx.service: Succeeded.
    Jul 04 02:58:28 minion1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
    #在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: 03:00:09.129614
        Duration: 745.065 ms
         Changes:   
    ----------
              ID: nginx-service
        Function: service.running
            Name: nginx
          Result: None
         Comment: Service nginx is set to start
         Started: 03:00:09.875923
        Duration: 40.279 ms
         Changes:   

    Summary for minion1
    ------------
    Succeeded: 2 (unchanged=1)
    Failed:    0
    ------------
    Total states run:     2
    Total run time: 785.344 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; vendor preset: disabled)
       Active: inactive (dead) since Sun 2021-07-04 02:58:28 EDT; 2min 16s ago
      Process: 27546 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
      Process: 27544 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
      Process: 27543 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
     Main PID: 27548 (code=exited, status=0/SUCCESS)

    Jul 04 02:56:07 minion1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Jul 04 02:56:07 minion1 nginx[27544]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Jul 04 02:56:07 minion1 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
    Jul 04 02:56:07 minion1 systemd[1]: Started The nginx HTTP and reverse proxy server.
    Jul 04 02:58:28 minion1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
    Jul 04 02:58:28 minion1 systemd[1]: nginx.service: Succeeded.
    Jul 04 02:58:28 minion1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
    #没有启动,因此高级状态并没有执行,只是测试,告诉我们将会做什么
先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值