Using Cookbooks
In the previous chapter, we discussed the important components related to a cookbook. In this chapter we use some cookbooks available from the community. We download and deploy these cookbooks on a node. We have picked up important cookbooks from the community and will try to explain exactly how to use them.
MySQL(4.1.2)
This program installs and configures the MySQL client or server. Different recipes are defined for installing and configuring the MySQL server on different platform machines.
The following platforms support this cookbook:
Dependencies
Prerequisite
Cookbook Download
You can download the cookbook on the workstation in your corresponding cookbooks directory, inside your chef repo, on your knife workstation from the Opscode community web site (see Figure 8-1), by running the following command on your knife workstation:
Knife cookbook site download mysql
Figure 8-1. Download MySQL cookbook from Opscode community
The downloaded cookbook will be in '.tar.gz'. The cookbook can be extracted from here (see Figure 8-2).
Figure 8-2. Extracting the cookbook
Once we extract the cookbook, we can move to the next step of uploading the cookbook to the chef server.
For setting up a MySQL server on a node, we first have to set the attribute values according to our setup. Certain attributes need to be changed before running the chef client on the node.
In your chef repo directory on your knife workstation, go to the cookbooks folder and open the MySQL cookbook directory, open 'attributes/server.rb' file in the editor of your choice. Set the values for the following attributes:
Default ['mysql']['port'] = 3306 (or any other as per your settings)
In the 'attributes/server_rhel.rb', set the values for following attributes:
You upload the cookbook (see Figure 8-3) to the chef server using knife (see Figure 8-3). Once you upload the cookbook (along with its dependencies), you can add it to the run list of the node.
Knife cookbook upload mysql
Figure 8-3. Uploading the cookbook
For the purpose of this book, a node ‘chef testing’ has been preconfigured and is utilized here.
Add the recipe to the run list of the node from your knife workstation (see Figure 8-4), and run chef client on the configured node.
Figure 8-4. Adding cookbook to the run list
Knife node run_list add node_name ‘recipe[mysql]’
Now, we run the chef client on the configured node (see Figure 8-5).
Figure 8-5. Running chef client
Figure 8-6. Creating log directory for MySQL
Figure 8-7. Creating MySQL configuration file
Figure 8-8. Updating the content of my.conf (from template)
Figure 8-9. Starting MySQL service and setting root password
After the client run has been ended (see Figure 8-10), we can check on the specified port to see whether the MySQL service is running (see Figure 8-11).
lsof –i :3306
Figure 8-10. Chef client run end
Figure 8-11. Checking MySQL service
Once the cookbook has been converged and all the resources have been updated, you can access your MySQL database admin by logging in as user 'mysql'.
Figure 8-12 is a snapshot of the '/etc/mysql_grants.sql' file which sets the password for the root user and also specifies other grant table permissions.
Figure 8-12. Mysql_grants file
You can see the password you specified in the attributes. Now you can log in to the MySQL console using this password and user as root (see Figure 8-13).
mysql –u root –p 'your_password_here'
Figure 8-13. Log in to the MySQL console
Platform
This cookbook is supported on the following platforms:
Dependencies
The following cookbooks are direct dependencies because they’re used for common “default” functionality:
Prerequisite
Cookbook Download
You can download the cookbook (see Figure 8-14) in your corresponding cookbooks directory inside your chef repo on the workstation, from the Opscode community web site, by running the following command on your knife workstation:
knife cookbook site download nginx
Figure 8-14. Downloading the cookbook
The downloaded cookbook will be in '.tar.gz'. You can extract the cookbook from here (see Figure 8-15).
tar –xvf nginx-2.5.0.tar.gz
Figure 8-15. Extracting the cookbook
Once you extract the cookbook, we can move to the next step of uploading the cookbook to the chef server.
Edit the following configuration attributes on your cookbook and upload them to the chef server once again, before running the chef client.
In your chef repo directory on your knife workstation, go to the cookbooks folder and open Nginx cookbook directory and open 'attributes/default.rb' file in the editor of your choice. Set the values of the following attributes (these are the values used for the purpose of this book) according to your settings:
We upload the cookbook to the chef server using knife (see Figure 8-16). Once you upload the cookbook (along with its dependencies), you can add it to the run list of the node.
knife cookbook upload nginx
Figure 8-16. Uploading the cookbook
Add the recipe to the run list of the node from your knife workstation, and run chef client on the configured client node.
knife node run_list add node_name 'recipe[nginx]'
Figure 8-17. Adding the recipe to the run_list
There’s some redundancy in that the config handling hasn’t been separated from the installation method (yet), so use only one of the recipes, default or source.
For the purpose of this book, we have used the default recipe. Run the chef client on the node.
Chef client (see Figures 8-18 – 8-21)
Figure 8-18. - Creating ohai_plugins directory
Figure 8-19. Setting up directory permissions and merging into node attributes
Figure 8-20. Creating and updating content of epel definition file in /etc/yum.repos.d
Figure 8-21. Enabling the epel repository
Now, the code to install and configure Nginx will run and all the required directories and configuration files will be created. (see Figures 8-22 – 8-29).
Figure 8-22. Creating and configuring directory settings
Figure 8-23. Creating sites-available directory
Figure 8-24. Creating/updating nxensite file
Figure 8-25. Setting directory permissions on /usr/sbin/nxensite file
Figure 8-26. Creating/updating nxdissite template
Figure 8-27. Creating/updating nginx.conf file
Figure 8-28. Sites-available configuration file
Figure 8-29. Client finish
At the end of client run, the recipe starts the nginx service.
We can check (see Figure 8-30) that Nginx is listening on port 80 by running the following command:
lsof –i :80
Figure 8-30. Checking if the service is running
Figure 8-31 shows the configuration directory which is created at 'etc/nginx' location. In the configuration directory, we can see different directories such as sites available, sites enabled, and the Nginx configuration file that were created by the recipes during the chef client run.
cd /etc/nginx
ls
Figure 8-31. Nginx Configuration directory
We create the log directory (see Figure 8-32) for nginx server at '/var/log/nginx'.
cd /var/log/nginx
Figure 8-32. Nginx log directory
Finally, to confirm that the Nginx server is successfully installed and working, you can visit the Nginx web page (see Figure 8-33) by entering the IP address of the node on which the n.ginx server is installed.
Figure 8-33. Verifying Nginx install
Platform
The following platforms support this cookbook:
Cookbook Download
You can download the cookbook for installing and configuring the Squid proxy server in the corresponding cookbooks directory inside your chef repo on your knife workstation, from the Opscode community web site, by running the following command on your knife workstation:
Knife cookbook site download squid
Figure 8-34. Downloading the Squid cookbook
The cookbook you downloaded is in 'tar.gz' format. You have to extract the cookbook from here (see Figure 8-35), as follows:
tar –xvf squid-0.4.0.tar.gz
Figure 8-35. Extracting the file
In your chef repo directory on your knife workstation, go to the cookbooks folder and open Squid cookbook directory and open 'attributes/default.rb' file in the editor of your choice. Set the values of the attributes according to your settings. Following are the values used for the purpose of this book.
You can also change the listen interface and cache memory size for the proxy server by changing the following attributes in default.rb:
Upload the cookbook (see Figure 8-36) to the chef server from your knife workstation, from inside the chef repo directory.
knife cookbook upload squid
Figure 8-36. Uploading the cookbook
Add the cookbook to the run list of the node (see Figure 8-37), and run chef client to install and configure Squid cache.
Knife node run_list add node_name 'recipe[squid]'
Figure 8-37. Adding the cookbook to the run_list
Client Run
Now, we run the chef client (see Figure 8-38) on the node on which we want to install and configure the Squid proxy server (see Figure 8-39).
Figure 8-38. Chef client run start
Figure 8-39. Squid install
Figure 8-40. Create mime.conf
Figure 8-41. Create squid.conf
Figure 8-42. Setting directory permissions and enabling service
Figure 8-43. Chef client finish
Once the client run has finished, we can determine if the Squid server was installed successfully by checking the Squid service (see Figure 8-44).
service squid status
Figure 8-44. Service status
Wordpress(2.1.0)
The following operating systems support this cookbook:
Dependencies
This cookbook is dependent on the following cookbooks:
Prerequisite
Cookbook Download
You can download the cookbook (see Figure 8-45) in your corresponding cookbooks directory inside your chef repo, on the knife workstation, from the Opscode community web site, by running the following command on your knife workstation:
knife cookbook site download wordpress
Figure 8-45. Downloading the cookbook
The cookbook that is downloaded is in 'tar.gz' format. The cookbook has to be extracted from here (see Figure 8-46), as follows:
tar –xvf wordpress-2.1.0.tar.gz
Figure 8-46. Extracting the cookbook
Cookbook Upload
In your chef repo directory on your knife workstation, go to the cookbooks folder and open Wordpress cookbook directory and open 'attributes/default.rb' file in the editor of your choice. Set the values of the attributes according to your settings. Following are the values used for the purpose of this book.
You can mention your own database name, user, and password, or you can use the defaults.
If you want to allow the multisite features for your Wordpress site, you can set the corresponding attribute value.
For the purpose of this book, we have set this attribute to 'false'. Now, upload the cookbook to the chef server (see Figure 8-47) by running the following command on your knife workstation, inside the chef repo directory:
knife cookbook upload wordpress
Figure 8-47. Uploading the cookbook
After you have uploaded the cookbook, add the cookbook to the run list (see Figure 8-48) of the node on which Wordpress is to be installed, using knife while inside the chef repo directory.
knife node run_list add node_name wordpress
Figure 8-48. Adding the cookbook to the run list
Client Run
Now, we run the chef client on the node on which we want to install and configure Wordpress (see Figure 8-49). The chef client command can be run from any location on the node file system. (However, it is recommended to run from the user’s home folder or the /root location).
Figure 8-49. Client run start
Figure 8-50. Installing support package autoconf
Figure 8-51. Installing support packages
Figure 8-52. Installing MySQL database
Figure 8-53. Installing and configuring Wordpress
Figure 8-54. Creating wp-config.php file
Figure 8-55. Chef client run finish
Now, go to a web browser and open the webadmin ui page for your Wordpress installation (see Figure 8-56).
http://your_ip_address
Figure 8-56. Verifying installation
The following platforms support this cookbook:
Dependencies
Cookbook Download
You can download the cookbook (see Figure 8-57) for installing and configuring the Tomcat Server from the Opscode cookbook community site in the cookbooks directory inside your chef repo, on your knife workstation, as follows:
Knife cookbook site download tomcat
Figure 8-57. Downloading the cookbook
The downloaded cookbook is in 'tar.gz' format. Figure 8-58 shows the extraction of the cookbook
tar –xvf squid-0.4.0.tar.gz
Figure 8-58. Extracting the cookbook
In your chef repo directory on your knife workstation, go to the cookbooks folder and open Tomcat cookbook directory and open 'attributes/default.rb' file in the editor of your choice.
Following are some important attributes whose values should be set according to the local setup. We use the values specified for the purpose of this book; they can be changed in the default.rb attribute file accordingly.
The rest of the attribute values can be used as mentioned in the cookbook.
Upload the cookbook (see Figure 8-59) using the following command:
knife cookbook upload tomcat
Figure 8-59. Uploading the cookbook
Now, add the recipe to the run list of the node on which Tomcat is to be installed and configured (see Figure 8-60).
knife node run_list add node_name 'recipe[tomcat]'
Figure 8-60. Adding recipe to run list
Now, we run the chef client on the node on which we want to install and configure Tomcat (see Figure 8-61). The chef client command can be run from any location on the node file system. (However, it is recommended to run from the user’s home folder or the /root location.
Figure 8-61. Chef client start
Figure 8-62. Installing Java
Figure 8-63. Setting Java environment variables
Figure 8-64. Installing the Tomcat package
Figure 8-65. Installing and configuring Tomcat
Figure 8-66. Creating the Tomcat configuration file
Figure 8-67. Setting read-write permissions on configuration file
Figure 8-68. Chef client finish
Once the client run is finished, we check the port 8080 to check whether the Tomcat service is running (see Figure 8-69).
service tomcat6 status
Figure 8-69. Verifying service status
Now, you can go to a web browser and visit the UI using the IP address of the node on port 8080 (see Figure 8-70).
http://your_ip_address:8080
Figure 8-70. Verifying installation
We have tried to cover whatever you need in order to use cookbooks. In the next chapter we start with the development of cookbooks.