本文是参考官网,如果有理解错误欢迎指正,问题欢迎留言一起探讨
官方文档:https://docs.chef.io/server/install_server/
重要前提:修改好自己的hostname(我配置的值为chefserver,workstation和node机器都需要能解析
),时钟同步,关闭防火墙
[root@chefworkstation ~] rpm -Uvh chef-server-core-12.19.31-1.sles12.x86_64.rpm
[root@chefworkstation ~] chef-server-ctl reconfigure
[root@chefworkstation ~] mkdir -p /path/to
[root@chefworkstation ~] chef-server-ctl user-create janedoe Jane Doe janed@example.com 'abc123' --filename /path/to/janedoe.pem
[root@chefworkstation ~] chef-server-ctl org-create 4thcafe 'Fourth Cafe, Inc.' --association_user janedoe --filename /path/to/4thcafe-validator.pem
为了方便管理我创建一个cw用户,并将cw加入admin组,后续安装workstation的时候会用到cw.pem文件[root@chefworkstation ~] chef-server-ctl user-create cw chef workstation cw@example.com '111111' --filename /path/to/cw.pem
[root@chefworkstation ~] chef-server-ctl org-user-add 4thcafe cw --admin
官方文档:https://docs.chef.io/workstation/install_workstation/
重要前提:修改好自己的hostname,能解析chefserver(server机器的hostname),配置时钟同步,关闭防火墙,最小内存4G
[root@chefworkstation ~] wget https://packages.chef.io/files/stable/chef-workstation/21.2.278/el/8/chef-workstation-21.2.278-1.el7.x86_64.rpm
[root@chefworkstation ~] yum localinstall chef-workstation-21.2.278-1.el7.x86_64.rpm
[root@chefworkstation ~] chef -v
Chef Workstation version: 21.2.278
Chef Infra Client version: 16.10.8
Chef InSpec version: 4.26.4
Chef CLI version: 3.1.1
Chef Habitat version: 1.6.181
Test Kitchen version: 2.10.0
Cookstyle version: 7.8.0
查看安装目录,默认workstation是安装在/opt下[root@chefworkstation opt] ls /opt/
chef-workstation
[root@chefworkstation ~] echo 'eval "$(chef shell-init bash)"' >> ~/.bashrc
重新开一个终端(重新开一个xshell)执行which ruby[root@chefworkstation ~] which ruby
/opt/chef-workstation/embedded/bin/ruby
[root@chefworkstation ~] cd /opt/
[root@chefworkstation ~] chef generate repo chef-repo
(chef命令帮助文档:https://docs.chef.io/workstation/ctl_chef/#chef-generate-repo)[root@chefworkstation opt] ll /opt/
drwxr-xr-x 6 root root 161 Aug 13 10:08 chef-repo
drwxr-xr-x 7 root root 186 Aug 13 09:46 chef-workstation
[root@chefworkstation opt] tree chef-repo/
chef-repo/
├── chefignore
├── cookbooks
│ ├── example
│ │ ├── attributes
│ │ │ └── default.rb
│ │ ├── metadata.rb
│ │ ├── README.md
│ │ └── recipes
│ │ └── default.rb
│ └── README.md
├── data_bags
│ ├── example
│ │ └── example_item.json
│ └── README.md
├── LICENSE
├── policyfiles
│ └── README.md
└── README.md
7 directories, 11 files
[root@chefworkstation opt] knife configure
#查看内容
[root@chefworkstation cookbooks] knife config show
Loading from credentials file /root/.chef/credentials
chef_server_url: https://chefserver/organizations/4thcafe
client_key: /root/.chef/cw.pem
config_file:
log_level: warn
log_location: STDERR
node_name: cw
profile: default
[root@chefworkstation opt] ll ~/.chef/
-rw-r--r-- 1 root root 36 Aug 13 10:08 chef_guid
-rw-r--r-- 1 root root 133 Aug 13 10:35 credentials
-rw-r--r-- 1 root root 1674 Aug 13 10:52 cw.pem
[root@chefworkstation opt]
credentials:knife命令的配置文件(类似于nginx.conf这种文件),详细文件解读参考: https://docs.chef.io/workstation/config_rb/#由于server使用自签证书,需要在workstation添加信任(knife ssl fetch)
[root@chefworkstation opt] knife ssl fetch
#查看所有client,如果返回结果类似如下则表示workstation可以和server正常通信
[root@chefworkstation opt] knife client list
4thcafe-validator
官方文档:https://docs.chef.io/workstation/knife_bootstrap/
重要前提:修改好自己的hostname,能解析chefserver(server机器的hostname),配置时钟同步,关闭防火墙
chef client安装是指在被管理的node上安装客户端程序chef client,该操作是在workstation上执行命令完成
在workstation上执行命令安装client(修改命令中的IP,服务器登录的账号密码)
#192.168.16.12: node节点的IP
#chefnode2:node节点的hostname
#root: 登录node服务器的用户名
#1:登录node服务器的用户名对应的密码
[root@chefworkstation opt]knife bootstrap 192.168.16.12 -N chefnode -U root -P 1
在workstation检查node是否安装成功
[root@chefworkstation opt] knife node list
chefnode
登录node节点,检查安装目录(默认安装路径是/opt/chef)
[root@chefnode chef] ll /opt/chef/
drwxr-xr-x 2 root root 204 Aug 12 13:21 bin
drwxr-xr-x 7 root root 67 Aug 12 13:21 embedded
-rw-r--r-- 1 root root 35962 Jul 28 03:24 LICENSE
drwxr-xr-x 2 root root 8192 Aug 12 13:21 LICENSES
-rw-r--r-- 1 root root 7002 Jul 28 03:25 version-manifest.json
-rw-r--r-- 1 root root 3500 Jul 28 03:25 version-manifest.txt
[root@chefnode chef]
knife命令详解:https://docs.chef.io/workstation/knife/
接下来讲解如何编写recipe,创建文件,文件夹,安装软件包操作,总体流程如下
workstation本地创建repo->在repo中创建cookbook->编写recipe->本地执行recipe(非必须)->上传cookbook到server->node执行recipe
[root@chefworkstation cookbooks] cd /opt/
[root@chefworkstation opt] chef generate repo myrepo
[root@chefworkstation opt] cd myrepo/cookbooks/
[root@chefworkstation cookbooks] chef generate cookbook mycookbook
[root@chefworkstation cookbooks] cd mycookbook/
[root@chefworkstation mycookbook] vim recipes/default.rb
写入内容如下注意(action和:之间有空格)
#使用directory resource创建文件夹
directory '/home/resource_directory' do
action :create
end
#使用file resource创建文件
file '/home/resource_directory/resource_file' do
content 'This is created by chef!'
end
#使用bash resoure执行linux命令
bash 'excute command' do
cwd '/home/'
code <<-EOH
mkdir -p resource_bash
echo "This is created by bash resource!" >> resource_bash_file.txt
EOH
end
#使用package resource安装telnet软件包
package 'telnet' do
action :install
end
[root@chefworkstation mycookbook] pwd
/opt/myrepo/cookbooks/mycookbook
[root@chefworkstation mycookbook] chef-client -z -o mycookbook
[root@chefworkstation mycookbook] ll /home/
total 4
drwxr-xr-x 2 root root 6 Aug 19 11:40 resource_bash
-rw-r--r-- 1 root root 34 Aug 19 11:40 resource_bash_file.txt
drwxr-xr-x 2 root root 27 Aug 19 11:40 resource_directory
[root@chefworkstation mycookbook] rpm -qa |grep telnet
telnet-0.17-66.el7.x86_64
[root@chefworkstation mycookbook] knife cookbook upload mycookbook --cookbook-path /opt/myrepo/cookbooks/
Uploading mycookbook [0.1.0]
Uploaded 1 cookbook.
可选操作:命令中需要指定cookbook的路径,我们可以把这个参数写到配置文件中,以后就不需要在命令中手动传参了[root@chefworkstation mycookbook] vim /root/.chef/credentials
#添加以下一行内容:具体可以添加哪些参数,请参考前文提到的credentials文件
cookbook_path = '/opt/myrepo/cookbooks/'
到此可能你有疑问,knife这个命令是干什么用的,官方介绍如下:[root@chefworkstation mycookbook] knife cookbook list
mycookbook 0.1.0
#编辑chefnode这个机器的run list,告诉它需要执行mycookbook的默认recipe, -e vi是指定编辑器,你也可以用vim或者其他
#为了方便这个也可以写在配置文件中:在credentials文件同目录创建XXX.d文件夹,然后在里面创建XXX.rb文件,写入以下一行
#knife[:editor] = 'vi'
[root@chefworkstation mycookbook] knife node edit chefnode -e vi
修改后内容如下(修改完成后:wq保存,和本地vi文件一样) {
"name": "chefnode",
"chef_environment": "_default",
"normal": {
"tags": [
]
},
"policy_name": null,
"policy_group": null,
"run_list": [
"recipe[mycookbook::default]"
]
}
#在node机器任意目录执行都可以,执行玩后检查文件,文件夹是否创建以及telnet包是否安装。
[root@chefnode home] chef-client
至此流程操作完成,关于role,environment,policyfile等更多内容请参考官网:https://docs.chef.io/