当前位置:网站首页>saltstack学习1入门基础
saltstack学习1入门基础
2022-07-30 11:32:00 【daydayup9527】
saltstack
运维工具介绍
ansible也是python写的。ansible完全基于ssh协议通信简单些。
puppet ruby语言写的,国外使用较多
Saltstack是c/s架构集中化管理平台,可以在成千上万台服务器上做到批量执行命令,根据不同业务特性进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理等。基于Python语言实现,结合轻量级消息队列(ZeroMQ) 与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、 python-msgpack和PyYAML等)构建。
https://repo.saltproject.io/
salt-master-3004.2-1.el7.noarch.rpm
salt-minion-3004.2-1.el7.noarch.rpm
zeromq-4.1.4-7.el7.x86_64.rpm(普遍使用,新版本还可以使用别的方式)
Saltstack运行模式
Master/ Minion:通过server/agent的方式进行管理,效率很高(批量管理1000台机器,25秒搞定。
Salt SSH:通过SSH方式进行管理,效率相对来说比较低(批量管理1000台机器,83秒搞定。
Local:本地,一台机器玩,不建议。
Saltstack特征
1)部署简单、方便。
2)支持大部分UNIX/Linux 及 Windows环境。
3)主从集中化管理。
4)配置简单、功能强大、扩展性强;。
5)主控端(master)和被控端(minion)基于证书认证,安全可靠。
6)支持API及自定义模块,可通过Python轻松扩展。
Saltstack服务安装
[[email protected] ~]# cat /etc/hosts
192.168.1.11 master
192.168.1.12 node1
装包
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] ~]# wget https://mirrors.aliyun.com/repo/Centos-7.repo
[[email protected] ~]# sudo rpm --import https://repo.saltproject.io/py3/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub
[[email protected] ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/7/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
#访问https://repo.saltproject.io/#rhel
[[email protected] ~]# yum -y install salt-master salt-minion
[[email protected] ~]# yum -y install salt-minion
起master服务
[[email protected] ~]# systemctl enable salt-master --now
[[email protected] ~]# ss -antup |grep salt #或python,版本可能不一样
tcp LISTEN 0 128 *:4505 *:* users:(("salt-master"...
tcp LISTEN 0 128 *:4506 *:* users:(("salt-master"...
[[email protected] ~]# ls /etc/salt/
master minion pki ...
起minion服务
[[email protected] ~]# vim /etc/salt/minion
16 master: 192.168.1.11 #master也可以是域名形式
112 #id: #默认是fqdn主机名,启动后改主机名会受影响,也可以定义为ip
[[email protected] ~]# systemctl enable salt-minion --now
[[email protected] ~]# cat /etc/salt/minion_id
master
Master 与Minion认证。
[[email protected] ~]# tree /etc/salt/pki/
/etc/salt/pki/
├── master
└── minion
[[email protected] ~]# vim /etc/salt/minion #同上面master修改
[[email protected] ~]# systemctl enable salt-minion --now
1) minion在第一次启动时,会在/etc/salt/pki/minion/公、私钥,然后将 minion.pub发送给master。
[[email protected] ~]# tree /etc/salt/pki/
/etc/salt/pki/
├── master
└── minion #minion启动后生成
├── minion.pem #私钥
└── minion.pub #公钥,将发送给master
2)master在接收到minion 的公钥后,放入/etc/salt/pki/master/minions_pre
[[email protected] ~]# tree /etc/salt/pki/
/etc/salt/pki/
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions #接收到公钥的目录,同意后将从minions_pre目录移动到此目录
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre #接收到公钥的目录,此时未同意,以client的主机名命名,并写入
│ │ ├── master #根据主机名命名去,主机名改动会找不到对应的主机
│ │ └── node1
[[email protected] ~]# cat /etc/salt/pki/master/minions_pre/node1
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAO...
[[email protected] ~]# cat /etc/salt/pki/minion/minion.pub
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAO...
3)通过salt-key命令接收,master将接收的公钥放入/etc/salt/pki/master/minions目录
[[email protected] ~]# salt-key #查看未接收的key
[[email protected] ~]# salt-key -a node* #接收认证,minion发给master的,支持通配符
[[email protected] ~]# salt-key -A #接收所有收到的公钥
The following keys are going to be accepted:
Unaccepted Keys:
master
node1
Proceed? [n/Y] y
同时将master的公钥发给minion,然后master就能对minion通信发送指令了。
[[email protected] ~]# tree /etc/salt/pki/
/etc/salt/pki/
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions #同意后记录minion.pub信息的文件将从minions_pre目录移动到此目录
│ │ ├── master
│ │ └── node1
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre #此时空了
│ └── minions_rejected
└── minion
├── minion_master.pub #minion被接收后将收到master的公钥,以便通信。AES加密,通信安全。
├── minion.pem
└── minion.pub
Master 与 Minion的连接
Saltstack master启动后默认监听4505和4506两个端口。
4505( publish port)为saltstack的消息发布系统,
4506(ret_port)为saltstack客户端与服务端通信的端口,接收。
使用lsof查看4505端口,会发现所有的minion在4505端口持续保持在ESTABLISHED状态。
[[email protected] ~]# ss -antup |grep 4505
[[email protected] ~]# lsof -i:4505
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
salt-mast 11141 root 18u IPv4 36496 0t0 TCP *:4505 (LISTEN)
salt-mast 11141 root 20u IPv4 85634 0t0 TCP master:4505->master:45760 (ESTABLISHED)
salt-mast 11141 root 21u IPv4 86455 0t0 TCP master:4505->node1:36432 (ESTABLISHED)
salt-mini 13968 root 22u IPv4 85633 0t0 TCP master:45760->master:4505 (ESTABLISHED)
检查通信
[[email protected] ~]# salt '*' test.ping
master:
True
node1:
True
注意如果你更改了minion的主机名,需要重新加入
[[email protected] ~]# systemctl stop minion
[[email protected] ~]# salt-key -d node1
[[email protected] ~]# rm -rf /etc/salt/minion_id
[[email protected] ~]# rm -rf /etc/salt/pki
[[email protected] ~]# systemctl start minion
配置salt的执行环境
[[email protected] ~]# vim +677 /etc/salt/master
...
file_roots:
base:
- /srv/salt/base
dev:
- /srv/salt/dev
test:
- /srv/salt/test
prod:
- /srv/salt/prod
[[email protected] ~]# mkdir -p /srv/salt/{base,dev,test,prod}
[[email protected] ~]# systemctl restart salt-master
[[email protected] ~]# mkdir /srv/salt/base/web
[[email protected] ~]# cd /srv/salt/base/web
[[email protected] web]# vim apache.sls
apache-install: #任务名全局唯一
pkg.installed: #pkg模块.方法
- name: httpd #包名
apache-service:
service.running: #service模块.方法
- name: httpd
- enable: True
[[email protected] web]# salt 'node1' state.sls web.apache
state.sls 远程执行的模块.方法
web.apache 目录名.sls结尾的文件名.目录可以不存在,默认是base环境下
默认执行是base环境下配置,加入saltenv=目录名,修改执行环境
[[email protected] web]# salt 'node1' state.sls web.apache saltenv=dev #改变环境
node1:
----------
ID: apache-install #apache.sls里的任务名
Function: pkg.installed #apache.sls里的模块方法
Name: httpd # #apache.sls里的包名
Result: True #返回值,成功
Comment: The following packages were installed/updated: httpd
Started: 13:17:12.775033
Duration: 15316.973 ms
Changes:
----------
apr:
----------
new:
1.4.8-7.el7
old:
apr-util:
----------
new:
1.5.2-6.el7
old:
httpd:
----------
new:
2.4.6-97.el7.centos.5
old:
httpd-tools:
----------
new:
2.4.6-97.el7.centos.5
old:
mailcap:
----------
new:
2.1.41-2.el7
old:
----------
ID: apache-service #apache.sls里的任务名
Function: service.running #apache.sls里的模块方法
Name: httpd #apache.sls里的模块里的相关参数信息
Result: True
Comment: Service httpd has been enabled, and is running
Started: 13:17:28.121745
Duration: 328.4 ms
Changes:
----------
httpd:
True
Summary for node1
------------
Succeeded: 2 (changed=2)
Failed: 0
------------
Total states run: 2
Total run time: 15.645 s
与ansible相似的是saltstack也具有幂等性
[[email protected] web]# grep top.sls /etc/salt/master
#state_top: top.sls #base环境默认的入口文件,一般不更改
[[email protected] base]# cat top.sls
base: #定义环境
'node*': #定义执行目标,支持正则
- web.apache #执行内容 目录名.目录下sls文件名,目录可以不存在
[[email protected] base]# salt '*' state.highstate
#这里的*是通知所有的节点,top.sls里的*定义要执行的节点,不包括的节点会显示红色,不执行
#测试参数test=True ,打印执行,但不会执行
[[email protected] base]# salt '*' state.highstate test=True
salt-cp命令
[[email protected] ~]# salt-cp '*' /etc/passwd /tmp/passwd
node1:
----------
/tmp/passwd:
True
[[email protected] ~]# salt '*' cmd.run 'ls /tmp/passwd'
node1:
/tmp/passwd
[[email protected] ~]# salt '*' pkg.install httpd
远程执行返回 salt.runners
[[email protected] ~]# salt '*' cmd.run 'uptime' -v
Executing job with jid 20220727134419729947 #显示jid信息
node1:
21:44:19 up 6:58, 2 users, load average: 0.00, 0.01, 0.05
[[email protected] ~]# salt-run jobs.lookup_jid 20220727134419729947 #查找jid任务信息
node1:
21:44:19 up 6:58, 2 users, load average: 0.00, 0.01, 0.05
[[email protected] ~]# vim /etc/salt/master #cache默认24小时。keep_jobs: 24
salt-ssh命令 类似ansible
https://docs.saltproject.io/en/latest/topics/ssh/index.html
[[email protected] ~]# yum -y install salt-ssh
[[email protected] ~]# cat /etc/salt/roster #类似于ansible的hosts文件
node1:
host: 192.168.1.12
user: root
passwd: 1
node2:
...
[[email protected] ~]# salt-ssh '*' test.ping -i #第一次连接加上-i,后面不需要了
[[email protected] ~]# salt-ssh '*' test.ping
node1:
True
边栏推荐
猜你喜欢
随机推荐
SQL 根据时间范围查询数据
TensorFlow custom training function
2022-07-29 顾宇佳 学习笔记 异常处理
Typroa 替代工具marktext
深入浅出零钱兑换问题——背包问题的套壳
概率论的学习整理--番外2:和二项式,组合相关的杨辉三角
PanGu-Coder: Function-level code generation model
SCM engineers written questions induction summary
向上管理读书笔记
概率论的学习和整理7:理解期望和方差还是要回到随机试验本身,期望不是平均值,方差的公式不同情况不同
LCD1602 display experiment developed by single chip microcomputer
How to add data to the request header when feign is called remotely
Microsoft SQL server hacked, bandwidth stolen
ansible学习笔记01
contentDocument contentWindow, canvas, svg, iframe
Matlab基础(5)——符号运算
STM32F1读取MLX90632非接触式红外温度传感器
C#调用explorer.exe打开指定目录
听到'演员工作比工人辛苦,吃得也不如工人好?'我笑了
电流继电器JL-8GB/11/AC220V
![[ASP.NET Core] Dependency Injection for Option Classes](/img/3f/820b6e33897cf385c3206c02d741f8.png)








