什么是盐栈?
SaltStack 是一个建立在动态通信总线上的开源基础设施管理平台。 SaltStack 的主要结构包含两个主要的工作组件:一个 Salt Master 和几个 Salt Minions。 Salt Master 是用于向 Salt Minion 及其配置发送命令的主要控制系统。 Salt Minions(它们也称为节点)是在托管服务器上工作并接收来自主服务器的命令及其配置的子系统。 交互设置至少需要两台服务器。 一个用于主节点,一个用于节点。 所有节点都以相同的方式配置。
安装前注意事项
在安装 SaltStack 之前需要提出两个问题。
- 升级 PyCrypto: 通常由默认系统包提供的 PyCrypto 库当前未维护并且可能不安全。 然而,为了向后兼容,Salt 需要 PyCrypto 作为“最小公分母”,因为 PyCrypto 未维护,最佳实践是手动升级系统以使用更现代且持续维护的库,例如 PyCryptodome 或者更好, M2加密货币.
- 由于 Python 2.7 在 2020 年 1 月 1 日达到其生命周期结束 (EOL) 状态,因此在 SaltStack 版本 3000 (Sodium) 及更高版本中已弃用 Python 2.x。
盐大师安装
我们将在 Master 服务器和 Minion 服务器上执行这些安装过程。 第一步是确保我们的系统包是最新的
[root@host ~]# apt-get update
Hit:1 https://by.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 https://by.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 https://by.archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:4 https://security.ubuntu.com/ubuntu bionic-security InRelease [88,7 kB]
Get:5 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic InRelease [2.126 B]
Get:6 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic/main amd64 Packages [3.200 B]
Ign:7 https://dl.bintray.com/tetrate/getenvoy-deb bionic InRelease
Get:8 https://dl.bintray.com/tetrate/getenvoy-deb bionic Release [5.356 B]
Get:10 https://security.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [38,5 kB]
Get:11 https://security.ubuntu.com/ubuntu bionic-security/main DEP-11 48x48 Icons [17,6 kB]
Get:12 https://security.ubuntu.com/ubuntu bionic-security/main DEP-11 64x64 Icons [41,5 kB]
Get:13 https://security.ubuntu.com/ubuntu bionic-security/universe amd64 DEP-11 Metadata [42,1 kB]
Get:14 https://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 48x48 Icons [16,4 kB]
Get:15 https://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 64x64 Icons [111 kB]
Get:16 https://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 DEP-11 Metadata [2.464 B]
Fetched 369 kB in 1s (258 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
[root@host ~]#
现在,我们需要下载 SaltStack 的存储库密钥并将其导入我们的系统。
[root@host ~]# wget -O- https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest/SALTSTACK-GPG-KEY.pub | sudo apt-key add -
--2020-03-08 19:00:32-- https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest/SALTSTACK-GPG-KEY.pub
Resolving repo.saltstack.com (repo.saltstack.com)... 54.192.230.5, 54.192.230.104, 54.192.230.29, ...
Connecting to repo.saltstack.com (repo.saltstack.com)|54.192.230.5|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1727 (1,7K) [application/octet-stream]
Saving to: 'STDOUT'
- 100%[==================>] 1,69K --.-KB/s in 0s
2020-03-08 19:00:34 (374 MB/s) - written to stdout [1727/1727]
OK
[root@host ~]#
接下来,我们可以添加存储库本身。
echo "deb https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic main" | sudo tee /etc/apt/sources.list.d/saltstack.list
deb https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic main
[root@host ~]#
现在,我们需要更新我们的 repo 列表,以便我们可用的系统包是最新的。
[root@host ~]# apt-get update
Hit:1 https://by.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 https://by.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:3 https://by.archive.ubuntu.com/ubuntu bionic-backports InRelease
Get:4 https://security.ubuntu.com/ubuntu bionic-security InRelease [88,7 kB]
Get:5 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic InRelease [2.126 B]
Get:6 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic/main amd64 Packages [3.200 B]
Ign:7 https://dl.bintray.com/tetrate/getenvoy-deb bionic InRelease
Get:8 https://dl.bintray.com/tetrate/getenvoy-deb bionic Release [5.356 B]
Get:10 https://security.ubuntu.com/ubuntu bionic-security/main amd64 DEP-11 Metadata [38,5 kB]
Get:11 https://security.ubuntu.com/ubuntu bionic-security/main DEP-11 48x48 Icons [17,6 kB]
Get:12 https://security.ubuntu.com/ubuntu bionic-security/main DEP-11 64x64 Icons [41,5 kB]
Get:13 https://security.ubuntu.com/ubuntu bionic-security/universe amd64 DEP-11 Metadata [42,1 kB]
Get:14 https://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 48x48 Icons [16,4 kB]
Get:15 https://security.ubuntu.com/ubuntu bionic-security/universe DEP-11 64x64 Icons [111 kB]
Get:16 https://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 DEP-11 Metadata [2.464 B]
Fetched 369 kB in 1s (258 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
[root@host ~]#
接下来,让我们安装 SaltStack Master 包。
[root@host ~]# apt -y install salt-api salt-cloud salt-master salt-ssh salt-syndic
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
git git-man ieee-data liberror-perl libnorm1 libpgm-5.2-0 libzmq5
python3-cherrypy3 python3-croniter python3-git python3-gitdb python3-gnupg
python3-jinja2 python3-libcloud python3-lockfile python3-msgpack
python3-netaddr python3-openssl python3-psutil python3-repoze.lru
python3-routes python3-smmap python3-webob python3-zmq salt-common
Suggested packages:
git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk
gitweb git-cvs git-mediawiki git-svn python-git-doc python-jinja2-doc
python-lockfile-doc ipython3 python-netaddr-docs python-openssl-doc
python3-openssl-dbg python-psutil-doc python3-paste python3-nose
python-webob-doc python3-botocore python3-pycurl python3-twisted
The following NEW packages will be installed:
git git-man ieee-data liberror-perl libnorm1 libpgm-5.2-0 libzmq5
python3-cherrypy3 python3-croniter python3-git python3-gitdb python3-gnupg
python3-jinja2 python3-libcloud python3-lockfile python3-msgpack
python3-netaddr python3-openssl python3-psutil python3-repoze.lru
python3-routes python3-smmap python3-webob python3-zmq salt-api salt-cloud
salt-common salt-master salt-ssh salt-syndic
0 upgraded, 30 newly installed, 0 to remove and 0 not upgraded.
Need to get 16,5 MB of archives.
After this operation, 98,8 MB of additional disk space will be used.
…
…
…
Created symlink /etc/systemd/system/multi-user.target.wants/salt-master.service → /lib/systemd/system/salt-master.service.
Setting up salt-api (3000+ds-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/salt-api.service → /lib/systemd/system/salt-api.service.
Setting up salt-syndic (3000+ds-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/salt-syndic.service → /lib/systemd/system/salt-syndic.service.
Processing triggers for systemd (237-3ubuntu10.39) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
[root@host ~]#
盐大师配置
使用 nano 或 vim 文本编辑器,打开文件 /etc/salt/master。
[root@host ~]# vim /etc/salt/master
现在,找到以下行。
#interface: 0.0.0.0
找到该值后,将其取消注释,然后指定服务器 IP 替换 0.0.0.0 IP 地址。 在这个 example,我们将使用 64.91.225.162。 这将是我们将控制 Minion 服务器的 IP 地址。
# The address of the interface to bind to:
interface: 64.91.225.162
使用 :wq 保存文件并退出。 编辑后,我们应该重新启动服务。
[root@host ~]# systemctl restart salt-master.service
[root@host ~]#
接下来,我们需要创建一个密钥,minions 将使用该密钥连接到主节点。 我们生成了一个唯一的密钥,因此您的密钥会有所不同。
root@host:~# salt-key -F master
Local Keys:
master.pem: f8:01:d9:13:19:3f:a2:37:a5:03:cb:e8:0e:3d:9f:75:7c:f7:36:10:1d:51:70:b2:4c:b8:5f:04:4b:04:fc:28
master.pub: d0:71:50:26:f7:05:d7:84:6c:f8:38:1f:ce:5b:d8:6a:80:6c:1a:76:05:a8:48:3a:d7:5a:86:ff:bc:14:3b:49
root@host:~#
您需要记下 master.pub 键,因为我们很快需要将其添加到 salt minion 的配置中。
最后,我们要在防火墙中打开默认端口。 默认情况下,Salt 使用端口 4505 和 4506。
[root@host ~]# ufw allow proto tcp from any to any port 4505,4506
Salt Minion 安装
首先,我们将在我们的 minion 服务器上安装软件。
[root@host ~]# apt -y install salt-minion
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
dctrl-tools debconf-utils
Suggested packages:
debtags python3-augeas
The following NEW packages will be installed:
dctrl-tools debconf-utils salt-minion
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.
Need to get 146 kB of archives.
After this operation, 527 kB of additional disk space will be used.
Get:1 https://by.archive.ubuntu.com/ubuntu bionic/main amd64 dctrl-tools amd64 2.24-2build1 [60,9 kB]
Get:2 https://by.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 debconf-utils all 1.5.66ubuntu1 [56,6 kB]
Get:3 https://repo.saltstack.com/py3/ubuntu/18.04/amd64/latest bionic/main amd64 salt-minion all 3000+ds-1 [28,1 kB]
Fetched 146 kB in 1s (235 kB/s)
Selecting previously unselected package dctrl-tools.
(Reading database ... 178806 files and directories currently installed.)
Preparing to unpack .../dctrl-tools_2.24-2build1_amd64.deb ...
Unpacking dctrl-tools (2.24-2build1) ...
Selecting previously unselected package debconf-utils.
Preparing to unpack .../debconf-utils_1.5.66ubuntu1_all.deb ...
Unpacking debconf-utils (1.5.66ubuntu1) ...
Selecting previously unselected package salt-minion.
Preparing to unpack .../salt-minion_3000+ds-1_all.deb ...
Unpacking salt-minion (3000+ds-1) ...
Setting up debconf-utils (1.5.66ubuntu1) ...
Setting up dctrl-tools (2.24-2build1) ...
Setting up salt-minion (3000+ds-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/salt-minion.service → /lib/systemd/system/salt-minion.service.
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.39) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
[root@host ~]#
Salt Minion 配置
接下来,我们将在 minion 服务器上配置软件。 默认情况下,Salt Minion 连接到上面在 /etc/hosts 文件中命名的主 Salt 服务器 DNS。 我们将需要修改此设置。 我们将再次使用我们的 vim 编辑器并打开文件 /etc/hosts 并添加 Salt Master 的 IP。 在本例中为 10.0.2.15。 创建该条目后,使用 :wq 保存文件。
127.0.0.1 localhost
64.91.225.162 saltmaster
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
现在,我们需要为 Salt minion 创建一个唯一标识符。
vim /etc/salt/minion_id
让我们为 minion 添加标识符名称。 应注意提前创建命名方案以允许额外的奴才,例如
- ubuntu-minion_01
- us.mi.lan-ubuntu_01
- company.location.dept-ubuntu_01
我们将使用 ubuntu-minion_01 作为我们的 salt minion 服务器名称。
接下来,复制 master.pub
我们在上面的主服务器上创建的指纹,并将此值设置为 master_finger
在 minion 配置文件 /etc/salt/minion_id. 您可以使用文件中的 /finger 在 vim 中搜索此值。 添加指纹后,使用 :wq 保存并退出文件。
# Fingerprint of the master public key to validate the identity of your Salt master
# before the initial key exchange. The master fingerprint can be found by running
# "salt-key -f master.pub" on the Salt master.
master_finger: 'd0:71:50:26:f7:05:d7:84:6c:f8:38:1f:ce:5b:d8:6a:80:6c:1a:76:05:a8:48:3a:d7:5a:86:ff:bc:14:3b:49'
如果您更改任何设置(如 salt master 的 IP 地址) /etc/salt/minion
),您需要重新启动 salt-minion 服务
[root@host ~]# systemctl restart salt-minion
现在,登录到 salt 主服务器并使用以下命令列出/检查接受的密钥。
root@host:~# salt-key -L
Accepted Keys:
Denied Keys:
Unaccepted Keys:
ubuntu-minion_01 <<<<<
Rejected Keys:
root@host:~#
如您所见,我们现在有一个等待被接受的密钥“ubuntu-minion_01”。 接下来,我们将在 salt 主服务器上接受 minion 的 salt-key。
root@host:~# salt-key -A
The following keys are going to be accepted:
Unaccepted Keys:
ubuntu-minion_01
Proceed? [n/Y] y
Key for minion ubuntu-minion_01 accepted.
root@host:~#
现在我们可以在 Salt Minion 上运行以下命令来检查 Minions 密钥指纹。
root@host:~# salt-call --local key.finger
local:
cf:b7:01:00:9f:78:27:03:42:91:f1:7c:f1:3f:81:2f:a3:e6:11:48:bc:10:ba:2b:0c:df:f0:78:e0:07:1c:df
root@host:~#
接下来,我们可以将上述值与我们在 Salt Master 上运行命令“salt-key –finger
root@host:~# salt-key –finger ubuntu-minion_01
Accepted Keys:
ubuntu-minion_01 <<<<<
Denied Keys:
Unaccepted Keys:
Rejected Keys:
root@host:~#
现在,让我们通过运行 test.ping
命令。
root@host:~# salt ubuntu-minion_01 test.ping
ubuntu-minion_01:
True
root@host:~#
成功!
结论
Saltstack 是当今最流行的 IT 自动化、远程任务执行和配置管理工具之一。 在本文中,我们学习了如何在 salt master 和 salt minion 服务器上安装和配置 Saltstack。
您想尝试提高解决许多服务器自动化任务的能力吗? 给我们打电话 800.580.4985,或打开 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问交谈,了解您今天如何利用这项技术!