SaltStack使用教程(一):安装并简单配置使用

来源:互联网 发布:php图片木马制作 编辑:程序博客网 时间:2024/04/29 23:00

http://nginxs.blog.51cto.com/4676810/1698491

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://nginxs.blog.51cto.com/4676810/1698491

介绍

      这个教程是我中秋休息的时候所做,为大家学习了解saltstack

      SaltStack名字是由它的故乡美国盐城。

      SaltStack是为我们批量管理千万台机器的基础工具,拓展起来也十分方便。SaltStack可以进行动态的编辑,配置管理,远程执行命令等等。

      Saltstack包括masterminionmaster是控制端,minion是被管理节点。

      这里先不废话,简单介绍安装(下面说到的salt就是咱们所说的SaltStack.


这里有两个节点

salt-master10.0.0.11

salt-minion10.0.0.21

 

这里先给两个节点安装个epel源,方便salt网络安装。

[root@salt-master ~] rpm -ivh http://mirrors.ustc.edu.cn/centos/6/extras/x86_64/Packages/epel-release-6-8.noarch.rpm

 

 

10.0.0.11(master)节点操作

安装salt-master,暂时不需要配置。

1
2
3
4
5
6
7
8
[root@salt-master ~]# hostname
salt-master
[root@salt-master ~]# yum -y install salt salt-master  salt-minion
安装配置salt-minion
[root@salt-master ~]#  egrep -v '^#|^$' /etc/salt/minion
master: 10.0.0.11
[root@salt-master ~]# /etc/init.d/salt-minion start
Starting salt-minion daemon:                               [确定]

 

 

 

10.0.0.21节点操作

安装配置salt-minion

1
2
3
4
5
6
7
[root@salt-minion1 ~]# hostname
salt-minion1
[root@salt-master ~]# yum -y install salt salt-minion
[root@salt-master ~]#  egrep -v '^#|^$' /etc/salt/minion
master: 10.0.0.11
[root@salt-minion1 ~]#  /etc/init.d/salt-minion start
Starting salt-minion daemon:                               [确定]

 

 

 

10.0.0.11(master)节点操作

查看salt-master接收到的密钥,分别是“10.0.0.11”和“10.0.021”,master通过这些密钥

 salt-key -L     查看当前密钥

                -A  接受所有来自客户端密钥

               --accept=example.com    接受指定的密钥

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@salt-master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
salt-master
salt-minion1
Rejected Keys:
  
[root@salt-master ~]# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
salt-master
salt-minion1
Proceed? [n/Y] y
Key for minion salt-master accepted.
Key for minion salt-minion1 accepted.

 

 

验证salt-master是否可以控制各个minion节点(查看各节点的IP)

1
2
3
4
5
[root@salt-master~]# salt '*' network.ip_addrs
salt-master:
    - 10.0.0.11
salt-minion1:
    - 10.0.0.21


添加salt-master配置

         刚刚只是把salt-minion加入到了salt-master管理,就像老板招聘了一群员工现在老板可以向员工发号命令了,如果老板有一部分多步骤且重复的指令,老板一般就会用到邮件和文档来让员工操作了,

salt-master是通过写sls配置管理minion上重复指令的,服务状态等等。这里需要先配置一下:

1
2
3
4
5
6
7
8
9
10
创建配置文件目录
[root@salt-master srv]# mkdir -p /srv/salt/base
在master配置里面指定配置文件目录
[root@salt-master ~]# vim /etc/salt/master
 file_roots:
   base:
     /srv/salt/base
[root@salt-master ~]# /etc/init.d/salt-master restart
Stopping salt-master daemon:                               [确定]
Starting salt-master daemon:                               [确定]


          salt-master默认会读取上面指定配置目录下的top.sls(这个是在master配置文件下的state_top: top.sls决定的)

sls配置文件的书写规则是状态ID从行首开始,执行模块或主机前加2个空格,其他属性也是逐级加两个空格,

且不能是同意行。

现在写一个:

     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[root@salt-master base]# tree
.
├── system
│   ├── files
│   │   └── hosts
│   └── hosts.sls
└── top.sls
  
2 directories, 3
files
[root@salt-master base]# cat top.sls               
base:                      
  "*":                    
    - system.hosts        
                           
  
[root@salt-master
base]# cat system/hosts.sls
/etc/hosts:                
  file.managed:           
    source: salt://system/files/hosts 
    - mode: 644            
    - user: root           
    - group: root


1
下面执行一下上面的配置文件状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
[root@salt-master system]# salt 'salt-minion1' state.highstate          
下面执行结果证明成功
salt-minion1:                                                        
----------
          ID: /etc/hosts                                    
    Function: file.managed                              
      Result: True                                                   
     Comment: File /etc/hosts updated          
     Started: 14:15:55.120499                       
    Duration: 24.284 ms                                  
     Changes:                                                  
              ----------
              diff:
                  --- 
                  +++ 
                  @@ -1,2 +1,4 @@
                   127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
                   ::1         localhost localhost.localdomain localhost6 localhost6.localdomain
                  +salt-master 10.0.0.11                   
                  +salt-minion1 10.0.0.21
  
Summary
------------
Succeeded: 1 (changed=1)                               
Failed:    0                                                           
------------
Total states run:     1

本节最后执行详解见下章节“SaltStack使用教程(二):目录和文件管理”

本文出自 “nginxs小白” 博客,请务必保留此出处http://nginxs.blog.51cto.com/4676810/1698491


0 0
原创粉丝点击