Salt安裝部署
一:salt簡介
? 早期運維人員會根據自己的生產環境來寫特定腳本完成大量重復性作業,這些腳本復雜且難以維護,系統管理員面臨的問題主要是1、系統配置管理,2、遠程執行命令,因此誕生了很多開源軟體,系統維護方面有fabric、puppet、chef、ansible、saltstack等,這些軟體擅長維護系統狀態或方便的對大量主機進行批量的命令執行,
salt靈活性強大,可以進行大規模部署,也能進行小規模的系統部署,salt的設計架構適用于任意數量的服務器,從少量本地網路系統到跨越數個資料中心,拓撲架構都是c/s模型,配置簡單,
不管是幾臺、幾百臺、幾千臺服務器,都可以使用salt在一個中心節點上進行管控,靈活定位任意服務器子集來運行命令,
Salt是python撰寫的,支持用戶通過python自定義功能模塊,也提供了大量的python API介面,用戶可以根據需要進行簡單快速的擴展,
官方中文檔案網站:http://docs.saltstack.cn/
官方安裝配置教程: http://docs.saltstack.cn/topic
github:https://github.com/saltstack/salt
二:背景環境
2.1:部署情況
| 機器IP/HOST | 安裝內容 | |
|---|---|---|
| 192.168.0.110 | master端(主控端) | |
| 192.168.0.101 | minion端(被控端) | |
| 192.168.0.102 | minion端(被控端) | |
| 192.168.0.103 | minion端(被控端) |
2.2:配置hosts檔案,用于加速域名決議,
# 服務端
[root@manager110 ~]# vim /etc/hosts
192.168.0.101 db101
192.168.0.102 db102
192.168.0.103 db103
192.168.0.110 manager110
# 客戶端: 所有機器101-103都相同操作
[root@db101 ~]# vim /etc/hosts
192.168.0.101 db101
192.168.0.102 db102
192.168.0.103 db103
192.168.0.110 manager110
2.3:埠號
- publish_port:4505
- ret_port:4506
三:下載安裝
安裝教程:http://docs.saltstack.cn/topics/installation/rhel.html
安裝教程:https://mirrors.aliyun.com/saltstack/#rhel
3.1:配置yum倉庫
-
配置官網鏡像倉庫和key
# 服務端和客戶端節點都要執行(我直接使用官網鏡像倉庫,沒有使用阿里云的) rpm --import https://repo.saltproject.io/salt/py3/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub curl -fsSL https://repo.saltproject.io/salt/py3/redhat/7/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo # 清空過期快取 sudo yum clean expire-cache -
配置阿里云鏡像倉庫
yum install -y https://mirrors.aliyun.com/saltstack/yum/redhat/salt-repo-latest-2.el7.noarch.rpm # 補充: 配置Redhat8/CentOS8 倉庫 #匯入key,配置salt倉庫 rpm --import https://repo.saltproject.io/py3/redhat/8/x86_64/latest/SALTSTACK-GPG-KEY.pub curl -fsSL https://repo.saltproject.io/py3/redhat/8/x86_64/latest.repo | tee /etc/yum.repos.d/salt.repo # 教程鏈接 https://mirrors.aliyun.com/saltstack/#rhel
3.2:主控端部署
sudo yum install salt-master
sudo yum install salt-minion
sudo yum install salt-ssh
sudo yum install salt-syndic
sudo yum install salt-cloud
sudo yum install salt-api
3.3:主控端設定開機自啟
sudo systemctl enable salt-master && sudo systemctl start salt-master
sudo systemctl enable salt-minion && sudo systemctl start salt-minion
sudo systemctl enable salt-syndic && sudo systemctl start salt-syndic
sudo systemctl enable salt-api && sudo systemctl start salt-api
3.4:被控端部署
sudo yum install salt-minion
3.5:被控端設定開機自啟
sudo systemctl enable salt-minion && sudo systemctl start salt-minion
四:配置
4.1:Master端和Minion端常用配置引數說明
Master端常用的配置引數說明:
-
interface:指定bind的地址,默認為0.0.0.0
-
publish_port:指定發布埠,默認為4505
-
ret_port:指定結果回傳埠,與minion組態檔中的master_port對應,默認為4506
-
uesr:指定master行程的運行用戶,如果調整,則需要調整部分目錄的權限,默認權限為root
-
timeout:指定timeout時間,如果minion規模龐大或絡狀況不好,建議增該值,默認5s
-
keep_jobs:默認情況下,minion會執行結果會回傳master,master會快取到本地的cachedir目錄,該引數指定快取多長時間,以供查看之前的執行結果,會占用磁盤空間,默認為24h
-
job_cache:master是否快取執行結構,如果規模龐大(超過5000臺),建議使用其他方式來存盤jobs,關閉本選項,默認為True
-
file_recv:是否允許minion傳送檔案到master上,默認是Flase
-
file_roots:指定file server目錄,默認為:
file_roots: base: - /srv/salt -
pillar_roots:指定pillar目錄,默認為:
pillar_roots: base: - /srv/pillar -
log_level:執行日志級別,支持的日志級別由'garbage', 'trace', 'debug', info', 'warning', 'error', ‘critical ’ ,默認為’warning’
Minion端常用的配置引數說明:
- master:指定master主機,默認為salt
- mastar_port:指定認證和執行結果發送到master的哪個埠,與master組態檔中的ret_port對應,默認為4506
- id:指定minion的標識,Salt內部使用id作為標識,唯一識別符號,默認為主機名
- user:指定運行minion的用戶,由于安裝包,啟動服務等操作需要特殊用戶,推薦使用root,默認為root
- cache_jobs:minion是否快取執行結果,默認為False
- backuo_mode:在檔案操作(file.managed或file.recurse)時,如果檔案發送更改,指定備份目錄,當前有效的值為minion,備份在cachedir/file_backups目錄下,以原始檔案名稱加時間戳來命名,默認為Disabled
- providers:指定模塊對應的providers,如果RHRL系列中,pkg對應的providers是yumpkg5
- renderer:指定配置管理系統中的渲染器,默認值為:yaml_jinja
- file_client:指定file client默認去哪里(remotr或local)尋找檔案,默認值為remote
- loglevel:指定日志級別,默認為warnning
- tcp_keepalive:minion是否與master保持keepalive檢查,zeromq3一下版本存在keepalive bug,會導致某些情況下鏈接例外后minion無法重連master,建議有條件的話吧zeromq3以上版本,默認為True
4.2:修改主控端的maser組態檔
最簡配置:不用調整master,直接使用默認master配置即可,
# 完整配置:(大部分是默認配置)
[root@manager110 salt]# cat /etc/salt/master
file_roots:
base:
- /srv/salt/base
grantcron:
- /srv/salt/grantcron
revokecron:
- /srv/salt/revokecron
publisher_acl:
dba:
- test.ping
- cmd.*
- cron.*
- cp.*
devuser:
- test.ping
- cmd.*
- cron.*
- cp.*
default_include: master.d/*.conf
interface: 0.0.0.0
ipv6: False
publish_port: 4505
user: root
ret_port: 4506 # 這里ret_port和后面minion配置中的master_port對應保持一致,
pidfile: /var/run/salt/salt-master.pid
conf_file: /etc/salt/master
pki_dir: /etc/salt/pki/master
cachedir: /var/cache/salt/master
keep_jobs: 2
gather_job_timeout: 10
timeout: 5
sock_dir: /var/run/salt/master
max_open_files: 100000
worker_threads: 12
zmq_backlog: 1000
open_mode: False
auto_accept: true
file_recv: True
file_recv_max_size: 1000
tcp_keepalive: True
tcp_keepalive_idle: 300
4.3:修改主控端的minion組態檔
最簡配置:配置master: 192.168.0.110即可;id可以不用配置,默認取hostname,
切記:“master: ” 冒號后要添加一個空格,然后再寫IP,
如果需要調整id,先洗掉/etc/salt/minion_id檔案,然后重啟minion,會自動在/etc/salt目錄下新生成一個minion_id檔案來存放id,
# 修改主控端的minion組態檔 修改 #id和#master:salt 這兩行 (注意書寫規范:每個冒號后面都要跟一個空格)
# #master: salt 指向管理端地址,這里是指向salt-master服務器,可以是IP、域名或主機名
# #id: xxx minion主機標識必須唯一,如果不配置,默認取機器的hostname,
[root@manager110 ~]# sed -i 's/^#master: salt/master: 192.168.0.110/g' /etc/salt/minion #注意master:后的空格
[root@manager110 ~]# sed -i 's/^#id:/id: manager110/g' /etc/salt/minion #注意id:后的空格
# 查看配置內容
[root@manager110 ~]# sed -n '/^master:/p' /etc/salt/minion
master: 192.168.0.110
[root@manager110 ~]# sed -n '/^id:/p' /etc/salt/minion
id: manager110
[root@manager110 salt]# cat /etc/salt/minion
master: 192.168.0.110
id: manager110
完整配置:(大部分都是默認配置)
[root@manager110 salt]# cat /etc/salt/minion
master: 192.168.0.110
ipv6: False
master_port: 4506
user: root
pidfile: /var/run/salt/salt-minion.pid
conf_file: /etc/salt/minion
pki_dir: /etc/salt/pki/minion
minion_id_caching: True
cachedir: /var/cache/salt/minion
sock_dir: /var/run/salt/minion
log_file: /var/log/salt/minion
log_level: warning
log_datefmt: '%H:%M:%S'
tcp_keepalive: True
tcp_keepalive_idle: 300
4.4:修改被控端的minion組態檔
最簡配置:配置master: 192.168.0.110即可;id可以不用配置,默認取hostname,
切記:“master: ” 冒號后要添加一個空格,然后再寫IP,
[root@db101 ~]# sed -i 's/^#master: salt/master: 192.168.0.110/g' /etc/salt/minion #注意master:后有空格
[root@db101 ~]# sed -i 's/^#id:/id: db101/g' /etc/salt/minion #注意id:后的空格
[root@db101 ~]# sed -n '/^master:/p' /etc/salt/minion
master: 192.168.0.110
[root@db101 ~]# sed -n '/^id:/p' /etc/salt/minion
id: db101
[root@db101 ~]# cat /etc/salt/minion
master: 192.168.0.110
id: db101
# 其它被控端部署同上,db101修改成對應的host名稱就可以了,
# db102修改
sed -i 's/^#master: salt/master: 192.168.0.110/g' /etc/salt/minion
sed -i 's/^#id:/id: db102/g' /etc/salt/minion
# db103修改
sed -i 's/^#master: salt/master: 192.168.0.110/g' /etc/salt/minion
sed -i 's/^#id:/id: db103/g' /etc/salt/minion
# 查看
sed -n '/^master:/p' /etc/salt/minion
sed -n '/^id:/p' /etc/salt/minion
完整配置:(大部分是默認配置)
[root@db101 ~]# cat /etc/salt/minion
master: 192.168.0.110
ipv6: False
master_port: 4506
user: root
pidfile: /var/run/salt/salt-minion.pid
conf_file: /etc/salt/minion
pki_dir: /etc/salt/pki/minion
minion_id_caching: True
cachedir: /var/cache/salt/minion
sock_dir: /var/run/salt/minion
log_file: /var/log/salt/minion
log_level: warning
log_datefmt: '%H:%M:%S'
tcp_keepalive: True
tcp_keepalive_idle: 300
4.5:重啟服務
# 主控端
systemctl restart salt-master
systemctl status salt-master
systemctl restart salt-minion
systemctl status salt-minion
# 被控端
systemctl restart salt-minion
systemctl status salt-minion
4.6:salt-minion和salt-master認證程序
- minion在第一次啟動時,會在/etc/salt/pki/minion/下自動生成一對密鑰,然后將公鑰發給master
- master收到minion的公鑰后,通過salt-key命令接受該公鑰,此時master的/etc/salt/pki/master/minions目錄將會存放以minion id命名的公鑰,然后master就能對minion發送控制指令了
- master 秘鑰對默認存盤在/etc/salt/pki/master/master.pub /etc/salt/pki/master/master.pem
- master 端認證的公鑰存盤在:/etc/salt/pki/master/minions/
- minion 秘鑰對默認存盤在/etc/salt/pki/minion/minion.pub /etc/salt/pki/minion/minion.pem
- minion 存放的master公鑰/etc/salt/pki/minion/minion_master.pub
- minion_id 默認存盤在/etc/salt/minion_id
若想修改minion名稱,需洗掉原來的minion_id,重啟重新生成一個minion_id
salt-key常用選項
-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@manager110 salt]# salt-key -L
Accepted Keys:
manager110
Denied Keys:
Unaccepted Keys:
Rejected Keys:
接受指定minion的新證書
[root@manager110 salt]# salt-key -a -y db101
The key glob '-y' does not match any unaccepted keys.
# 查看salt master和minion的版本是否一致,如果不一致保持一致,
[root@manager110 salt]# salt-minion --versions-report

接受所有minion的新證書
[root@manager110 manager110]# salt-key -y -A
4.7:debug
[root@db101 salt]# salt-minion -l debug
[DEBUG ] TiamatPipPathFinder.find_spec: First time trying to load log4mongo
[DEBUG ] TiamatPipPathFinder.find_spec: First time trying to load raven
[DEBUG ] Reading configuration from /etc/salt/minion
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[DEBUG ] TiamatPipPathFinder.find_spec: First time trying to load log4mongo
[DEBUG ] TiamatPipPathFinder.find_spec: First time trying to load raven
[DEBUG ] Configuration file path: /etc/salt/minion
[INFO ] Setting up the Salt Minion "db101"
[INFO ] An instance is already running. Exiting the Salt Minion
[INFO ] Shutting down the Salt Minion
The Salt Minion is shutdown.
4.8:查看日志
[root@manager110 salt]# cat /var/log/salt/master
[root@manager110 salt]# cat /var/log/salt/minion
[root@db101 salt]# tail -f /var/log/salt/minion
2023-03-21 21:38:47,174 [salt.minion :1089][ERROR ][95757] Minion unable to successfully connect to a Salt Master.
2023-03-21 21:39:07,335 [salt.minion :1141][ERROR ][95757] Error while bringing up minion for multi-master. Is master at 192.168.0.110 responding?
# 查看埠是否通
[root@db101 salt]# telnet 192.168.0.110 4505
Trying 192.168.0.110...
telnet: connect to address 192.168.0.110: No route to host
# 埠不通,直接關閉防火墻
$ systemctl stop firewalld && systemctl disable firewalld
# 重新查看
[root@manager110 manager110]# salt-key -L
Accepted Keys:
db101
db102
db103
manager110
Denied Keys:
Unaccepted Keys:
Rejected Keys:

五:常用模塊、命令、執行
5.1:列出所有salt的sys模塊
[root@manager110 manager110]# salt db101 sys.list_modules
5.2:test模塊下的ping方法測驗主機是否存在
# 指定一個minion
[root@manager110 manager110]# salt db101 test.ping
db101:
True
# 指定多個minions : -L:list,可指定多個minions,多個minions之間用空格或者逗號隔開
[root@manager110 manager110]# salt -L "db101 db102 db103" test.ping
[root@manager110 manager110]# salt -L "db101,db102,db103" test.ping
db102:
True
db101:
True
db103:
True
# 所有的minions
[root@manager110 manager110]# salt "*" test.ping
db101:
True
db102:
True
manager110:
True
db103:
True
5.3:cmd超級模塊,所有shell命令都能執行
[root@manager110 manager110]# salt '*' cmd.run 'ps -ef | grep python'
db101:
root 1053 1 0 Mar16 ? 00:00:37 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 97014 97013 0 22:09 ? 00:00:00 /bin/sh -c ps -ef | grep python
root 97016 97014 0 22:09 ? 00:00:00 grep python
manager110:
root 1143 1 0 Mar19 ? 00:00:17 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 55232 78679 64 22:09 pts/0 00:00:00 /opt/saltstack/salt/run/run * cmd.run ps -ef | grep python MainProcess
root 55247 55243 0 22:09 ? 00:00:00 /bin/sh -c ps -ef | grep python
root 55249 55247 0 22:09 ? 00:00:00 grep python
db102:
root 1047 1 0 Mar17 ? 00:00:28 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 66497 66496 0 22:09 ? 00:00:00 /bin/sh -c ps -ef | grep python
root 66499 66497 0 22:09 ? 00:00:00 grep python
db103:
root 1052 1 0 Mar17 ? 00:00:29 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root 113232 113231 0 22:09 ? 00:00:00 /bin/sh -c ps -ef | grep python
root 113234 113232 0 22:09 ? 00:00:00 grep python
5.4:salt-run命令
該命令執行runner(salt帶的或者自定義的,runner以后會講),通常在master端執行,比如經常用到的manage
salt-run [options] [runner.func]
salt-run manage.status ##查看所有minion狀態
salt-run manage.down ##查看所有沒在線minion
salt-run manage.up ##查看所有在線minion
5.5:salt-key密鑰管理,通常在master端執行
salt-key [options]
salt-key -L ##查看所有minion-key
salt-key -a <key-name> ##接受某個minion-key
salt-key -d <key-name> ##洗掉某個minion-key
salt-key -A ##接受所有的minion-key
salt-key -D ##洗掉所有的minion-key
5.6:salt-call
該命令通常在minion上執行,minion自己執行可執行模塊,不是通過master下發job
salt-call [options] <function> [arguments]
salt-call test.ping ##自己執行test.ping命令
salt-call cmd.run 'ifconfig' ##自己執行cmd.run函式
5.7:salt-cp
分發檔案到minion上,不支持目錄分發,通常在master運行
salt-cp [options] '<target>' SOURCE DEST
salt-cp '*' testfile.html /tmp
salt-cp 'test*' index.html /tmp/a.html
5.8:salt-ssh
5.9:salt-master
master 運行命令
salt-master [options]
salt-master ##前臺運行master
salt-master -d ##后臺運行master
salt-master -l debug ##前臺debug輸出
5.10:salt-minion
minion運行命令
salt-minion [options]
salt-minion ##前臺運行
salt-minion -d ##后臺運行
salt-minion -l debug ##前臺debug輸出
5.11:salt-syndic
syndic是salt的代理
十:參考文章
參考文章:https://blog.csdn.net/hbhe0316/article/details/122701252
參考文章:https://blog.csdn.net/m0_46674735/article/details/113928894
參考文章:[死磕salt系列-salt組態檔 - biglittleant - 博客園 (cnblogs.com)](https://www.cnblogs.com/biglittleant/p/8743843.html#:~:text=死磕salt系列-salt組態檔 1 interface%3A 服務器監聽地址, 2 ipv6%3A 是否啟用ipv6, 3,5 timeout%3A 設定saltAPI和命令默認超時時間, 6 state_verbose%3A 顯示回傳狀態的詳細資訊程度, False 只回傳變化的地方及執行結果,)
參考文章:zmq導致master收不到minion的key - doublexi - 博客園 (cnblogs.com)
參考文章:salt常用命令、模塊、執行 - davygeek - 博客園 (cnblogs.com)
salt命令詳解:https://docs.saltproject.io/en/getstarted/fundamentals/remotex.html, https://docs.saltproject.io/en/getstarted/system/execution.html
執行函式模塊:https://docs.saltproject.io/en/latest/ref/modules/all/index.html#all-salt-modules
salt 插件:https://docs.saltproject.io/en/getstarted/system/plugins.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/547763.html
標籤:Python
