自动化运维工具SaltStack配置管理(2)

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让我们放的位置,可以在SaltStackMaster配置文件中查找file_roots即可看到。

[root@node01 ~]# vim /etc/salt/master
.........
file_roots:
  base:
    - /srv/salt/base
  prod:
    - /srv/salt/prod
  test:
    - /srv/salt/test
.........

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

3 directories, 0 files
[root@node01 ~]# systemctl restart salt-master

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

2. 用SaltStack配置一个apache实例


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

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

1 directory, 0 files
[root@node01 base]# cd web/apache/
[root@node01 apache]# vim apache.sls
[root@node01 apache]# cat apache.sls
apache-install:
  pkg.installed:
    - name: httpd

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

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

//在node02上检查是否安装httpd并查看端口号
 [root@node02 ~]# rpm -qa |grep httpd
[root@node02 ~]# ss -anlt
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                         :::*

//执行状态描述文件
[root@node01 ~]# ls /srv/salt/base/web/apache/
apache.sls
[root@node01 ~]# salt 'node02' state.sls web.apache.apache

2.2 在node02上验证

[root@node02 ~]# ss -anlt
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            :::80                         :::*                  
LISTEN     0      128            :::22                         :::*                  
LISTEN     0      100           ::1:25                         :::*                  
[root@node02 ~]# 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 二 2020-08-04 20:17:34 CST; 4min 27s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 19783 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─19783 /usr/sbin/httpd -DFOREGROUND
           ├─19791 /usr/sbin/httpd -DFOREGROUND
           ├─19792 /usr/sbin/httpd -DFOREGROUND
           ├─19793 /usr/sbin/httpd -DFOREGROUND
           ├─19794 /usr/sbin/httpd -DFOREGROUND
           └─19795 /usr/sbin/httpd -DFOREGROUND

8月 04 20:17:09 node02 systemd[1]: Starting The Apache HTTP Server...
8月 04 20:17:24 node02 httpd[19783]: AH00557: httpd: apr_sockaddr_info_get() fai...02
8月 04 20:17:24 node02 httpd[19783]: AH00558: httpd: Could not reliably determin...ge
8月 04 20:17:34 node02 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.

由以上内容可知apache确实已部署成功。
执行状态文件的技巧:

先用test.ping测试需要执行状态文件的主机是否能正常通信,然后再执行状态文件

3 拓展练习:用SaltStack配置一个nginx实例

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

//创建nginx.sls
[root@node01 ~]# cd /srv/salt/base/
[root@node01 base]# ls
web
[root@node01 base]# cd web/
[root@node01 web]# cat nginx/nginx.sls 
apache-install:
  pkg.installed:
    - name: nginx

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

[root@node01 ~]# ls /srv/salt/base/web/nginx/
nginx.sls

//执行状态描述文件
[root@node01 ~]# salt 'node02' state.sls web.nginx.nginx 
node02:
----------
          ID: apache-install
    Function: pkg.installed
        Name: nginx
      Result: True
     Comment: The following packages were installed/updated: nginx
     Started: 21:24:06.425268
    Duration: 19236.308 ms
     Changes:   
              ----------
              centos-indexhtml:
                  ----------
                  new:
                      7-9.el7.centos
                  old:
              gperftools-libs:
                  ----------
                  new:
                      2.6.1-1.el7
                  old:
              libxslt:
                  ----------
                  new:
                      1.1.28-5.el7
                  old:
              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: apache-service
    Function: service.running
        Name: nginx
      Result: True
     Comment: Service nginx has been enabled, and is running
     Started: 21:24:25.728542
    Duration: 1197.794 ms
     Changes:   
              ----------
              nginx:
                  True

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

3.2 在node02上验证

[root@node02 ~]# ss -anlt
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                         :::*                  
[root@node02 ~]# ss -anlt
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                         :::*                  
[root@node02 ~]# 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 二 2020-08-04 21:24:26 CST; 7min ago
 Main PID: 32450 (nginx)
   CGroup: /system.slice/nginx.service
           ├─32450 nginx: master process /usr/sbin/nginx
           └─32451 nginx: worker process

8月 04 21:24:26 node02 systemd[1]: Starting The nginx HTTP and reverse proxy ser.....
8月 04 21:24:26 node02 nginx[32444]: nginx: the configuration file /etc/nginx/ng...ok
8月 04 21:24:26 node02 nginx[32444]: nginx: configuration file /etc/nginx/nginx....ul
8月 04 21:24:26 node02 systemd[1]: Failed to parse PID from file /run/nginx.pid:...nt
8月 04 21:24:26 node02 systemd[1]: Started 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、付费专栏及课程。

余额充值