How to deploy containerized Flask application to a Kubernetes with Kubeadm on Virtual Machines - Part 1
March 16, 2023
This is the first part of a series of hands-on tutorials for deploying containerized flask application to a Kubernetes with Kubeadm on Virtual Machine.
Series
Overview
In this tutorial we are going a create a Kubernetes cluster with 1 master and 1 worker using Virtual Box.
1. Install VirtualBox
Depending on your OS, the installation will be different. You can refer to the offical link for the installation.
If you are on Ubuntu, you can use the following command to install VirtualBox.
sudo apt update
sudo apt install virtualbox
2. Download Ubuntu ISO
We will be using Ubuntu 22.04 Desktop version. You can download the ISO file from the offical website.
3. Create a Master Virtual Machine
3.1 Create a virtual machine
Let’s first create a master virtual machine.
- Click
Newon VirtualBox. - For
Name, typemasteror or anything you like. - For
Type, chooseLinux - For
Version, chooseUbuntu (64bit)and clickNext - For
Memory Size, selecte at least4GBjust to make it safer. - From now until
File location and size, just choose default value and clickNext. - At
File location and size, you need to choose at least20GB. Then clickCreate.
3.2 Change CPU count
We need to change the CPU count to at least 2 as Kubenetes needs at least 2 CPU per node.
- Once the
Machineis created, clickSettingswhile selecting the machine. - Go to
System>Processorand change the processor count to at least2CPU.
3.3 Load ISO file to Storage
We need to load ISO to the drive.
- From the
Settingsof the machine, Go toStorage. - Then click
EmptyunderController: IDE. - Then
Optical Driveshoud appers on the left side. - Click on the
disk iconand adrop down menuwill appear. - Select
Choose a disk file..and load the Ubuntu ISO file that you download from the previous section. - Click
OK.
3.4 Installation Ubuntu on Virtual Machine
- Click
Startwhile selecting the machine. - Select
Try or Install Ubuntuafter the machine booted up. - Select
Install Ubuntuand following the instruction. - Choose
Minimal Installationduring theUpdates and other softwareto minimize the installation time. - Then click
Install Nowto install. Do not worry if you seeErase disk and install Ubuntu. It will only erase data from this virtual machine which we don’t have anything on. Then again just follow the instruction. - While at
Who are you?, make sure you selectYour computer's namethat you can recognize. I choosemaster-vboxandworker-vboxfor the master and the worker respectivily. - Pick your
usernameandpasswordyou like. Then clickOkand finish the installation.
4. Crate a Worker Virtual Machine.
Just repeated a previous steps to crate a work virtual machine. Take note of the following while creating the worker machine.
- Set storage Size to be at least
20GB. - Set memory to be at least
2GB. - Set at least
2 CPUcount. - Choose
computer namewhich must be different from the master machine.
5. Setup NAT Networks
We want to use ssh from our physical machine to two virtual machines. So we need to setup the network.
5.2 Create a NAT Network
- From
VirtualBox, go toFile>Preferences. - Then go to
Networkand click a+ network cardicon. - Double click on it the network. Set the network you like. Mine is
K8sClusterand clickOK.
5.3 Connect virtual machines NAT Network
- Shutdown both virtual machines.
- Then click
Settingsof the machine. - Click
Network - Choose
Attached totoNAT Network. Select the NAT network name you created previously. Then clickOK. You need to connect both virtual machines to NAT Network.
5.1 Getting IP address of each virtual machine
- Turn on both virtual machines.
- Go into each virtual machine and open the terminal.
-
Type in the following command and take note of the IP address.
ip addr showWe need IP addresses from both virtual machines.
5.2 Install SSH server
-
While we are the terminal, install
openssh serverusing the following commmand for the both machines.sudo apt install openssh-server -
Check the installation with the following command.
sudo apt install openssh-serverYou should see
active (running).
5.3 SSH Port Fowarding
- Go to the
NAT Networkdialog that you just created. - Click
Port Fowardingbutton. -
Add the following rules. You need to set the
Guest IPas IP addresses that you just recorded from the previous section 5.1.Name Protocol Host IP Host Port Guest IP Guest Port ssh master TCP 127.0.0.1 2222 10.0.2.15 22 ssh worker TCP 127.0.0.1 2223 10.0.2.4 22 - After you set the values, click
OK.
5.3 Test the SSH connection to Virtual Machines
Let’s ssh to our virtual machines.
- Make sure you already have ssh client installed on your physical machine. If not, use
sudo apt install openssh-clientto install. -
From your physical machine’s terminal, I use the following command to connect to our
mastervirtual machine.ssh -p 2222 aknay@127.0.0.1
The command should be ssh -p <HOST POST> <USERNAME>@127.0.0.1.
After successful connection, you should see that your hostname is changed to the hostname that you connected.