在Chef客户端节点上配置Chef Knife,上传Cookbooks并运行配方
在上一篇文章中,我们研究了如何在Ubuntu 20.04,Ubuntu 18.04和CentOS 8上安装Chef Server。
我还将尝试制作在CentOS 7上Chef Server的安装指南。
这篇有关如何配置Chef Knife,编写测试Cookbook并将其上传到Chef Server并最终在服务器(节点)上运行
设置准备工作
安装的Chef Server和Workstation:Ubuntu 20.04,Ubuntu 18.04和CentOS 8
对于Arch Linux用户,请使用:如何在Arch Linux上安装Chef Development Kit
安装Chef Workstation后,我们将获得" knife"命令。
在继续本指南之前,请确保已安装Chef Server和Configured Chef Workstation。
一切准备就绪后,我们就可以开始研究knife的设置和用法了。
knife简介
Knife是一个命令行工具,可在工作站和Chef服务器之间提供接口。
通过刀子,我们可以将菜谱上传到Chef服务器,并与所管理的节点进行交互。
总之,可以使用刀子来管理:节点–由ChefCookbook和菜谱管理的服务器角色,环境和数据包各种云环境中的资源。
将Chef客户端安装到节点上在Chef服务器刀上搜索索引数据需要使用两个文件来对Chef服务器进行身份验证RSA私钥:对Chef服务器的每个请求都通过RSA公钥对进行身份验证.Chef服务器保存了公共部分;我们拥有私人部分2.
刀配置文件配置文件通常命名为 knife.rb
。
此配置文件包含信息,例如Chef服务器的URL,RSA私钥的位置以及菜谱的默认位置。
这两个文件通常都位于一个名为 .chef
默认情况下,每一次刀运行,它都会在当前工作目录中查找 .chef
目录如果 .chef
目录不存在,小刀在目录树中搜索.chef目录
配置knife环境(在工作站计算机上)
在本部分中,我们将配置Knife,使其能够与Chef Server进行通信。
步骤1:生成Chef存储库目录
首先在工作站计算机上生成Chef存储库
chef generate repo chef-repo cd chef-repo
厨师仓库目录应该有
$ls -1 chefignore cookbooks data_bags environments LICENSE README.md roles
步骤2:配置Git
ChefDK将Git组件添加到工作站,并在用于生成Chef存储库的目录中初始化Git存储库。
我们唯一要做的工作就是配置Git添加用户名和电子邮件,以及添加和提交生成的任何新文件。
git config --global user.name gitusername git config --global user.email Hyman@theitroad
步骤3:将.chef目录添加到.gitignore文件
我们需要告诉Git忽略对所有文件的跟踪 .chef
目录
echo ".chef" > .gitignore
添加文件并提交
git add . git commit -m "initial commit"
步骤4:配置刀
创建一个 .chef
文件夹内的目录 chef-repo
。
cd chef-repo mkdir .chef cd .chef
这 .chef
目录应包含两个文件:刀配置文件, knife.rb
RSA私钥从Chef服务器下载RSA私钥–这是在Chef服务器安装期间生成的,请参阅如何在Ubuntu 18.04 LTS上安装Chef Automation Server。
$scp chef-server:/home/chefadmin.pem .
代替 chef-server
与厨师服务器地址,以及 /home/chefadmin.pem
与私钥的位置一起创建 knife.rb
文件
$vim knife.rb
添加类似于以下内容的内容
current_dir = File.dirname(__FILE__) log_level :info log_location STDOUT node_name "chefadmin" client_key "#{current_dir}/chefadmin.pem" chef_server_url "https://chef-server/organizations/mycompany" cookbook_path ["#{current_dir}/../cookbooks"]
mycompany应该与在Chef服务器上创建的组织名称匹配chef服务器是Chef服务器的域名-在工作站计算机上可解析chefadmin应该是在chef服务器上创建的用户名我们也可以使用Organization Validator,但是首先下载验证者私钥。
$scp chef-server:/home/mycompany-validator.pem .
然后配置刀:
current_dir = File.dirname(__FILE__) log_level :info log_location STDOUT node_name 'chefadmin' client_key "#{current_dir}/chefadmin.pem" validation_client_name 'mycompany-validator' validation_key "#{current_dir}/mycompany-validator.pem" chef_server_url "https://chef-server/organizations/mycompany" cookbook_path ["#{current_dir}/../cookbooks"]
从Chef服务器中获取SSL证书。
$knife ssl fetch
验证下载的SSL证书
$knife ssl check Connecting to host chef-server:443 Successfully verified certificates from `chef-server' $file trusted_certs/chef-server.crt trusted_certs/chef-server.crt: PEM certificate
通过运行客户端列表,确认knife.rb的设置正确:
$knife client list
此命令应输出验证者名称。
编写测试厨师食谱
在本节中,我们将创建一个简单的食谱,以使用以下命令安装和配置Apache Web服务器: Hello Chef World
网页。
步骤1:产生食谱
使用命令语法生成食谱:
chef generate cookbook COOKBOOK_PATH/COOKBOOK_NAME (options)
举例来说,我们将菜谱命名为 apache_server
$cd chef-repo $chef generate cookbook cookbooks/install_apache Generating cookbook apache_server - Ensuring correct cookbook file content - Ensuring delivery configuration - Ensuring correct delivery build cookbook content Your cookbook is ready. Type `cd cookbooks/install_apache` to enter it. There are several commands you can run to get started locally developing and testing your cookbook. Type `delivery local --help` to see a full list. Why not start by writing a test? Tests for the default recipe are stored at: test/integration/default/default_test.rb If you'd prefer to dive right in, the default recipe can be found at: recipes/default.rb
步骤2:产生HTML索引页面范本
食谱模板是用于动态生成静态文本文件的嵌入式Ruby(ERB)模板。
产生 index.html
模板将被复制到Apache服务器
$cd cookbooks $chef generate template install_apache index.html Recipe: code_generator::template * directory[./install_apache/templates] action create - create new directory ./install_apache/templates * template[./install_apache/templates/index.html.erb] action create - create new file ./install_apache/templates/index.html.erb - update content in file ./install_apache/templates/index.html.erb from none to e3b0c4 (diff output suppressed by config)
编辑档案 index.html.erb
$vim install_apache/templates/index.html.erb
这是一个示例
<html> <body> <h1>Hello Chef World from <%= node['fqdn'] %></h1> </body> </html>
<%=%>语法使我们可以在模板文件中提供占位符。
当Chef-client运行时,占位符将替换为其值 node['fqdn']
节点的标准域名。
除非另行设置,否则将其用作节点的名称。
步骤3:为Apache创建配方
现在创建一个具有资源定义的Apache Web服务器配方:安装基本的系统软件包– vim,bash-completion,curl&wget安装Apache Web服务器软件包– CentOS/RHEL/Fedora的httpd和Debian系列的apache2启动Apache服务并将其设置为从bootCopy开始 index.html.erb
模板到 /var/www/html/index.html
这是完整的配方文件 install_apache/recipes/default.rb
## Cookbook:: install_apache # Recipe:: default ## Copyright:: 2016, The Authors, All Rights Reserved. # Install basic packages package 'Install basic packages' do package_name %w(vim wget curl bash-completion) end # Install Apache web server package 'Install Apache web server' do case node[:platform] when 'redhat', 'centos', 'fedora' package_name 'httpd' when 'ubuntu', 'debian' package_name 'apache2' end end # Start and enable the service service 'Start and enable apache service' do case node[:platform] when 'redhat', 'centos', 'fedora' service_name 'httpd' when 'ubuntu', 'debian' service_name 'apache2' end action [:enable, :start] end # Copy apache template template '/var/www/html/index.html' do source 'index.html.erb' mode '0644' case node[:platform] when 'redhat', 'centos', 'fedora', 'scientific' owner 'apache' group 'apache' when node[:platform] owner 'www-data' group 'www-data' end end
另外,编辑元数据文件以指定食谱版本和相同的Git存储库URL。
vim install_apache/metadata.rb
我的内容如下:
################################ name 'install_apache' maintainer 'Josphat Mutai' maintainer_email 'Hyman@theitroad' license 'All Rights Reserved' description 'Installs/Configures install_apache' long_description 'Installs/Configures Apache Web Server' version '0.1.0' chef_version '>= 13.0' # The `issues_url` points to the location where issues for this cookbook are # tracked. A `View Issues` link will be displayed on this cookbook's page when # uploaded to a Supermarket. ## issues_url 'https://github.com/<insert_org_here>/install_apache/issues' # The `source_url` points to the development repository for this cookbook. A # `View Source` link will be displayed on this cookbook's page when uploaded to # a Supermarket. ## source_url 'https://github.com/<insert_org_here>/install_apache'
步骤4:将食谱上传到Chef服务器
我们已经准备好要上传到Chef Server的测试食谱。
$knife cookbook upload install_apache Uploading install_apache [0.1.0] Uploaded 1 cookbook.
通过在Chef服务器上列出食谱来进行确认
$knife cookbook list chef-client 11.0.1 cron 6.2.1 install_apache 0.1.0 logrotate 2.2.0
引导节点
这 knife bootstrap
是用于引导节点的命令。
使用此命令时,我们可以根据通常通过SSH连接到节点的方式指定参数。
我们可以通过以下方式连接到节点:基于密钥的身份验证密码身份验证通常建议通过基于密钥的身份验证而不是密码身份验证,因为它更安全,但是我们可以使用这两种方法来引导节点。
无论采用哪种方法, --node-name
参数唯一地标识Chef服务器的节点,其值可以是我们想要的任何值。 knife bootstrap
命令
--ssh-user --sudo --node-name --run-list
特定于基于密钥的身份验证的选项
--identity-file
密码验证专用的选项
--ssh-password --use-sudo-password
使用密码身份验证引导节点
命令语法为:
$knife bootstrap ADDRESS --ssh-user USER --sudo --node-name nodename --run-list 'recipe[recipe-name]'
用逗号分隔多个食谱
'recipe[nginx],recipe[php-fpm],recipe[haproxy]'
对于我们的示例案例,我们将使用:
$knife bootstrap 192.168.18.9 -x vagrant --sudo -P 'vagrant' -N centos-01 -r 'recipe[install_apache]'
替换:将ADDRESS替换为远程节点的外部地址,将USER替换为用户名centos-01,将节点名称替换为install_apache并使用菜谱名称要使用root用户
$knife bootstrap 192.168.18.11 -x root -P 'password' -N centos-01 \ -r 'recipe[install_apache]'
如果我们收到类似的错误消息 ERROR: Net::SSH::HostKeyMismatch: fingerprint ..does not match for "IP"
,我们可能需要从计算机中删除有问题的密钥 ~/.ssh/config
使用:
$ssh-keygen -R IPADDRESS
或者使用 --no-host-key-verify
选项(不建议使用,因为安全性较低)
$knife bootstrap IPADDRESS --no-host-key-verify <OPTIONS>
使用密钥身份验证进行引导
$knife bootstrap 192.168.18.9 -x vagrant --sudo --identity-file ~/.ssh/private_key \ -N centos-01 -r 'recipe[install_apache]'
Knife Node Bootstrap将:登录到节点安装Chef-client配置Chef-client运行Chef-client输出示例:
要确认结果,请检查节点是否与Chef服务器关联。
$knife node list centos-01
我们可以使用以下命令 knife node show
查看有关节点的数据。
$knife node show centos-01 Node Name: centos-01 Environment: _default FQDN: cent-01 IP: 192.168.121.136 Run List: recipe[install_apache] Roles: Recipes: install_apache, install_apache::default Platform: centos 7.5.1804 Tags:
在浏览器上打开节点IP地址,以查看我们的网页是否正常运行
更新我们节点的配置
命令 knife ssh
使我们可以在菜谱更改时更新节点的配置。
使用具有属性的密码身份验证来更新节点
knife ssh 'name:nodename' 'sudo chef-client' --ssh-user USER --ssh-password 'PASSWORD' --attribute ipaddress
删除节点数据
Chef在被管理的节点和被授权对Chef服务器进行API调用的客户端之间进行区分。
通过运行以下命令删除节点数据:
knife node delete nodename --yes knife client delete nodename --yes
这 knife node delete
从Chef服务器中删除节点的元数据,然后 knife client delete
从Chef服务器的API客户端列表中删除条目(包括RSA密钥对的公共部分)。
删除Chef Cookbook /角色和RSA私钥
从Chef服务器删除菜谱
knife cookbook delete cookbookname --all --yes
如果我们省略 --all
参数,系统会提示我们选择要删除的版本要从Chef服务器中删除角色
$knife role delete myrole --yes
从节点中删除RSA私钥在引导过程中,会在节点上生成一个RSA私钥,以使节点能够对Chef服务器进行API调用。
该密钥保存到 /etc/chef/client.pem
在Linux系统上,例如,如果我们打算第二次引导节点来练习该过程,则需要登录到该节点并删除RSA私钥文件,如下所示
$sudo rm /etc/chef/client.pem