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值匹配