一、SaltStack概述
Salt,,一種全新的基礎設施管理方式,部署輕松,在幾分鐘內可運行起來,擴展性好,很容易管理上萬臺服務器,速度夠快,服務器之間秒級通訊。
salt底層采用動態的連接總線, 使其可以用于編配, 遠程執行, 配置管理等等.
多種配置管理工具對比:
Puppet(rubby開發,現在很少使用)
ansible(python開發,輕量級,沒有agent,大規模環境下使用ssh會很慢,串行傳輸)
Saltstack(python開發,遠程執行、配置管理、事件驅動基礎設施、使用saltcloud可以管理私有云和公有云)
官方文檔:<https://docs.saltstack.com/en/getstarted/>
官方提供官方yum源:repo.slatstack.com-->可以使用cobbler自定義yum倉庫進行同步
官方安裝源:<http://repo.saltstack.com/2016.11.html#rhel>
Saltstack組件:
SaltMaster
SaltMinion
Execution Modules
環境說明:
主機名 | IP地址 | 說明 | 系統 |
---|---|---|---|
linux-node1.example.com | 192.168.56.11 | 模式:master | Centos 7.4 |
linux-node2.example.com | 192.168.56.12 | 模式:minion | Centos 7.4 |
二、SaltStack安裝
1.安裝指定的yum源
[root@linux-node1 ~]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-2016.11-2.el7.noarch.rpm
[root@linux-node2 ~]# yum install https://repo.saltstack.com/yum/redhat/salt-repo-2016.11-2.el7.noarch.rpm
2.安裝salt-master和salt-minion
[root@linux-node1 ~]# yum install -y salt-master
[root@linux-node1 ~]# yum install -y salt-minion
[root@linux-node2 ~]# yum install -y salt-minion
3.修改minion配置并啟動
[root@linux-node1 ~]# systemctl start salt-master #啟動salt-master
[root@linux-node1 ~]# vim /etc/salt/minion #配置salt-minion
master: 192.168.56.11 #可以是主機名需要解析(指定服務端的IP地址),冒號有空格
id: 唯一標識符,可以不配,不配默認就是主機名
[root@linux-node1 ~]# systemctl start salt-minion #啟動salt-minion
[root@linux-node2 salt]# vim minion
master: 192.168.56.11 #可以是主機名需要解析(指定服務端的IP地址),冒號有空格
id: 唯一標識符,可以不配,不配默認就是主機名
[root@linux-node2 salt]# systemctl start salt-minionminion配置中有一個id配置,默認是hostname,如果id配置和hostname不一致會導致無法進行通信,那么當hostname做了修改,或者錯誤的時候該怎么配置呢?
①關閉salt-minion
②salt-key -d id 在master上刪除minion的id
③minion上刪除pki目錄
④minion上刪除minion_id文件
⑤修改完成,啟動minion
#此處必須先停掉minion修改,并刪除相應的文件,否則會默認地去查找原先的配置,已踩坑#以下是剛裝完查看minion_id變成了www.test123.com。進行修改成linux-node2.example.com
[root@linux-node2 salt]# cat minion_id
www.test123.com
[root@linux-node2 salt]# systemctl stop salt-minion
[root@linux-node2 salt]# rm -rf pki
[root@linux-node2 salt]# rm -rf minion_id
[root@linux-node2 salt]# systemctl start salt-minion
[root@linux-node2 salt]# cat minion_id
linux-node2.example.com
4.配置說明
[root@linux-node2 salt]# ll
總用量 124
-rw-r----- 1 root root 2624 9月 15 23:19 cloud
drwxr-xr-x 2 root root 6 9月 16 00:41 cloud.conf.d
drwxr-xr-x 2 root root 6 9月 16 00:41 cloud.deploy.d
drwxr-xr-x 2 root root 6 9月 16 00:41 cloud.maps.d
drwxr-xr-x 2 root root 6 9月 16 00:41 cloud.profiles.d
drwxr-xr-x 2 root root 6 9月 16 00:41 cloud.providers.d
-rw-r----- 1 root root 46034 9月 15 23:19 master
drwxr-xr-x 2 root root 6 9月 16 00:41 master.d
-rw-r----- 1 root root 35101 1月 16 10:29 minion
drwxr-xr-x 2 root root 27 1月 16 11:47 minion.d
-rw-r--r-- 1 root root 23 1月 16 11:45 minion_id
drwxr-xr-x 3 root root 19 1月 16 11:45 pki
-rw-r----- 1 root root 26984 9月 15 23:19 proxy
drwxr-xr-x 2 root root 6 9月 16 00:41 proxy.d
-rw-r----- 1 root root 344 9月 15 23:19 roster說明:
(1)salt-minion首次啟動會在/etc/salt/pki/minion目錄下生成公鑰和秘鑰
[root@linux-node2 salt]# ll /etc/salt/pki/minion/
總用量 12
-rw-r--r-- 1 root root 450 1月 16 11:47 minion_master.pub
-r-------- 1 root root 1674 1月 16 11:45 minion.pem
-rw-r--r-- 1 root root 450 1月 16 11:45 minion.pub(2)并且在salt-master的/etc/salt/pki/master/minion_pre中存放了salt-minion的公鑰。
[root@linux-node1 ~]# ll /etc/salt/pki/master/minions_pre/
linux-node1.example.com
linux-node2.example.com
5.配置salt-master和slat-minion通信
[root@linux-node1 salt]# salt-key
Accepted Keys: 同意的
Denied Keys: 拒絕的
Unaccepted Keys: 等待同意的
linux-node1.example.com
linux-node2.example.com
Rejected Keys:
同意認證的方法:
分為三種:
[root@linux-node1 salt]# salt-key -A
[root@linux-node1 salt]# salt-key -a 指定id
[root@linux-node1 salt]# salt-key -a 支持通配符
[root@linux-node1 master]# salt-key -a linux*
The following keys are going to be accepted:
Unaccepted Keys:
linux-node1.example.com
linux-node2.example.com
Proceed? [n/Y] Y
Key for minion linux-node1.example.com accepted.
Key for minion linux-node2.example.com accepted.salt-key 命令參數介紹
-L 列出所有
-d 刪除指定的支持通配符
-D 刪除所有
-A 添加所有
-a 指定添加同意之后生成的文件
pki/
├── master
│ ├── master.pem
│ ├── master.pub
│ ├── minions
│ │ ├── linux-node1.example.com
│ │ └── linux-node2.example.com
│ ├── minions_autosign
│ ├── minions_denied
│ ├── minions_pre
│ └── minions_rejected
└── minion├── minion_master.pub 同意之后master發送公鑰├── minion.pem└── minion.pub
三、SaltStack遠程執行和配置管理
1.遠程執行
第一條命令:
[root@linux-node1 master]# salt '*' test.ping
linux-node2.example.com:True
linux-node1.example.com:True說明:
salt:命令
*:匹配目標,使用通配符
test.ping:模塊.方法
#此處的ping并非ICMP的ping命令,而是master向minion發送了一個包,minion收到了,返回一個True[root@linux-node1 ~]# salt '*' cmd.run 'uptime'
linux-node1.example.com:11:51:47 up 21 days, 5:57, 2 users, load average: 0.04, 0.03, 0.05
linux-node2.example.com:11:51:47 up 12 days, 6:26, 2 users, load average: 0.00, 0.03, 0.05
[root@linux-node1 ~]# salt '*' cmd.run 'w'
linux-node1.example.com:11:52:11 up 21 days, 5:58, 2 users, load average: 0.03, 0.02, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/2 192.168.56.1 06Jan18 6:51 3.27s 3.27s -bashroot pts/3 192.168.56.1 06Jan18 3.00s 6:17 0.46s /usr/bin/python /usr/bin/salt * cmd.run w
linux-node2.example.com:11:52:11 up 12 days, 6:26, 2 users, load average: 0.00, 0.03, 0.05USER TTY FROM LOGIN@ IDLE JCPU PCPU WHATroot pts/1 192.168.56.1 Mon10 21:59m 0.28s 0.28s -bashroot pts/3 192.168.56.1 06Jan18 6:59 4.82s 0.02s -bash
[root@linux-node1 ~]# salt '*' cmd.run 'df -h'
linux-node2.example.com:Filesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 18G 17G 1.1G 95% /devtmpfs 905M 0 905M 0% /devtmpfs 916M 12K 916M 1% /dev/shmtmpfs 916M 41M 876M 5% /runtmpfs 916M 0 916M 0% /sys/fs/cgroup/dev/sda1 497M 171M 326M 35% /boottmpfs 184M 0 184M 0% /run/user/0/dev/loop0 4.1G 4.1G 0 100% /mnt
linux-node1.example.com:Filesystem Size Used Avail Use% Mounted on/dev/mapper/centos-root 18G 11G 7.2G 60% /devtmpfs 905M 0 905M 0% /devtmpfs 916M 28K 916M 1% /dev/shmtmpfs 916M 57M 860M 7% /runtmpfs 916M 0 916M 0% /sys/fs/cgroup/dev/sda1 497M 171M 326M 35% /boottmpfs 184M 0 184M 0% /run/user/0[root@linux-node1 ~]# netstat -tulnp|grep minion
minion不需要監聽端口,說明minion需要主動去鏈接master,master監聽端口為4505、4506
[root@linux-node1 ~]# netstat -tulnp|grep python
tcp 0 0 0.0.0.0:4505 0.0.0.0:* LISTEN 37039/python
tcp 0 0 0.0.0.0:4506 0.0.0.0:* LISTEN 37045/python #master和minion默認使用一個叫zeroMQ進行并行通信,zeroMQ屬于底層(傳輸層)的消息隊列,
#相當于一個發布與訂閱系統,比如你訂了一個教室聽課,那么所有訂了此間課室的人都能聽到老師的課程。[root@linux-node1 ~]# lsof -ni:4505
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
salt-mast 37039 root 16u IPv4 3394584 0t0 TCP *:4505 (LISTEN)
salt-mast 37039 root 18u IPv4 3412804 0t0 TCP 192.168.56.11:4505->192.168.56.12:43126 (ESTABLISHED)
salt-mast 37039 root 19u IPv4 3412811 0t0 TCP 192.168.56.11:4505->192.168.56.11:38262 (ESTABLISHED)
salt-mini 39623 root 27u IPv4 3412810 0t0 TCP 192.168.56.11:38262->192.168.56.11:4505 (ESTABLISHED)
查看4505端口,我們可以發現salt-minion使用一個隨機端口通過4505端口與salt-master通信,master使用4505端口發送指定到salt-minion上進行執行。而4606端口是用于接收數據的返回,用于zeroMQ的請求與響應的系統。
可以通過date命令查看salt的并行通信,可以看到是同時返回
[root@linux-node1 ~]# salt '*' cmd.run 'date'
linux-node2.example.com:Tue Jan 16 12:01:52 CST 2018
linux-node1.example.com:Tue Jan 16 12:01:52 CST 2018
2.配置管理
- (1)saltstack是使用YAML的格式作為管理文件的格式,下面的YAML的樣例:
YAML樣例: house: family: name: Doe parents:- John- Jane children:- Paul- Mark- Simone address: number: 34 street: Main Street city: Nowheretown zipcode: 12345
-
(2)YAML的規則:
①縮進表示層級關系,默認縮進是2個空格、4個空格、6個空格
②冒號后面有個空格,以冒號結尾可以有空格,可以無空格
③短橫線代表一個列表,短橫線后面有個空格 - (3)定義yaml文件放的位置:salt內置一個fileserver,在master文件配置:file_roots
[root@linux-node1 ~]# vim /etc/salt/master #定義yaml文件放的位置,base環境是必備的
file_roots:base:- /srv/salt/basedev:- /srv/salt/devtest:- /srv/salt/testprod:- /srv/salt/prod
[root@linux-node1 ~]# mkdir -p /srv/salt/{base,dev,test,prod}
[root@linux-node1 ~]# systemctl restart salt-master
[root@linux-node1 ~]# cd /srv/salt/base/
[root@linux-node1 base]# mkdir web
[root@linux-node1 web]# vim apache.sls #編寫安裝apache的YAML文件
apache-install:pkg.installed:---------->模塊pkg,方法installed,會匹配操作系統進行選擇安裝的方法- name: httpd--------->裝的包的名稱apache-service:----------->id要唯一service.running:-------->狀態模塊service,running為模塊的方法- name: httpd--------->管理服務的名稱- enable: True-------->設置開機自動啟動[root@linux-node1 ~]# salt 'linux-node2.example.com' state.sls web.apache
#如果apache.sls的位置是在prod目錄下,需要在后面增加saltenv=prod
#salt 'linux-node2.example.com' state.sls web.apache saltenv=prod實現自動化安裝,需要寫一個top.sls
[root@linux-node1 base]# pwd
/srv/salt/base
[root@linux-node1 base]# vim top.sls #必須在base環境下寫
base:'linux-node1.example.com':- web.apache'linux-node2.example.com':- web.apache
****************************
如果只有一個任務在全部機子上執行,也可以:
base:'*'- web.apache
****************************
[root@linux-node1 ~]# salt '*' state.highstate #去top.sls讀取,*代表通知哪些主機salt '*' state.highstate test=True #在不想影響當前主機的運行情況,可以使用test=True 進行預測試
轉載于:https://blog.51cto.com/jinlong/2061645