Setup NGINX using Chef to a VM (Ubuntu)

IaC - Infrastructure as Code

Infrastructure as Code (IaC) is a paradigm in which infrastructure provisioning and management are done using code and automation, treating infrastructure elements such as servers, networks, and configurations as programmable entities. Instead of manually setting up and configuring servers and resources, IaC involves writing code to define and deploy infrastructure. 

Concepts of Infrastructure as Code:

1.       Declarative Configuration: IaC uses declarative code to define the desired state of your infrastructure. You describe what you want the infrastructure to look like rather than specifying the steps to get there.

2.       Version Control: IaC code, just like software code, can be stored in version control systems (e.g., Git). This allows you to track changes, collaborate with others, and revert to previous configurations if needed.

3.       Automation: IaC automates the provisioning, configuration, and management of infrastructure. This reduces human error, ensures consistency, and saves time compared to manual setups.

4.       Idempotency: IaC tools are designed to be idempotent, meaning you can run the same code multiple times without causing unintended side effects. The system will only make necessary changes to match the desired state.

5.       Reproducibility: With IaC, you can easily recreate identical infrastructure environments in different locations, such as development, testing, and production.

Benefits of Infrastructure as Code:

1.       Consistency: IaC ensures that your infrastructure is consistent across environments. This reduces the "works on my machine" problem and promotes reliable software deployments.

2.       Speed and Efficiency: Automating infrastructure provisioning and configuration significantly speeds up the process. New environments can be created and updated quickly, reducing deployment times.

3.       Scalability: IaC makes it easier to scale your infrastructure horizontally or vertically by defining resources and configurations as code. You can quickly spin up new servers or instances as needed.

4.       Reduced Risk: Manual configurations are prone to errors and inconsistencies. IaC reduces human error and minimizes the risk of misconfigurations that can lead to security vulnerabilities or downtime.

5.       Documentation: IaC code serves as living documentation. It's clear, explicit, and describes how your infrastructure is set up, making it easier for new team members to understand and contribute.

6.       Collaboration: IaC promotes collaboration between development, operations, and other teams. Infrastructure changes are code changes, which can be reviewed, tested, and approved like any other code.

7.       Versioning and Rollbacks: With version control, you can roll back to a previous state of infrastructure if an issue arises, enabling quick recovery and reducing downtime.

8.       Compliance and Auditing: IaC helps maintain compliance by providing a clear audit trail of changes made to infrastructure configurations over time.

9.       Cost Optimization: IaC allows you to define and manage resources more precisely, reducing the likelihood of over-provisioning and saving costs.

10.   Easier Disaster Recovery: IaC simplifies disaster recovery by allowing you to recreate entire environments quickly using code.

Overall, IaC streamlines the process of managing infrastructure, improves collaboration, reduces errors, enhances scalability, and increases the speed at which software is deployed. It's a fundamental practice in modern DevOps and software development workflows.
Top of Form

Chef:

·         Established in 2008, Chef Software came into existence. Our initial offering, known as Chef (currently referred to as Chef Infra), comprises a collection of utilities designed to streamline the process of configuring your server infrastructure, whether it's based in the cloud or on-premises.

·         Chef possesses the capability to seamlessly integrate with cloud-centric platforms like Microsoft Azure, Google Cloud, and Amazon Web Services (AWS), enabling the automatic provisioning and configuration of computing resources hosted in these cloud environments.

·         To illustrate, consider a scenario where a sizable retail enterprise intends to set up and arrange 50 servers for an impending sales event. They could leverage Chef Infra to automate the deployment of this infrastructure.

·         In October 2020, Progress Software acquired Chef Software, which now functions as a distinct business division within Progress Software. Chef Software has consistently maintained its prominence in the realms of DevOps and DevSecOps, retaining its leadership position.

Components of Chef:

1.       Chef Server:

This is the central hub that stores configuration data, known as "cookbooks," and manages the communication between nodes (servers) and the Chef client. It allows you to store and manage your infrastructure's configuration and policies.

2.       Chef Client:

The Chef client runs on each node that you want to manage with Chef. It communicates with the Chef server to retrieve configuration information and apply it to the node. The client ensures that the node's state matches the desired configuration described in the cookbooks.

3.       Cookbooks:

Cookbooks are the fundamental units of configuration in Chef. They consist of recipes, attributes, templates, and other resources. A cookbook defines how a specific piece of software or configuration should be installed, configured, and managed on a node.

User upload a set of Chef cookbooks to the Chef Infra server. You can think of Chef cookbooks as a set of configuration files, called recipes, that will instantiate, configure, and maintain your infrastructure nodes in an automated fashion. (A node is a physical or virtual machine.)

The Chef Infra server in turn loads those cookbooks to the correct nodes. Chef Infra can do this on a massive scale thus eliminating the tedious task of manually configuring your infrastructure nodes individually.

4.       Recipes:

Recipes are collections of resources and instructions that define the desired state of a particular component on a node. They are written in a domain-specific language (DSL) and describe the sequence of actions needed to configure a specific part of the system.

5.       Nodes:

Nodes are the servers or systems that are managed using Chef. Each node has a corresponding client that communicates with the Chef server to receive instructions and configuration information.

Workflow of using Chef:

The typical workflow of using Chef involves the following steps:

1.       Write Cookbooks:

Create or customize cookbooks to define the desired configuration of your infrastructure. This includes specifying how software should be installed, configurations applied, and services managed.

2.       Upload Cookbooks:

Upload your cookbooks to the Chef server, making them available to the Chef clients.

3.       Node Registration: Register nodes (servers) with the Chef server by installing the Chef client on them.

4.       Apply Configurations: The Chef client on each node regularly checks with the Chef server for configuration updates. It then applies the required changes to ensure that the node's actual state matches the desired state described in the cookbooks.

5.       Continuous Management: As your infrastructure evolves, you can update and modify your cookbooks, and the changes will be propagated to the nodes, ensuring consistency and desired configuration.

Setup Chef in the VM

Install Chef in Ubuntu

$ wget https://packages.chef.io/files/stable/chef-workstation/21.10.640/ubuntu/20.04/chef-workstation_21.10.640-1_amd64.deb

$ sudo dpkg -i chef-workstation_21.10.640-1_amd64.deb




Check the version:

$ chef -v




Setup Nginx in a local machine using Chef

Create a folder – chef-repo/cookbooks

$ mkdir chef-repo

$ cd chef-repo

$ mkdir cookbooks

$ cd cookbooks

Create cookbook with name – mynginx

$ chef generate cookbook mynginx



Edit default.rb and add contents for nginx

$ sudo nano mynginx/recipes/default.rb



Generate cookbook file – index.html (should be inside mynginx folder)

$ cd mynginx/

$ chef generate file index.html




Add contents to the index.html

$ sudo nano files/index.html






Generate cookbook template – index.html (should be inside mynginx folder)

$ chef generate template nginx.conf




Add template for nginx to ./templates/nginx.conf.erb








Run chef command

$ sudo chef-client -z --runlist "mynginx"





Access nginx (public URL)