If you are a programmer, software developer, software tester, designer, one fine day a project team comes in and you test the software on your machine, you have to build the same system. But for your computer, for convenience of development and testing, and to limit the questions “Why can’t my machine run, but your machine can’t”, then we can use Vagrant to synchronize the development environment. develop
What is Vagrant?
Vagrant is a tool for working with virtual environments and in most cases this means working with virtual machines. Vagrant provides a simple and easy-to-use command client to manage these environments and an interpreter for text-based definitions of what each environment should look like, called vagrantfiles. Vagrant is open source, which means anyone can download, modify it, and share it freely.
Although many virtual machine programmers provide their own command line interfaces, and technically provisioning virtual machines through these programs can be done directly or through shell scripts, the Another advantage provided by adding an extra layer is simplicity, cross-system interoperability and A more consistent approach that can theoretically be used with any virtual environment running on any any other system.
By providing a common text-based format for working with virtual machines, your environment can be defined in code, making it easy to backup, modify, share, and manage with control. revision control. It also means that instead of sharing an entire virtual machine image, which can be many gigabytes, every time a change is made to the configuration, a simple text file can only share a few kilobytes.
IP | Hostname | vCPU | RAM | DISK |
---|---|---|---|---|
192.168.56.2 | microk8s-master-1 | 1 core | 2G | 50G |
192.168.56.3 | microk8s-master-2 | 1 core | 2G | 50G |
192.168.56.4 | microk8s-master-3 | 1 core | 2G | 50G |
192.168.56.5 | microk8s-worker-1 | 1 core | 2G | 50G |
192.168.56.6 | microk8s-worker-2 | 1 core | 2G | 50G |
192.168.56.7 | microk8s-worker-3 | 1 core | 2G | 50G |
192.168.56.8 | microk8s-worker-4 | 1 core | 2G | 50G |
Install Vagrant
Step 1: We install Oracle VM VirtualBox 7
Please refer to article 2.1
https://viblo.asia/p/bai-2-tao-may-ao-ubuntu-2204-bang-oracle-vm-virtualbox-7-r1QLxQZgVAw
Step 2: Install Vagrant
We go to the vagrant homepage to download the installation file https://developer.hashicorp.com/vagrant/downloads depending on the operating system, then we will choose that installation file.
Step 3: After successful installation, create the file Vagrantfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | Vagrant.configure("2") do |config| config.vm.box = "ubuntu/focal64" config.winrm.timeout = 1800 # 30 minutes config.vm.boot_timeout = 1800 # 30 minutes config.vm.provider "virtualbox" do |vb| vb.memory = 2048 vb.cpus = 2 end config.vm.provision "shell", inline: <<-EOF apt install net-tools ifconfig snap install microk8s --classic microk8s.status --wait-ready microk8s.enable dns usermod -a -G microk8s vagrant echo "alias kubectl='microk8s.kubectl'" > /home/vagrant/.bash_aliases chown vagrant:vagrant /home/vagrant/.bash_aliases echo "alias kubectl='microk8s.kubectl'" > /root/.bash_aliases chown root:root /root/.bash_aliases echo " #master 192.168.56.2 microk8s-master-01 192.168.56.3 microk8s-master-02 192.168.56.4 microk8s-master-03 #worker 192.168.56.5 microk8s-worker-01 192.168.56.6 microk8s-worker-02 192.168.56.7 microk8s-worker-03 192.168.56.8 microk8s-worker-04 " > /etc/hosts cat /etc/hosts EOF config.vm.define "microk8s_master_01" do |microk8s_master_01| microk8s_master_01.vm.hostname = "microk8s-master-01" microk8s_master_01.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_master_01.vm.network "private_network", ip: "192.168.56.2" microk8s_master_01.vm.provider "virtualbox" do |vb| vb.name = "microk8s-master-01" end end config.vm.define "microk8s_master_02" do |microk8s_master_02| microk8s_master_02.vm.hostname = "microk8s-master-02" microk8s_master_02.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_master_02.vm.network "private_network", ip: "192.168.56.3" microk8s_master_02.vm.provider "virtualbox" do |vb| vb.name = "microk8s-master-02" end end config.vm.define "microk8s_master_03" do |microk8s_master_03| microk8s_master_03.vm.hostname = "microk8s-master-03" microk8s_master_03.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_master_03.vm.network "private_network", ip: "192.168.56.4" microk8s_master_03.vm.provider "virtualbox" do |vb| vb.name = "microk8s-master-03" end end config.vm.define "microk8s_worker_01" do |microk8s_worker_01| microk8s_worker_01.vm.hostname = "microk8s-worker-01" microk8s_worker_01.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_worker_01.vm.network "private_network", ip: "192.168.56.5" microk8s_worker_01.vm.provider "virtualbox" do |vb| vb.name = "microk8s-woker-01" end end config.vm.define "microk8s_worker_02" do |microk8s_worker_02| microk8s_worker_02.vm.hostname = "microk8s-worker-02" microk8s_worker_02.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_worker_02.vm.network "private_network", ip: "192.168.56.6" microk8s_worker_02.vm.provider "virtualbox" do |vb| vb.name = "microk8s-worker-02" end end config.vm.define "microk8s_worker_03" do |microk8s_worker_03| microk8s_worker_03.vm.hostname = "microk8s-worker-03" microk8s_worker_03.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_worker_03.vm.network "private_network", ip: "192.168.56.7" microk8s_worker_03.vm.provider "virtualbox" do |vb| vb.name = "microk8s-worker-03" end end config.vm.define "microk8s_worker_04" do |microk8s_worker_04| microk8s_worker_04.vm.hostname = "microk8s-worker-04" microk8s_worker_04.vm.network "public_network", bridge: "VirtualBox Host-Only Ethernet Adapter" microk8s_worker_04.vm.network "private_network", ip: "192.168.56.8" microk8s_worker_04.vm.provider "virtualbox" do |vb| vb.name = "microk8s-worker-04" end end end |
Step 4: Configure the network
Here I create 2 networks public_network and private_network
- public_network: Fail to connect to the Internet
- private_network: Can’t connect to the Internet, can only connect to VirtualBox’s internal LAN
Step 5: Vagrant up
After creating, we use the Vagrant up command to run the virtualized environment
1 2 | Vagrant up |
Depending on your network and configuration, this step is about 30 -> 60p
Then you open Oracle VM VirtualBox and see if the virtual machines have been created and started
Step 5: Vagrant ssh and join nodes to master
So successfully started, continue to ssh into the machine microk8s-master-01 with the command
1 2 | vagrant ssh microk8s_master_01 |
Continue to create tokens to connect to the microk8s cluster
1 2 | microk8s add-node |
then we ssh into the microk8s-master-02 and microk8s-master-03 to join the master
We use the command microk8s join to connect 2 machines to cum master
1 2 | microk8s join 192.168.56.2:25000/f9b92e3f904dd17cba2332a88a3092da/a25a7c633d5c |
We use the command microk8s kubectl get no to check if 2 nodes master 01 vs master 02 have joined
1 2 | microk8s kubectl get no |
So the master cluster has joined successfully, now we do it with the worker cluster, then we ssh into the machines microk8s-worker-01 , microk8s-worker-02 , microk8s-worker-03 , microk8s-worker-04 to join to worker machines
1 2 | vagrant ssh microk8s_worker_01 |
1 2 | vagrant ssh microk8s_worker_02 |
1 2 | vagrant ssh microk8s_worker_03 |
1 2 | vagrant ssh microk8s_worker_04 |
After successfully logging into the workers, go to the microk8s-master-01 and type the command **microk8s add-node **
1 2 | microk8s add-node |
We use token with ip 192.168.56.2 and add –worker at the end, and apply to all worker machines
1 2 | microk8s join 192.168.56.2:25000/{toke} --worker |
We use the command microk8s kubectl get no on master 01 to check if the woker nodes are connected.
So we have successfully created a virtual machine and connected 3 masters with 4 workers
Thank you for following my article, if you find it interesting, please give me an upvote to have more motivation to share more useful articles.