Saltstack的yum部署和基础使用(基于CentOS 7的部署)

salt-master 192.168.81.11 centOS 7.3
salt-minions1 192.168.81.12 centOS 7.3
salt-minions2 192.168.81.60 centOS 7.0

关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
配置yum源
vi /etc/yum.repo.d/saltstack.repo
[saltstack-repo]
name=SaltStack repo for Red Hat Enterprise Linux $releasever
baseurl=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest
enabled=1
gpgcheck=1
gpgkey=https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/SALTSTACK-GPG-KEY.pub
https://repo.saltstack.com/yum/redhat/$releasever/$basearch/latest/base/RPM-GPG-KEY-CentOS-7
安装SaltStack存储库和密钥
yum install https://repo.saltstack.com/yum/redhat/salt-repo-latest-1.el7.noarch.rpm
yum clean expire-cache
yum upgrade
yum -y install epel-release
解决依赖关系
yum install zeromq3 m2crypto python-crypto python-jinja2 python-msgpack python-yaml python-zmq -y
salt-master安装
yum install -y salt-master

修改配置文件

vim /etc/salt/master
#master消息发布端口 Default: 4505
publish_port: 4505
#工作线程数,应答和接受minion Default: 5
worker_threads: 100
#客户端与服务端通信的端口 Default: 4506
ret_port: 4506
#自动接受所有客户端
auto_accept: True
#启动salt-master
systemctl start salt-master
#检查是否启动成功
netstat -tnlp
tcp        0      0 0.0.0.0:4505            0.0.0.0:*              LISTEN      20839/python
tcp        0      0 0.0.0.0:4506            0.0.0.0:*              LISTEN      20845/python
salt-minions1 & salt-minions2 安装
yum install -y salt-minion
#修改配置文件
vim /etc/salt/minion
# master IP或域名
master: 192.168.81.11
# 客户端与服务端通信的端口。 Default: 4506
master_port: 4506
# id minion的唯一标示。Default: hostname
id: minion_192.168.81.XX
启动minion
systemctl start salt-minion
salt-master查看(master接受minion的minion.pub ,存放/etc/salt/pki/master/minions目录,以minion的id命名)
[root@controller ~]# ls /etc/salt/pki/master/
master.pem  minions          minions_denied  minions_rejected
master.pub  minions_autosign  minions_pre
salt-minion查看(minion将master的public key,存放/etc/salt/pki/minion目录)
[root@compute01 ~]# ls /etc/salt/pki/minion
minion_master.pub  minion.pem  minion.pub
[root@localhost ~]# ls /etc/salt/pki/minion
minion.pem  minion.pub
查看master的key列表
[root@controller ~]# salt-key -L
Accepted Keys:(接受的key)
minion_192.168.81.12
minion_192.168.81.60
Denied Keys:(否认的key)
Unaccepted Keys:(未接受的key)
Rejected Keys:(拒绝的key)

salt-key常用参数说明:

格式:salt-key 参数 [minion端ID(可以是IP,也可以是主机名) [-y]
-L 列出当前所有认证,包括Accepted Keys、Denied Keys、Unaccepted Keys、Rejected Keys
-a 添加某个或某些个未接受(Unaccepted Keys)认证
-A 添加所有未接受(Unaccepted Keys)认证
-d 删除某个或某些个已接受(Accepted Keys)认证
-D 删除所有已接受(Accepted Keys)认证
-y 使用该参数可免去证书操作的交互,除非对minion端很信任,一般不建议使用
-h 帮助
[root@controller ~]# salt '*' test.ping
minion_192.168.81.12:
True
minion_192.168.81.60:
True
查看grains分类:
salt '*' grains.ls
#查看grains所有信息:
salt '*' grains.items
#查看grains某个信息(系统版本):
salt '*' grains.item osrelease
[root@controller ~]# salt '*' grains.item osrelease
minion_192.168.81.12:
----------
osrelease:
7.3.1611
minion_192.168.81.60:
----------
osrelease:
7.3.1611
在minion上自定义grains信息

编辑minion配置文件/etc/salt/minion或创建新文件/etc/salt/grains文件
编辑后重启salt-minion

[root@compute01 ~]# vim /etc/salt/grains
test:
- test1
[root@compute01 ~]# systemctl restart salt-minion
[root@localhost ~]# vim /etc/salt/grains
test:
- test2
[root@localhost ~]# systemctl restart salt-minion
[root@controller ~]# salt '*' grains.item test
minion_192.168.81.12:
----------
test:
- test1
minion_192.168.81.60:
----------
test:
- test2
匹配grains 某个信息
[root@controller ~]# salt -G 'os:CentOS' test.ping
minion_192.168.81.12:
True
minion_192.168.81.60:
True

自定义节点组到配置文件里,通过一个组进行操作:

$cat /etc/salt/master.d/nodegroups.conf
nodegroups:
test1: 'L@test1,test2,test3 or *.test.com'
test2: 'G@os:CentOS and *.test.com'
test3: '* and not G@os:CentOS'

用法:

salt -N 'test1' test.ping
#分批执行,每次2个
salt '*' -b 2 test.ping
#分批执行,每次总共的25%
salt '*' -b 25% test.ping
[root@controller ~]# salt '*' cmd.run 'uptime'
minion_192.168.81.12:
22:33:57 up  2:55,  2 users,  load average: 0.00, 0.01, 0.05
minion_192.168.81.60:
22:45:21 up  6:00,  3 users,  load average: 0.00, 0.01, 0.05

cmd.run的使用

salt '*' cmd.run_all 'uptime'
#执行命令并只返回错误输出
salt '*' cmd.run_stderr 'uptime'
#执行命令并只返回正确输出
salt '*' cmd.run_stdout 'uptime'
#上传本地脚本到服务器上执行,返回所有信息
#创建脚本路径:vim /srv/salt/scripts/test1.sh
salt '*' cmd.script salt://scripts/test1.sh```

你可能感兴趣的:(Saltstack的yum部署和基础使用(基于CentOS 7的部署))