测试环境为ubuntu 16.04,能连公网,不需要外网ip,域名等
首先chef官网注册账号,比如叫A,用户名是a,公司是b
登录官方chef manage
https://manage.chef.io/login
可以看到组织b和用户a
点击组织右边设置下载config.rb
然后点击用户a右边设置
保存私钥,之后会用到
安装chef client
curl -L https://www.opscode.com/chef/install.sh | sudo bash
之后会在root目录下生成.chef文件夹,这里存放我们刚才保存的私钥,比如是a.pem
然后修改下config.rb的内容,同样放到这个文件夹
# See http://docs.chef.io/config_rb.html for more information on knife configuration options
current_dir = File.dirname(__FILE__)
log_level :info
log_location STDOUT
node_name "testnode"
client_key "#{current_dir}/a.pem"
chef_server_url "https://api.chef.io/organizations/b"
cookbook_path ["#{current_dir}/../nodes/cookbooks"]
修改保存完毕,将当前节点通知给chef server
机器上执行
knife bootstrap 节点ip或主机名 --sudo -x 节点用户名 -P 密码 -N testnode
添加成功后就可以在manage的控制台看到节点信息了,下面是我的节点信息供读者参考
我们在安装chef client的时候root目录已经为我们生成了nodes目录,里边有当前节点的attributes信息的json
我们在config.rb中配置的目录是/root/nodes/cookbooks,所以我们要创建这个文件夹并创建一个简单的cookbook,这里我们从超市下载一个示例,然后修改
mkdir /root/nodes/cookbooks
knife supermarket install example
修改示例cookbook
vim example/recipes/default.rb
execute 'repo_test' do
command 'netstat -nltp>> /tmp/netstat.txt'
ignore_failure true
end
上传到chef server
knife upload cookbook example
knife upload cookbook example_resources
knife cookbook list
manage上可以看到已经上传了
然后在manage的node下边编辑run_list
把cookbook拖过来
chef client运行并查看文件,成功
root@ubuntu:~/nodes/cookbooks# chef-client
Starting Chef Infra Client, version 16.7.61
Patents: https://www.chef.io/patents
resolving cookbooks for run list: ["example"]
Synchronizing Cookbooks:
- example (0.1.14)
- example_resources (1.0.14)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: example::default
* execute[repo_test] action run
- execute netstat -nltp>> /tmp/netstat.txt
Running handlers:
Running handlers complete
Chef Infra Client finished, 1/1 resources updated in 24 seconds
root@ubuntu:~/nodes/cookbooks# cat /tmp/netstat.txt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 52064/sshd
tcp6 0 0 :::22 :::* LISTEN 52064/sshd