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
New
on VirtualBox. - For
Name
, typemaster
or or anything you like. - For
Type
, chooseLinux
- For
Version
, chooseUbuntu (64bit)
and clickNext
- For
Memory Size
, selecte at least4GB
just 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
Machine
is created, clickSettings
while selecting the machine. - Go to
System
>Processor
and change the processor count to at least2
CPU.
3.3 Load ISO file to Storage
We need to load ISO to the drive.
- From the
Settings
of the machine, Go toStorage
. - Then click
Empty
underController: IDE
. - Then
Optical Drive
shoud appers on the left side. - Click on the
disk icon
and adrop down menu
will 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
Start
while selecting the machine. - Select
Try or Install Ubuntu
after the machine booted up. - Select
Install Ubuntu
and following the instruction. - Choose
Minimal Installation
during theUpdates and other software
to minimize the installation time. - Then click
Install Now
to 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 name
that you can recognize. I choosemaster-vbox
andworker-vbox
for the master and the worker respectivily. - Pick your
username
andpassword
you like. Then clickOk
and 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 CPU
count. - Choose
computer name
which 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
Network
and click a+ network card
icon. - Double click on it the network. Set the network you like. Mine is
K8sCluster
and clickOK
.
5.3 Connect virtual machines NAT Network
- Shutdown both virtual machines.
- Then click
Settings
of the machine. - Click
Network
- Choose
Attached to
toNAT 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 show
We need IP addresses from both virtual machines.
5.2 Install SSH server
-
While we are the terminal, install
openssh server
using the following commmand for the both machines.sudo apt install openssh-server
-
Check the installation with the following command.
sudo apt install openssh-server
You should see
active (running)
.
5.3 SSH Port Fowarding
- Go to the
NAT Network
dialog that you just created. - Click
Port Fowarding
button. -
Add the following rules. You need to set the
Guest IP
as 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-client
to install. -
From your physical machine’s terminal, I use the following command to connect to our
master
virtual 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.