DEV Community

Cover image for Deploy your development infrastructure with 1 cmd💡
Arthur William
Arthur William

Posted on • Updated on

Deploy your development infrastructure with 1 cmd💡

Vagrant is command line utility for managing the lifecycle of virtual machines. It help you to deploy a cluster of server with just one command line. And for that, we only need to create vagrantfile which will contain configuration of the VM you want to deploy.

Vagrant will create VM based on the Virtualisation software you're using. In my case, I'll use Virtualbox. But it's also work with VMware, Docker, Hyper-V or a custom provider (AWS). These virtualisation tools are called Providers in Vagrant.

A pratical use case of vagrant can be : "In a company, if a developer want to test a feature on his computer before pushing to production, he can use a vagrantfile build by for example a devops engineer and get all an infrastructure similar to production".

Image description
The image above present global architecture of Vagrant.

Get Started with Vagrant

I'm using Ubuntu 20.04 for this lab

How to install vagrant ?

Before installing vagrant, you need to install virtualisation tool for example virtualbox in case. Run the command below if you want to use virtualbox and it's not installed.

sudo apt update
sudo apt install virtualbox -y
Enter fullscreen mode Exit fullscreen mode

Add repository addition dependencies if it's not installed on your system.

sudo apt install -y apt-transport-https \
                    ca-certificates \
                    curl \
                    software-properties-common
Enter fullscreen mode Exit fullscreen mode

Add the official Vagrant APT repository to your system

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
Enter fullscreen mode Exit fullscreen mode

And finaly run command below to install vagrant

sudo apt update
sudo apt install vagrant -y
vagrant --version
Enter fullscreen mode Exit fullscreen mode

How to use Vagrant ?

Vagrantfile

We'll use a simple vagrantfile for this tuto. You can check the full documentation on how to create vagrantfile here.

# -*- mode: ruby -*-
# vi: set ft=ruby :

BRIDGE_NETWORK = "Realtek RTL8852AE WiFi 6 802.11ax PCIe Adapter"
#replace with with your bridge network

### Master (Kubernetes Master Node)
MASTER_RAM = 4096
MASTER_CPU = 4

### Cluster nodernetes
N = 2
RAM = 2048
CPU = 2
PRIVATE_IP_PREFIX = "192.168.130."
PUBLIC_IP_PREFIX = "192.168.1."

Vagrant.configure("2") do |config|
  config.vm.provision "shell", inline: "echo Hello"

  (1..N).each do |machine_id|
    config.vm.define "node-0#{machine_id}" do |node|
      node.vm.box = "peru/ubuntu-20.04-server-amd64"
      node.vm.hostname = "node-0#{machine_id}"
      node.vm.provider "virtualbox" do |v|
        v.name = "node-0#{machine_id}"
        v.memory = RAM
        v.cpus = CPU
      end
      node.vm.network "public_network", bridge: BRIDGE_NETWORK, ip: "#{PUBLIC_IP_PREFIX}#{170+machine_id}"
      node.vm.network "private_network", ip: "#{PRIVATE_IP_PREFIX}#{170+machine_id}"
      # config.vm.provision "ansible" do |ansible|
      #   ansible.playbook = "ansible-playbook/site_staging.yml"
      # end
#you can use playbook there to configure your vm after their creation
    end
  end

  config.vm.define "master" do |master|
    master.vm.box = "peru/ubuntu-20.04-server-amd64"
    master.vm.provider "virtualbox" do |v|
      v.name = "master"
      v.memory = MASTER_RAM
      v.cpus = MASTER_CPU
    end
    master.vm.network "public_network", bridge: BRIDGE_NETWORK, ip: "#{PUBLIC_IP_PREFIX}#{170}"
    master.vm.network "private_network", ip: "#{PRIVATE_IP_PREFIX}#{170}"
  end

end
Enter fullscreen mode Exit fullscreen mode

This image present code in vagrantfile for configuration of a VM worker node in Kubernetes Cluster. Full Code here

Deploy a Cluster with Vagrant

After writing your vagrantfile, we just need to run the command vagrant up to create our cluster.

vagrant validate //look for error in our vagrantfile
vagrant up
Enter fullscreen mode Exit fullscreen mode

Delete the cluster by using the command below

vagrant destroy
Enter fullscreen mode Exit fullscreen mode

Remote ssh to your VM using the command below

ssh vagrant@ip //ip of the VM your want to connect
Enter fullscreen mode Exit fullscreen mode

The default username and password in VM create with vagrant is vagrant.

Top comments (0)