一、安装基础:
1.导入SaltStack仓库key:
1
2
3
|
wget https:
//repo
.saltstack.com
/yum/rhel7/SALTSTACK-GPG-KEY
.pub
rpm --
import
SALTSTACK-GPG-KEY.pub
rm
-f SALTSTACK-GPG-KEY.pub
|
2.创建新的YUM源文件并“/etc/yum.repos.d/saltstack.repo”编辑如下内容
1
2
3
4
5
6
|
[saltstack-repo]
name=SaltStack repo
for
RHEL
/CentOS
7
baseurl=https:
//repo
.saltstack.com
/yum/rhel7
enabled=1
gpgcheck=1
gpgkey=https:
//repo
.saltstack.com
/yum/rhel7/SALTSTACK-GPG-KEY
.pub
|
3.安装SaltStack软件
1
2
3
4
5
|
yum clean expire-cache
yum update
安装salt-minion, salt-master
yum
install
salt-master
yum
install
salt-minion
|
4.修改配置文件
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
|
minion:
vi
/etc/salt/minion
master: 11.1.0.44
cat
/etc/salt/minion
|
grep
"^ master"
master: 11.1.0.44
打开该文件,这里有两项是必须配置项。其一、找到如下行
# resolved, thenthe minion will fail to start.
#master: salt
在其下增加master主机的配置
# resolved, then the minion will fail to start.
#master: salt
master: 11.1.0.44
这里直接配置的是IP地址,也可以配置成主机名,如果配置成主机名的话,需要在
/etc/hosts
文件中master主机对应的IP ,
如果使用内部DNS的例外,可以在内部DNS上的统一配置。
其二、找到如下段部分
Rejected Keys:在其下增加一行内容
id
: host111
这里是指定当前主机的
id
号,这在后面master认证和master调用命令执行时显示的名称,可以根据实际识别需要填写。
另外需要注意的是,以上两处配置冒号后面都需要有一个空格,不然会报如下错误:
master:
vi
/etc/salt/master
interface: 11.1.0.44
cat
/etc/salt/master
|
grep
"^ interface"
interface: 11..1.0.44
注意:master和interface前面有两个空格,如果没有启动的时候回出现错误;
|
5.启动服务:
1
2
3
4
5
6
|
Master
chkconfig salt-master on
systemctl restart salt-master.service
Minion
chkconfig salt-minion on
systemctl restart salt-minion.service
|
6,测试saltstack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
查看minion列表:
[root@salt-master~]
# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
host111
Rejected Keys:
接收所有key:
salt-key -A
在这个过程中会提示Y确认,确认即可;
显示结果:
Key
for
minion host111 accepted.
[root@salt-master ~]
# salt-key -L
Accepted Keys:
host111
Denied Keys:
Unaccepted Keys:
Rejected Keys:
|
如果对客户端信任,可以让master自动接受请求,在master端/etc/salt/master配置
1
|
auto_accept: True
|
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
28
29
30
31
32
33
34
35
36
37
38
39
40
|
注:可以通过以下方法确认具体的版本:
[root@salt-master ~]
# salt '*' test.versions_report
host111:
Salt Version:
Salt: 2016.3.4
Dependency Versions:
cffi: Not Installed
cherrypy: Not Installed
dateutil: Not Installed
gitdb: Not Installed
gitpython: Not Installed
ioflo: Not Installed
Jinja2: 2.7.2
libgit2: Not Installed
libnacl: Not Installed
M2Crypto: Not Installed
Mako: Not Installed
msgpack-pure: Not Installed
msgpack-python: 0.4.6
mysql-python: Not Installed
pycparser: Not Installed
pycrypto: 2.6.1
pygit2: Not Installed
Python: 2.7.5 (default, Nov 20 2015, 02:00:19)
python-gnupg: Not Installed
PyYAML: 3.11
PyZMQ: 15.3.0
RAET: Not Installed
smmap: Not Installed
timelib: Not Installed
Tornado: 4.2.1
ZMQ: 4.1.4
System Versions:
dist: centos 7.2.1511 Core
machine: x86_64
release: 3.10.0-327.el7.x86_64
system: Linux
version: CentOS Linux 7.2.1511 Core
|
二、命令执行
1、测试master和minion之间的通信是否正常
1
|
[root@salt-master ~]
# salt '*' test.ping
|
1
2
3
4
5
6
7
8
|
host111:
True
True代表正常,*代表所有主机,也可以选择单台或者按组及正则进行匹配等,
这个可以参看下官方相关文档。其默认执行的正则是shell正则,也可以使用其他正则或组等,如下:
salt
'shell正则'
命令
salt -E
'prel 正则'
salt -N $group 命令
salt -L
'server_id1,server_id2,server_id3'
命令
|
2、执行命令操作
1
2
3
4
5
6
7
8
9
10
|
常用的操作类似如下
salt
'*'
cmd.run
"ab -n 10 -c 2 http://www.111cn.net/"
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@salt-master ~]
# salt '*' cmd.run 'nginx -v'
host111:
nginx version: nginx
/1
.10.2
|
好了至此我们的saltstack 基本使用方法已经给出,后续会演示更深层次像函数的用法
本文转自 sjfbjs 51CTO博客,原文链接:http://blog.51cto.com/11886896/1874751