- Bootstrap another node with your chef-server.
If you get the following error:
Failed to read the private key /etc/chef/validation.pem: # In the same error logs you will see a line:
Delete your validation key in order to use your user credentials instead
Please check that you don't have any validation.pem key in /etc/chef, if any, then delete it.
You can verify by executing one of the following:
knife client list
knife node show chef-node2 - Create a new cookbook postgresql:
chef generate cookbook cookbooks/postgresql - Edit cookbooks/postgresql/recipes/default.rb
[code]
package 'postgresql-server' do
notifies :run, 'execute[postgresql-init]'
end
execute 'postgresql-init' do
command 'postgresql-setup initdb'
action :nothing
end
service 'postgresql' do
action [:enable, :start]
end
<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
[/code]
The notifies :run, 'execute[postgresql-init]' will send a run signal to the execute task and execute the init command only when the package is installed.
Check it with foodcritic and then push it to the git repo. - Login to your chef-manage UI and select your node, click on "Edit run list" and remove all the recipes associated with it.
- In your chef-workstation we will now create a role. Execute the following command:
knife role create web
You will get the following error:
ERROR: RuntimeError: Please set EDITOR environment variable
We will have to set a default editor. Here we'll use vi.
Execute the following commands:
vi ~/.bashrc
export EDITOR= $(which vi)
source ~/.bashrc
Then execute the create role command.
This will openup the role file in vi editor with the following content:
[code]
{
"name": "web",
"description": "",
"json_class": "Chef::Role",
"default_attributes": {
},
"override_attributes": {
},
"chef_type": "role",
"run_list": [
],
"env_run_lists": {
}
}
<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
[/code] - Add the following in the run list section:
"recipe[apache]",
"recipe[apache::websites]",
"recipe[apache::motd]"
Save and close. - You can check the run list for your role in your chef-manage UI in policies section in roles.
- You can also edit the existing role with command:
knife role edit web - You can add the role to the node with the following command:
knife node run_list set chef-node2 "role[web]" - You can check the newly added role with the following command:
knife node show chef-node2 - Then we need to issue the chef-client command from the workstation for all the nodes having role "web".
knife ssh "role:web" "sudo chef-client" -x gslab -P
If you get the following error:
WARNING: Failed to connect to chef-node2.local -- SocketError: getaddrinfo: Name or service not known.
Then add the host chef-node2.local to /etc/hosts file. Then execute the knife ssh command again. - Then upload the postgresql cookbook:
knife cookbook upload postgresql - Create role database:
knife create role database - The add the role to a node:
knife node run_list set chef-node "role[postgresql]" - Then edit the postgresql default.rb recipe. Edit the line to match the following:
notifies :run, 'execute[postgresql-init]', :immediately
Then upload the cookbook: knife cookbook upload postgresql - Then execute the following command:
knife ssh "role:database" "sudo chef-client" -x gslab -P
Search
- Edit the websites.rb recipe in the apache cookbook.
Add the following after the file section in it:
[code]
webnodes = search("node", "role:web")
webnodes.each do |node|
puts node
end
<span data-mce-type="bookmark" id="mce_SELREST_start" data-mce-style="overflow:hidden;line-height:0" style="overflow:hidden;line-height:0" ></span>
[/code]
- Upload the cookbook:
knife cookbook upload apache - Then execute chef-client on chef-node2 you will see an output as below. In the output you can see the hostname added:
Starting Chef Client, version 13.3.42
resolving cookbooks for run list: ["apache", "apache::websites", "apache::motd"]
Synchronizing Cookbooks:
- apache (0.2.1)
Installing Cookbook Gems:
Compiling Cookbooks...
node[chef-node2]
Converging 4 resources
Recipe: apache::default
* yum_package[apache2] action install (up to date)
* service[apache2] action start (up to date)
* service[apache2] action enable (up to date)
Recipe: apache::websites
* file[default www] action create (up to date)
Recipe: apache::motd
* file[/etc/motd] action create (up to date)Running handlers:
Running handlers complete
Chef Client finished, 0/5 resources updated in 08 seconds
Environments
- Now edit the apache metadata.rb file and change the version to 1.0. Then edit the websites.rb, change the content to "Hello World v1.0". then upload the apache cookbook.
- Again edit the apache metadata.rb file and change the version to 2.0. Then edit the websites.rb, change the content to "Hello World v2.0". then upload the apache cookbook.
- Then login to the chef manage UI and then in the policy section -> Environment -> Create.
Add the name as "staging" and some description. click on Next. Then in the constraints section select the name apache, Operator as "=", Version as 2.0.0.
Click on Add then Click on Create Environment.
Add the name as "production" and some description. click on Next. Then in the constraints section select the name apache, Operator as "=", Version as 1.0.0.
Click on Add. Click on Create Environment. - Then on the Nodes page click on chef-node Edit the environment to be Production. Save it.
- Then click on the chef-node2 and change the environment to be staging. Save it.
- Then execute chef-client on both nodes. You should see that the chef-node is executing the apache version 2.0.0 (Production) and chef-node2 is executing the apache version 1.0.0 (Staging).
- You can hit the IP address of both the servers in the browser and see that the webpage show different result.
- Thus we have separated the cookbooks and nodes as per environemt.
Comments
Post a Comment