SaltStack是一个开源的、新的基础平台管理工具,使用Python语言开发,同时提供Rest API方便二次开发以及和其他运维管理系统进行集成。相对于出道比较早的Puppet,SaltStack先天的优势就是简单、易用,可以非常快速的在团队中推广和使用,而且运行多平台。
192.168.16.130 master
192.168.16.193 minion
hostnamectl set-hostname master
hostnamectl set-hostname minion1
iptables -F #清空防火墙规则
systemctl stop firewalld #关闭防火强服务
systemctl disable firewalld #禁止防火墙开机自启
master
机器上装yum install salt-master -y
minion
机器上装yum install salt-minion -y
192.168.16.190 master
192.168.16.193 minion1
[root@master ~]# grep -vE "^$|#" /etc/salt/master
interface: 0.0.0.0
publish_port: 4505
user: root
worker_threads: 5
ret_port: 4506
[root@minion1 ~]# grep -vE "^$|#" /etc/salt/minion
master: master
master_port: 4506
user: root
id: minion1
[root@master ~]# systemctl start salt-master
[root@minion1 ~]# systemctl start salt-minion
在master上输入 salt-key -L
查看是否读取到了minion的信息
[root@master ~]# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
minion1
Rejected Keys:
salt-key -f minion1
[root@master ~]# salt-key -f minion1
Unaccepted Keys:
minion1: 4f:51:a8:33:9d:65:8d:81:0b:84:6f:08:f7:b6:23:9c
salt-call --local key.finger
[root@minion1 ~]# salt-call --local key.finger
local:
4f:51:a8:33:9d:65:8d:81:0b:84:6f:08:f7:b6:23:9c
minion1
的密钥,使用 salt-key -a minion1
进行管理[root@master ~]# salt-key -a minion1
The following keys are going to be accepted:
Unaccepted Keys:
minion1
Proceed? [n/Y] y
Key for minion minion1 accepted.
salt ‘*‘ test.ping
测试服务器是否存活[root@master ~]# salt '*' test.ping
minion1:
True
salt '*' test.ping #发送命令接口,ping一下被管理的机器,是否存活,返回true活着,负责挂掉了,或者salt-minion 服务器宕机了
salt # 是核心管理命令
"*" # 目标匹配
test.ping # 是test模块下的一个功能函数
salt-key 参数如下
-L #查看KEY状态
-A #允许所有
-D #删除所有
-a #认证指定的key
-d #删除指定的key
-r #注销掉指定key(该状态为未被认证)
test.fib
生成斐波那契数列[root@master ~]# salt 'minion2' test.fib 50
minion2:
|_
- 0
- 1
- 1
- 2
- 3
- 5
- 8
- 13
- 21
- 34
- 2.14576721191e-06
salt ‘*‘ cmd.run ‘touch /tmp/一给我里giao‘ --summary
输出salt命令执行结果的详细情况[root@master ~]# salt 'minion2' cmd.run 'touch /tmp/一给我里giao' --summary
[root@minion1 tmp]# ls
vmware-root 一给我里giao
[root@master ~]# salt '*' cmd.run "systemctl status nginx"
minion2:
Unit nginx.service could not be found.
ERROR: Minions returned with non-zero exit code
[root@master ~]# salt 'minion*' cmd.run "yum install nginx -y"
minion1:
Loaded plugins: fastestmirror
Repository epel is listed more than once in the configuration
Repository epel-debuginfo is listed more than once in the configuration
Repository epel-source is listed more than once
.
.
.
[root@master ~]# salt '*' cmd.run "systemctl status nginx"
minion1:
* nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Dec 25 22:13:21 minion1 systemd[1]: Unit nginx.service cannot be reloaded because it is inactive.
ERROR: Minions returned with non-zero exit code
minion1
上的信息:
[root@minion2 tmp]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Dec 25 22:13:21 minion2 systemd[1]: U
[root@master 192.168.11.72 ~]$salt '*' pkg.install "nginx"
#卸载minion上的nginx
[root@master 192.168.11.72 ~]$salt '*' pkg.remove "nginx"
#检查pkg包的版本
[root@master 192.168.11.72 ~]$salt '*' pkg.version "nginx"
[root@master ~]# salt '*' service.start "redis"
minion1:
True
[root@master ~]# salt '*' service.status "redis"
minion1:
True
[root@master ~]# salt '*' service.status "redis" --out=json
{
"minion1": true
}
[root@master ~]# salt '*' grains.item fqdn_ip4 --out=json
{
"minion2": {
"fqdn_ip4": [
"192.168.16.56"
]
}
}
原文:https://www.cnblogs.com/zhufanyu/p/12101123.html