服务器 Salt 配置管理

服务器 Salt 配置管理

Official site: docs.saltproject.io/en/latest/c...

If you are a Python Developer, I am confident that you can easily dive into Salt and customize it to suit your specific business needs.

一、介绍

1. 基本介绍

  • Salt 是一个开源的自动化工具,用 Python 编写,支持远程执行命令、配置管理、状态管理、安装软件包、监控等功能。它使用轻量级的消息传递协议来与 Minion 进行通信,并通过 Master 端控制 Minion 端的配置和任务。Salt 可以扩展到数百或数千台服务器的规模,具有灵活的扩展性和可定制性,适用于各种规模的 IT 环境。

Salt 功能:

  • 远程执行
  • 配置管理
  • 云管理

运行方式:

  • Local
  • Master/Minion
  • Salt SSH

2. 工具对比

  • Ansible 通常使用 SSH 协议进行通信,因此被称为"无客户端代理"(Agentless)。它采用基于 YAML 语言的声明性配置模型,使用户能够轻松编写、维护和共享配置代码,从而简化了配置管理的过程。
  • Salt 支持各种插件和模块,并允许用户编写自定义扩展以满足更多自动化需求。同时,Salt 提供更为细致和丰富的命令和状态管理机制,可以实现更为复杂的自动化任务。
  • 选择哪个工具取决于用户的具体需求和环境。如果需要高度可定制的自动化机制和更为复杂的配置管理,那么 Salt 可能更适合。如果需要轻量、易用的自动化工具和易于编写和维护的配置代码,那么 Ansible 可能更适合。

3. 名词解释

概念 说明
Master MasterSaltStack 控制节点的中心,用于管理 Salt 配置和数据。它是 Salt 控制节点的核心组件,负责接收和分发 Salt 命令以及管理 Minion 的认证、配置和控制。
Minion MinionSaltStack 中的客户端,通过与 Master 通信来执行命令并将结果返回给 MasterMinion 可以在远程服务器、虚拟机或容器中安装,并通过 Master 对其进行管理和控制。Minion 的配置信息由 Master 通过 Salt 的管理工具进行配置和下发。
Pillar Saltstack 的一种配置管理工具,用于存储敏感数据和配置信息,如密码、密钥、数据库连接等,与 states 配置文件一起使用。Pillar 的数据可以在所有 Minion 上进行访问。
Grains SaltstackMinion 上运行的 Python 脚本,用于收集和存储有关 Minion 系统和环境的信息,如操作系统版本、CPU 型号、内存、IP 地址等。这些信息可以用于执行针对 Minion 的特定任务,如软件包安装、配置文件管理等。

二、Salt 配置管理

1. Minion 端配置

1.1 Configuring the Salt Minion

  • /etc/salt/minion 文件

Template:docs.saltproject.io/en/latest/r...

Bash 复制代码
# Set the location of the salt master server. If the master server cannot be
# resolved, then the minion will fail to start.
#master: salt

# Explicitly declare the id for this minion to use, if left commented the id
# will be the hostname as returned by the python call: socket.getfqdn()
# Since salt uses detached ids it is possible to run multiple minions on the
# same machine but with different ids, this can be useful for salt compute
# clusters.
#id:

1.2 Configuring the Salt Grains

  • /etc/salt/grains 文件
Bash 复制代码
# Custom static grains for this minion can be specified here and used in SLS files just like all other grains.

# Set a custom 'environment' grain to indicate the deployment environment
environment: production

# Set a custom 'location' grain to indicate the physical location of the Minion
location:
  datacenter: DC1
  cabinet: 13
  cab_u: 14-15

# Set a custom 'role' grain to indicate the role that the Minion plays
role: webserver

# Set a custom 'registration_date' grain to indicate the date when the Minion was registered
registration_date: 2022-04-15

# Set a custom 'virtual' grain to indicate whether the Minion is a virtual machine
virtual: false

1.3 Restart the Salt Minion Service

  • 重启 Minion 服务,重新加载配置并让 Master 重新管理
Bash 复制代码
systemctl restart salt-minion.service

2. Grains 定制开发

2.1 Example Requirement

  • 编写一个判断 Keepalived 运行在哪个节点的脚本,Shell 脚本判断逻辑如下:
Bash 复制代码
#!/bin/bash
#filename: check_keepalived_vip.sh

# get VIP defined in keepalived.conf
vip=`grep -i "virtual_ipaddress" -A 1 /etc/keepalived/keepalived.conf | awk -F '/' 'END{gsub(/^[ \t]+|[ \t]+$/, ""); print $1}'`

# check if it exist in the IP address list
if ip a | grep -q "$vip"; then
    echo "active"
else
    echo "inactive"
fi

#EOF

2.2 Configuring the Salt Mater

Bash 复制代码
#####      File Server settings      #####
##########################################
# Salt runs a lightweight file server written in zeromq to deliver files to
# minions. This file server is built into the master daemon and does not
# require a dedicated port.

# The file server works on environments passed to the master, each environment
# can have multiple root directories, the subdirectories in the multiple file
# roots cannot match, otherwise the downloaded files will not be able to be
# reliably ensured. A base environment is required to house the top file.
# Example:
# file_roots:
#   base:
#     - /srv/salt/
#   dev:
#     - /srv/salt/dev/services
#     - /srv/salt/dev/states
#   prod:
#     - /srv/salt/prod/services
#     - /srv/salt/prod/states
#
file_roots:
  base:
    - /srv/salt
Bash 复制代码
# 需在master端创建标准的grains目录
mkdir -p /srv/salt/_grains

# 切换至该目录,编写自定义grains脚本,应存储到该路径下
cd /srv/salt/_grains

2.3 Create the Custom Grains Script

  • 编写 Grains Python 脚本文件,大概逻辑如下:
Python 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#filename: node_status_grains.py

import subprocess


def node_status():
    """ 判断当前节点的角色,及判断业务是否运行在当前节点上,换言之,该节点目前是否为active节点 """

    # 判断当前操作系统是否为 debian 11
    os_info = subprocess.check_output(['cat', '/etc/os-release']).decode()
    if 'VERSION="11' not in os_info:
        return None

    # 判断当前节点的角色,根据主机名规则
    hostname_info = subprocess.check_output(['hostname']).decode().strip()
    if hostname_info[-2:] == '01':
        role = 'master'
    elif hostname_info[-2:] == '02':
        role = 'slave'
    else:
        return None

    # 判断当前节点是否为 active 节点
    vip = subprocess.check_output(["grep", "-i", "virtual_ipaddress", "-A", "1", "/etc/keepalived/keepalived.conf", "|", "awk", "-F", "/", "'{gsub(/^[ \t]+|[ \t]+$/, ""); print $1}'"]).decode().strip()
    if vip in subprocess.check_output(['ip', 'a']).decode():
        active = True
    else:
        active = False

    grains = { "app_role": role, "is_active": active }
    return grains

2.4 Sync and Call the Grains Script

  • 将通过在 Master 端编写 Grains 来将特定的 Grains 数据发送给指定的一组 Minion。可以使用 Grains 匹配器来选择要发送 GrainsMinion
Bash 复制代码
# 同步grains到指定minion主机列表(角色为webserver且不是虚拟机)
salt -G 'role:webserver && virtual:false' saltutil.sync_grains
  • 查看 Minion 端的缓存结果
Bash 复制代码
# 会生成 py 和 pyc 文件
ll /var/cache/salt/minion/extmods/grains
  • 调用 Salt Grains 命令
Bash 复制代码
salt -G 'role:webserver' grains.item app_role
salt -G 'role:webserver' grains.item is_active

3. Salt 模块开发

3.1 Example Requirement

  • 实现一个通过 ethtool 查看 BMC IP 的模块:
Bash 复制代码
# 底层 Shell 命令行
ipmitool lan print 1 2> /dev/null | awk 'BEGIN {FS="[: ]"} /IP Address\s+:/{val=$NF} END {if(length(val)>0) print "BMC_IP:", val; else print "BMC_IP: Null"}'

# Salt 的 Ad-hoc 方式
salt '*' cmd.run 'ipmitool lan print 1 2> /dev/null | awk '''BEGIN {FS="[: ]"} /IP Address\s+:/{val=$NF} END {if(length(val)>0) print "BMC_IP:", val; else print "BMC_IP: Null"}''''

# Salt 的自定义模块
 ⬇️ 👇 🦐🍜 👇 ⬇️

3.2 Origin and Custom Modules

  • 原生 Salt 模块位置(可参考源码来编写自定义模块):
Bash 复制代码
# find命令all-in-one
find / -type d -path "*/python*/site-packages/salt/modules" -execdir ls -l {} ;

# find结合awk实现查找
find / -name "salt" | awk '//python.*/site-packages/{system("ls -l "$0"/modules")}'

# find结合grep/sed/xargs分步实现
find / -name "salt" | grep -i "/python.*/site-packages" | sed 's|$|/modules|' | xargs ls -l
  • 新建自定义 Salt 模块目录:
Bash 复制代码
# 需在master端创建标准的模块目录
mkdir -p /srv/salt/_modules

# 切换至该目录,编写自定义模块,应存储到该路径下
cd /srv/salt/_modules

3.3 Create the Custom Module

  • 编写 Salt 模块脚本文件,大概逻辑如下:
Python 复制代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#filename: mod_ethtool.py

def show_bmcip():
    """
    A Salt module to retrieve the IP address of the BMC using ipmitool and awk.
    :return: The IP address of the BMC or an empty string if not found.
    """
    cmd = """ipmitool lan print 1 2> /dev/null | awk 'BEGIN {FS="[: ]"} /IP Address\s+:/{val=$NF} END {if(length(val)>0) print val; else print "Null"}'"""
    ret = __salt__['cmd.run'](cmd)
    return "BMC_IP: " + ret

3.4 Sync and Call the Salt Module

  • 自定义模块同步至指定受管主机组
Bash 复制代码
salt -G 'virtual:false' saltutil.sync_modules
  • 测试运行:
Bash 复制代码
# salt '【匹配主机】' 【模块文件名】.【函数名】
salt -G 'virtual:false' mod_ethtool.show_bmcip

三、Salt Plugins 插件

1. Ansiblegate

docs.saltproject.io/en/latest/r...

1.1 功能介绍

  • salt.modules.ansiblegate 模块是 SaltStack 的一个插件,它允许使用 Ansibleplaybookroles 来管理 SaltStack 的远程主机。具体来说,它提供了以下功能:
  1. 允许使用 Ansible playbookroles 来定义和管理 SaltStackstatespillars
  2. 允许在 SaltStackstatespillars 中引用 Ansible playbookroles 的变量和参数。
  3. 允许在 SaltStack 中执行 Ansible playbookroles 的任务。
  • 使用 salt.modules.ansiblegate 模块,可以实现 SaltStackAnsible 的混合使用,以获得更强大的配置管理和自动化能力。
  • 例如,可以使用 Ansibleplaybook 来定义和管理复杂的配置任务,然后使用 SaltStackstatespillars 来引用和执行这些任务。

1.2 执行方式

  • Salt 中,salt.modules.ansiblegate 模块可以通过两种方式来执行 Ansible playbookroles 任务:
  1. 使用 SSHWinRM 等协议连接到目标主机,然后在目标主机上执行 Ansible playbookroles 的任务。
  2. SaltMasterMinion 之间建立 SSH 隧道连接,并在 Minion 上执行 Ansible playbookroles 的任务,然后将执行结果返回到 Master
  • 这两者都可以实现通过 Salt 控制台来管理和执行 Ansible playbookroles,但它们有不同的适用场景和要求。

    • 如果使用 SSHWinRM 等协议连接到目标主机,则需要在目标主机上安装 AnsiblePython 环境,并且目标主机必须能够通过 SSHWinRM 等协议连接到。这种方式适用于需要在远程主机上执行 Ansible playbookroles 的任务,并且可以保证目标主机安全的情况。
    • 如果使用 Master-Minion 隧道连接,则可以通过 Salt Master-Minion 之间的安全连接执行 Ansible playbookroles 的任务,而无需在目标主机上安装 AnsiblePython 环境。这种方式适用于需要在受管节点上执行 Ansible playbookroles 的任务,但是受管节点上无法安装 AnsiblePython 环境的情况。
  • 需要注意的是,在使用 salt.modules.ansiblegate 模块时,要根据具体情况选择合适的方式来执行 Ansible playbookroles 的任务,并保证安全和可靠性。

四、参考命令

Bash 复制代码
# 测试联通性
salt '*' test.ping

# 列出node01的grains选项
salt 'node01*' grains.ls

# 查看node01的grains分类
salt 'node01*' grains.items

# 查看node01的os
salt '*saltminion01*' grains.get os

# 查看node01的sn
salt '*saltminion01*' grains.get server_id

# 筛选类型为centos的机器批量执行pwd命令
salt -G os:CentOS cmd.run 'pwd'
相关推荐
小黑爱编程38 分钟前
【LInux】HTTPS是如何实现安全传输的
linux·安全·https
BeyondESH43 分钟前
Linux线程同步—竞态条件和互斥锁(C语言)
linux·服务器·c++
wn5311 小时前
【Go - 类型断言】
服务器·开发语言·后端·golang
鱼饼6号1 小时前
Prometheus 上手指南
linux·运维·centos·prometheus
Asher Gu1 小时前
Linux系统编程入门 | 模拟实现 ls -l 命令
linux
希冀1231 小时前
【操作系统】1.2操作系统的发展与分类
后端
c无序1 小时前
【Linux进程控制】进程程序替换
linux
GoppViper2 小时前
golang学习笔记29——golang 中如何将 GitHub 最新提交的版本设置为 v1.0.0
笔记·git·后端·学习·golang·github·源代码管理
爱上语文3 小时前
Springboot的三层架构
java·开发语言·spring boot·后端·spring
serve the people3 小时前
springboot 单独新建一个文件实时写数据,当文件大于100M时按照日期时间做文件名进行归档
java·spring boot·后端