How to Create and Configure a Multi-VM Vagrant Setup for Web and Database Servers

josephibehdev

Joseph Ibeh

Posted on November 17, 2024

How to Create and Configure a Multi-VM Vagrant Setup for Web and Database Servers

Introduction

Virtualization is an essential tool for developers and system administrators. It allows you to simulate multiple servers on a single physical machine, helping you test, develop, and simulate real-world environments. Vagrant is a popular open-source tool that makes it easy to manage virtual machines (VMs) and set up reproducible development environments.

In this article, we’ll walk through how to create a multi-VM setup using Vagrant. Our goal is to set up a basic infrastructure with two web servers (web01 and web02) and one database server (db01). We’ll also show you how to automate the provisioning of these VMs using a simple shell script.

Prerequisites
Before we get started, make sure you have the following tools installed:

  • Vagrant: A tool for building and managing virtualized environments.
  • VirtualBox: A virtualization platform that Vagrant uses to create and manage virtual machines.
  • Git (optional): For version control and saving your project to GitHub.

You can install both Vagrant and VirtualBox from their official websites:

Project Overview
In this setup, we will create:

  • 3 VMs:
    • web01: A web server running Ubuntu 20.04.
    • web02: Another web server running Ubuntu 20.04.
    • db01: A database server running Ubuntu 20.04, with MySQL installed and provisioned automatically.

All VMs will have private IPs so they can communicate with each other within the network.

Step-by-Step Guide

  1. Setting Up the Vagrantfile The core of the setup is the Vagrantfile, a configuration file where you define your virtual machines and their settings. Below is the full code for the Vagrantfile:
Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"  

  config.vm.define "web01" do |web01|
    web01.vm.hostname = "web01"
    web01.vm.network "private_network", ip: "192.168.56.41"
    web01.vm.provider "virtualbox" do |vb|
      vb.memory = "512"
      vb.cpus = 1
    end
  end

  config.vm.define "web02" do |web02|
    web02.vm.hostname = "web02"
    web02.vm.network "private_network", ip: "192.168.56.42"
    web02.vm.provider "virtualbox" do |vb|
      vb.memory = "512"
      vb.cpus = 1
    end
  end

  config.vm.define "db01" do |db01|
    db01.vm.hostname = "db01"
    db01.vm.network "private_network", ip: "192.168.56.43"
    db01.vm.provider "virtualbox" do |vb|
      vb.memory = "1024"
      vb.cpus = 2
    end

    db01.vm.provision "shell", inline: <<-SHELL
      sudo apt-get update -y
      sudo apt-get install -y wget unzip mysql-server -y
      sudo systemctl enable mysql
      sudo systemctl start mysql
      echo "Database server setup completed."
    SHELL
  end
end
Enter fullscreen mode Exit fullscreen mode

Explanation of Key Sections:

  • config.vm.box = "ubuntu/focal64": This sets the base VM image to Ubuntu 20.04 (Focal Fossa).
  • config.vm.define "web01" and config.vm.define "web02": These sections define the web01 and web02 VMs, assigning them IPs and setting up VirtualBox memory and CPU options.
  • config.vm.define "db01": This defines the database server, assigns an IP, and provisions it by installing MySQL.

2. Running the Vagrantfile
Once you have the Vagrantfile in place, navigate to the directory where it is saved and run the following command:

vagrant up
Enter fullscreen mode Exit fullscreen mode

output
vagrant up
This will create and start the virtual machines as defined in your Vagrantfile. Vagrant will download the required box (Ubuntu 20.04) and create the VMs. The provisioning script will also install MySQL on db01.

3. Accessing the VMs
Once the VMs are up and running, you can SSH into them to check their status or interact with the environment.

For db01 (the database server), you can SSH like this:

vagrant ssh db01
Enter fullscreen mode Exit fullscreen mode

output
vagrant ssh dboi
To check if MySQL is running, you can use:

mysql --version
Enter fullscreen mode Exit fullscreen mode

output
mysql --version

4. Testing the Setup
Now that your VMs are running, it’s a good idea to verify everything is set up correctly. Here’s how you can check:

  • For web01 and web02: Make sure they are able to ping each other.
  • For db01: Verify that MySQL is running and can accept connections from the web servers.

Testing Connectivity Between VMs

  1. Check if web01 can ping web02 and vice versa: Since all the VMs are on a private network, they should be able to communicate with each other. To test this, you can SSH into web01 and try pinging web02:

On web01, run:

   ping 192.168.56.42
Enter fullscreen mode Exit fullscreen mode

output
ping
It means web01 can communicate with web02. You should also repeat the process by logging into web02 and pinging web01:

   ping 192.168.56.41
Enter fullscreen mode Exit fullscreen mode

output

ping
It confirms that the web servers can communicate with each other.

  1. Test connectivity to db01 from web01 and web02: To ensure that your web servers can access the database server (db01), SSH into web01 or web02 and try pinging the database server (db01):
   ping 192.168.56.43
Enter fullscreen mode Exit fullscreen mode

output
ping
your web servers can reach the database server since the pings are successful.

Testing MySQL on db01

  1. Check if MySQL is running: SSH into db01 and check if MySQL is running:
   vagrant ssh db01
   sudo systemctl status mysql
Enter fullscreen mode Exit fullscreen mode

output

vagrant ssh
This indicate that MySQL is active (running).

  1. Verify MySQL Installation: To ensure that MySQL is properly installed and functioning, you can check the MySQL version:
   mysql --version
Enter fullscreen mode Exit fullscreen mode

output

mysql v
This output shows the MySQL version that is installed on db01.

Conclusion
In this tutorial, we’ve created a simple multi-VM environment using Vagrant, consisting of two web servers and a database server with MySQL. We also automated the database server’s setup with a shell provisioning script.

By using Vagrant, you can easily replicate this environment, experiment with different configurations, and quickly tear it down when you're done. This setup is perfect for testing, learning, or developing web applications in isolated environments.

💖 💪 🙅 🚩
josephibehdev
Joseph Ibeh

Posted on November 17, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related