Set up a server with Chef (Quick and easy)

  • Tutorial
The best way to learn Chef is to use Chef
- getchef.com



Chef is a server configuration tool for Infrastructure as a Code (IaaC).
For me personally, Chef is, first of all, the ability to store the service architecture in the form of roles, and modify it, avoiding the routine steps of installing packages and configuration.

Chef allows you to execute any scripts on the server, providing its own syntax for describing them. Cross-platform and structured.
It has a fairly high entry threshold. The reasons are: confusing at first glance terminology and the difference in the use of Chef and Chef solo.
Solo is a simple way for beginners to work with Chef. It allows you to manage the configuration without the need for an additional service server, which limits functionality. However, these limitations make themselves felt when managing a large fleet of servers.

This article may be called the Chef Synopsis.
Perhaps the text will be similar to the one translated by Prompt, but I think that it is better not to translate the terms used in the text, I will be glad to know your opinion on this matter.


Terms

  • knife - a console utility that allows you to execute scripts from a local machine in the remote north. The main tool of a real cook.
  • recipe is a script executed on the server. It can perform any task: from creating directories to installing and configuring Nginx.
  • cookbook - a collection of scripts (recipes). There are many ready-made cookbooks (for example, for installing MySQL, etc.) that you can use without getting inside the script.
  • role - Server role. For example mysql or nginx. A role can have an arbitrary number of cookbooks. A server can have several roles.
  • node - a server with the specified IP address.


Using

It's time to put all the terms in a logical chain and understand how Chef is used.

To configure the server, you describe recipes inside cookbooks, and execute them with the knife utility on a node that has one or more roles.

And now, practice

You need to have Ruby, and if you do not have it, then it's time to install Ruby-lang.org

Install and configure Nginx on a fresh server.

Create a directory, for example chef-test and go to it.
We install the necessary tools for the job: chef, knife-solo and berkshelf.
Berkshelf - cookbook manager, analog to bundler

Create a Gemfile with the following contents:
source "https://rubygems.org"
gem 'knife-solo'
gem 'berkshelf'

The bundle command completes the installation.
bundle


In the current directory, create a kitchen
knife solo init .
berks init .

In the created Berksfile we add the cookboks we need
cookbook 'nginx'

And download them
berks install


We describe the server configuration (node)
In the nodes directory, create a file and name it as follows:.json
The file describes: which scripts (recipes) must be run on a given server (node)
{
  "nginx": {
    "version": "1.6.0",
    "install_method": "source",
    "default_site_enabled": true,
  },
  "run_list": [
    "recipe[nginx]"
  ]
}

run_list indicates which recipes or groups of recipes (role) to execute.
In this option, the recipe from the Nginx cookbook is executed.

Run Chef and enjoy the machine for a person.
knife solo bootstrap username@host -i ~/.ssh/ssh_key.pem

The result will be a finished server with a working web server.

If you need to make changes, for example, change the version of Nginx, just change the configuration in the node file and execute
knife solo cook username@host -i ~/.ssh/ssh_key.pem

Chef only applies recipes to which changes have been made, so you can not worry about the safety of the server configuration.

I hope this article has helped overcome the Chef barrier, as then you will find only the joy of the ease of use of this excellent tool.

If you are interested in this topic, in the next article I would like to describe the process of creating a typical infrastructure for rails applications in Amazon AWS.

Chef Alternatives



useful links



We use these tools in the Staply project .
We will talk about it very soon!
image

Only registered users can participate in the survey. Please come in.

Do you use server deployment automation?

  • 50.7% No, I configure everything myself 96
  • 13.2% Yes, using self-written scripts 25
  • 35.9% Yes, using Chef, Puppet, etc. 68

Also popular now: