Chef 与 Salt
chef、salt;声明式配置。
30 分钟
Chef 与 Salt
Chef 与 Salt 都是成熟的 Master-Agent(或 Masterless) 配置管理工具:Chef 以 Ruby DSL Recipe 著称,Salt 以 YAML State + 高速消息总线 见长。二者都支持 声明式 描述期望状态,在企业裸机与混合云 VM 场景仍广泛使用。本课衔接 dcp-intro 的 CM 概念,并与 terraform-essentials 创建的 VM 对接;容器层见 kubernetes-essentials,云选型见 devops-cloud-essentials。
学习目标
- 读懂 Chef Cookbook / Recipe / Resource 结构与 Chef Server 工作流
- 理解 Salt State SLS、Grain、Pillar 与
salt '*' state.apply推送模型 - 对比 Chef 与 Salt 在语言、扩展性、学习曲线上的差异
Chef 核心模型
# recipes/default.rb — 安装并启动 Nginx
package 'nginx' do
action :install
end
service 'nginx' do
action [:enable, :start]
subscribes :restart, 'template[/etc/nginx/nginx.conf]', :delayed
end
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
variables(server_name: 'coderoadmap.local')
notifies :restart, 'service[nginx]', :delayed
end
# metadata.rb 片段
name 'coderoadmap_nginx'
version '1.0.0'
depends 'nginx', '~> 9.0'
Chef Infra Server 存 Cookbook 与 Node 对象;Agent(chef-client)周期拉取 Run List 并收敛。Test Kitchen + InSpec 常用于 Cookbook 测试。
Salt 核心模型
# salt/nginx/init.sls
nginx_pkg:
pkg.installed:
- name: nginx
nginx_service:
service.running:
- name: nginx
- enable: True
- require:
- pkg: nginx_pkg
nginx_config:
file.managed:
- name: /etc/nginx/conf.d/coderoadmap.conf
- source: salt://nginx/files/coderoadmap.conf.jinja
- template: jinja
- context:
upstream: "{{ pillar['backend_host'] }}:8080"
- watch_in:
- service: nginx_service
# pillar/coderoadmap.sls(敏感/环境差异放 Pillar)
backend_host: 127.0.0.1
env: staging
Salt Master 通过 ZeroMQ 或 TCP 与 Minion 通信;Pillar 类似 Chef 的 data bag / role 变量,Grain 是 Minion 自动采集的静态事实(OS、IP)。