CHAPTER 8

image

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.

Platform

The following platforms support this cookbook:

  • Debian/Ubuntu
  • CentOS 5.x,6.x, Red Hat 5.x,6.x, Fedora(18.x,19.x,20.x)
  • Mac OS X (Using homebrew)

Dependencies

  • Requires Opscode’s OpenSSL cookbook for secure password generation.

Prerequisite

  • Client machine must have a running Internet connection.

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

9781430262954_Fig08-01.jpg

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).

9781430262954_Fig08-02.jpg

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.

Cookbook Upload

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:

  • default ['mysql']['server']['basedir'] = '/usr'
  • default ['mysql']['server']['tmpdir'] = ['/tmp']
  • default ['mysql']['server']['directories']['run_dir'] = '/var/run/mysqld'
  • default ['mysql']['server']['directories']['log_dir'] = '/var/lib/mysql'
  • default ['mysql']['server']['directories']['slow_log_dir'] = '/var/log/mysql'
  • default ['mysql']['server']['directories']['confd_dir'] = '/etc/mysql/conf.d'
  • default ['mysql']['server']['service_name'] = 'mysqld'
  • default ['mysql']['server_root_password'] = 'rootpass'

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

9781430262954_Fig08-03.jpg

Figure 8-3. Uploading the cookbook

Client Run

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.

9781430262954_Fig08-04.jpg

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).

9781430262954_Fig08-05.jpg

Figure 8-5. Running chef client

9781430262954_Fig08-06.jpg

Figure 8-6. Creating log directory for MySQL

9781430262954_Fig08-07.jpg

Figure 8-7. Creating MySQL configuration file

9781430262954_Fig08-08.jpg

Figure 8-8. Updating the content of my.conf (from template)

9781430262954_Fig08-09.jpg

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

9781430262954_Fig08-10.jpg

Figure 8-10. Chef client run end

9781430262954_Fig08-11.jpg

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.

9781430262954_Fig08-12.jpg

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'

9781430262954_Fig08-13.jpg

Figure 8-13. Log in to the MySQL console

Nginx(2.4.2)

Platform

This cookbook is supported on the following platforms:

  • Ubuntu 10.04, Ubuntu 12.04
  • CentOS 5.8, 6.3

Dependencies

The following cookbooks are direct dependencies because they’re used for common “default” functionality:

  • Build essential (for nginx::source)
  • Ohai (for nginx::ohai_plugin)

Prerequisite

  • Client machine must have a running Internet connection.

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

9781430262954_Fig08-14.jpg

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

9781430262954_Fig08-15.jpg

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.

Cookbook Upload

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:

  • default ['nginx']['dir']          = '/etc/nginx'
  • default ['nginx']['script_dir']   = '/usr/sbin'
  • default ['nginx']['log_dir']      = '/var/log/nginx'
  • default ['nginx']['binary']       = '/usr/sbin/nginx'
  • default ['nginx']['default_root'] = '/var/www/nginx-default'
  • default ['nginx']['upstart']['foreground'] = true
  • default ['nginx']['pid'] = '/var/run/nginx.pid'

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

9781430262954_Fig08-16.jpg

Figure 8-16. Uploading the cookbook

Client Run

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]'

9781430262954_Fig08-17.jpg

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-188-21)

9781430262954_Fig08-18.jpg

Figure 8-18. - Creating ohai_plugins directory

9781430262954_Fig08-19.jpg

Figure 8-19. Setting up directory permissions and merging into node attributes

9781430262954_Fig08-20.jpg

Figure 8-20. Creating and updating content of epel definition file in /etc/yum.repos.d

9781430262954_Fig08-21.jpg

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-228-29).

9781430262954_Fig08-22.jpg

Figure 8-22. Creating and configuring directory settings

9781430262954_Fig08-23.jpg

Figure 8-23. Creating sites-available directory

9781430262954_Fig08-24.jpg

Figure 8-24. Creating/updating nxensite file

9781430262954_Fig08-25.jpg

Figure 8-25. Setting directory permissions on /usr/sbin/nxensite file

9781430262954_Fig08-26.jpg

Figure 8-26. Creating/updating nxdissite template

9781430262954_Fig08-27.jpg

Figure 8-27. Creating/updating nginx.conf file

9781430262954_Fig08-28.jpg

Figure 8-28. Sites-available configuration file

9781430262954_Fig08-29.jpg

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

9781430262954_Fig08-30.jpg

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

9781430262954_Fig08-31.jpg

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

9781430262954_Fig08-32.jpg

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.

9781430262954_Fig08-33.jpg

Figure 8-33. Verifying Nginx install

Squid(0.4.0)

Platform

The following platforms support this cookbook:

  • Amazon (> = 0.0.0)
  • CentOS (> = 0.0.0)
  • Debian (> = 0.0.0)
  • Fedora (> = 0.0.0)
  • Red Hat (> = 0.0.0)
  • Scientific (> = 0.0.0)
  • Suse (> = 0.0.0)
  • Ubuntu (> = 0.0.0)

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

9781430262954_Fig08-34.jpg

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

9781430262954_Fig08-35.jpg

Figure 8-35. Extracting the file

Cookbook Upload

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.

  • node['squid']['port']=' 3128'
  • Network caching proxy: node['squid']['network'] = 'your ip address range'

You can also change the listen interface and cache memory size for the proxy server by changing the following attributes in default.rb:

  • default ['squid']['listen_interface'] = 'eth0'
  • default ['squid']['cache_size'] = '100'
  • default ['squid']['cache_mem'] = '2048'

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

9781430262954_Fig08-36.jpg

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]'

9781430262954_Fig08-37.jpg

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).

9781430262954_Fig08-38.jpg

Figure 8-38. Chef client run start

9781430262954_Fig08-39.jpg

Figure 8-39. Squid install

9781430262954_Fig08-40.jpg

Figure 8-40. Create mime.conf

9781430262954_Fig08-41.jpg

Figure 8-41. Create squid.conf

9781430262954_Fig08-42.jpg

Figure 8-42. Setting directory permissions and enabling service

9781430262954_Fig08-43.jpg

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

9781430262954_Fig08-44.jpg

Figure 8-44. Service status

Wordpress(2.1.0)

Platform

The following operating systems support this cookbook:

  • CentOS (> = 0.0.0)
  • Debian (> = 0.0.0)
  • Fedora (> = 0.0.0)
  • Oracle (> = 0.0.0)
  • Red Hat (> = 0.0.0)
  • Scientific (> = 0.0.0)
  • Ubuntu (> = 0.0.0)
  • Windows (> = 0.0.0

Dependencies

This cookbook is dependent on the following cookbooks:

  • MySQL, Version > = 1.0.5
  • PHP, Version > = 0.0.0
  • Apache2, Version > = 0.99.4
  • IIS, Version > = 1.6.2
  • OpenSSL, Version > = 0.0.0
  • Database, Version > = 1.6.0
  • Build essential, Version > = 0.0.0

Prerequisite

  • Client machine must have a running Internet connection.

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

9781430262954_Fig08-45.jpg

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

9781430262954_Fig08-46.jpg

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.

  • default ['wordpress']['db']['name'] = "wordpressdb"
  • default ['wordpress']['db']['user'] = "wordpressuser"
  • default ['wordpress']['db']['pass'] = nil

If you want to allow the multisite features for your Wordpress site, you can set the corresponding attribute value.

  • default ['wordpress']['allow_multisite'] = false

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

9781430262954_Fig08-47.jpg

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

9781430262954_Fig08-48.jpg

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).

9781430262954_Fig08-49.jpg

Figure 8-49. Client run start

9781430262954_Fig08-50.jpg

Figure 8-50. Installing support package autoconf

9781430262954_Fig08-51.jpg

Figure 8-51. Installing support packages

9781430262954_Fig08-52.jpg

Figure 8-52. Installing MySQL database

9781430262954_Fig08-53.jpg

Figure 8-53. Installing and configuring Wordpress

9781430262954_Fig08-54.jpg

Figure 8-54. Creating wp-config.php file

9781430262954_Fig08-55.jpg

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

9781430262954_Fig08-56.jpg

Figure 8-56. Verifying installation

Tomcat(0.15.0)

The following platforms support this cookbook:

Platform

  • Amazon (> = 0.0.0)
  • CentOS 6+
  • Debian (> = 0.0.0)
  • Fedora (> = 0.0.0)
  • Red Hat 7+
  • Ubuntu (> = 0.0.0)

Dependencies

  • Java, Version (> = 0.0.0)
  • OpenSSL, Version (> = 0.0.0)

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

9781430262954_Fig08-57.jpg

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

9781430262954_Fig08-58.jpg

Figure 8-58. Extracting the cookbook

Cookbook Upload

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.

  • node ["tomcat"]["port"] – 8080
  • node ["tomcat"]["proxy_port"] – nil

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

9781430262954_Fig08-59.jpg

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]'

9781430262954_Fig08-60.jpg

Figure 8-60. Adding recipe to run list

Client Run

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.

9781430262954_Fig08-61.jpg

Figure 8-61. Chef client start

9781430262954_Fig08-62.jpg

Figure 8-62. Installing Java

9781430262954_Fig08-63.jpg

Figure 8-63. Setting Java environment variables

9781430262954_Fig08-64.jpg

Figure 8-64. Installing the Tomcat package

9781430262954_Fig08-65.jpg

Figure 8-65. Installing and configuring Tomcat

9781430262954_Fig08-66.jpg

Figure 8-66. Creating the Tomcat configuration file

9781430262954_Fig08-67.jpg

Figure 8-67. Setting read-write permissions on configuration file

9781430262954_Fig08-68.jpg

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

9781430262954_Fig08-69.jpg

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

9781430262954_Fig08-70.jpg

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset