Skip to content

johntoby/docker-kubernetes-python-app

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

docker-kubernetes-python-app

This project is a Python flask application, that is containerized by docker and deployed using Kubernetes.

The Architecture of the project will flow like this: johntoby python app drawio

Prerequisites

To successfully build, package and deploy the application, the following needs to be installed on your machine - either local machine or virtual machine:

  • Docker: the containerization engine. Click HERE to install docker.
  • Minikube: a local kubernetes installation which makes it easy to learn and develop applications on kubernetes easily. Click HERE to install minikube.
  • Kubectl: a command line utility to interact with kubernetes clusters. Install kubectl from HERE
  • VS code: code editor to build your code and configuration files. Install VS Code from HERE

Write your python code

Create an app.py file on your vscode and write a simple python code using flask framework to return Hello JT...Welcome to Kodecamp to the screen

app py

Then create another file called reuirements.txt to store your major application requirement. Here, we have just one requirement which is flask:

requirements txt

Dockerize the application

After creating the application source code, and the requirements file, we will write a Dockerfile to containerize the application using a python slim image

dockerfile1

dockerfile2

To build the docker image, run this simple command

docker build -t johntoby/python-app .

This will build the application image named johntoby/python-app

docker-images-list

To run the application locally using docker and expose it on port 5000, we will run the following command:

docker run -it -p 5000:5000 johntoby/python-app

docker run working

Since the application is running on an ec2 instance, I will browse the public ip of my ec2 instance on port 5000 i.e. 54.89.171.65:5000

finalapp-port-5000

This confirms the application is successfully dockerized and running on port 5000.

Push the image to dockerhub

Login to dockerhub and authenticate to your CLI. Then run this command to push the local image to dockerhub

docker push johntoby/python-app

docker hub image

Deply the application to kubernetes

We will use minikube to deploy the application on kubernetes.

Run this command to start minikube

minikube start

Create deployment.yaml file to deploy the image

deployment yaml

run this command to check if your deployment is created successfully

kubectl get deployment

deployment-created

Create your service.yaml file to expose the application on port 5000 service yaml

Run this command to check if your service is successfully created

kubectl get service

service-created

To check the service, run this command

minikube service

service-running

Finally, to port-forward the service to be able to view it on the browser, run this command:

kubectl port-forward service/kodecamp-service 5000:5000

The application should be available on the browser at localhost:5000

finalapp-port-5000

Thanks.

About

A simple python flask app that is containerized by docker and deployed using kubernetes

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published