Multi-vm setup in vagrant (IAC) - Day 17/90

Multi-vm setup in vagrant (IAC) - Day 17/90

ยท

2 min read

Virtualization has become an essential part of modern development and testing workflows. Vagrant, a powerful open-source tool, makes it easier to manage and deploy virtual machines. In this guide, we'll walk through the process of setting up multiple virtual machines using a Vagrantfile, making your development environment more scalable and efficient. ๐ŸŒ๐Ÿ’ป

Prerequisites ๐Ÿ› ๏ธ

Before diving into the setup, ensure that you have Vagrant installed on your system. You can download it from the official website: Vagrant Downloads.

The Vagrantfile ๐Ÿ”ง

Let's start by creating a Vagrantfile, the configuration file that defines the virtual machines. Copy and paste the following code into your Vagrantfile:

Vagrant.configure("2") do |config|
    config.vm.define "web01" do |web01|
        web01.vm.box = "ubuntu/focal64"
        web01.vm.hostname = "web01"
        web01.vm.network "private_network", ip: "192.168.38.49"
    end

    config.vm.define "web02" do |web02|
        web02.vm.box = "ubuntu/focal64"
        web02.vm.hostname = "web02"
        web02.vm.network "private_network", ip: "192.168.38.50"
    end

    config.vm.define "web03" do |web03|
        web03.vm.box = "ubuntu/focal64"
        web03.vm.hostname = "web03"
        web03.vm.network "private_network", ip: "192.168.38.51"
    end

    config.vm.define "db01" do |db01|
        db01.vm.box = "centos/7"
        db01.vm.hostname = "db01"
        db01.vm.network "private_network", ip: "192.168.38.53"
        db01.vm.provision "shell", inline: <<-SHELL
            yum install -y wget unzip mariadb-server
            systemctl restart mariadb
            systemctl enable mariadb
        SHELL
    end
end

This Vagrantfile defines four virtual machines: web01, web02, web03 using Ubuntu 20.04, and db01 using CentOS 7 with a provisioned MariaDB server.

Explanation of the Vagrantfile ๐Ÿ“

  • Each config.vm.define block defines a virtual machine with a specific name.

  • config.vm.box sets the base box for the virtual machine.

  • config.vm.hostname sets the hostname of the virtual machine.

  • config.vm.network defines a private network with a specified IP address.

  • For db01, a provision script is executed to install and configure MariaDB.

Bringing Up the Virtual Machines ๐Ÿš€

Now that the Vagrantfile is ready, open a terminal and navigate to the directory containing the Vagrantfile. Run the following command:

vagrant up

This command will download the specified base boxes and create the virtual machines as per the configuration in the Vagrantfile.

Let us check vm in virtual box:

Accessing the Virtual Machines ๐ŸŒ

You can access each virtual machine using the following commands:

vagrant ssh web01
vagrant ssh web02
vagrant ssh web03
vagrant ssh db01

Feel free to explore and customize each virtual machine according to your development needs.

Conclusion ๐ŸŽ‰

Congratulations! You've successfully set up multiple virtual machines using Vagrant. This approach allows you to create a flexible and reproducible development environment. Experiment with different configurations and enhance your development workflow. Happy coding! ๐Ÿš€๐Ÿ‘จโ€๐Ÿ’ป

Did you find this article valuable?

Support 90 days of Devops by becoming a sponsor. Any amount is appreciated!

ย