DevOps Configuration with Chef
DevOps Configuration with Chef
Patil Pratishthan’s
D. Y. Patil Institute of Master of Computer Applications and Management
(Approved by AICTE, New Delhi & Affiliated to Savitribai Phule Pune University)
Dr. D. Y. Patil Educational Complex, Sector 29, Pradhikaran, Akurdi, Pune – 411 044
Tel No: (020)27640998, Website: www.dypimca.ac.in, E-mail : director@dypimca.ac.in
---------------------------------------------------------------------------------------------------------------------
-
MONOGRAPH
Workstation
• The Workstation is the location from which all of Chef configurations are managed.
• This machine holds all the configuration data that can later be pushed to the central Chef
Server.
• These configurations are tested in the workstation before pushing it into the Chef Server.
• A workstation consists of a command-line tool called Knife, that is used to interact with
the Chef Server.
• There can be multiple Workstations that together manage the central Chef Server.
• Workstations are responsible for performing the below functions:
• Writing Cookbooks and Recipes that will later be pushed to the central Chef
Server
• Managing Nodes on the central Chef Server
Recipes
• A Recipe is a collection of resources that describes a particular configuration or policy.
• It describes everything that is required to configure part of a system. The user writes
Recipes that describe how Chef manages applications and utilities (such as Apache HTTP
Server, MySQL, or Hadoop) and how they are to be configured.
• These Recipes describe a series of resources that should be in a particular state, i.e.
Packages that should be installed, services that should be running, or files that should be
written.
Cookbooks
• Multiple Recipes can be grouped together to form a Cookbook. A Cookbook defines a
scenario and contains everything that is required to support that scenario:
• Recipes, which specifies the resources to use and the order in which they are to be
applied
• Attribute values
• File distributions
• Templates
• Extensions to Chef, such as libraries, definitions, and custom resources
• A local Chef repository: This is the place where every configuration component of central
Chef Server is stored. This Chef repository can be synchronized with the central Chef
Server (again using the knife utility itself).
Chef Server
• The Chef Server acts as a hub for configuration data.
• The Chef Server stores Cookbooks, the policies that are applied to Nodes, and metadata
that describes each registered Node that is being managed by the Chef-Client.
• Nodes use the Chef-Client to ask the Chef Server for configuration details, such as
Recipes, Templates, and file distributions.
• The Chef-Client then does as much of the configuration work as possible on the Nodes
themselves (and not on the Chef Server).
• Each Node has a Chef Client software installed, which will pull down the configuration
from the central Chef Server that are applicable to that Node.
• This scalable approach distributes the configuration effort throughout the organization.
Chef Nodes
• Nodes can be a cloud based virtual server or a physical server in your own data center,
that is managed using central Chef Server.
• The main component that needs to be present on the Node is an agent that will establish
communication with the central Chef Server. This is called Chef Client.
• Chef Client performs the following functions:
• It is responsible for interacting with the central Chef Server.
• It manages the initial registration of the Node to the central Chef Server.
• It pulls down Cookbooks, and applies them on the Node, to configure it.
• Periodic polling of the central Chef Server to fetch new configuration items, if any.
Advantages of Chef
• Can automate an entire infrastructure using Chef. All tasks that were manually being
done, can now be done via Chef tool.
• Can configure thousands of nodes within minutes using Chef.
• Chef automation works with the majority of the public cloud offerings like AWS
• Chef will not only automate things, but will also keep the systems under consistent check,
and confirm that the system is in fact configured the way it is required (Chef
Agent/Client does this job). If somebody makes a mistake by modifying a file, Chef will
correct it.
• An entire infrastructure can be recorded in the form of a Chef repository, that can be used
as a blueprint to recreate the infrastructure from scratch.
Workstation Setup
Knife is Chef’s command-line tool to interact with the Chef server, for uploading
cookbooks.
It provides an interface between the chefDK (Repo) on the local machine and the Chef
server.
Knife provides a set of commands to manage Chef infrastructure.
It helps in managing −
Chef nodes
Cookbook
Recipe
Environments
Cloud Resources
Cloud Provisioning
Installation on Chef client on Chef nodes
Knife Setup
• In order to set up knife, move to .chef directory and create a knife.rb inside the chef repo,
which tells knife about the configuration details.
Organization Setup
• A single instance of the Chef Infra Server can support many organizations.
• Each organization has a unique set of groups and users.
• Each organization manages a unique set of nodes, on which a Chef Infra Client is
installed and configured so that it may interact with a single organization on the Chef
Infra Server.
Organization Setup
• Using multiple organizations within the Chef Infra Server ensures that the same toolset,
coding patterns and practices, physical hardware, and product support effort is being
applied across the entire company, even when:
Multiple product groups must be supported—each product group can have its own security
requirements, schedule, and goals
Updates occur on different schedules—the nodes in one organization are managed
completely independently from the nodes in another
Individual teams have competing needs for object and object types—data bags,
environments, roles, and cookbooks are unique to each organization, even if they share the
same name.
Environments
Chef helps in performing environment specific configuration. It is always a good idea to
have a separate environment for development, testing, and production.
Chef enables grouping nodes into separate environments to support an ordered
development flow.
_default Environment
Each organization will always start with at least a single environment called default
environment, which is always available to the Chef server.
A default environment cannot be modified in anyway.
Any kind of changes can only be accommodated in the custom environment that we create.
Environment Attributes
An attribute can be defined in an environment and then used to override the default settings
in the node.
When the Chef client run takes place, then these attributes are compared with the default
attributes that are already present in the node.
When the environment attributes take precedence over the default attributes, Chef client
will apply these settings and values when the Chef client run takes place on each node.
Attribute
An attribute is a specific detail about a node.
Attributes are used by Chef Infra Client to understand:
The current state of the node
What the state of the node was at the end of the previous Chef Infra Client
run
What the state of the node should be at the end of the current Chef Infra
Client run
Attributes are defined by:
The node as saved on the Chef Infra Server
Attributes passed using JSON on the command line
Cookbooks (in attribute files and/or recipes)
Policyfiles
A default attribute is automatically reset at the start of every chef-client run and has the
default
lowest attribute precedence. Use default attributes as often as possible in cookbooks.
Use the force_default attribute to ensure that an attribute defined in a cookbook (by an
force_default attribute file or by a recipe) takes precedence over a default attribute set by a role or an
environment.
A normal attribute is a setting that persists in the node object. A normal attribute has a
normal
higher attribute precedence than a default attribute.
A default attribute is automatically reset at the start of every chef-client run and
default has the lowest attribute precedence. Use default attributes as often as possible in
cookbooks.
A normal attribute is a setting that persists in the node object. A normal attribute
normal
has a higher attribute precedence than a default attribute.
An override attribute is automatically reset at the start of every chef-client run and
has a higher attribute precedence than default, force_default,
override and normal attributes. An override attribute is most often specified in a recipe, but
can be specified in an attribute file, for a role, and/or for an environment. A
cookbook should be authored so that it uses override attributes only when required.
Working Method
Run knife-preflight on the given cookbook to find out which nodes and roles have the given
cookbook in their expanded run lists.
~/chef-repo $ knife preflight ntp
Searching for nodes containing ntp OR ntp::default in their
expanded run_list...
2 Nodes found
www-staging.example.com
cms-staging.example.com
Searching for roles containing ntp OR ntp::default in their
expanded run_list...
3 Roles found
your_cms_role
your_www_role
your_app_role
Found 6 nodes and 3 roles using the specified search criteria
Run-list Format
A run-list must be in one of the following formats: fully qualified, cookbook, or default.
"role[NAME]“
OR
"recipe[COOKBOOK::RECIPE]“
Use a comma to separate roles and recipes when adding more than one item the run-list:
recipe[COOKBOOK::RECIPE],COOKBOOK::RECIPE,role[NAME]"
Data Bags
Data bags store global variables as JSON data. Data bags are indexed for searching and
can be loaded by a cookbook or accessed during a search.
A data bag item may be encrypted using shared secretencryption.
This allows each data bag item to store confidential information (such as a database
password) or to be managed in a source control system (without plain-text data appearing
in revision history).
Each data bag item may be encrypted individually; if a data bag contains multiple
encrypted data bag items, these data bag items are not required to share the same encryption
keys.
Create a Data Bag
A data bag can be created in two ways: using knife or manually.
Using knife
knife data bag create DATA_BAG_NAME (DATA_BAG_ITEM)
knife data bag from file BAG_NAME ITEM_NAME.json
file in “admins” directory => changes to Chef Infra Server
knife data bag from file admins ITEM_NAME.json
Manually
One or more data bags and data bag items can be created manually under the data_bags
directory in the chef-repo.
mkdir data_bags/data_bag_folder
vi data_bags/data_bag_folder/ITEM_NAME.json
Directory Structure
All data bags are stored in the data_bags directory of the chef-repo.
Example:
- data_bags
- admins – data_bag1
- admin1.json - data_item
- admin2.json
- db_users – data_bag2
- user1.json - data_item
- user2.json
- db_config – data_bag3
- small.json - data_item
- medium.json
Data Bag Items
knife can encrypt and decrypt data bag items when the knife data bag subcommand is run
with the create, edit, from file, or show arguments and the following options:
Eg. knife data bag create passwords mysql --secret-file /tmp/my_data_bag_key
“passwords” - name of the data bag
“mysql” - name of the data bag item
“/tmp/my_data_bag_key” - path to the location in which the file that contains the secret-
key is located
Verify Encryption
When the contents of a data bag item are encrypted, they will not be readable until they are
decrypted
Eg. knife data bag show passwords mysql
It will return,
id: mysql
pass:
cipher: aes-256-cbc
encrypted_data:
JZtwXpuq4Hf5ICcepJ1PGQohIyqjNX6JBc2DGpnL2WApzjAUG9SkSdv75TfKSjX4
iv: VYY2qx9b4r3j0qZ7+RkKHg==
version: 1
Decrypt
An encrypted data bag item is decrypted with a knife command :
Eg. knife data bag show --secret-file /tmp/my_data_bag_key passwords mysql
It will return JSON output :
{
"id": "mysql",
"pass": "thesecret123",
"user": "fred"
}
Edit a Data Bag with Knife
Use the edit argument to edit the data contained in a data bag.
If encryption is being used, the data bag will be decrypted, the data will be made available
in the $EDITOR, and then encrypted again before saving it to the Chef Infra Server.
Eg. knife data bag edit admins ITEM_NAME