本文共 4423 字,大约阅读时间需要 14 分钟。
SaltStack由Master和Minion构成,Master 是服务端, 表示一台服务器;Minion是客户服务端,表示多台服务器。在Master上发送命令给符合条件的Minion,Minin就会执行相应的命令,Master和Minion之间是通过ZeroMQ (消息队列)进行通信的。
SaltStack的Master端监听4505与4506端口,4505为Master和Minion认证通信端口,4506为Master用来发送命令或者接收Minion的命令执行返回信息。
当客户端启动后,会主动连接Master端注册,然后一直保持该TCP连接,而Master通过这条TCP连接对客户端进行控制。如果连接断开,Master对客户端将不能进行控制。但是,当客户端检查到连接断开后,会定期向Master端请求注册连接。
角色 | 主机名 | IP地址 |
---|---|---|
master | master | 192.168.10.157 |
minion | web1 | 192.168.10.161 |
minion | web2 | 192.168.10.129 |
master:vim /etc/hostname master //修改主机名vim /etc/hosts192.168.10.157 master192.168.10.161 web1192.168.10.129 web2init 6 //重启
web1:vim /etc/hostname web1 //修改主机名vim /etc/hosts192.168.10.157 master192.168.10.161 web1192.168.10.129 web2init 6 //重启
web2:vim /etc/hostname web2 //修改主机名vim /etc/hosts192.168.10.157 master192.168.10.161 web1192.168.10.129 web2init 6 //重启
yum install epel-release -y
yum install salt-master -y
vim /etc/salt/masterinterface: 192.168.10.157 //15行,改为为本机地址 auto_accept: True //215行,自动认证被控端的认证 file_roots: //416-418行,去掉注释开启 base: - /srv/salt //这个目录默认是没有的,需要创建。pillar_roots: //529-531行, 去掉注释开启 base: - /srv/pillar //这个目录默认是没有的,需要创建。pillar_opts: True //552行,开启pillar功能nodegroups: //710行,组的分类 group1: 'web1' group2: 'web2'
cat /etc/salt/master | grep -v ^$ | grep -v ^#
mkdir /srv/salt mkdir /srv/pillar
systemctl stop firewalld.servicesetenforce 0 //关闭防火墙和安全功能systemctl start salt-master.service 开启服务netstat -ntap | egrep '4505|4506'
yum install salt-minion -y
vim /etc/salt/minionmaster: 192.168.10.157 //16行,指定控制端IPid: web1 //78行,指定本机主机名,web2则为web2
systemctl stop firewalld.servicesetenforce 0 //关闭防火墙和安全功能systemctl start salt-minion.service //开启服务
salt '*' test.pingweb2: Trueweb1: True
vim /srv/salt/top.slsbase: '*': //表示在所有的客户端执行apache模块 - apache
vim /srv/salt/apache.slsapache-service: pkg.installed: - names: - httpd - httpd-devel service.running: - name: httpd - enable: True
systemctl restart salt-master
salt '*' state.highstateweb2:---------- ID: apache-service Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 15:22:08.547203 Duration: 1363.436 ms Changes: ---------- ID: apache-service Function: pkg.installed Name: httpd-devel Result: True Comment: Package httpd-devel is already installed. Started: 15:22:09.910806 Duration: 0.533 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd is already enabled, and is in the desired state Started: 15:22:09.911959 Duration: 360.802 ms Changes: Summary------------Succeeded: 3Failed: 0------------Total states run: 3web1:---------- ID: apache-service Function: pkg.installed Name: httpd Result: True Comment: Package httpd is already installed. Started: 15:22:08.546047 Duration: 1364.252 ms Changes: ---------- ID: apache-service Function: pkg.installed Name: httpd-devel Result: True Comment: Package httpd-devel is already installed. Started: 15:22:09.910497 Duration: 0.516 ms Changes: ---------- ID: apache-service Function: service.running Name: httpd Result: True Comment: Service httpd is already enabled, and is in the desired state Started: 15:22:09.911733 Duration: 375.981 ms Changes: Summary------------Succeeded: 3Failed: 0------------Total states run: 3接上图通过执行结果看到了三个ID,它们相当于三个任务,第一个安装httpd,第二个安装httpd-devel,第三个启动。并且显示三个都成功了,失败为零。
web1:
web2:实验完成
转载于:https://blog.51cto.com/13641879/2155348