服务器 Salt 配置管理

发表于:2023-12-14 09:37

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:Pokeya    来源:稀土掘金

  服务器 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. 名词解释
  二、Salt 配置管理
  1. Minion 端配置
  1.1 Configuring the Salt Minion
  /etc/salt/minion 文件
  Template:docs.saltproject.io/en/latest/r…
  # 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 文件
  # 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 重新管理。
  systemctl restart salt-minion.service
  2. Grains 定制开发
  2.1 Example Requirement
  编写一个判断 Keepalived 运行在哪个节点的脚本,Shell 脚本判断逻辑如下:
  #!/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
  #####      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
  # 需在master端创建标准的grains目录
  mkdir -p /srv/salt/_grains
  # 切换至该目录,编写自定义grains脚本,应存储到该路径下
  cd /srv/salt/_grains
  2.3 Create the Custom Grains Script
  编写 Grains 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 匹配器来选择要发送 Grains 的 Minion。
  # 同步grains到指定minion主机列表(角色为webserver且不是虚拟机)
  salt -G 'role:webserver && virtual:false' saltutil.sync_grains
  查看 Minion 端的缓存结果:
  # 会生成 py 和 pyc 文件
  ll /var/cache/salt/minion/extmods/grains
  调用 Salt Grains 命令:
  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 的模块:
  # 底层 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 模块位置(可参考源码来编写自定义模块):
  # 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 模块目录:
  # 需在master端创建标准的模块目录
  mkdir -p /srv/salt/_modules
  # 切换至该目录,编写自定义模块,应存储到该路径下
  cd /srv/salt/_modules
  3.3 Create the Custom Module
  编写 Salt 模块脚本文件,大概逻辑如下:
  #!/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
  自定义模块同步至指定受管主机组
  salt -G 'virtual:false' saltutil.sync_modules
  测试运行:
  # salt '【匹配主机】' 【模块文件名】.【函数名】
  salt -G 'virtual:false' mod_ethtool.show_bmcip
  三、Salt Plugins 插件
  1. Ansiblegate
  1.1 功能介绍
  salt.modules.ansiblegate 模块是 SaltStack 的一个插件,它允许使用 Ansible 的 playbook 和 roles 来管理 SaltStack 的远程主机。具体来说,它提供了以下功能:
  允许使用 Ansible playbook 和 roles 来定义和管理 SaltStack 的 states 和 pillars。
  允许在 SaltStack 的 states 和 pillars 中引用 Ansible playbook 和 roles 的变量和参数。
  允许在 SaltStack 中执行 Ansible playbook 和 roles 的任务。
  使用 salt.modules.ansiblegate 模块,可以实现 SaltStack 和 Ansible 的混合使用,以获得更强大的配置管理和自动化能力。
  例如,可以使用 Ansible 的 playbook 来定义和管理复杂的配置任务,然后使用 SaltStack 的 states 和 pillars 来引用和执行这些任务。
  1.2 执行方式
  在 Salt 中,salt.modules.ansiblegate 模块可以通过两种方式来执行 Ansible playbook 和 roles 任务:
  使用 SSH 或 WinRM 等协议连接到目标主机,然后在目标主机上执行 Ansible playbook 和 roles 的任务。
  在 Salt 的 Master 和 Minion 之间建立 SSH 隧道连接,并在 Minion 上执行 Ansible playbook 和 roles 的任务,然后将执行结果返回到 Master。
  这两者都可以实现通过 Salt 控制台来管理和执行 Ansible playbook 和 roles,但它们有不同的适用场景和要求。
  如果使用 SSH 或 WinRM 等协议连接到目标主机,则需要在目标主机上安装 Ansible 和 Python 环境,并且目标主机必须能够通过 SSH 或 WinRM 等协议连接到。这种方式适用于需要在远程主机上执行 Ansible playbook 和 roles 的任务,并且可以保证目标主机安全的情况。
  如果使用 Master-Minion 隧道连接,则可以通过 Salt Master-Minion 之间的安全连接执行 Ansible playbook 和 roles 的任务,而无需在目标主机上安装 Ansible 和 Python 环境。这种方式适用于需要在受管节点上执行 Ansible playbook 和 roles 的任务,但是受管节点上无法安装 Ansible 和 Python 环境的情况。
  需要注意的是,在使用 salt.modules.ansiblegate 模块时,要根据具体情况选择合适的方式来执行 Ansible playbook 和 roles 的任务,并保证安全和可靠性。
  四、参考命令
  # 测试联通性
  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'
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号