SaltStack安装及使用

1. SaltStack安装及使用

官网:https://saltproject.io/

1.1. 什么是SaltStack Master

SaltStack或Salt是基于Python的开源软件,用于基于事件的IT自动化,远程任务执行和配置管理。SaltStack支持“基础架构即代码”方法来进行数据中心系统和网络的部署和管理,配置自动化,SecOps编排,漏洞修复以及混合云控制。(维基百科提供

SaltStack Master是服务器节点,它是控制所有Salstack Minions的中央服务器。Saltstack Master拥有SaltStack奴才的清单和公钥,并对这些奴才执行远程执行。

1.2. SaltStack Master上安装Python

1
sudo yum install -y python3

1.3. 安装SaltStack官方Yum存储库

https://repo.saltstack.com/#rhel

安装最新版本。更新将安装最新版本,即使它是一个新的主版本。

运行以下命令安装SaltStack存储库和密钥

centos7

1
2
sudo rpm --import https://repo.saltproject.io/py3/redhat/7/x86_64/latest/SALTSTACK-GPG-KEY.pub
curl -fsSL https://repo.saltproject.io/py3/redhat/7/x86_64/latest.repo | sudo tee /etc/yum.repos.d/salt.repo

centos8

1
2
sudo 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 | sudo tee /etc/yum.repos.d/salt.repo

接着运行

1
sudo yum clean expire-cache
  • 安装,配置 master
1
2
3
4
5
6
7
8
sudo yum install -y salt-master salt-minion salt-ssh salt-syndic salt-cloud salt-api
或者
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
  • 安装,配置 slave
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装salt-minion
yum install -y salt-minion

# 修改配置文件
vim /etc/salt/minion
master: 10.211.55.4 # master的地址

master:
- 10.211.55.4
- 10.211.55.5
random_master: True

id: slave_id # 客户端在salt-master中显示的唯一ID

# 启动
service salt-minion start

加入开机启动

1
2
systemctl enable --now salt-master 
systemctl enable --now salt-minion

查看启动状态

1
systemctl status  salt-master salt-minion

服务端口

1
2
4505 用于连接slave,发布订阅
4506 接受响应,模式为zmq(消息队列)
  • 在master上授权
1
2
3
4
salt-key -L                # 查看已授权和未授权的slave
salt-key -a salve_id # 接受指定id的salve
salt-key -r salve_id # 拒绝指定id的salve
salt-key -d salve_id # 删除指定id的salve

1.4. 常用命令

  • 在master服务器上对slave进行远程操作

1.salt简单使用

1
2
3
4
5
6
7
8
salt '*' cmd.run 'ifconfig' # 对所有slave操作用 " * "
salt 'slave_id' cmd.run 'ifconfig' # 对指定slave操作用 "slave_id"

salt可以直接让minion执行模块命令,也可以直接执行shell命令
1.salt -C ‘wy-pe1 and wy-pe2 or wy-peN’ test.ping -C表示多参数(表示在测试多台主机的存活状态)
# salt '*' disk.usage 查看磁盘使用情况(使用内建模块查看所有minion端的磁盘使用情况)
#salt '*' cmd.run 'df -h' 使用cmd.run直接调用远程shell命令(功能同上)
# salt '*' cmd.run 'cat /root/lall' 查看客户端主机的/root/lall文件

2.nodegroup对minion进行分组

1
2
3
4
5
6
7
8
nodegroups:
group1: ‘L@foo.domain.com,bar.domain.com,baz.domain.com or bl*.domain.com’
group2: ‘G@os :Debian and foo.domain.com’
group3:’wy-pe2′
进行分组测试:
# salt -N group3 test.ping
wy-pe2:
True

3.grains对minion基本信息的管理:

1
2
salt ‘wy-pe2′ grins.ls                                                            查看grains分类
salt ‘wy-pe2′ grins.items 查看minnon基本信息(硬件参数)

4.pillar对敏感信息的管理,只有匹配到的节点才能获取和使用

1
默认pillar数据定义文件存储路径:/srv/pillar

1.5. debug排错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@pw-aimm-srv master]# salt-minion -l debug
[DEBUG ] Reading configuration from /etc/salt/minion
[DEBUG ] Including configuration from '/etc/salt/minion.d/_schedule.conf'
[DEBUG ] Reading configuration from /etc/salt/minion.d/_schedule.conf
[DEBUG ] Using cached minion ID from /etc/salt/minion_id: pw-aimm-srv
[DEBUG ] Using pkg_resources to load entry points
[DEBUG ] Override __grains__: <module 'salt.loaded.int.log_handlers.sentry_mod' from '/usr/lib/python3.6/site-packages/salt/log/handlers/sentry_mod.py'>
[DEBUG ] Configuration file path: /etc/salt/minion
[WARNING ] Insecure logging configuration detected! Sensitive data may be logged.
[INFO ] Setting up the Salt Minion "pw-aimm-srv"
[INFO ] An instance is already running. Exiting the Salt Minion
[INFO ] Shutting down the Salt Minion
[DEBUG ] Stopping the multiprocessing logging queue listener
[DEBUG ] closing multiprocessing queue
[DEBUG ] joining multiprocessing queue thread
[DEBUG ] Stopped the multiprocessing logging queue listener
The Salt Minion is shutdown.

1.6. Ubuntu 安装 saltstack

  • UBUNTU 20

运行以下命令以导入SaltStack存储库密钥,并创建/etc/apt/sources.list.d/salt.list:

1
2
3
4
# Download key
sudo curl -fsSL -o /usr/share/keyrings/salt-archive-keyring.gpg https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest/salt-archive-keyring.gpg
# Create apt sources list file
echo "deb [signed-by=/usr/share/keyrings/salt-archive-keyring.gpg] https://repo.saltproject.io/py3/ubuntu/20.04/amd64/latest focal main" | sudo tee /etc/apt/sources.list.d/salt.list

接着运行

1
sudo apt-get update

1.安装配置master

1
2
3
4
5
6
7
8
sudo apt-get install salt-master salt-minion salt-ssh salt-syndic salt-cloud salt-api
或者
sudo apt-get install salt-master
sudo apt-get install salt-minion
sudo apt-get install salt-ssh
sudo apt-get install salt-syndic
sudo apt-get install salt-cloud
sudo apt-get install salt-api

2.安装配置slave

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 安装salt-minion
sudo apt-get install -y salt-minion

# 修改配置文件
vim /etc/salt/minion
master: 10.211.55.4 # master的地址

master:
- 10.211.55.4
- 10.211.55.5
random_master: True

id: slave_id # 客户端在salt-master中显示的唯一ID

# 启动
sudo systemctl restart salt-minion
-------------本文结束感谢您的阅读-------------