Saltstack


Saltstack

简介

SaltStack是一个服务器基础架构集中化管理平台,具备配置管理、远程执行、监控等功能,一般可以理解为简化版的puppet和加强版的func。SaltStack基于Python语言实现,结合轻量级消息队列(ZeroMQ)与Python第三方模块(Pyzmq、PyCrypto、Pyjinjia2、python-msgpack和PyYAML等)构建。通过部署SaltStack环境,我们可以在成千上万台服务器上做到批量执行命令,根据不同业务特性进行配置集中化管理、分发文件、采集服务器数据、操作系统基础及软件包管理等,SaltStack是运维人员提高工作效率、规范业务配置与操作的利器。

特点

  • 基于python开发的C/S架构配置管理工具
  • 底层使用ZeroMQ消息队列pub/sub方式通信
  • 使用SSL证书签发的方式进行认证管理,传输采用AES加密

服务架构

在saltstack架构中服务器端叫Master,客户端叫Minion。

在Master和Minion端都是以守护进程的模式运行,一直监听配置文件里面定义的ret_port(接受minion请求)和publish_port(发布消息)的端口。

当Minion运行时会自动连接到配置文件里面定义的Master地址ret_port端口进行连接认证。

saltstack除了传统的C/S架构外,其实还有一种叫做masterless的架构,其不需要单独安装一台 master 服务器,只需要在每台机器上安装 Minion端,然后采用本机只负责对本机的配置管理机制服务的模式。

四大功能与四大运行方式

SaltStack有四大功能,分别是:

  • 远程执行
  • 配置管理/状态管理
  • 云管理(cloud)
  • 事件驱动

SaltStack可以通过远程执行实现批量管理,并且通过描述状态来达到实现某些功能的目的。

SaltStack四大运行方式: - local本地运行 - Master/Minion传统方式 - Syndic分布式 - Salt ssh

控制端

关闭防火墙SELINUX
[root@master ~]# systemctl disable --now firewalld.service 
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@master ~]# vim /etc/selinux/config 
[root@master ~]# setenforce 0

配置yum源
[root@master ~]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@master ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo          CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo              CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo             CentOS-Linux-Sources.repo
[root@master ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 8 PY3
baseurl=https://repo.saltproject.io/py3/redhat/8/x86_64/latest
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=1
gpgkey=https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@master ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo          CentOS-Linux-HighAvailability.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-Media.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Plus.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-PowerTools.repo
CentOS-Linux-Devel.repo              CentOS-Linux-Sources.repo
CentOS-Linux-Extras.repo             salt.repo
CentOS-Linux-FastTrack.repo

安装saltstack主控端
[root@master ~]# dnf -y install salt-minion salt-master
[root@master ~]# cd /etc/salt/
[root@master salt]# ls
cloud         cloud.deploy.d  cloud.profiles.d   master    minion    pki    proxy.d
cloud.conf.d  cloud.maps.d    cloud.providers.d  master.d  minion.d  proxy  roster
[root@master salt]# vim minion
#master: salt                       
master: 192.168.145.188                 添加此内容

启动服务
[root@master salt]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128                  [::]:22                 [::]:*                
[root@master salt]# systemctl enable --now salt-master
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /usr/lib/systemd/system/salt-master.service.
[root@master salt]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128               0.0.0.0:4505            0.0.0.0:*                
LISTEN   0        128               0.0.0.0:4506            0.0.0.0:*                
LISTEN   0        128                  [::]:22                 [::]:*                
[root@master salt]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.
[root@master salt]# ss -antl
State    Recv-Q   Send-Q      Local Address:Port       Peer Address:Port   Process   
LISTEN   0        128               0.0.0.0:22              0.0.0.0:*                
LISTEN   0        128               0.0.0.0:4505            0.0.0.0:*                
LISTEN   0        128               0.0.0.0:4506            0.0.0.0:*                
LISTEN   0        128                  [::]:22                 [::]:*         

客服端

关闭防火墙和selinux
[root@localhost ~]# systemctl disable --now firewalld.service
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# vim /etc/selinux/config
[root@localhost ~]# setenforce 0

配置yum源
[root@localhost ~]# rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo          CentOS-Linux-FastTrack.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-HighAvailability.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Media.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-Plus.repo
CentOS-Linux-Devel.repo              CentOS-Linux-PowerTools.repo
CentOS-Linux-Extras.repo             CentOS-Linux-Sources.repo
[root@localhost ~]# curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo
[salt-latest-repo]
name=Salt repo for RHEL/CentOS 8 PY3
baseurl=https://repo.saltproject.io/py3/redhat/8/x86_64/latest
skip_if_unavailable=True
failovermethod=priority
enabled=1
enabled_metadata=1
gpgcheck=1
gpgkey=https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub
[root@localhost ~]# ls /etc/yum.repos.d/
CentOS-Linux-AppStream.repo          CentOS-Linux-HighAvailability.repo
CentOS-Linux-BaseOS.repo             CentOS-Linux-Media.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-Plus.repo
CentOS-Linux-Debuginfo.repo          CentOS-Linux-PowerTools.repo
CentOS-Linux-Devel.repo              CentOS-Linux-Sources.repo
CentOS-Linux-Extras.repo             salt.repo
CentOS-Linux-FastTrack.repo

安装saltstack被控端
[root@localhost ~]# dnf -y install salt-minion
[root@localhost ~]# cd /etc/salt/
[root@localhost salt]# ls
cloud         cloud.deploy.d  cloud.profiles.d   master    minion    pki    proxy.d
cloud.conf.d  cloud.maps.d    cloud.providers.d  master.d  minion.d  proxy  roster
[root@localhost salt]# vim minion
#master: salt
master: 192.168.145.188                 master主机IP

认证机制

saltstack主控端是依靠openssl证书来与受控端主机认证通讯的,受控端启动后会发送给主控端一个公钥证书文件,在主控端用salt-key命令来管理证书

salt-minion与salt-master的认证过程:

  • minion在第一次启动时,会在/etc/salt/pki/minion/下自动生成一对密钥,然后将公钥发给master

常用选项

    -L      //列出所有公钥信息
    -a minion    //接受指定minion等待认证的key
    -A      //接受所有minion等待认证的key
    -r minion    //拒绝指定minion等待认证的key
    -R      //拒绝所有minion等待认证的key
    -f minion   //显示指定key的指纹信息
    -F      //显示所有key的指纹信息
    -d minion   //删除指定minion的key
    -D      //删除所有minion的key
    -y      //自动回答yes

认证过程

查看
[root@localhost salt]# tree pki
pki
|-- master
`-- minion

2 directories, 0 files

启动服务并设置开机自启
[root@localhost salt]# systemctl enable --now salt-minion
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /usr/lib/systemd/system/salt-minion.service.

再次查看
[root@localhost salt]# tree pki
pki
|-- master
`-- minion
    |-- minion.pem
    `-- minion.pub

2 directories, 2 files
  • master收到minion的公钥后,通过salt-key命令接受该公钥。此时master的/etc/salt/pki/master/minions目录将会存放以minion id命名的公钥,然后master就能对minion发送控制指令了
查看当前证书情况
[root@master ~]# salt-key -L
Accepted Keys:          已接收的key
Denied Keys:            已拒绝的key
Unaccepted Keys:        没有接受(待处理)的key
192.168.145.190
master
Rejected Keys:          已拒绝的key

接受指定minion的新证书
[root@master ~]# salt-key -a 192.168.145.190
The following keys are going to be accepted:
Unaccepted Keys:
192.168.145.190
Proceed? [n/Y] y
Key for minion 192.168.145.190 accepted.
[root@master ~]# salt-key -L
Accepted Keys:
192.168.145.190
Denied Keys:
Unaccepted Keys:
master
Rejected Keys:

[root@localhost salt]# tree pki
pki
|-- master
`-- minion
    |-- minion.pem
    |-- minion.pub
    `-- minion_master.pub

2 directories, 3 files

接受所有minion的新证书
[root@master ~]# salt-key -yA
The following keys are going to be accepted:
Unaccepted Keys:
master
Key for minion master accepted.
[root@master ~]# salt-key -L
Accepted Keys:
192.168.145.190
master
Denied Keys:
Unaccepted Keys:
Rejected Keys:

远程执行

[root@master ~]# salt '192.168.145.190' test.ping
192.168.145.190:
    True
[root@master ~]# salt '192.168.145.190*' test.ping
192.168.145.190:
    True
[root@master ~]# salt '192*' test.ping
192.168.145.190:
    True
[root@master ~]# salt '*' test.ping
master:
    True
192.168.145.190:
    True
[root@master ~]# salt -S '192.168.145.0/24' test.ping
master:
    True
192.168.145.190:
    True
[root@master ~]# salt '192.168.145.190'  cmd.run hostname
192.168.145.190:
    localhost.localdomain

命令使用

语法:salt [options] '<target>' <function> [arguments]

常用的options
    --version       //查看saltstack的版本号
    --versions-report   //查看saltstack以及依赖包的版本号
    -h      //查看帮助信息
    -c CONFIG_DIR   //指定配置文件目录(默认为/etc/salt/)
    -t TIMEOUT      //指定超时时间(默认是5s)
    --async     //异步执行
    -v      //verbose模式,详细显示执行过程
    --username=USERNAME     //指定外部认证用户名
    --password=PASSWORD     //指定外部认证密码
    --log-file=LOG_FILE     //指定日志记录文件

常用target参数
    -E      //正则匹配
    -L      //列表匹配 
    -S      //CIDR匹配网段
    -G      //grains匹配
    --grain-pcre    //grains加正则匹配
    -N      //组匹配
    -R      //范围匹配
    -C      //综合匹配(指定多个匹配)
    -I      //pillar值匹配


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM