DevOps Classroom Series – 14/Mar/2021

Infrastructure as Code (IAC)

  • The idea behind IAC is that you write and execute the code to define, deploy, update and destroy your infrastructure.
  • A Key insight of DevOps is that you can manage everything in code, including servers, databases, networks application configuration, deployment process and so on
  • We have five broad categories of IAC tools
    • Adhoc scripts
      • In this we automate the task by breaking down into discrete steps and use any scripting language (Bash, Python, Ruby, Powershell) and execute them on the server
    • Configuration Management tools
      • These tools are designed to install and manage software on existing servers
      • Chef, Puppet, Ansible, SaltStack all are configuration management tools
      • Advantages
        • Idempotence
        • Distribution
        • Code conventions
    • Server Templating tools:
      • An alternative to configuration management is server templating tools.
      • Instead of launching bunch of server and configuring them by running same code on each one. The idea behind server templating tools is to create image of the server that captures a fully self contained snapshot of Operating System.
      • In Server templating we can use other IAC tools to install softwares inside the image
      • Example: Packer
    • Orchestration tools
      • Server Templating tools are great for creating VMs but to manage them we need some kind of orchestration tools
      • Example: Kubernetes
    • Provisioning tools:
      • Provisioning tools such as Terraform, Cloudformation, ARM Template are responsible for create servers them selves.
      • We can use provisioning tools not only to create servers but also other resources such as network, loadbalancer, database etc

Packer

  • The basic idea of packer is to create the virtual machine image
  • In the case of aws we call the vm image as Amazon Machine Image (AMI)
  • So the idea is to create AMI with the application pre installed in it Preview
  • In the case of Azure we create a vm image Preview
  • The workflow of packer Preview
  • Packer documentation Refer Here
  • Terminology in packer
    • Builders: These are responsible for creating images for various platform Preview
    • Provisioners: Inside the machine, we need to install software, so to do this we use provisioners Preview
    • Template: In packer templates are JSON files which define one or more builders to create multiple machine images.
  • For understanding Json Refer Here
  • Both Packer and Terraform are from the same organization Hashicorp and they are developed in GO language. we will have only one executable
  • Install Packer
    • Traditional Approach:
      • Download the package Refer Here and add packer to the PATH so that we can run packer from command line Preview
    • Windows: Install packer using chocolatey Refer Here Preview Preview
    • Mac: use homebrew Refer Here
    • Linux: follow the traditional approach

Packer Templates

  • From packer 1.7.0 packer officially supports two kinds of templates
    • Json templates: These are supported in the older version of packer also
    • HCL Templates: Theser are newly supported from packer 1.7.0
  • Lets Start working with JSON Templates:

Packer JSON Templates

  • Template Structure:
    • The template is JSON object that has set of keys configuring various components of packer
    • Packer has the following keys
      • builders: This is required section which consist of defintion of one or more builders where packer needs to create images
      • description: This is optional and is used to describe what template does
      • provisioners: This is optional and is array of object that define the provisioner to be used to install/deploy application
      • variables: This is optional and definde one or more key/value string that define the user variables in a template
      • post-processor: this is optional and it defines the post processing steps
  • Basic template in Json
{
    "builders": [],
    "description": "This is hello-world packer",
    "provisioners": []
}
  • Sample Template
{
    "builders": [
      {
        "type": "amazon-ebs",
        "access_key": "...",
        "secret_key": "...",
        "region": "us-east-1",
        "source_ami": "ami-fce3c696",
        "instance_type": "t2.micro",
        "ssh_username": "ubuntu",
        "ami_name": "packer {{timestamp}}"
      }
    ],
  
    "provisioners": [
      {
        "type": "shell",
        "script": "setup_things.sh"
      }
    ]
  }

Creating our first packer image in AWS

  • Lets try to create our image in AWS using packer

  • The amazon machine image should be based on ubuntu 18.04 and should contain lamp stack Refer Here

  • Manual Steps:

    • Create a ubuntu 18.04 based ec2 instance Preview Preview Preview Preview Preview Preview Preview
    • Now login into vm instance created and execute the installation steps for creating lamp server

    Preview Preview

    • Steps for creating lamp server
    #!/bin/bash
    sudo apt update
    sudo apt install apache2 -y
    sudo apt install php libapache2-mod-php php-mysql php-cli -y
    echo '<?php phpinfo(); ?>' | sudo tee /var/www/html/info.php
    sudo systemctl enable apache2
    sudo systemctl restart apache2
    
    • Now create an ami Preview Preview Preview Preview
    • Wait for the ami to be created and create an ec2 instance with this ami to check if the application can be used directly
  • Using packer lets try to create a template with a builder and a shell provisioner Refer Here for the changeset

  • Now lets find the commands Preview Preview Preview Preview

  • For the first run, lets use -debug. This -debug stops at every logical step and user has to use the ENTER key to proceed Preview Preview

  • Now lets create ec2 instance from this ami id and check if the application is preinstalled

  • Exercise: Create a packer template to create an ami to install game of life based on ubuntu 18

sudo apt update
sleep 30s
sudo apt install openjdk-8-jdk -y
sleep 10s
sudo apt install tomcat8 -y
cd /tmp
wget https://referenceappkhaja.s3-us-west-2.amazonaws.com/gameoflife.war
sudo cp /tmp/gameoflife.war /var/lib/tomcat8/webapps/gameoflife.war
sudo systemctl enable tomcat8
sudo systemctl start tomcat8
  • Try to create the same game of life image based on centos 7

Next Steps

  • Lets try to create a gameoflife image in azure
  • Lets look at new HCL based packer syntax.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About learningthoughtsadmin