In this article, we'll discuss how to set up a small K3s cluster using Vagrant and VirtualBox. We'll cover the key concepts involved in this process, including how to enable communication between the two VirtualBox VMs. By the end of this article, you should have a solid understanding of how to build and manage a small K3s cluster using Vagrant and VirtualBox.
Prerequisites
Before we begin, there are a few prerequisites that you should be aware of. First, you'll need to have Vagrant installed on your system. You can download the latest version of Vagrant from the official website: https://www.vagrantup.com/downloads.html. Additionally, you'll need to have VirtualBox installed. You can download VirtualBox from the official website: https://www.virtualbox.org/wiki/Downloads.
Creating a Vagrantfile
Once you have Vagrant and VirtualBox installed, you can create a new Vagrantfile. This file will define the configuration for your new VMs. Here's an example Vagrantfile that you can use as a starting point:
Vagrant.configure("2") do |config|
config.vm.define "vm1" do |vm1|
vm1.vm.box = "ubuntu/focal64"
vm1.vm.network "private_network", ip: "192.168.50.10"
vm1.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
config.vm.define "vm2" do |vm2|
vm2.vm.box = "ubuntu/focal64"
vm2.vm.network "private_network", ip: "192.168.50.11"
vm2.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
end
In this example Vagrantfile, we define two VMs: vm1 and vm2. Both VMs are using the same base box (Ubuntu Focal 64-bit). We also define a private network for each VM, which will allow them to communicate with each other. The IP addresses for the private networks are 192.168.50.10 and 192.168.50.11, respectively.
Enabling Communication Between the VMs
Now that we have our Vagrantfile set up, we can start our VMs using the following commands:
vagrant up vm1
vagrant up vm2
Once the VMs have started, we can log into each VM using the following commands:
vagrant ssh vm1
vagrant ssh vm2
At this point, you may be wondering how to enable communication between the two VMs. By default, the private network that we defined in the Vagrantfile is not accessible from the host machine. However, we can modify the Vagrantfile to make the private network accessible from the host machine. Here's an updated Vagrantfile that includes this modification:
Vagrant.configure("2") do |config|
config.vm.define "vm1" do |vm1|
vm1.vm.box = "ubuntu/focal64"
vm1.vm.network "private_network", ip: "192.168.50.10"
config.vm.network "forwarded_port", guest: 22, host: 2222, auto\_correct: true
vm1.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
config.vm.define "vm2" do |vm2|
vm2.vm.box = "ubuntu/focal64"
vm2.vm.network "private\_network", ip: "192.168.50.11"
config.vm.network "forwarded\_port", guest: 22, host: 2223, auto\_correct: true
vm2.vm.provider "virtualbox" do |vb|
vb.memory = "2048"
end
end
end
In this updated Vagrantfile, we've added a "forwarded\_port" configuration for each VM. This configuration maps the guest port (22) to a host port (2222 or 2223). This allows us to log into each VM from the host machine using the following commands:
ssh vagrant@localhost -p 2222
ssh vagrant@localhost -p 2223
Now that we have communication enabled between the host machine and each VM, we can move on to installing and configuring K3s.
Installing and Configuring K3s
Before we install K3s, we need to install Docker on each VM. We can do this using the following commands:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb\_release -cs) stable"
sudo apt update
sudo apt install docker-ce -y
Once Docker is installed, we can install K3s using the following commands:
curl -sfL https://get.k3s.io | sh -
This will install K3s on each VM. By default, K3s will start automatically after installation. We can verify that K3s is running using the following command:
sudo k3s kubectl get nodes
At this point, we have a functional K3s cluster with two nodes. We can now start deploying applications to the cluster.
- We've discussed how to set up a small K3s cluster using Vagrant and VirtualBox.
- We've covered the key concepts involved in this process, including how to enable communication between the two VirtualBox VMs using a private network and forwarded ports.
- We've also discussed how to install and configure K3s, including how to verify that the cluster is running correctly.