Chef 简明教程

Chef - Overview

Chef 是一项由 Opscode 开发的开源技术。Opscode 联合创始人 Adam Jacob 被称为 Chef 的创始人。这项技术使用 Ruby 编码来开发基础构建模块,如配方和菜谱。Chef 用于基础设施自动化,并协助减少基础设施管理的手动且重复的任务。

Chef is an open source technology developed by Opscode. Adam Jacob, co-founder of Opscode is known as the founder of Chef. This technology uses Ruby encoding to develop basic building blocks like recipe and cookbooks. Chef is used in infrastructure automation and helps in reducing manual and repetitive tasks for infrastructure management.

Chef 对不同的构建模块有它自己的惯例,而这些模块是管理和自动化基础设施所必须的。

Chef have got its own convention for different building blocks, which are required to manage and automate infrastructure.

Why Chef?

Chef 是一种用于自动化基础设施配置的配置管理技术。它是基于 Ruby DSL 语言开发的。它用于简化配置任务和管理公司的服务器。它有能力与任何云技术集成。

Chef is a configuration management technology used to automate the infrastructure provisioning. It is developed on the basis of Ruby DSL language. It is used to streamline the task of configuration and managing the company’s server. It has the capability to get integrated with any of the cloud technology.

在 DevOps 中,我们使用 Chef 来部署和管理内部和云端的服务器及应用程序。

In DevOps, we use Chef to deploy and manage servers and applications in-house and on the cloud.

Features of Chef

以下是 Chef 最突出的功能:

Following are the most prominent features of Chef −

  1. Chef uses popular Ruby language to create a domain-specific language.

  2. Chef does not make assumptions on the current status of a node. It uses its mechanisms to get the current status of machine.

  3. Chef is ideal for deploying and managing the cloud server, storage, and software.

Advantages of Chef

Chef 提供了以下优点:

Chef offers the following advantages −

  1. Lower barrier for entry − As Chef uses native Ruby language for configuration, a standard configuration language it can be easily picked up by anyone having some development experience.

  2. Excellent integration with cloud − Using the knife utility, it can be easily integrated with any of the cloud technologies. It is the best tool for an organization that wishes to distribute its infrastructure on multi-cloud environment.

Disadvantages of Chef

Chef 的一些主要缺点如下:

Some of the major drawbacks of Chef are as follows −

  1. One of the huge disadvantages of Chef is the way cookbooks are controlled. It needs constant babying so that people who are working should not mess up with others cookbooks.

  2. Only Chef solo is available.

  3. In the current situation, it is only a good fit for AWS cloud.

  4. It is not very easy to learn if the person is not familiar with Ruby.

  5. Documentation is still lacking.

Key Building Blocks of Chef

Recipe

可以将其定义为用于管理基础设施的一系列属性的集合。配方中存在的这些属性用于更改现有状态或设置特定基础设施节点。它们在 Chef 客户端运行期间加载,并与节点(机器)的现有属性进行比较。然后它进入配方中的节点资源中定义的状态。它是菜谱的主力。

It can be defined as a collection of attributes which are used to manage the infrastructure. These attributes which are present in the recipe are used to change the existing state or setting a particular infrastructure node. They are loaded during Chef client run and comparted with the existing attribute of the node (machine). It then gets to the status which is defined in the node resource of the recipe. It is the main workhorse of the cookbook.

Cookbook

菜谱是一系列配方。它们是上传到 Chef 服务器的基本构建模块。当 Chef 运行发生时,它确保其内部的配方能将给定的基础设施达到配方中列出的所需状态。

A cookbook is a collection of recipes. They are the basic building blocks which get uploaded to Chef server. When Chef run takes place, it ensures that the recipes present inside it gets a given infrastructure to the desired state as listed in the recipe.

Resource

它是用来管理具有不同状态的基础设施的配方的一个基本组件。一个配方中可以有多个资源,这有助于配置和管理基础设施。例如:

It is the basic component of a recipe used to manage the infrastructure with different kind of states. There can be multiple resources in a recipe, which will help in configuring and managing the infrastructure. For example −

  1. package − Manages the packages on a node

  2. service − Manages the services on a node

  3. user − Manages the users on the node

  4. group − Manages groups

  5. template − Manages the files with embedded Ruby template

  6. cookbook_file − Transfers the files from the files subdirectory in the cookbook to a location on the node

  7. file − Manages the contents of a file on the node

  8. directory − Manages the directories on the node

  9. execute − Executes a command on the node

  10. cron − Edits an existing cron file on the node

Attribute

它们主要是设置。可以将其视为一个键值对,它可以是任何想要在 cookbooks 中使用的东西。有几种不同的属性可以应用,对节点操作的最终设置有着不同的优先级。

They are basically settings. They can be thought of as a key value pair of anything which one wants to use in the cookbook. There are several different kinds of attributes that can be applied, with a different level of precedence over the final settings that the node operates under.

File

它是 cookbook 中的一个子目录,包含任何将放置在使用 cookbooks 的节点上的静态文件。那么可以将配方声明为一个将文件从该目录移到最终节点的资源。

It’s a subdirectory within the cookbook that contains any static file which will be placed on the nodes that uses the cookbooks. A recipe then can be declared as a resource that moves the files from that directory to the final node.

Templates

它们与文件相似,但不是静态的。模板文件的扩展名为 .ebr,这意味着它们包含嵌入式 Ruby。它们主要用于在文件中替换属性值,以创建最终的文件版本,该版本将被放置在节点上。

They are similar to files, but they are not static. Template files end with the .ebr extension, which means they contain embedded Ruby. They are mainly used to substitute an attribute value into the files to create the final file version that will be placed on the node.

Metadata.rb

它用于管理有关软件包的元数据。这包括名称和软件包的详细信息等详细信息。它还包括依赖信息,这些信息会说明此 cookbooks 需要哪些 cookbooks 才能运行。这允许 Chef 服务器正确构建节点的运行列表,并确保所有部分都能正确传输。

It is used to manage the metadata about the package. This includes details like the name and details of the package. It also includes things such as dependency information that tells which cookbooks this cookbook needs to operate. This allows the Chef server to build the run-list of the node correctly and ensures that all of the pieces are transferred correctly.

Default Cookbook Structure

C:\chef\cookbooks\nginx>tree
Folder PATH listing for volume Local Disk
Volume serial number is BE8B-6427
C: ├───attributes
├───definitions
├───files
│   └───default
├───libraries
├───providers
├───recipes
├───resources
└───templates
    └───default

以下是 Chef 相关技术的列表。

Following is the list of Chef related technologies.

Puppet

Puppet 提供了一种在任何地方运行的软件传递和操作的标准化方法。它是 Linux、Unix 和 Windows 系统的一个自动化管理引擎,它可以根据集中化规范执行管理任务。

Puppet provides a standard way of delivering and operating software, no matter where it runs. It is an automated administrative engine for Linux, Unix, and Windows system that performs administrative tasks based on centralized specification.

主要的 features of Puppet 有如下内容:

The primary features of Puppet are as follows −

  1. Implementing new systems with a uniform configuration.

  2. Updating the systems and upgrading the security and software packages.

  3. Incorporating new features and adding dexterous capabilities.

  4. Customizing configurations for ensuring the availability of data sources.

  5. Optimizing the available resources and minimizing the cost.

  6. Simplifying the roles and enabling the team to focus on core and productive issues.

  7. Getting a bird’s eye view of the available infrastructure.

Ansible

Ansible 是一个极为简单的 IT 自动化平台,它让您的应用程序和系统更易于部署。无需编写脚本或自定义代码来部署和更新您的应用程序——使用接近英语的语言以 SSH 形式自动化,无需在远程系统上安装任何代理。

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy. Avoid writing scripts or custom code to deploy and update your applications — automate in a language that approaches plain English, using SSH, with no agents to install on remote systems.

主要 features of Ansible 如下 −

The primary features of Ansible are as follows −

  1. Simple and easy to learn

  2. Written in Python

  3. Agentless

  4. YAML-based Playbooks

  5. Ansible galaxy

SaltStack

SaltStack 用于数据驱动配置。它是一种基于动态通信总线的新型基础设施管理方法。它用于数据驱动的编排、任何基础设施的远程执行以及任何应用程序堆栈的配置管理。

SaltStack is used for data-driven configuration. It is a new approach of infrastructure management built on dynamic communication bus. It is used for data-driven orchestration, remote execution for any infrastructure, and configuration management for any app stack.

Fabric

Fabric 是一种基于 Python 的编程语言,作为 Python 的 API 开发,需要在 Python 代码中导入才能配置和管理基础设施。

Fabric is a Python-based programming language, which is developed as an API of Python which needs to be imported in Python code in order to configure and manage an infrastructure.