学习Saltstack已经有一段时间了,不过现在我还是不知道如何对Saltstack做一个全面的定义。按照大家公认的说法,我们可以这样来定义Saltstack,一个整合了Puppet和 Chef的功能,更加强大,更适合大规模批量管理服务器的自动化工具,基于ZeroMQ通信,使用python开发的简单高可用工具。还是按照之前的老套路,先从安装和入门说起。
一、Saltstack架构概述
Saltstack基于C/S架构,服务端master和客户端minions ,其主要由以下部分组成:
二、Saltstack的安装配置
1、master的安装
- rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
- yum install salt-master
默认配置文件位于/etc/salt/master ,默认不需要更改该配置文件。master端有两个端口需要在iptables上放行
- 4505(publish_port):salt 的消息发布系统
- 4506(ret_port):salt 客户端与服务端通信的端口
启动方法:/etc/init.d/salt-master start
2、minion客户端的安装
- rpm -ivh http://mirrors.sohu.com/fedora-epel/6/x86_64/epel-release-6-8.noarch.rpm
- yum install salt-minion
客户端的配置文件为/etc/salt/minion ,打开该文件,这里有两项是必须配置项。其一、找到如下行
- # resolved, then the minion will fail to start.
- #master: salt
在其下增加master主机的配置
- # resolved, then the minion will fail to start.
- #master: salt
- master: 192.168.10.16
我这里直接配置的是IP地址,也可以配置成主机名,如果配置成主机名的话,需要在/etc/hosts文件中master主机对应的IP ,如果使用内部DNS的例外,可以在内部DNS上的统一配置。
其二、找到如下段部分
- # Since salt uses detached ids it is possible to run multiple minions on the
- # same machine but with different ids, this can be useful for salt compute
- # clusters.
- #id:
在其下增加一行内容为:
- id: host174
这里是指定当前主机的id号,这在后面master认证和master调用命令执行时显示的名称,可以根据实际识别需要填写。另外需要注意的是,以上两处配置冒号后面都需要有一个空格,不然会报如下错误:
- Starting salt-minion daemon: [ERROR ] Error parsing configuration file: /etc/salt/minion - while scanning a simple key
- in "<string>", line 44, column 1:
- id:host172
- ^
- could not found expected ':'
- in "<string>", line 46, column 1:
- # Append a domain to a hostname ...
- ^
除以上两个必须配置项外,客户端还有其他很多可配置项,如user(默认是root)、root_dir(默认是根目录)等选项,这些配置后可以控制master端的权限。
配置文件配置完成后,可以通过/etc/init.d/salt-minion start命令启动minion 。
三、C/S认证
和puppet一样, salt 的 master 和 minions 是通过证书通信的,故存在证书的信任颁发问题。
在master端:salt-key -L 查看当前需要接受的keys(master和minions都需要把服务开启)。
- [root@localhost ]# salt-key -L
- Accepted Keys:
- host174
- Unaccepted Keys:
- Rejected Keys:
这里可以看到我已经认证过一台id为host174的主机 。我再新增一个172的主机:
- [root@localhost conf]# salt-key -L
- Accepted Keys:
- host174
- Unaccepted Keys:
- host172
- Rejected Keys:
- [root@localhost conf]# salt-key -A
- The following keys are going to be accepted:
- Unaccepted Keys:
- host172
- Proceed? [n/Y] Y
- Key for minion host172 accepted.
- [root@localhost conf]# salt-key -L
- Accepted Keys:
- host172
- host174
- Unaccepted Keys:
- Rejected Keys:
我上面用的-A参数,该参数意思是接受所有认证主机的认证,也可以使用 -a id名 只认证单独的主机。默认认证完成后会在/etc/salt/pki/master/minions目录找到以ID名命令的文件,里面存放的是密钥文件。
如果对客户端信任,可以让master自动接受请求,在master端/etc/salt/master配置
- auto_accept: True
注:认证这里zeromq2版本下有一个bug --minion与master的连接断开后,不会自动重连的问题,zeromq3正常,这个主要在centos5下经常遇到。具体可以通过以下方法确认具体的版本:
- [root@localhost minions]# salt '*' test.versions_report
- host172:
- Salt: 2014.1.0
- Python: 2.6.6 (r266:84292, Jun 18 2012, 14:18:47)
- Jinja2: 2.2.1
- M2Crypto: 0.20.2
- msgpack-python: 0.1.13
- msgpack-pure: Not Installed
- pycrypto: 2.0.1
- PyYAML: 3.10
- PyZMQ: 2.2.0.1
- ZMQ: 3.2.4
- host174:
- Salt: 2014.1.0
- Python: 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
- Jinja2: unknown
- M2Crypto: 0.20.2
- msgpack-python: 0.1.13
- msgpack-pure: Not Installed
- pycrypto: 2.0.1
- PyYAML: 3.10
- PyZMQ: 2.2.0.1
- ZMQ: 3.2.4
如果更换版本,可以到这里下载http://download.opensuse.org/repositories/home:/fengshuo:/zeromq/CentOS_CentOS-5/x86_64/ ,centos6版本的epel源里有,也可以在这里找到,只需把centos-5改成centos-6就行了。
四、命令执行
1、测试master和minion之间的通信是否正常
- [root@localhost minions]# salt '*' test.ping
- host172:
- True
- host174:
- True
True代表正常,*代表所有主机,也可以选择单台或者按组及正则进行匹配等,这个可以参看下官方相关文档 。其默认执行的正则是shell正则,也可以使用其他正则或组等,如下:
- salt 'shell正则' 命令
- salt -E 'prel 正则'
- salt -N $group 命令
- salt -L 'server_id1,server_id2,server_id3' 命令
2、执行命令操作
常用的操作类似如下
- salt '*' cmd.run "ab -n 10 -c 2 http://www.google.com/"
- salt '*' grains.ls 查看grains分类
- salt '*' grains.items 查看grains所有信息
- salt '*' grains.item osrelease 查看grains某个信息
- salt '*' cmd.run "/App/nginx/sbin/nginx -v"
命令执行使用cmd.run参数,由于输出内容较多,不再一一列出,这里只列出一台查看nginx版本的:
- [root@localhost minions]# salt '*' cmd.run "/App/nginx/sbin/nginx -v"
- host174:
- Tengine version: Tengine/1.5.2 (nginx/1.2.9)
- host172:
- Tengine version: Tengine/1.4.6 (nginx/1.2.9)