Saltstack的安装和配置

1.安装salt
因为系统自带的yum源不支持saltstack安装包的支持,所以需要安装第三方yum源(epel)
# yum -y install epel-release

salt分为主服务器(控制端)和从服务器(被控制端)
控制端安装:
yum -y install salt-master
被控制端安装:
yum -y install salt-master

2.saltstack防火墙配置
在主控服务器上添加TCP 4505,TCP 4506的规则,而在被控制端无需添加防火墙规则,,原理是被控制端直接直接与主控制端的zeromq建立长连接,接收广播的任务信息并执行。具体操作是在主控服务器上添加两条iptables规则:
# iptables -I INPUT -m state --state new -m tcp -p tcp --dport 4505 -j ACCEPT
# iptables -I INPUT -m state --state new -m tcp -p tcp --dport 4506 -j ACCEPT

3. 配置salt的配置文件
master主控端配置:
路径:/etc/salt/master

#指定通信的ip地址
interface:192.168.5.30
#开启自动认证
auto_accept: True
#指定saltstack文件根目录位置
file_roots:
base:
- /srv/salt/

minion被控端配置
路径:/etc/salt/minion

#指定主控端的ip地址
master: 192.168.5.30
user: root
pidfile: /var/run/salt-minion.pid
#指令主控端的ip地址
id: test-1

分别在主控端和被控端对服务进行启用,看saltstack能否自动发现认证。
# salt "*" test.ping
test-1:
True
因为是自动认证,所以服务就重启就直接可以看到是通过的。

4.手动证书认证
salt-key -L 显示已经或未认证的被控制端id
# salt-key -L
Accepted Keys:
test-1
Denied Keys:
Unaccepted Keys:
Rejected Keys:

salt-key -D 删除所以认证主机的id证书
salt-key -d ID 删除单个id证书
salt-key -A 接受所有id证书请求
salt-key -a id 接受单个id证书请求

5.利用saltstack远程执行命令
命令格式:salt '<目标>' <参数> <方法>
查看主机的内存使用情况:
# salt 'test-1' cmd.run "free -m"
test-1:
total used free shared buffers cached
Mem: 980 633 347 3 27 217
-/+ buffers/cache: 388 592
Swap: 1983 0 1983

其中针对<操作目标>,saltstack提供多种方法对被控端主机(id)进行过滤。
1)-E ,--pcre,通过正则表达式进行匹配
# salt -E '^test-*' test.ping
test-1:
True
test-2:
True

2)-L,list,以主机ID名列表的方式进行过滤
# salt -L 'test-1,test-2' grains.item osfullname
test-1:
----------
osfullname:
CentOS
test-2:
----------
osfullname:
CentOS

3)-G,--grain,根据被控主机grains信息进行匹配过滤
# salt -G 'osrelease:6.6' cmd.run 'hostname'
web-1:
Goun-4
test-2:
linux-3
test-1:
linux-2

4)-I,--pillar,根据被控主机的pillar信息进行匹配过滤,格式为“对象名称,对象值”,例如,过滤所有具备‘apache:httpd’pillar值的主机。实:探测具有“nginx:root:/data”信息主机的连通性。


其中pillar属性配置文件如下,详细解释见02章。
nginx:
root:/data

5)-N,--nodegroup,根据主控端master配置文件中的分组进行过滤。以笔者定义的组为例。
vim /etc/salt/master
nodegroups:
test: 'L@test-1,test-2'
web: 'L@web-1'

其中,L@表示后面的主机id格式为列表,即主机id以逗号为分割;G@表示以grain格式描述;S@表示以IP子网或地址格式描述。

例:
# salt -N web test.ping
web-1:
True

6)-C,--compound,根据条件运算符not,and,or去匹配不同规则的主机。
例:探测以test开头并且操作系统为CentOS的主机连通性
# salt -C 'E@^t.* and G@os:CentOS' cmd.run hostname
test-2:
linux-3
test-1:
linux-2

其中,not语句不能作为第一个条件执行,不过可以通过以下方法来规避,示例:探测非test开头
的主机连通性
# salt -C '* and not E@t.*' cmd.run hostname
web-1:
Goun-4

7)-S,--ipcidr,根据被控主机的IP地址或IP子网进行匹配
# salt -S '192.168.5.50' cmd.run hostname
test-2:
linux-3
# salt -S '192.168.5.0/24' cmd.run hostname
test-2:
linux-3
web-1:
Goun-4
test-1:
linux-2

posted @ 2017-09-20 14:47  Goun  阅读(266)  评论(0编辑  收藏  举报