DevOps Tools part I : Getting started with Chef
After learning how to build webapps using Ruby on Rails, i have started looking at other tools that use this language.
I came across Chef - it is a tool that simplifies the way you perform infrastructure automation and configuration management tasks. Chef is a way to turn infrastructure into code which makes it ideal for continuous deployment. It allows you to quickly deploy entire environments instead of only single applications. It uses Ruby and handles configuration by packing details into what it calls recipes.
Chef is made up of several components that have specific roles:Â
-Chef server : It stores recipes as well as configuration data. It is the central machine that other machines ('the nodes') will use for deployment configuration.
-Chef nodes : they represent the target for deployment. A node could be any physical, virtual or cloud machine that is configured to be maintained by a chef-client, an application used to communicate with the chef server.
A Chef workstation is where the chef configuration details are created or edited. The code is then pushed to the Chef server where it will be available to be easily deployable to any node.
I tested out Chef on a CentOs virtual machine and was amazed at how easy it was to use.
Very similarly to Rails commands you run this command to generate a cookbook named webserver:
chef generate cookbook webserver
A cookbook provides structure to your recipes and enables you to more easily reference external files, such as our web server's home page. In essence, a cookbook helps you stay organized.
Then to generate a homepage template you'd run :
chef generate template webserver index.html
In this newly created file (index.html.erb) you could write a static hello world or use erb templating to add dynamic information about your server.
Then you will need to update the recipe to enable and start Apache and to reference your template.
package 'httpd'
service 'httpd' do action [:start, :enable] end template '/var/www/html/index.html' do source 'index.html.erb' end
You are now ready to run your webserver locally with this command :
chef-client --local-mode --runlist webserver
Check your local host ! Voila ! You automated your local web server !
Another cool thing you could do is generate dynamic web pages by using erb templating, for example your index.html.erb file could contain this :
<h1>hello from <%= node['ipaddress'] %></h1>
You can dynamically add all sorts of information about your server such as the node's IP address, total installed memory, number of CPU's etc...
I enjoyed learning more about Chef and I am looking forward to start using it more and setting it up in the context of continuous delivery for various web apps.