Salt Master 集群:共享文件系统、动态加入与隔离存储迁移

与 Salt 传统的高可用方案相比,Master 集群有几个优势。集群放在负载均衡器之后,Minion 只需要知道负载均衡器的 IP 地址;添加或移除 Master 不必重新配置 Minion。集群中的 Master 还会共同承担所有作业的负载,使管理员更容易扩容,以处理更多 Minion 和更大的作业。

最低要求

集群需要 TCP 负载均衡器,转发到各 Master 的发布端口与请求端口(通常为 4505、4506),并且节点之间需要可靠的局域网。除此之外,每个节点必须访问同一套身份与服务材料:

  • cluster_pki_dir:共享集群公钥、私钥及 Minion 密钥;
  • cachedir:作业和 grain 缓存;
  • 集群提供的 file_roots 与 pillar_roots 目录树。

这些材料可以通过两种方式提供:

  • 共享文件系统(默认):每个节点在 cluster_pki_dir、cachedir、file_roots 和 pillar_roots 挂载同一个 NFS、Gluster 等共享存储。这是最初的 Master 集群模式,也是本教程用 Gluster 和 HAProxy 演示的拓扑。
  • 隔离文件系统(3008.0 及以后):各节点将 cluster_isolated_filesystem 设为 True。每个 Master 保留本地的上述目录;新加入的 Master 在被提升为 Raft 投票节点前,经由集群传输从已有节点同步密钥、拒绝的密钥、file_roots 和 pillar_roots。作业与缓存状态通过 Raft 和 HashRing 层在 Master 之间流转。

集群中的每个 Master 保有自己的公钥、私钥和内存中的 AES 密钥,同时也能访问保存集群级公钥、私钥的 cluster_pki_dir。集群级 AES 密钥在此生成并保存。以集群模式工作时,Minion 密钥也保存在 cluster_pki_dir,而不是单个 Master 的 pki_dir。

拓扑:共享文件系统与隔离文件系统

隔离文件系统模式及 cluster_isolated_filesystem 是 3008.0 引入的功能。

这两种拓扑只在向 Master 提供所需内容的方式上不同;节点间的线协议、cluster_pool_port 承载的 Raft RPC,以及负载均衡器配置都相同。

项目 共享文件系统 隔离文件系统
cluster_pki_dir 内容 所有节点使用同一路径 各节点使用本地目录
cachedir 内容 所有节点使用同一路径 各节点使用本地目录
file_roots / pillar_roots 所有节点使用同一路径 各节点本地保存;加入时通过集群传输推送,之后通过 salt-run cluster.sync_roots 同步
作业 / grain 缓存路由 经由共享 cachedir 经由 Raft + HashRing
建议的 keys.cache_driver localfs_key(默认) mmap_key,参见内存映射缓存后端
添加 Master 挂载共享存储后启动 动态加入,自动执行状态同步

选择时可以考虑:

  • 如果已经运行可靠的集群文件系统,并希望集中编辑 file_roots 与 pillar_roots,共享方式合适;共享文件系统失效会导致整个集群失效。
  • 隔离方式消除了共享存储依赖,新 Master 可以从仅安装 Salt 的机器加入。代价是在单个 Master 修改 file_roots 或 pillar_roots 后,必须显式执行 salt-run cluster.sync_roots 将修改传播到其他节点,修改不会立即出现在所有节点上。

无论采用哪种拓扑,cluster_pool_port 都承载节点间的 Raft RPC;4505、4506 经由负载均衡器承载 Minion 的发布和返回流量;cluster_secret 用来认证申请加入的新 Master。

参考实现

Gluster 的配置参见官方快速入门。

HAProxy 配置:

frontend salt-master-pub
    mode tcp
    bind 10.27.5.116:4505
    option tcplog
    # This timeout is equal to the publish_session setting of the
    # masters.
    timeout client 86400s
    default_backend salt-master-pub-backend

backend salt-master-pub-backend
    mode tcp
    #option log-health-checks
    log global
    balance roundrobin
    timeout connect 10s
    # This timeout is equal to the publish_session setting of the
    # masters.
    timeout server 86400s
    server rserve1 10.27.12.13:4505 check
    server rserve2 10.27.7.126:4505 check
    server rserve3 10.27.3.73:4505 check

frontend salt-master-req
    mode tcp
    bind 10.27.5.116:4506
    option tcplog
    timeout client  1m
    default_backend salt-master-req-backend

backend salt-master-req-backend
    mode tcp
    log global
    balance roundrobin
    timeout connect 10s
    timeout server 1m
    server rserve1 10.27.12.13:4506 check
    server rserve2 10.27.7.126:4506 check
    server rserve3 10.27.3.73:4506 check

Master 配置:

id: 10.27.12.13
cluster_id: master_cluster
cluster_peers:
  - 10.27.7.126
  - 10.27.3.73
cluster_pki_dir: /my/gluster/share/pki
cachedir: /my/gluster/share/cache
file_roots:
  base:
    - /my/gluster/share/srv/salt
pillar_roots:
  base:
    - /my/gluster/share/srv/pillar

以上地址、共享目录均是官方示例,需要按实际拓扑调整;端口角色和超时配置关系应保持一致。

动态加入

动态加入在 3008.0 引入。新的 Master 无需重新配置已有节点,就能加入运行中的集群。新节点需要与集群一致的 cluster_id、cluster_pki_dir 和 cluster_secret,并在 cluster_peers 中至少配置一个可达节点,不必事先列出完整成员。

启动时,新 Master 与这些节点进行 discover/join 握手。成功后,它取得共享的集群公钥与当前内存中的 AES 会话密钥,并被加入每个节点的 cluster_peers。

加入节点的配置:

id: 10.27.9.42
cluster_id: master_cluster
cluster_peers:
  - 10.27.12.13
cluster_pki_dir: /my/gluster/share/pki
cluster_secret: "d8b4c2e1f07a4c3e8a1b5d0a9c7f3e42b6d9a1c4f8e2b7d0a3c6e9f1b4d7a0c3"
cachedir: /my/gluster/share/cache

上面的 cluster_secret 是官方公开示例值,不能作为部署中的真实共享秘密。将新 Master 加入负载均衡器的后端池,让发布和返回流量能够到达它。

安全要求:

  • cluster_secret 负责加入认证。生产环境必须设置高熵值;空或未设置的值会匹配对端的空秘密,相当于没有认证。
  • discover 和 join 载荷分别由各 Master 签名;cluster_secret、AES 会话密钥和集群密钥用接收方的公钥加密。集群传输必须限制在可信网络内:同时掌握 cluster_secret 和传输访问权的攻击者仍可以加入集群。
  • 新 Master 通常从共享的 cluster_pki_dir 读取集群公钥。无法访问该目录时,用 cluster_pub_fingerprint 固定公钥指纹。

移除节点时,先将它从负载均衡器移除,停止该 Master,删除它的 cluster_pki_dir/peers/<peer_id>.pub,再重启剩余 Master。要防止移除的节点重新加入,还应轮换 cluster_secret。

从共享文件系统集群迁移

cluster_isolated_filesystem、pki.migrate_to_mmap 及下述集群 runner 都在 3008.0 引入。

在负载均衡器逐台排空 Master 的前提下,以下步骤将运行中的共享存储集群转为隔离文件系统模式,使 Minion 不感知停机。是否能满足这一前提,仍取决于实际负载均衡、剩余容量与集群健康状况。

  1. 将密钥缓存驱动切换为 mmap_key。集群仍使用共享文件系统时,在一台 Master 运行:

    salt-run pki.migrate_to_mmap
    

    这会把所有已接受、待接受、已拒绝与已驳回的 Minion 密钥,从磁盘上的 localfs_key 布局转换为内存映射缓存后端描述的 mmap_key 布局。共享文件系统中现在保存着各节点都能读取的 mmap 数据块。

  2. 更新各节点的 Master 配置。每台 Master 添加:

    cluster_isolated_filesystem: True
    keys.cache_driver: mmap_key
    

    此时让 cluster_pki_dir、cachedir、file_roots 和 pillar_roots 继续指向共享路径;接下来再将它们迁到本地。

  3. 逐台迁移节点,每次只处理一台。

    1. 在负载均衡器中排空该节点,使 Minion 停止向它发送流量。
    2. 停止 salt-master。
    3. 将共享挂载中的 cluster_pki_dir 与 cachedir 复制到本地路径,并修改配置指向本地副本。也可以同时复制 file_roots 和 pillar_roots 并改用本地路径。
    4. 启动 salt-master。它以 learner 身份重新加入,从其他节点经集群传输同步密钥、拒绝的密钥、file_roots 和 pillar_roots,再提升回 voter。
    5. 将它重新加入负载均衡器。
  4. 验证新拓扑。在任一 Master 运行:

    salt-run cluster.members
    salt-run cluster.ring_info
    

    cluster.members 显示所有 Raft voter,以及尚未追上的 learner;cluster.ring_info 显示把作业和 grain 缓存路由到指定节点的 HashRing 状态。两者都应列出所有节点,并且没有卡住的 learner。

  5. 最后移除共享挂载。所有节点完全改用本地路径、集群健康后,才卸载共享文件系统。把这一步留到最后,能让逐台迁移过程中遇到问题的节点继续退回共享存储。

迁移完成后:

  • 在单个 Master 编辑 file_roots 或 pillar_roots 后,显式推送到其他节点:

    salt-run cluster.sync_roots
    

    该 runner 经由与加入时状态同步相同的加密集群传输向节点分发内容。查看每个节点的 Master 日志,用 state-sync ... installed N items 记录确认送达。

  • 添加新 Master 时,动态加入流程会自动执行状态同步,不需要额外 runner。

来源:Salt Project:Master Cluster,Salt Project 贡献者。官方文档版权页标注 Broadcom;上游 NOTICE 保留 Copyright 2012-2022 VMware, Inc.。原文及配置依据上游 Apache License 2.0 提供。本版本翻译完整正文与对比表,保留全部配置块,补充公开示例秘密不能用于部署及迁移适用条件。2026-10-03 核对 latest 文档;仅完成静态核验,未搭建或迁移集群。

Salt 上游 NOTICE
Copyright 2012-2022 VMware, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apache License 2.0 许可全文
                                 Apache License
                           Version 2.0, January 2004
                        http://www.apache.org/licenses/

   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

   1. Definitions.

      "License" shall mean the terms and conditions for use, reproduction,
      and distribution as defined by Sections 1 through 9 of this document.

      "Licensor" shall mean the copyright owner or entity authorized by
      the copyright owner that is granting the License.

      "Legal Entity" shall mean the union of the acting entity and all
      other entities that control, are controlled by, or are under common
      control with that entity. For the purposes of this definition,
      "control" means (i) the power, direct or indirect, to cause the
      direction or management of such entity, whether by contract or
      otherwise, or (ii) ownership of fifty percent (50%) or more of the
      outstanding shares, or (iii) beneficial ownership of such entity.

      "You" (or "Your") shall mean an individual or Legal Entity
      exercising permissions granted by this License.

      "Source" form shall mean the preferred form for making modifications,
      including but not limited to software source code, documentation
      source, and configuration files.

      "Object" form shall mean any form resulting from mechanical
      transformation or translation of a Source form, including but
      not limited to compiled object code, generated documentation,
      and conversions to other media types.

      "Work" shall mean the work of authorship, whether in Source or
      Object form, made available under the License, as indicated by a
      copyright notice that is included in or attached to the work
      (an example is provided in the Appendix below).

      "Derivative Works" shall mean any work, whether in Source or Object
      form, that is based on (or derived from) the Work and for which the
      editorial revisions, annotations, elaborations, or other modifications
      represent, as a whole, an original work of authorship. For the purposes
      of this License, Derivative Works shall not include works that remain
      separable from, or merely link (or bind by name) to the interfaces of,
      the Work and Derivative Works thereof.

      "Contribution" shall mean any work of authorship, including
      the original version of the Work and any modifications or additions
      to that Work or Derivative Works thereof, that is intentionally
      submitted to Licensor for inclusion in the Work by the copyright owner
      or by an individual or Legal Entity authorized to submit on behalf of
      the copyright owner. For the purposes of this definition, "submitted"
      means any form of electronic, verbal, or written communication sent
      to the Licensor or its representatives, including but not limited to
      communication on electronic mailing lists, source code control systems,
      and issue tracking systems that are managed by, or on behalf of, the
      Licensor for the purpose of discussing and improving the Work, but
      excluding communication that is conspicuously marked or otherwise
      designated in writing by the copyright owner as "Not a Contribution."

      "Contributor" shall mean Licensor and any individual or Legal Entity
      on behalf of whom a Contribution has been received by Licensor and
      subsequently incorporated within the Work.

   2. Grant of Copyright License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      copyright license to reproduce, prepare Derivative Works of,
      publicly display, publicly perform, sublicense, and distribute the
      Work and such Derivative Works in Source or Object form.

   3. Grant of Patent License. Subject to the terms and conditions of
      this License, each Contributor hereby grants to You a perpetual,
      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
      (except as stated in this section) patent license to make, have made,
      use, offer to sell, sell, import, and otherwise transfer the Work,
      where such license applies only to those patent claims licensable
      by such Contributor that are necessarily infringed by their
      Contribution(s) alone or by combination of their Contribution(s)
      with the Work to which such Contribution(s) was submitted. If You
      institute patent litigation against any entity (including a
      cross-claim or counterclaim in a lawsuit) alleging that the Work
      or a Contribution incorporated within the Work constitutes direct
      or contributory patent infringement, then any patent licenses
      granted to You under this License for that Work shall terminate
      as of the date such litigation is filed.

   4. Redistribution. You may reproduce and distribute copies of the
      Work or Derivative Works thereof in any medium, with or without
      modifications, and in Source or Object form, provided that You
      meet the following conditions:

      (a) You must give any other recipients of the Work or
          Derivative Works a copy of this License; and

      (b) You must cause any modified files to carry prominent notices
          stating that You changed the files; and

      (c) You must retain, in the Source form of any Derivative Works
          that You distribute, all copyright, patent, trademark, and
          attribution notices from the Source form of the Work,
          excluding those notices that do not pertain to any part of
          the Derivative Works; and

      (d) If the Work includes a "NOTICE" text file as part of its
          distribution, then any Derivative Works that You distribute must
          include a readable copy of the attribution notices contained
          within such NOTICE file, excluding those notices that do not
          pertain to any part of the Derivative Works, in at least one
          of the following places: within a NOTICE text file distributed
          as part of the Derivative Works; within the Source form or
          documentation, if provided along with the Derivative Works; or,
          within a display generated by the Derivative Works, if and
          wherever such third-party notices normally appear. The contents
          of the NOTICE file are for informational purposes only and
          do not modify the License. You may add Your own attribution
          notices within Derivative Works that You distribute, alongside
          or as an addendum to the NOTICE text from the Work, provided
          that such additional attribution notices cannot be construed
          as modifying the License.

      You may add Your own copyright statement to Your modifications and
      may provide additional or different license terms and conditions
      for use, reproduction, or distribution of Your modifications, or
      for any such Derivative Works as a whole, provided Your use,
      reproduction, and distribution of the Work otherwise complies with
      the conditions stated in this License.

   5. Submission of Contributions. Unless You explicitly state otherwise,
      any Contribution intentionally submitted for inclusion in the Work
      by You to the Licensor shall be under the terms and conditions of
      this License, without any additional terms or conditions.
      Notwithstanding the above, nothing herein shall supersede or modify
      the terms of any separate license agreement you may have executed
      with Licensor regarding such Contributions.

   6. Trademarks. This License does not grant permission to use the trade
      names, trademarks, service marks, or product names of the Licensor,
      except as required for reasonable and customary use in describing the
      origin of the Work and reproducing the content of the NOTICE file.

   7. Disclaimer of Warranty. Unless required by applicable law or
      agreed to in writing, Licensor provides the Work (and each
      Contributor provides its Contributions) on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
      implied, including, without limitation, any warranties or conditions
      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
      PARTICULAR PURPOSE. You are solely responsible for determining the
      appropriateness of using or redistributing the Work and assume any
      risks associated with Your exercise of permissions under this License.

   8. Limitation of Liability. In no event and under no legal theory,
      whether in tort (including negligence), contract, or otherwise,
      unless required by applicable law (such as deliberate and grossly
      negligent acts) or agreed to in writing, shall any Contributor be
      liable to You for damages, including any direct, indirect, special,
      incidental, or consequential damages of any character arising as a
      result of this License or out of the use or inability to use the
      Work (including but not limited to damages for loss of goodwill,
      work stoppage, computer failure or malfunction, or any and all
      other commercial damages or losses), even if such Contributor
      has been advised of the possibility of such damages.

   9. Accepting Warranty or Additional Liability. While redistributing
      the Work or Derivative Works thereof, You may choose to offer,
      and charge a fee for, acceptance of support, warranty, indemnity,
      or other liability obligations and/or rights consistent with this
      License. However, in accepting such obligations, You may act only
      on Your own behalf and on Your sole responsibility, not on behalf
      of any other Contributor, and only if You agree to indemnify,
      defend, and hold each Contributor harmless for any liability
      incurred by, or claims asserted against, such Contributor by reason
      of your accepting any such warranty or additional liability.

   END OF TERMS AND CONDITIONS

   APPENDIX: How to apply the Apache License to your work.

      To apply the Apache License to your work, attach the following
      boilerplate notice, with the fields enclosed by brackets "[]"
      replaced with your own identifying information. (Don't include
      the brackets!)  The text should be enclosed in the appropriate
      comment syntax for the file format. We also recommend that a
      file or class name and description of purpose be included on the
      same "printed page" as the copyright notice for easier
      identification within third-party archives.

   Copyright [yyyy] [name of copyright owner]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容