➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Multi Vagrant VM File | If we look at our previous Vagrantfile, we have a folder and we placed a Vagrantfile in that folder. So you can have just one VM per folder. If you want multiple VMs, then you need to manage different folders. You need to manage them with different commands. It’ll be very convenient.
But we want to manage multiple VMs together from one single Vagrantfile. This would be especially for an application stack that needs multiple services running in multiple VMs, like a database on a separate VM, application service on a separate VM, and frontend service on a separate VM. But together they work and form one single application.
In such cases, you need to create a multi-VM Vagrantfile. Vagrant Multi-Machine.
Create a folder “multiVM”, and create a Vagrantfile in it.
mkdir -p /c/install/vagrant-vms/multivm/ && cd /c/install/vagrant-vms/multivm/
touch Vagrantfile
Put the below content in the newly created Vagrantfile.
# Vagrant configuration using version 2 syntax
Vagrant.configure("2") do |config|
# Define the first web server VM
config.vm.define "web01" do |web01|
web01.vm.box = "ubuntu/focal64" # Specify the Vagrant box to use
web01.vm.hostname = "web01" # Set the hostname for the VM
# Configure a private network with a static IP
web01.vm.network "private_network", ip: "192.168.56.41"
end
# Define the second web server VM
config.vm.define "web02" do |web02|
web02.vm.box = "ubuntu/focal64"
web02.vm.hostname = "web02"
web02.vm.network "private_network", ip: "192.168.56.42"
end
# Define the third web server VM
config.vm.define "web03" do |web03|
web03.vm.box = "ubuntu/focal64"
web03.vm.hostname = "web03"
web03.vm.network "private_network", ip: "192.168.56.43"
end
# Define the database server VM
config.vm.define "db01" do |db01|
db01.vm.box = "centos/7"
db01.vm.hostname = "db01"
db01.vm.network "private_network", ip: "192.168.56.44"
# Provisioning script to set up the database server
db01.vm.provision "shell", inline: <<-SHELL
yum install -y wget unzip mariadb-server -y # Install necessary packages
systemctl start mariadb # Start the MariaDB service
systemctl enable mariadb # Enable MariaDB to start on boot
# Additional provisioning steps for db01 can be added here
SHELL
end
end
Explanation:
- Web Server VMs (web01, web02, web03): These sections define three Ubuntu-based virtual machines with distinct hostnames and private network IP addresses. Each VM uses the
ubuntu/focal64
box. - Database Server VM (db01): This section defines a CentOS-based virtual machine with a distinct hostname and private network IP address. It also includes a shell provisioner to install and configure MariaDB.
- Provisioning Script: The provisioning script for
db01
installswget
,unzip
, andmariadb-server
, starts the MariaDB service, and enables it to start automatically on boot.
Start the VM:-
vagrant up
We used to SSH using vagrant SSH
command, but now we have multiple environments. Therefore we have to give names like vagrant SSH web01
.
$ vagrant ssh
This command requires a specific VM name to target in a multi-VM environment.
$ vagrant ssh web01
If we do vagrant halt
then it will shut all the VMS, But if we want to shut some specific VM then we can specify the name vagrant halt web01. Similarly vagrant destroy will destroy all the VMs, and to destroy some specific VMs we can specify vagrant destroy web01. Similarly to start only specific VMs we can specify vagrant up web01
.
Clean up:- Destroy the vagrant.
vagrant destroy
vagrant global-status --prune
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!