Kategorie: Infrastructure as Code

  • Installing Jenkins in AWS using Vagrant and Ansible

    This tutorial builds up upon the previous introductions to Vagrant and Ansible. Firstly, we will set up a Vagrant Box with Amazon Web Services (AWS). Subsequently, we will use the Ansible script to configure Jenkins in this Box, using the same Playbook as in the previous blog post.

    Prerequisites

    Setting up an AWS Account

    I assume that you have no AWS account yet. Please register for an AWS account and choose the Basic Plan. If you are new to AWS, Amazon provides a so-called AWS Free Tier that lets you test AWS for 12 months.

    The web-interface can be very confusing at times as AWS is a powerful tool that provides tons of services.  Here we will solely focus on the Elastic Compute Cloud (EC2). Before you configure our Vagrantfile, we will have to set-up AWS. For the sake of simplicity and focus, I will only get into details if necessary.

    Create a new Access Key

    First of all, we need to create an access key. It is not recommended, to work with the root security credentials, as they give full access to your AWS account. Please add a new user using the IAM console (Identity and Access Management).  I added a user named vagrant-user with Programmatic Access. I add this user to a group I call admin and give this group AdministratorAccess (we can change this later).

    AWS Add User
    AWS Add User

    After the user was successfully added, I retrieve the Access Key ID and the Secret Access Key. Please store this information as it won’t be available again. These credentials are needed to create a new EC2 instance with Vagrant.

    Create a new Key Pair

    A new Key-Pair can be created in the EC2 console (Services -> EC2 -> Key Pairs). This Key-Pair is used for SSH access to your EC2 instance, once the instance was created. I choose the name vagrant-key.

    AWS Generate KeyPair
    AWS Create a new Key Pair

    The public key will be stored in AWS, the private key file is automatically downloaded (in this case vagrant-key.pem). Store this key in a safe place and set permission to read-only for the current user (chmod 400 vagrant-key.pem).

    Creating a Security Group

    Next, we have to create a Security Group to allow an SSH (and optionally an HTTP) connection to the EC instance. The connection to the EC2 instance will be created when the EC2 instance is created by Vagrant. You can use the default-group that should already exist or create a new group. I will create a group called vagrant-group and allow SSH and HTTP from anywhere (inbound-traffic).

    Creating a AWS Security Group
    Creating an AWS Security Group

    Choose an Image

    Images in EC2 are called AMI (Amazon Machine Image) and are identified by an ID (e.g. ami-8c122be9). The IDs are region-specific, i.e. not every image is available in any region. This is important as you will set the AMI and the region in the provider configuration in your Vagrantfile later, and you will run into errors if the AMI is not available in your region.

    You will find a list of the available images and their IDs when you launch a new EC2 instance.

    My AWS account is in the us-east-2 region (the region will show up in your EC2 dashboard) and I choose the image Ubuntu Server 16.04 LTS (HVM), SSD Volume Type with the IDami-5e8bb23b (as we used an Ubuntu distribution for our VirtualBox in the Vagrant tutorial). We need this information later in our Vagrantfile.

    Preparing Vagrant

    Adding the Vagrant AWS Provider

    As Vagrant doesn’t come with a built-in AWS provider, it has to be installed manually as a plugin

    vagrant plugin install vagrant-aws

    To check if the plugin was successfully installed use (the plugin should appear in the list)

    vagrant plugin list

    Adding a Vagrant Dummy Box

    The definition of a Vagrant box is mandatory in the Vagrantfile. If you work with AWS you will use Amazon Machine Images (as mentioned above). Hence, the definition of a Vagrant box is only a formality. For this purpose, the author of the AWS plugin for Vagrant has created a dummy-box. To add this box to Vagrant use:

    vagrant box add aws-dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

    Vagrant Configuration

    I will now use the information I collected above to populate the Vagrantfile

    Vagrant.configure("2") do |config|
    
        config.vm.box = "aws-dummy"
    
        config.vm.provider :aws do |aws, override|
    
           aws.access_key_id = "AKIAJZ3GLCDOCOSTQKZA"
           aws.secret_access_key = "8AbPIrAXP8r14+SqCiM/9kXsvYzqxh9e27+NDoRQ"
           aws.keypair_name = "vagrant-key"
    
           aws.region = "us-east-2"
           aws.instance_type = "t2.micro"
           aws.ami = "ami-5e8bb23b"
    
           aws.security_groups = ['vagrant-group']
    
           # https://github.com/mitchellh/vagrant-aws/issues/365
           config.vm.synced_folder ".", "/vagrant-test", disabled: true
    
           override.ssh.username = "ubuntu"
           override.ssh.private_key_path = "./vagrant-test.pem"
    
           config.vm.provision "shell", inline: "which python || sudo apt -y install python"
    
       end
    
    end
    

    When launching the instance, I encountered the problem that I was asked for SMB credentials (on MacOS!). I solved this problem by disabling folder-sync as mentioned in the support forum.

    config.vm.synced_folder ".", "/vagrant-test", disabled: true

    The ssh-username depends on the AMI, for the selected Ubuntu-AMI, the username is „ubuntu“, for the Amazon Linux 2 AMI (HVM), SSD Volume Type (ami-8c122be9), it is „ec2-user“.

    In addition, I install Python as a pre-requisite for Ansible

    config.vm.provision "shell", inline: "which python || sudo apt -y install python"
    

    As for instance type I chose a small t2.micro instance (1 Virtual CPU, 1 GB RAM), that is included in the AWS Free Tier.

    Launching the EC2 Instance

    To launch the instance, use

    vagrant up --provider=aws

    In the console, you should see an output similar to

    Bringing machine 'default' up with 'aws' provider...
    ==> default: Warning! The AWS provider doesn't support any of the Vagrant
    ==> default: high-level network configurations (`config.vm.network`). They
    ==> default: will be silently ignored.
    ==> default: Launching an instance with the following settings...
    ==> default: -- Type: t2.micro
    ==> default: -- AMI:ami-5e8bb23b
    ==> default: -- Region: us-east-2
    ==> default: -- Keypair: vagrant-key
    ==> default: -- Security Groups: ["vagrant-group"]
    ==> default: -- Block Device Mapping: []
    ==> default: -- Terminate On Shutdown: false
    ==> default: -- Monitoring: false
    ==> default: -- EBS optimized: false
    ==> default: -- Source Destination check:
    ==> default: -- Assigning a public IP address in a VPC: false
    ==> default: -- VPC tenancy specification: default
    ==> default: Waiting for instance to become "ready"...
    ==> default: Waiting for SSH to become available...
    ==> default: Machine is booted and ready for use!

    If you encounter any problems, you can call vagrant up in debug-mode

    vagrant up --provider=aws --debug

    You know should be able to see the EC2 instance in the EC2 console in the state running. The security group we created earlier (vagrant-should) should have been linked. In this view, you can also retrieve the public DNS and IP address to access this instance from the Internet.

    AWS EC2 Instance Overview
    AWS EC2 Instance Overview

    You can now access the instance using

    vagrant ssh

    In addition, you can log into your EC2 instance using SSH and your DNS

    ssh -i "vagrant-key.pem" ubuntu@ec2-18-191-42-181.us-east-2.compute.amazonaws.com

    For a more detailed explanation, just choose your running instance and click on the Connect button (in the Instance Overview Page displayed above).

    Installing Jenkins

    One way to use the Ansible Playbook to provision the EC2 instance with Jenkins is to include it as an Ansible Provisioner in the Vagrantfile

    config.vm.provision "ansible" do |ansible|
        ansible.playbook = "playbook.yml"
    end

    Another way is to include the EC2 instance in the Inventory of Ansible (/etc/ansible/hosts ).

    TASK [Update apt packages] ************************************************************
    web1 ansible_host=127.0.0.1 ansible_user=vagrant ansible_port=2200
    web2 ansible_host=127.0.0.1 ansible_user=vagrant ansible_port=2222
    ec2 ansible_host=18.191.42.181 ansible_user=ubuntu

    Subsequently, add the ec2 host to our Ansible Playbook, we created in the Ansible tutorial

    name: Install Jenkins software
     hosts: web1, web2, ec2
     gather_facts: true
     become: yes
     ...

    To run the Playbook use

    ansible-playbook -l ec2 jenkins.yml

    If everything works smoothly, Jenkins should now be installed on your EC2 instance.

    If you run into an authentification error (access denied) you might have to add the public SSH key of your EC2 instance to the authentication agent with

    ssh-add vagrant-key.pem

    Before you can access Jenkins you have to allow an inbound connection on port 8080 in the Security Group vagrant-group (we previously created).

    Opening Port 8080 to access Jenkins
    Opening Port 8080 to access Jenkins

    If you now call the URL (or IP address) of the EC2 instance in your browser on Port 8080, you should see the Jenkins installation-screen. Voilà.

    http://ec2-18-191-42-181.us-east-2.compute.amazonaws.com:8080/login?from=%2F
    Jenkins Installation Screen
    Jenkins Installation Screen

    Further Remarks

    Of course, there are more elegant ways to construct the Vagrantfile, e.g. by reading the credentials from environment variables or from ~/.aws. I have chosen this format for sake of simplicity. There are also many more configuration options than describe here. Please have a further look at the vagrant-aws documentation.

    Make sure to destroy your EC2 instance when you don’t need it anymore, as it will consume your credits. The Billing Dashboard provides a good summary of your usage.

  • Vagrant in a Nutshell

    Vagrant is a command-line tool to create virtual environments using a scripting language, i.e. with Vagrant you can create and configure virtual machines by using a text editor. The use of a text-file for configuration makes it easy to document the configuration, to work in collaboration and to store the configuration in a version control system. Vagrant makes it easy for developers to simulate a production environment on their local development machines.

    Vagrant supports different providers out-of-the-box, such as VirtualBox and Hyper-V, others, such as VMWare, Docker, Google Cloud Platform or Amazon Webservices via plugins.

    In this short tutorial, we will create a script to set-up two virtual machines, each with a different configuration to run them in a Virtual Box. We will later configure these two machines using Ansible to set-up a Jenkins server and an Integration environment.

    Prerequisites

    Before we start, please install VirtualBox on your local machine. If you encounter any problems during installation on a Mac you might consult this article. Subsequently, create a folder for the Vagrant configuration (I created a folder in/vagrant my home directory). Install Vagrant (on my Mac I use brew cask install vagrant).

    Setting Up a simple Virtual Machine

    The file that contains the configuration is named Vagrantfile. Use a text editor to create a Vagrantfile with the following content:

    Vagrant.configure("2") do |config|
        config.vm.define "server1" do |web|
            web.vm.box = "ubuntu/xenial64"
        end
    end
    

    The (“2”) is the Vagrant version (the current version number is 2). The name encapsulated by the vertical lines is the name of the configuration (here “config”).

    web.vm.box = "ubuntu/xenial64" defines the so-called box that we want to install on our Virtual Machine (VirtualBox), in our case a basic Ubuntu Linux. A box is basically a package format for the different environments (from different Linux distributions to fully provisioned environments, e.g. Jenkins or ELK stack). You will find these boxes in the Vagrant Cloud.

    To install and run the VirtualBox, use

    vagrant up

    in the command line in the directory that contains the Vagrantfile. Usually, when you run a vagrant-command, Vagrant will climb up the directory tree and runs the first Vagrantfile it finds.

    It can take a couple of minutes for the box (ubuntu/xenial64) to be downloaded, set-up and run. Voilà, now you should have an Ubuntu Linux in VirtualBox up and running. The virtual machine configuration is located in .vagrant in your /vagrant.

    Vagrant Virtual Box
    Vagrant VirtualBox

    To see if your Virtual Box was created either open VirtualBox or by using

    vagrant status
    Current machine states:
    
    server1                   running (virtualbox)
    
    The VM is running. To stop this VM, you can run `vagrant halt` to
    shut it down forcefully, or you can run `vagrant suspend` to simply
    suspend the virtual machine. In either case, to restart it again,
    simply run `vagrant up`.

    To access the Virtual Machine use

    vagrant ssh

    To stop your VirtualBox, use

    vagrant halt

    Other command line instructions you will use are

    • vagrant destroy – to stop and delete the Virtual Machine
    • vagrant reload – to restart the environment and applying the settings in the Vagrantfile again, without destroying the Virtual Machine.
    • vagrant provision – executes the provision part in the Vagrantfile (to re-configure the environment)
    Vagrant State Diagram
    Vagrant State Diagram

    For more on Vagrant commands use vagrant --help or consult the documentation.

    The Vagrantfile

    Now we will use a Vagrantfile to create two Virtual Machines that we will later use to install Jenkins and SonarQube. I will first provide the Vagrantfile and go into more detail below.

    Vagrant.configure("2") do |config|
        config.vm.define "server1" do |web|
            web.vm.provider "virtualbox" do |vb|
               vb.memory = "2048"
               vb.cpus = "1"
            end
            web.vm.box = "ubuntu/xenial64"
            web.vm.hostname = "server1"
    
            web.vm.network "private_network", type: "dhcp"
    
            web.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"
            web.vm.provision "shell", inline: "which python || sudo apt -y install python"
    
        end
    
        config.vm.define "server2" do |web|
            web.vm.provider "virtualbox" do |vb|
               vb.memory = "2048"
               vb.cpus = "2"
            end
            web.vm.box = "ubuntu/xenial64"
            web.vm.hostname = "server2"
    
            web.vm.network "private_network", type: "dhcp"
    
            web.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"
            web.vm.provision "shell", inline: "which python || sudo apt -y install python"
    
        end
    
    end
    

    To install and run the two Virtual Machines, use

    vagrant up

    Using vagrant status you should now see something like this

    Current machine states:
    
    server1                   running (virtualbox)
    server2                   running (virtualbox)
    
    This environment represents multiple VMs. The VMs are all listed
    above with their current state. For more information about a specific
    VM, run `vagrant status NAME`.

    Instead of using vagrant ssh you should now be able to log into your newly created Virtual Machines using their IP-address and ports (vagrant is the default-user). I configured the Virtual Machines to retrieve the IP addresses via DHCP. You can obtain the ssh-information with

    vagrant ssh-config [server-name], e.g. vagrant ssh-config server1

    You should retrieve an output such as

    Host server1
      HostName 127.0.0.1
      User vagrant
      Port 2222
      UserKnownHostsFile /dev/null
      StrictHostKeyChecking no
      PasswordAuthentication no
      IdentityFile /Users/vividbreeze/vagrant/.vagrant/machines/server1/virtualbox/private_key
      IdentitiesOnly yes
      LogLevel FATAL

    Now you can log into server1 using (server2 respectively)

    ssh vagrant@127.0.0.1 -p2222

    The vagrant-user has sudo privileges by default. If you want to switch to the root user, simply use

    sudo su

    You can set/change the root-user password with

    sudo passwd root

    Provider Configuration

    We can configure our provider (VirtualBox) with specific parameters, in this case, the memory and the number of CPUs. More on the configuration of the different providers can be found in the Vagrant documentation.

    web.vm.provider "virtualbox" do |vb|
       vb.memory = "1024"
       vb.cpus = "1"
    end

    Network Settings

    In the example, I let the DCHP server assign an IP in the private network (not accessible from the Internet) to the Virtual Machine:

    config.vm.network "private_network", type: "dhcp"

    You can also assign static IP to the Virtual Machine using

    web.vm.network "private_network", ip: "192.168.33.11"
    

    In addition, you can define a public network, so your machine will be accessible from outside the host machine

    web.vm.network "public_network"

    If you have more than one network (which is often the case), you will be prompted to select a network after running vagrant up. You specify the network using (you need to provide the full name

    web.vm.network "public_network", bridge: "en0: Wi-Fi (AirPort)"

    Some providers, such as VirtualBox, can be run in a closed environment that is not accessible from the machine it is running on. Port Forwarding enables you to create rules to forward traffic from the host machine to the VirtualMachine.

    web.vm.network "forwarded_port", guest: 80, host: 8080

    For more about Network configuration, please consult the Vagrant documentation.

    Provisioning

    Provisioning means to configure the environments via shell scripts or configuration management tools such as Chef or Ansible. We will only do a basic configuration here. As I will use Ansible for provisioning the machine, Python has to be installed.

    web.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"
    web.vm.provision "shell", inline: "which python || sudo apt -y install python"
    

    You can also call other provisioning tools, such as Ansible Playbooks directly from the Vagrantfile.

    web.vm.provision "ansible" do |ansible|
       ansible.playbook = "playbook.yml"
    end

    SSH

    Most of the Vagrant boxes come with two users – root and vagrant (password: vagrant). You can log into a box by using vagrant ssh [machine-name]. You can omit this when overwriting the ~/.ssh/authorized_keys file with a new file that includes the public key of your host machine. You then can log into the machine with ssh vagrant@[machine-url] -p [port].

    web.vm.provision "file", source: "~/.ssh/id_rsa.pub", destination: "~/.ssh/authorized_keys"
    

    Shared Folders

    Although not used in the example, you are able to share files between your Virtual Machine and your local environment.

    web.vm.synced_folder "./sync_folder", "/vagrant", create: true

    This allows you e.g. to store the configuration on your local machine to easily and quickly reconfigure the software running on your remote machines without connection the server via ssh.

    Further Remarks

    Vagrant is an easy way to design simple infrastructure as code, mainly used in development environments, e.g. you cannot design a large infrastructure with several components spanning different networks. In the latter case, I recommend using Terraform or Apache Mesos.

    I use Vagrant mainly for testing changes of a configuration before I roll it out in production, e.g. I use a local Jenkins VM, running with the same (virtual) hardware configuration as in production on my local machine. Hence, I test changes of the build pipeline or the configuration of plugins locally before rolling them out on the production system. I also use it for testing new software before I role it out in production.

    Tools that are in a way similar to Vagrant (for setting up infrastructure as code for a development environment) are Nanobox or Docker.

    Of course, this was only a short tutorial that should provide you with the basics to set-up a virtual machine in Vagrant. For further reading, I recommend the Vagrant Documentation.

    Links