saltstack

9. 配置管理安装Apache

下面进行的演示是远程通过 yum 方式安装 Apache。步骤如下:

(1)配置

[[email protected] ~]# vim /etc/salt/master

找到如下内容

# file_roots:
#   base:
#     - /srv/salt/

去掉#注释

file_roots:
  base:
    - /srv/salt

[[email protected] ~]# mkdir /srv/salt
[[email protected] ~]# vim /srv/salt/top.sls
[[email protected] ~]# cat /srv/salt/top.sls
base:
  'nb1':
    - apache
[[email protected] ~]#

[[email protected] ~]# vim /srv/salt/apache.sls
[[email protected] ~]# cat /srv/salt/apache.sls
apache-service:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel
  service.running:
    - name: httpd
    - enable: True
[[email protected] ~]#

注意:apache-service 是自定义的 id 名。pkg.installed
为包安装函数,下面是要安装的包的名字。service.running
也是一个函数,来保证指定的服务启动,enable 表示开机启动。

(2)重启服务

[[email protected] ~]# salt-master start 
^C[WARNING ] Stopping the Salt Master
[WARNING ] Stopping the Salt Master
[WARNING ] Stopping the Salt Master

Exiting on Ctrl-c

Exiting on Ctrl-c

Exiting on Ctrl-c
You have mail in /var/spool/mail/root
[[email protected] ~]# salt-master start

[[email protected] ~]# salt 'nb1' state.highstate
nb1:
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd
      Result: True
     Comment: Package httpd is already installed.
     Started: 03:38:36.137884
    Duration: 1250.258 ms
     Changes:   
----------
          ID: apache-service
    Function: pkg.installed
        Name: httpd-devel
      Result: True
     Comment: The following packages were installed/updated: httpd-devel
     Started: 03:38:37.388313
    Duration: 33668.276 ms
     Changes:   
              ----------
              apr-devel:
                  ----------
                  new:
                      1.4.8-3.el7
                  old:
              apr-util-devel:
                  ----------
                  new:
                      1.5.2-6.el7
                  old:
              cyrus-sasl:
                  ----------
                  new:
                      2.1.26-20.el7_2
                  old:
              cyrus-sasl-devel:
                  ----------
                  new:
                      2.1.26-20.el7_2
                  old:
              httpd:
                  ----------
                  new:
                      2.4.6-45.el7.centos.4
                  old:
                      2.4.6-45.el7.centos
              httpd-devel:
                  ----------
                  new:
                      2.4.6-45.el7.centos.4
                  old:
              httpd-tools:
                  ----------
                  new:
                      2.4.6-45.el7.centos.4
                  old:
                      2.4.6-45.el7.centos
              openldap-devel:
                  ----------
                  new:
                      2.4.40-13.el7
                  old:
----------
          ID: apache-service
    Function: service.running
        Name: httpd
      Result: True
     Comment: Service httpd has been enabled, and is running
     Started: 03:39:11.080192
    Duration: 6685.669 ms
     Changes:   
              ----------
              httpd:
                  True

Summary
------------
Succeeded: 3 (changed=2)
Failed:    0
------------
Total states run:     3
[[email protected] ~]# 

说明 Apache 远程安装已成功。

[[email protected] ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2017-08-18 03:39:17 EDT; 2min 10s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11613 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
   CGroup: /system.slice/httpd.service
           ├─11613 /usr/sbin/httpd -DFOREGROUND
           ├─11715 /usr/sbin/httpd -DFOREGROUND
           ├─11716 /usr/sbin/httpd -DFOREGROUND
           ├─11717 /usr/sbin/httpd -DFOREGROUND
           ├─11718 /usr/sbin/httpd -DFOREGROUND
           └─11719 /usr/sbin/httpd -DFOREGROUND

Aug 18 03:39:16 nb1 systemd[1]: Starting The Apache HTTP Server...
Aug 18 03:39:16 nb1 httpd[11613]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 192.168.1.161. Set the 'ServerN...his message
Aug 18 03:39:17 nb1 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
[[email protected] ~]# 

配置

master:
https://docs.saltstack.com/en/latest/ref/configuration/master.html
http://arlen.blog.51cto.com/7175583/1423997

minion:
https://docs.saltstack.com/en/latest/ref/configuration/minion.html
http://arlen.blog.51cto.com/7175583/1424008

第三行,声明使用哪些states目录,salt会寻找每个目录下的init.sls文件。

11.1文件管理

(1)服务端配置

[[email protected] ~]# vim /srv/salt/top.sls
[[email protected] ~]# cat /srv/salt/top.sls
base:
  'nb1':
    - apache
  'nb2':
    - filetest
[[email protected] ~]# 

新建 filetest.sls 文件

[[email protected] ~]# vim /srv/salt/filetest.sls
[[email protected] ~]# cat /srv/salt/filetest.sls
file-test:
  file.managed:
    - name: /tmp/filetest.txt
    - source: salt://test/123/1.txt
    - user: root
    - group: root
    - mode: 644
[[email protected] ~]# 

注意:第一行的 file-test
为自定的名字,表示该配置段的名字,可以在别的配置段中引用它;source指定文件从哪里拷贝,这里的
test 目录相当于是 /srv/salt/test 目录;name指定远程客户端要生成的文件。

新建所要测试的源文件

[[email protected] ~]# mkdir -p /srv/salt/test/123/
[[email protected] ~]# echo "file test" > /srv/salt/test/123/1.txt
[[email protected] ~]#

执行命令:

[[email protected] ~]# salt 'nb2' state.highstate
nb2:
----------
          ID: file-test
    Function: file.managed
        Name: /tmp/filetest.txt
      Result: True
     Comment: File /tmp/filetest.txt updated
     Started: 03:59:13.664379
    Duration: 505.159 ms
     Changes:   
              ----------
              diff:
                  New file
              mode:
                  0644

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[[email protected] ~]# 

(2)客户端验证

[[email protected] ~]# cat /tmp/filetest.txt 
file test
[[email protected] ~]# 

使用pillar

Target:

#salt使用 -l 选项来使用pillar
➜ pillar git:(master) ✗ sudo salt -l ”roles:webserver” test.ping

sls文件中使用:

#如根据系统环境安装git
➜ salt git:(master) ✗ cat git_env.sls
git:
  pkg.installed:
    - name: {{pillar[‘git’]}}

#或者:
git:
  pkg.installed:
    - name: {{ salt[‘pillar.get’](‘git’, ‘git’) }}

SaltStack安装配置

15.问题

 [[email protected] ~]# salt-master start
[ERROR   ] An extra return was detected from minion nb1, please verify the minion, this could be a replay attack
[ERROR   ] An extra return was detected from minion nb1, please verify the minion, this could be a replay attack

执行一次命令,返回两个值

 [[email protected] ~]# salt '*' cmd.run   'df -h'
nb1:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G  4.3G   44G   9% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   24K  3.9G   1% /dev/shm
    tmpfs                3.9G  385M  3.5G  10% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
nb1:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G  4.3G   44G   9% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   24K  3.9G   1% /dev/shm
    tmpfs                3.9G  385M  3.5G  10% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
nb0:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G   27G   22G  55% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   16K  3.9G   1% /dev/shm
    tmpfs                3.9G  394M  3.5G  11% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
    /dev/loop0           7.8G  7.8G     0 100% /var/ftp/iso-home
[[email protected] ~]#

问题产生的原因在node2节点上重复启动

 [[email protected] ~]# salt-minion start
^C[WARNING ] Stopping the Salt Minion
[WARNING ] Exiting on Ctrl-c
[[email protected] ~]# 

ctrl+c终止第二次的salt-minion启动即可。

启动

sudo service salt-master start
sudo service salt-minion start

安装被管理端

13.2 删除 cron

(1)修改 crontest.sls 文件 把 cron.present: 改成 cron.absent:
注意:两者不能共存,要想删除一个 cron,那之前的 present
就得替换掉或者删除掉。

[[email protected] ~]# vim /srv/salt/crontest.sls
[[email protected] ~]# cat /srv/salt/crontest.sls
cron-test:
  cron.absent:
    - name: /bin/touch /tmp/111.txt
    - user: root
    - minute: '*'
    - hour: 20
    - daymonth: 1-10
    - month: '3,5'
    - dayweek: '*'
[ro
[email protected] ~]#

(2)执行命令

[[email protected] ~]# salt 'nb1' state.highstate
nb1:
----------
          ID: cron-test
    Function: cron.absent
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt removed from root's crontab
     Started: 02:56:03.583557
    Duration: 29.663 ms
     Changes:   
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[[email protected] ~]# 

(3)客户端验证

[[email protected] ~]# crontab -l
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.160
# Lines below here are managed by Salt, do not edit
[[email protected] ~]#

查看grains信息

sudo salt ‘qianlnk’ grains.items

salt执行命令的格式如下:

8.1通过Minion配置文件定义Grains

先介绍下比较简单的Grains自定义方法,就是通过Minion配置文件定义

Minions的Grains信息是在Minions服务启动的时候汇报给Matser的,所以我们需要修改好Minion配置文
件后重启Minion服务。在Minion的/etc/salt/minion配置文件中默认有一些注释行。这里就是在Minion上
的minion配置文件中如何定义Grains信息例子。下面只需根据自动的需求按照以下格式去填写相应的
键值对就行,大家注意格式就行,SaltStack的配置文件的默认格式都是YAML格式:

# Custom static grains for this minion can be specified here and used in SLS
# files just like all other grains. This example sets 4 custom grains, with
# the 'roles' grain having two values that can be matched against.
#grains:
#  roles:
#    - webserver
#    - memcache
#  deployment: datacenter4
#  cabinet: 13
#  cab_u: 14-15

为了统一管理Minion的Grains信息,需要把这
些注释复制到minion.d/grains文件中

自定义 grains,客户端上配置

[[email protected] ~]# vi /etc/salt/minion

# Custom static grains for this minion can be specified here and used in SLS
# files just like all other grains. This example sets 4 custom grains, with
# the 'roles' grain having two values that can be matched against.
grains:
  roles:
    - nginx
  env:
    - test
  myname:
    - hadron
#  deployment: datacenter4
#  cabinet: 13
#  cab_u: 14-15

重启salt-minion

[[email protected] ~]# ps -aux|grep salt-minion
root      38792  0.0  0.1 231928 15388 pts/0    S    02:32   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      38795  0.5  0.3 547648 28872 pts/0    Sl   02:32   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      43928  0.3  0.1 231928 15384 pts/0    S    02:34   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      43933  1.8  0.3 547648 28784 pts/0    Sl   02:34   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      45693  0.0  0.0 112648   960 pts/0    S+   02:34   0:00 grep --color=auto salt-minion
root      50604  0.0  0.1 231928 15384 pts/0    S    Aug17   0:00 /usr/bin/python /usr/bin/salt-minion start
root      50607  0.0  0.3 760916 29024 pts/0    Sl   Aug17   0:48 /usr/bin/python /usr/bin/salt-minion start
root      92074  0.0  0.1 231928 15388 pts/0    S    01:58   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      92077  0.0  0.3 547916 26832 pts/0    Sl   01:58   0:01 /usr/bin/python /usr/bin/salt-minion restart
[[email protected] ~]# kill 38792 43928 45693 50604
-bash: kill: (45693) - No such process
[[email protected] ~]# ps -aux|grep salt-minion
root      43933  1.2  0.3 547648 28784 pts/0    Sl   02:34   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      46529  0.0  0.0 112648   956 pts/0    S+   02:35   0:00 grep --color=auto salt-minion
root      92074  0.0  0.1 231928 15388 pts/0    S    01:58   0:00 /usr/bin/python /usr/bin/salt-minion restart
root      92077  0.0  0.3 547916 26832 pts/0    Sl   01:58   0:02 /usr/bin/python /usr/bin/salt-minion restart
[1]   Terminated              salt-minion start
[3]-  Terminated              salt-minion restart
[4]+  Terminated              salt-minion restart
[[email protected] ~]# kill 92077 92074 43933
-bash: kill: (43933) - No such process
[[email protected] ~]# ps -aux|grep salt-minion
root      48215  0.0  0.0 112648   960 pts/0    S+   02:36   0:00 grep --color=auto salt-minion
[2]+  Terminated              salt-minion restart
[[email protected] ~]# salt-minion restart &
[1] 49052
[[email protected] ~]# 

服务端获取 grains

[[email protected] ~]# salt 'nb1' grains.item role env myname
nb1:
    ----------
    env:
        - test
    myname:
        - hadron
    role:
        - nginx
[[email protected] ~]# 
[[email protected] ~]# salt 'nb1' grains.item role
nb1:
    ----------
    role:
        - nginx
[[email protected] ~]#

注意:grains 在远程执行命令时很方便。我们可以按照 grains
的一些指标来操作。比如把所有的 web 服务器的 grains 的 role 设置为
nginx,那这样我们就可以批量对 nginx 的服务器进行操作了:

[[email protected] ~]# salt -G role:nginx cmd.run 'hostname'
nb1:
    nb1
[[email protected] ~]# 

[[email protected] ~]# salt -G os:CentOS cmd.run 'hostname'
nb1:
    nb1
nb0:
    nb0
nb2:
    nb2
[[email protected] ~]#

有哪些匹配?

所有操作目标参数:

Target Options: 
-E, --pcre                   正则匹配 
-L, --list                   列表匹配 
-G, --grain                  grains 匹配 
--grain-pcre                 grains 加正则匹配 
-N, --nodegroup              组匹配 
-R, --range                  范围匹配 
-C, --compound               综合匹配( 指定多个匹配) 
-I, --pillar                 pillar 值匹配 
-S, --ipcidr                 minions网段地址匹配 

target:执行salt命令的目标,可以使用正则表达式

Saltstack 快速入门教程

调试Salt

# 开启debug日志
salt-minion -l debug

# 设置超时
salt ‘*’ state.highstate -t 60

sls文件存放根路径在master配置文件中定义,默认为/srv/salt,该目录在操作系统上不存在,需要手动创建。

1.介绍

Saltstack 比 Puppet 出来晚几年,是基于Python 开发的,也是基于 C/S
架构,服务端 master 和客户端 minions ;Saltstack 和 Puppet 很像,可以说
Saltstatck 整合了 Puppet 和 Chef
的功能,更加强大,更适合大规模批量管理服务器,并且它比 Puppet
更容易配置。 三大功能:
远程命令执行,配置管理(服务,文件,cron,用户,组),云管理。

查看grains的功能

➜ ~ sudo salt "cn1" sys.list_functions pillar 
cn1: 
  - pillar.data 
  - pillar.ext 
  - pillar.fetch 
  - pillar.file_exists 
  - pillar.get 
  - pillar.item 
  - pillar.items 
  - pillar.keys 
  - pillar.ls 
  - pillar.obfuscate 
  - pillar.raw 

Pillar数据跟特定的Minion关联,可以用来传递Minion自己的信息。管理员也可以自定义自己的pillar来管理minion。

salt states的核心是sls文件,该文件使用YAML语法定义了一些k/v的数据。

12.1远程执行命令

(1)接着编辑之前的 top.sls 文件

[[email protected] ~]# vim /srv/salt/top.sls
[[email protected] ~]# cat /srv/salt/top.sls
base:
  'nb1':
    - cmdtest
  'nb2':
    - filedir
[[email protected] ~]# 

(2)新建 cmdtest.sls 文件

[[email protected] ~]# vim /srv/salt/cmdtest.sls
[[email protected] ~]# cat /srv/salt/cmdtest.sls
cmd-test:  
  cmd.run:
    - onlyif: test -f /tmp/1.txt
    - names:
      - touch /tmp/cmdtest.txt
      - mkdir /tmp/cmdtest
    - user: root
[[email protected] ~]# 

条件 onlyif 表示若 /tmp/1.txt文件存在,则执行后面的命令;可以使用
unless,两者正好相反。

[[email protected] ~]# echo "hello" > /tmp/1.txt
[[email protected] ~]# cat /tmp/1.txt 
hello
[[email protected] ~]#

(3)执行命令

[[email protected] ~]# salt 'nb1' state.highstate
nb1:
----------
          ID: cmd-test
    Function: cmd.run
        Name: touch /tmp/cmdtest.txt
      Result: True
     Comment: Command "touch /tmp/cmdtest.txt" run
     Started: 02:23:07.347360
    Duration: 565.866 ms
     Changes:   
              ----------
              pid:
                  7209
              retcode:
                  0
              stderr:
              stdout:
----------
          ID: cmd-test
    Function: cmd.run
        Name: mkdir /tmp/cmdtest
      Result: True
     Comment: Command "mkdir /tmp/cmdtest" run
     Started: 02:23:07.913505
    Duration: 208.682 ms
     Changes:   
              ----------
              pid:
                  7245
              retcode:
                  0
              stderr:
              stdout:

Summary
------------
Succeeded: 2 (changed=2)
Failed:    0
------------
Total states run:     2
[[email protected] ~]#

(4)验证

[[email protected] ~]# ll /tmp|grep cmd
drwxr-xr-x 2 root root   6 Aug 21 02:23 cmdtest
-rw-r--r-- 1 root root   0 Aug 21 02:23 cmdtest.txt
[[email protected] ~]# 

sls中使用

#在top.sls中使用 

'os:Ubuntu': 
  - match: grain 
  - webserver 

在top.sls入口中对系统是Ubuntu的服务器执行webserver.sls里定义的状态信息。 

对比ansible,Saltstack缺点是需要安装客户端。为了速度建议选择SaltStack

13.管理任务计划

自定义module

根据官网的说法,自定义module应该存放在/srv/salt/_modules/目录下。同步到minion的方法有三种:

state.apply
saltutil.sync_modules
saltutil.sync_all

例子:

#encoding = utf8

def test():
    return 'this is a test'

def get_target():
    return __grains__['target']

def get_domain(service):
    dom = __pillar__[service]['domain']
    return dom

def deploy(service,version):
    target = get_target()
    domain = get_domain(service)
    cmd = "gover -s "+service+" -t "+target+" -v "+str(version)+" -d "+ domain
    return __salt__['cmd.run'](cmd)

如,在master新建/etc/salt/master.d/nodegroups.conf ,yaml格式

8.2 pillar

pillar 和 grains 不一样,是在 master 上定义的,并且是针对 minion
定义的一些信息。像一些比较重要的数据(密码)可以存在 pillar
里,还可以定义变量等。

(1)服务端自定义配置 pillar

[[email protected] ~]# vim /etc/salt/master

找到如下内容,

#pillar_roots:
#  base:
#    - /srv/pillar
#

去掉#号,修改为

pillar_roots:
  base:
    - /srv/pillar

[[email protected] ~]# mkdir /srv/pillar

自定义配置文件,内容如下

[[email protected] ~]# vim /srv/pillar/test.sls
[[email protected] ~]# cat /srv/pillar/test.sls
conf: /etc/test123.conf
myname: hadron
[[email protected] ~]# 

总入口文件,内容如下

[[email protected] ~]# vim /srv/pillar/top.sls
[[email protected] ~]# cat /srv/pillar/top.sls
base:
  'nb1':
    - test
[[email protected] ~]#

重启master

[[email protected] ~]# ps -aux|grep salt-master
root      29178  0.0  0.3 313076 26816 pts/3    S+   Aug17   0:00 /usr/bin/python /usr/bin/salt-master start
root      29242  0.5  0.4 407192 32856 pts/3    Sl+  Aug17   1:24 /usr/bin/python /usr/bin/salt-master start
root      29243  0.0  0.2 395004 22692 pts/3    Sl+  Aug17   0:00 /usr/bin/python /usr/bin/salt-master start
root      29244  0.0  0.3 395004 24292 pts/3    Sl+  Aug17   0:00 /usr/bin/python /usr/bin/salt-master start
root      29245  0.0  0.2 313076 22016 pts/3    S+   Aug17   0:00 /usr/bin/python /usr/bin/salt-master start
root      29250  0.0  0.3 1204752 28560 pts/3   Sl+  Aug17   0:01 /usr/bin/python /usr/bin/salt-master start
root      29251  0.0  0.3 1205064 28624 pts/3   Sl+  Aug17   0:01 /usr/bin/python /usr/bin/salt-master start
root      29252  0.0  0.3 1205068 28596 pts/3   Sl+  Aug17   0:01 /usr/bin/python /usr/bin/salt-master start
root      29255  0.0  0.3 1205068 28648 pts/3   Sl+  Aug17   0:01 /usr/bin/python /usr/bin/salt-master start
root      29258  0.0  0.3 1205072 28584 pts/3   Sl+  Aug17   0:01 /usr/bin/python /usr/bin/salt-master start
root      29261  0.0  0.2 689932 22668 pts/3    Sl+  Aug17   0:00 /usr/bin/python /usr/bin/salt-master start
root      93354  0.0  0.0 112652   960 pts/2    S+   03:07   0:00 grep --color=auto salt-master
[[email protected] ~]# kill 29178 29242 29243 29244 29245 29250 29251 29252 29255 29258 29261

在单独终端启动

[[email protected] ~]# salt-master start

注意:当更改完 pillar 配置文件后,我们可以通过刷新 pillar 配置来获取新的
pillar 状态

[[email protected] ~]# salt '*' saltutil.refresh_pillar
nb1:
    True
nb0:
    True
nb2:
    True
[[email protected] ~]# 

验证

[[email protected] ~]# salt 'nb1' pillar.items
nb1:
    ----------
    conf:
        /etc/test123.conf
    myname:
        hadron
[[email protected] ~]# salt 'nb1' pillar.item conf
nb1:
    ----------
    conf:
        /etc/test123.conf
[[email protected] ~]# salt 'nb1' pillar.item myname
nb1:
    ----------
    myname:
        hadron
[[email protected] ~]# 

pillar 同样可以用来作为 salt 的匹配对象

[[email protected] ~]# salt -I 'conf:/etc/test123.conf' test.ping
nb1:
    True
[[email protected] ~]# salt -I 'conf:/etc/test123.conf' cmd.run 'w'
nb1:
     03:17:08 up 67 days, 14:25,  1 user,  load average: 0.02, 0.12, 0.24
    USER     TTY      FROM             [email protected]   IDLE   JCPU   PCPU WHAT
    root     pts/0    hadron           Mon21   24:44   2.38s  0.16s -bash
[[email protected] ~]#

Salt state树

# 在/etc/salt/master中配置file_roots
file_roots: 
  base: 
  - /srv/salt 
salt ''  [arguments]

4.配置

怎么使用grains?

运维开发语言热衷于python(后期可做二次开发),排除Puppet、Chef

4.1 Salt minion配置

用vi/vim打开/etc/salt/minion,找到配置选项master所在行,一般在第16行。修改内容,去掉#,冒号后有一个空格,将salt更改为master。

示例操作如下:

[[email protected] ~]# vi /etc/salt/minion

[[email protected] ~]# vi /etc/salt/minion

# Set the location of the salt master server. If the master server cannot be
# resolved, then the minion will fail to start.
master: master

如果找不到master对应的行,可以直接在文件尾部添加一行master: mastermaster: 192.168.1.160

也可以使用sed命令直接修改配置文件

[[email protected] ~]# sed -i 's/#master: salt/master: 192.168.1.160/g' /etc/salt/minion

什么是saltstack?

Saltstack开源项目始于2011年,使用Python开发的一套C/S架构的运维工具,由Master和Minion组成,通过ZeroMQ进行通信。
项目地址:
https://github.com/saltstack/salt
官网地址:
http://www.saltstack.com
官方文档:
http://docs.saltstack.com
OR
http://docs.saltstack.cn
开发语言: Python
运行模式: C/S

minion在第一次启动时,会在/etc/salt/pki/minion/(该路径在/etc/salt/minion里面设置)下自动生成minion.pem(private
key)和 minion.pub(public key),然后将
minion.pub发送给master。master在接收到minion的public
key后,通过salt-key命令accept minion public
key,这样在master的/etc/salt/pki/master/minions下的将会存放以minion
id命名的 public key,然后master就能对minion发送指令了。
认证命令如下:

6.配置认证

(1)在服务端上操作 新打开一个nb0终端

[[email protected] ~]# salt-key -a nb0
The following keys are going to be accepted:
Unaccepted Keys:
nb0
Proceed? [n/Y] y
Key for minion nb0 accepted.
[[email protected] ~]#

[[email protected] ~]# salt-key -a nb1
The following keys are going to be accepted:
Unaccepted Keys:
nb1
Proceed? [n/Y]y
Key for minion nb1 accepted.
[[email protected] ~]#

[[email protected] ~]# salt-key -a nb2
The following keys are going to be accepted:
Unaccepted Keys:
nb2
Proceed? [n/Y] y
Key for minion nb2 accepted.
You have mail in /var/spool/mail/root
[[email protected] ~]#

[[email protected] ~]# salt-key
Accepted Keys:
nb0
nb1
nb2
Denied Keys:
Unaccepted Keys:
Rejected Keys:
[[email protected] ~]# 

注意: 在==大规模部署Minion==的时候可以设置自动接受指定等待认证的 key

在修改/etc/salt/master前,先备份一下

[[email protected] ~]# cp /etc/salt/master /etc/salt/master.bak

通过vi打开/etc/salt/master

[[email protected] ~]# vi /etc/salt/master

找到#auto_accept: False一行,修改False为True

# Enable auto_accept, this setting will automatically accept all incoming
# public keys from the minions. Note that this is insecure.
#auto_accept: False

或者通过sed命令修改

[[email protected] ~]# sed -i 's/#auto_accept: False/auto_accept: True/g' /etc/salt/master

ctrl+c停止salt-master,然后再启动

[[email protected] ~]# salt-master start 
^C[WARNING ] Stopping the Salt Master
[WARNING ] Stopping the Salt Master

Exiting on Ctrl-c
[WARNING ] Stopping the Salt Master

Exiting on Ctrl-c

Exiting on Ctrl-c
[[email protected] ~]# salt-master start

(2)测试验证

[[email protected] ~]# salt '*' test.ping
nb2:
    True
nb1:
    True
nb0:
    True
[[email protected] ~]#

这里的 * 必须是在 master 上已经被接受过的客户端,可以通过 salt-key 查到

远程执行命令

[[email protected] ~]# salt '*' cmd.run   'df -h'
nb0:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G   26G   22G  55% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   16K  3.9G   1% /dev/shm
    tmpfs                3.9G  402M  3.5G  11% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
    /dev/loop0           7.8G  7.8G     0 100% /var/ftp/iso-home
nb1:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G  4.3G   44G   9% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   12K  3.9G   1% /dev/shm
    tmpfs                3.9G  377M  3.5G  10% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
nb2:
    Filesystem           Size  Used Avail Use% Mounted on
    /dev/mapper/cl-root   48G  4.9G   43G  11% /
    devtmpfs             3.9G     0  3.9G   0% /dev
    tmpfs                3.9G   12K  3.9G   1% /dev/shm
    tmpfs                3.9G  401M  3.5G  11% /run
    tmpfs                3.9G     0  3.9G   0% /sys/fs/cgroup
    /dev/sda1           1014M  139M  876M  14% /boot
    /dev/mapper/cl-home   24G   33M   24G   1% /home
    tmpfs                781M     0  781M   0% /run/user/0
[[email protected] ~]# 

首先我们要知道在安装SaltStack的时候Master
跟Minion端都分别安装了哪些文件,这样有利于我
们去了解SaltStack日后的一些日常操作。大家可以
通过以下命令查看SaltStack安装部署的时候分别安
装了哪些命令(yum安装方式)。

[[email protected] ~]# rpm -ql salt-master
/etc/salt/master            #salt master 配置文件
/usr/bin/salt               #salt master  核心操作命令
/usr/bin/salt-cp            #salt  文件传输命令
/usr/bin/salt-key           #salt  证书管理命令
/usr/bin/salt-master        #salt master  服务命令
/usr/bin/salt-run           #salt master runner 命令
/usr/bin/salt-unity
/usr/lib/systemd/system/salt-master.service
/usr/share/man/man1/salt-cp.1.gz
/usr/share/man/man1/salt-key.1.gz
/usr/share/man/man1/salt-master.1.gz
/usr/share/man/man1/salt-run.1.gz
/usr/share/man/man1/salt-unity.1.gz
/usr/share/man/man7/salt.7.gz
[[email protected] ~]#

[[email protected] ~]# salt --version
salt 2015.5.10 (Lithium)
[[email protected] ~]#

saltstack简介

  1. 复合匹配

    salt -C ‘G@os:CentOS or L@Minion’ test.ping

  2. Pillar值匹配

    salt -I ‘key:value’ test.ping

12.远程执行

前面提到远程执行命令
test.ping,cmd.run,点前面的是模块,点后面的是函数;这样总归是不太规范化,下面详细介绍怎么远程执行命令和脚本。

安装saltstack

yum -y install salt-master
service salt-master start

14.Saltstack 常用命令

Salt state的top文件

top.sls是state系统的入口文件,它在大规模配置管理工作中负责指定哪些设备调用哪些states.sls文件。top.sls是salt
state默认的sls文件,在未做特别制定的时候salt
state会默认调用top.sls来执行。如:

sudo salt ‘cn1’ state.hightate

top.sls需要手动创建,位置则是salt state树定义的目录/srv/salt下。

SaltStack官网文档地址:https://docs.saltstack.com

10.states文件

salt states的核心是sls文件,该文件使用YAML语法定义了一些k/v的数据。

sls文件存放根路径在master配置文件中定义,默认为/srv/salt,该目录在操作系统上不存在,需要手动创建。

在salt中可以通过salt://代替根路径,例如你可以通过salt://top.sls访问/srv/salt/top.sls。

在states中top文件也由master配置文件定义,默认为top.sls,该文件为states的入口文件。
一个简单的sls文件如下:

apache:
 pkg.installed
 service.running
   - require:
     - pkg: apache

说明:此SLS数据确保叫做”apache”的软件包(package)已经安装,并且”apache”服务(service)正在运行中。

  • 第一行,被称为ID说明(ID Declaration)。ID说明表明可以操控的名字。
  • 第二行和第四行是State说明(State
    Declaration),它们分别使用了pkg和service states。pkg
    state通过系统的包管理其管理关键包,service
    state管理系统服务(daemon)。
    在pkg及service列下边是运行的方法。方法定义包和服务应该怎么做。此处是软件包应该被安装,服务应该处于运行中。
  • 第六行使用require。本方法称为”必须指令”(Requisite
    Statement),表明只有当apache软件包安装成功时,apache服务才启动起来

salt-master是通过写sls配置管理minion上重复指令的,服务状态等等。

salt
states的核心是sls文件,该文件使用YAML语法定义了一些k/v的数据。sls文件存放根路径在master配置文件中定义,默认为/srv/salt,该目录在操作系统上不存在,需要手动创建。

[[email protected] ~]# mkdir -p /srv/salt/base

针对管理对象Target的操作,Module

  • 查看所有的module列表

sudo salt "cn1" sys.list_modules  
cn1: 
  - aliases 
  - alternatives 
  - archive 
  - artifactory 
... 
  • 查看指定module的所有function

➜ pillar git:(master) ✗ sudo salt "cn1" sys.list_functions cmd 
cn1: 
  - cmd.exec_code 
  - cmd.exec_code_all 
  - cmd.has_exec 
  - cmd.powershell 
  - cmd.retcode 
  - cmd.run 
  - cmd.run_all 
  - cmd.run_bg 
  - cmd.run_chroot 
  - cmd.run_stderr 
  - cmd.run_stdout 
  - cmd.script 
  …
  • 查看指定module用法

➜ pillar git:(master) ✗ sudo salt "cn1" sys.doc cmd 
cmd.exec_code: 

  Pass in two strings, the first naming the executable language, aka - 
  python2, python3, ruby, perl, lua, etc. the second string containing 
  the code you wish to execute. The stdout will be returned. 

  CLI Example: 

  salt '*' cmd.exec_code ruby 'puts "cheese"' 
apache:
 pkg.installed
 service.running
   - require:
     - pkg: apache

3.2 客户端安装

[[email protected] ~]# yum install -y epel-release
[[email protected] ~]# yum install -y salt-minion

什么是grains?

Grains是服务器的一系列粒子信息,也就是服务器的一系列物理,软件环境信息。在执行salt的sls时候可以根据Grains信息的不同对服务器进行匹配分组,例如可以根据系统是centos服务器跟系统是redhat环境的安装不同的软件包。

top.sls可以这样定义:

13.1 建立 cron

(1)编辑 top.sls 文件

[[email protected] ~]# vim /srv/salt/top.sls
[[email protected] ~]# cat /srv/salt/top.sls
base:
  'nb1':
    - crontest
  'nb2':
    - shelltest
[[email protected] ~]#

(2)编辑 crontest.sls 文件

[[email protected] ~]# vim /srv/salt/crontest.sls
[[email protected] ~]# cat /srv/salt/crontest.sls
cron-test:
  cron.present:
    - name: /bin/touch /tmp/111.txt
    - user: root
    - minute: '*'
    - hour: 20
    - daymonth: 1-10
    - month: '3,5'
    - dayweek: '*'
[[email protected] ~]#

注意,*需要用单引号引起来。当然我们还可以使用 file.managed 模块来管理
cron,因为系统的 cron都是以配置文件的形式存在的。

(3)执行命令

[[email protected] ~]# salt 'nb1' state.highstate
nb1:
----------
          ID: cron-test
    Function: cron.present
        Name: /bin/touch /tmp/111.txt
      Result: True
     Comment: Cron /bin/touch /tmp/111.txt added to root's crontab
     Started: 02:47:51.454886
    Duration: 1478.963 ms
     Changes:   
              ----------
              root:
                  /bin/touch /tmp/111.txt

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[[email protected] ~]# 

(4)客户端验证

[[email protected] ~]# crontab -l
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.81
00      03      *       *       *       ntpdate 192.168.1.160
# Lines below here are managed by Salt, do not edit
# SALT_CRON_IDENTIFIER:/bin/touch /tmp/111.txt
* 20 1-10 3,5 * /bin/touch /tmp/111.txt
[[email protected] ~]# 

master接受minion的key

> sudo salt-key -L                
Accepted Keys:
Denied Keys:
Unaccepted Keys:
qianlnk
Rejected Keys:

> sudo salt-key -a qianlnk
The following keys are going to be accepted:
Unaccepted Keys:
qianlnk
Proceed? [n/Y] y
Key for minion qianlnk accepted.

> sudo salt-key -L        
Accepted Keys:
qianlnk
Denied Keys:
Unaccepted Keys:
Rejected Keys:

arguments:function的参数

12.2 远程执行脚本

(1)接着编辑之前的 top.sls 文件

[[email protected] ~]# vim /srv/salt/top.sls
[[email protected] ~]# cat /srv/salt/top.sls
base:
  'nb1':
    - cmdtest
  'nb2':
    - shelltest
[[email protected] ~]# 

(2)新建 shelltest.sls 文件

[[email protected] ~]# vim /srv/salt/shelltest.sls
[[email protected] ~]# cat /srv/salt/shelltest.sls
shell-test:
  cmd.script:
    - source: salt://test/1.sh
    - user: root
[[email protected] ~]# 

(3)新建 1.sh 脚本文件

[[email protected] ~]# vim /srv/salt/test/1.sh
[[email protected] ~]# cat /srv/salt/test/1.sh
#!/bin/bash
touch /tmp/shelltest.txt
if [ -d /tmp/shelltest ]
then
    rm -rf /tmp/shelltest
else
    mkdir /tmp/shelltest
fi
[[email protected] ~]#

(4)执行命令

[[email protected] ~]# salt 'nb2' state.highstate
nb2:
----------
          ID: shell-test
    Function: cmd.script
      Result: True
     Comment: Command 'shell-test' run
     Started: 02:35:33.341722
    Duration: 585.072 ms
     Changes:   
              ----------
              pid:
                  48228
              retcode:
                  0
              stderr:
              stdout:

Summary
------------
Succeeded: 1 (changed=1)
Failed:    0
------------
Total states run:     1
[[email protected] ~]# 

(5)客户端验证

[[email protected] ~]# ll /tmp|grep shell
drwxr-xr-x 2 root root   6 Aug 21 02:35 shelltest
-rw-r--r-- 1 root root   0 Aug 21 02:35 shelltest.txt
[[email protected] ~]# 

通过上面的例子,我们实现了远程执行脚本;如果我们想一键远程安装 LAMP 或者
LNMP,那么只需把本例中的 1.sh 脚本替换成 一键安装的脚本就行。

查看grains的功能

sudo salt ‘qianlnk’ sys.list_functions grains
qianlnk: 
  - grains.append 
  - grains.delval 
  - grains.fetch 
  - grains.filter_by 
  - grains.get 
  - grains.get_or_set_hash 
  - grains.has_value 
  - grains.item 
  - grains.items 
  - grains.ls 
  - grains.remove 
  - grains.set 
  - grains.setval 
  - grains.setvals 

安装、配置管理端(master)

14.3 显示存活的客户端

[[email protected] ~]# salt-run manage.up
- nb0
- nb1
- nb2
[[email protected] ~]# 

基本的配置

文件 /etc/salt/master

interface: 192.168.199.86

文件 /etc/salt/minion

master: 192.168.199.86
id: qianlnk # minion的识别ID,可以是IP,域名,或是可以通过DNS解析的字符串

图片 1

14.1拷贝文件到客户端

[[email protected] ~]# salt 'nb1' cp.get_file salt://apache.sls /tmp/cp.txt
nb1:
    /tmp/cp.txt
[[email protected] ~]#

[[email protected] ~]# cat /tmp/cp.txt 
apache-service:
  pkg.installed:
    - names:
      - httpd
      - httpd-devel
  service.running:
    - name: httpd
    - enable: True
[[email protected] ~]# 

安装

Installs the latest release. Updating installs the latest release even
if it is a new major version.

1.1、Run the following command to import the SaltStack repository key:

wget -O - https://repo.saltstack.com/apt/ubuntu/16.04/amd64/latest/SALTSTACK-GPG-KEY.pub | sudo apt-key add -

1.2、Save the following file to /etc/apt/sources.list.d/saltstack.list:

deb http://repo.saltstack.com/apt/ubuntu/16.04/amd64/latest xenial main

1.3、Install

> sudo apt-get update
> sudo apt-get install salt-master
> sudo apt-get install salt-minion
> sudo apt-get install salt-ssh
> sudo apt-get install salt-syndic
> sudo apt-get install salt-cloud
> sudo apt-get install salt-api

1.4、show version

> salt --version
salt 2016.3.3 (Boron)
  • 第一行,被称为ID说明(ID Declaration)。ID说明表明可以操控的名字。
  • 第二行和第四行是State说明(State
    Declaration),它们分别使用了pkg和service states。pkg
    state通过系统的包管理其管理关键包,service
    state管理系统服务(daemon)。
    在pkg及service列下边是运行的方法。方法定义包和服务应该怎么做。此处是软件包应该被安装,服务应该处于运行中。
  • 第六行使用require。本方法称为”必须指令”(Requisite
    Statement),表明只有当apache软件包安装成功时,apache服务才启动起来。

3.1 服务端安装

yum install -y epel-release

yum install -y salt-master salt-minion

[[email protected] ~]# yum install -y epel-release
[[email protected] ~]# yum install -y salt-master salt-minion

简单的例子

cat top.sls
base:                       # 匹配状态树的base目录配置
  ‘*’:                        # 匹配minion
    - webserver         # 表示执行base目录下的webserver.sls状态文件或者webserver/init.sls文件

cat webserver.sls
apache:                   # ID声明,跟yum install apache指定的名称一样
  pkg:                      # 状态声明,说明要做什么状态操作,pkg使用系统本地的软件包管理工具
    - installed           # 函数声明,说明该sls文件指定的执行操作函数
  service:                #管理系统守护进程
    - running
    - require:
      - pkg: apache

# 更新所有minion的状态
sudo salt “*” state.highstate

注:这样就可以在所有的minion中安装apache了。

当然也可以不要top.sls,直接指定sls文件来执行
sudo salt ‘cn1’ state.sls webserver

或者使用apply function,apply会根据后面的参数来决定调用state.highstate还是state.sls
sudo salt ‘cn1’ state.apply webserver

目前市场上主流的开源自动化配置管理工具有puppet、chef、ansible、saltstack等等。到底选择那个比较好?可以从以下几方面考虑:

Saltstack 快速入门教程,Saltstack入门教程

测试

> sudo salt "qianlnk" test.ping
qianlnk:
  True

> sudo salt "qianlnk" cmd.run 'uptime'
qianlnk:
  13:34:25 up 14 days, 2:37, 1 user, load average: 0.04, 0.18, 0.16
  1. 正则表达式

    salt -E ‘Minion*’ test.ping #主机名以Minion开通

  2. 列表匹配

    salt -L Minion,Minion1 test.ping

  3. Grians匹配

    salt -G ‘os:CentOS’ test.ping

5.启动服务

(1)服务端

[[email protected] ~]# salt-master start

在后端运行

[[email protected] ~]# salt-master start &
[3] 35438
[[email protected] ~]#

(2)客户端

[[email protected] ~]# salt-minion start &
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate

[[email protected] ~]# salt-minion start &
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate

[[email protected] ~]# salt-minion start
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate
[ERROR   ] The Salt Master has cached the public key for this node, this salt minion will wait for 10 seconds before attempting to re-authenticate

minion在第一次启动时,会在/etc/salt/pki/minion/(该路径在/etc/salt/minion里面设置)下自动生成minion.pem(private
key)和 minion.pub(public key),然后将
minion.pub发送给master。master在接收到minion的public
key后,通过salt-key命令accept minion public
key,这样在master的/etc/salt/pki/master/minions下的将会存放以minion
id命名的 public key,然后master就能对minion发送指令了。

自定义grains

存在形式:

1、core grains 
2、在/etc/salt/grains 中自定义 
3、在/etc/salt/minion 中自定义 
4、在_grains目录中自定义grains,同步到minions 
  • core grains是系统设定的grains,如果自定义的grains的名称跟core
    grains中的一样则会将之覆盖掉。
  • 在minion端的/etc/salt/grains中定义:

root@17ca9e9efc8a:/etc/salt# cat grains  
roles: 
  - zookeeper 
  - kafka 

注意!该目录下不存在grains文件的话自己创建,定义grains后需要重启salt-minion。

测试:

sudo salt -G “roles:kafka”  test.ping
cn2: 
  True
  • 在/etc/salt/minion中定义,类似在/etc/salt/grains中定义。
  • 在master端,_grains目录中定义:

➜  cd /srv/salt 
➜  mkdir _grains 
➜  cd _grains  
➜  vi my_grain.py 
#添加内容 
➜  cat my_grain.py  
def my_grains(): 
grains = {'roles' : ['phpserver','webserver']} 
return grains 

➜  sudo salt -E "dk*" saltutil.sync_grains     #刷新minion的grains 
dk2: 
  - grains.my_grain 
dk1: 
  - grains.my_grain 
➜  sudo salt -E "dk*" grains.item roles 
dk2: 
  ---------- 
  roles: 
  - phpserver 
  - webserver 
dk1: 
  ---------- 
  roles: 
  - phpserver 
  - webserver 

➜ sudo salt -G "roles:webserver" test.ping 
dk2: 
  True 
dk1: 
  True 

一个简单的sls文件如下:

3. 安装

在SaltsStack架构中服务端叫作Master,客户端叫作Minion,都是以守护进程的模式运行,一直监听配置文件中定义的ret_port(saltstack客户端与服务端通信的端口,负责接收客户端发送过来的结果,默认4506端口)和publish_port(saltstack的消息发布系统,默认4505端口)的端口。当Minion运行时会自动连接到配置文件中定义的Master地址ret_port端口进行连接认证。

  • Master:控制中心,salt命令运行和资源状态管理
  • Minion :
    需要管理的客户端机器,会主动去连接Mater端,并从Master端得到资源状态信息,同步资源管理信息
  • States:配置管理的指令集
  • Modules:在命令行中和配置文件中使用的指令模块,可以在命令行中运行
  • Grains:minion端的变量,静态的
  • Pillar:minion端的变量,动态的比较私密的变量,可以通过配置文件实现同步minions定义
  • highstate:为minion端下发永久添加状态,从sls配置文件读取.即同步状态配置
  • salt_schedule:会自动保持客户端配置

命令行中使用

对操作系统是Ubuntu的服务器执行test.ping 
➜ salt sudo salt -G "os:Ubuntu" test.ping 
dk1: 
  True 
dk2: 
  True 

显示cpu架构是x86_64的服务器的cpu个数 
➜ salt sudo salt -G 'cpuarch:x86_64' grains.item num_cpus 
dk2: 
  ---------- 
  num_cpus: 
  4 
dk1: 
  ---------- 
  num_cpus: 
  4 

通过下面命令可以查看所有的function:
salt ‘10.252.137.141’ sys.doc cmd

8.管理对象属性

Grains是SaltStack组件中非常重要的组件之
一,因为我们在做配置部署的过程中会经常使用
它,Grains是SaltStack记录Minion的一些静态信息
的组件,我们可以简单地理解为Grains里面记录着
每台Minion的一些常用属性,比如CPU、内存、磁
盘、网络信息等,我们可以通过grains.items查看某
台Minion的所有Grains信息,Minions的Grains信息
是Minions启动的时候采集汇报给Master的,在实际
应用环境中我们需要根据自己的业务需求去自定义 一些Grains

管理对象Target

在saltstack系统中,我们的管理对象叫作Target。在Master上我们可以采用不同的Target去管理不同的Minion。这些Target可以是主机名、系统信息、定义的分组、甚至是自定义绑定的对象。

SaltStack远程执行

14.4 命令下执行服务端的脚本

[[email protected] ~]# vim /srv/salt/test/shell.sh
[[email protected] ~]# cat /srv/salt/test/shell.sh
#! /bin/bash
echo "hadron.cn" > /tmp/shell.txt
[[email protected] ~]# 

[[email protected] ~]# salt 'nb2' cmd.script salt://test/shell.sh
nb2:
    ----------
    pid:
        86257
    retcode:
        0
    stderr:
    stdout:
[[email protected] ~]# 

[[email protected] ~]# cat /tmp/shell.txt 
hadron.cn
[[email protected] ~]# 

自定义pillar

在master的sls文件中定义,master配置需要指定pillar的目录。

pillar_roots: 
  base: 
  - /srv/pillar 

➜ pillar git:(master) ✗ pwd
/srv/pillar

➜ pillar git:(master) ✗ cat top.sls 
base:
  '*':
  - data
  - git

➜ pillar git:(master) ✗ cat data/init.sls
roles: webserver
➜ pillar git:(master) ✗ cat git.sls
{% if grains[‘os’] == ‘CentOs’ %}
git: git
{% elif grains[‘os’] == ‘Debian’ %}
git: git-core
{% endif %}

默认情况下,master配置文件中的所有数据都会添加到pillar中,且对所有的minion可用。如果要禁止这一个默认值,可以设置master文件中的选项:
pillar_opts: False

pillar跟sls一样有自己的top.sls文件作为入口来组织其他的pillar。base中
“×”制定所有minion拥有pillar:mongodb、zookeeper、kafka、supervisor,
然后是各个minion组有自己的init.sls。kafka.sls中定义kafka对应的一些信息。

修改完pillar文件后需要用以下的命令刷新以下minion的信息:

➜ pillar git:(master) ✗ sudo salt 'cn1' saltutil.refresh_pillar 
cn1: 
  True 
  1. Master:控制中心,salt命令运行和资源状态管理
  2. Minion :
    需要管理的客户端机器,会主动去连接Mater端,并从Master端得到资源状态
  3. 信息,同步资源管理信息
  4. States:配置管理的指令集
  5. Modules:在命令行中和配置文件中使用的指令模块,可以在命令行中运行
  6. Grains:minion端的变量,静态的
  7. Pillar:minion端的变量,动态的比较私密的变量,可以通过配置文件实现同步minions定义
  8. highstate:为minion端下发永久添加状态,从sls配置文件读取.即同步状态配置
  9. salt_schedule:会自动保持客户端配置