下面用实战视角讲清楚:服务器运维中 怎样用 Chef 来做自动化配置管理。内容从「是什么 → 怎么装 → 怎么用 → 最佳实践」一步步来。
没有 Chef 时:
用 Chef 后:
典型用途:
| 概念 | 作用 |
|---|---|
| Cookbook | 配置包(像“软件安装说明书”) |
| Recipe | Cookbook 里的具体步骤 |
| Resource | 最小操作单元(package / file / service) |
| Node | 被管理的服务器 |
常用架构:
Chef Server(可选)
↓
Chef Client(装在被管服务器)
↓
Cookbook → Recipe → Resource
适合:
# CentOS / Ubuntu
curl https://omnitruck.chef.io/install.sh | sudo bash -s -- -P chef-workstation
验证:
chef --version
chef generate cookbook my_server
目录结构:
my_server/
├── recipes/
│ └── default.rb
├── attributes/
├── templates/
recipes/default.rb
package 'nginx' do
action :install
end
service 'nginx' do
action [:enable, :start]
end
file '/var/www/html/index.html' do
content 'Hello from Chef'
end
chef-client --local-mode --runlist 'recipe[my_server]'
✅ 服务器自动:
Chef Server
├── 管理 Cookbook
├── 记录 Node 状态
└── 下发配置
节点注册:
knife bootstrap 192.168.1.10 \
-x root -P password \
-N web01 \
-r 'recipe[my_server]'
chef-client# 安装软件
package 'docker'
# 创建用户
user 'deploy' do
home '/home/deploy'
shell '/bin/bash'
end
# 写文件
template '/etc/nginx/nginx.conf' do
source 'nginx.conf.erb'
end
# 执行命令
execute 'reload nginx' do
command 'nginx -s reload'
action :run
end
✅ 一切配置写 Cookbook,不手工改服务器
✅ Cookbook 进 Git,版本可控
✅ 先 test kitchen 测试,再上生产
✅ 用 roles / environments 区分 dev / prod
✅ 敏感信息用 chef-vault / encrypted data bag
✔ 服务器多
✔ 配置复杂
✔ 强调一致性
✔ 有 DevOps 意识
❌ 不适合:
如果你愿意,我可以:
你现在是 个人服务器 还是 公司运维环境?
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。