Getting Hands-on with Kubernetes using Simple Python Application (Part I)

As in our daily lives as a Cloud and DevOps engineers, we are very much familiar with Docker and containers. Whenever we opt in for containerizing any application or a service and it slowly starts expanding, the very first question which pops-up in our mind is how we are gonna manage that containerized environment, how we are gonna scale it according to the increasing requirements. Answering all these queries Kubernetes came into play. It is also known as K8s, is an open-source system for automating deployment, scaling, and management of containerized applications.

In this quick blogpost, we will go towards a deeper understanding of the Kubernetes orchestration application via the use of the Minikube implementation of Kubernetes to simplify the installation. We will be having a Python Flask application with MySQL backend. We will be using this setup in K8s and try to explore the core concepts of scaling, auto-healing, service discovery, and other beneficial concepts that Kubernetes offers.

Starting off with Minikube

Minikube is used as a local Kubernetes, focusing on making it easy to learn and develop for Kubernetes. In order to install the minikube in macOS, you can jump to the docs or you can easily install it using homebrew.

brew install minikube

Once it is installed, We will be setting it up on the macOs, we will start off with initializing the cluster using the following command;

$ minikube start
output of the minikube command

Once the minikube environment is being ready, we can also verify it by using the checking the private IP of the minikube setup with the following command;

$ minikube ip
Minikube status and IP address command for verification

Just to have a basic concept, Kubernetes has three Object Types we should know about:

  • Pods — runs one or more closely related containers, It is the smallest deployable units
  • Services — sets up networking in a Kubernetes cluster.
  • Deployment — Maintains a set of identical pods, ensuring that they have the correct config and that the right number of them exist.

Basic Python Flask Application Setup

Now that we have a minikube setup ready to play with, let’s create a demo application using Python Flask, a light-weight Python web server framework, to see how we can deploy and scale an application. Here in this repository, we have setup a basic flask application. You just have to clone it and the setup is ready. After cloning the repo, we have to run the following commands in order to get the application up and running

$ docker build -t pyapplication .
$ docker run -itd --name python-app -p 5000:5000 pyapplication

In order to check the results, jump to your browser and check via htttp://localhost:5000 You will see the hello world web page.

Application Deployment via YAML Configuration

In order to deploy the python-flask application to the K8s cluster on minikube locally, we will be writing the YAML configuration files that describe the desired state of the components that will go into this application deployment, making the solution much more manageable/maintainable. Create a file named ‘app-deployment.yml’ with the content listed below:

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pyapplication
labels:
app: flask
spec:
selector:
matchLabels:
app: flask
replicas: 3
strategy:
type: RollingUpdate
template:
metadata:
labels:
app: flask
spec:
containers:
- name: pyapplication
image: pyapplicationimage
imagePullPolicy: Never
ports:
- containerPort: 5000
name: flask-container
---
apiVersion: v1
kind: Service
metadata:
name: pyapplication
labels:
app: flask
spec:
ports:
- port: 5000
protocol: TCP
name: flask
selector:
app: flask
type: LoadBalancer
...

After saving this file, we will be running the kubectl command and pass the file as the argument to indicate to Kubernetes it needs to deploy what is specified in this file;

$ kubectl apply -f app-deployment.yml

Once you apply the configuration and run the command kubectl get pods then you will see your pods in a running state. As we have mentioned replicas: 3 in our app-deployment.ymlfile that is why we are seeing 3 pods with different names.

kubectl get pods command results

Moreover in order to see the results, we can use the minikube interface for getting our exposed URL through which we can see our results in our browser. run the command Once you apply the configuration and run the command minikube service pyapplication --url

minikube service deployment URL

In order to check the results, jump to your browser and check via htttp://localhost:52671 You will see the hello world web page.

Kubectl CLI cheatsheet

Some of the commands to remember in order to quickly check the status of your cluster and deployments;

$ kubectl get componentstatus <gets status of all the components of control plane>
$ kubectl create -f <name.yml> (create k8s object from / deployment from YAML)
$ kubectl get deployment <metadata-name> -o yaml (gives deployed full YAML)
$ Kubectl get pods — all-namespaces
$ kubectl get pods — all-namespaces -o wide <gives nodes for the respective pods>
$ kubectl get namespaces
$ kubectl label pods <podname> key=value (put labels on running pods)
$ kubectl get pods -L <key> (-L for getting pods with mentioned label key)
$ kubectl get pods — field-selector status.phase=Running (shows all running pods)
$ kubectl get pods — all-namespaces — show-labels -o wide
$ kubectl get services <service-name>
$ kubectl exec <pod-name> — <command> (Execute a command from a pod)
E.g: kubectl exec busybox — curl 10.10.10.45:80
$ kubectl cluster-info (address of master & services)
$ kubectl config view (configuration)
$ kubectl describe nodes (show all nodes detail)
$ kubectl describe pods (show all pods detail)
$ kubectl get services — all-namespaces (show all services)
$ kubectl api-resources -o wide (view all resources)
$ kubectl get endpoints kube-scheduler -n kube-system -o yaml (view the kube scheduler endpoint / check current leader through annotation)
$ kubectl scale deployment <namespace> — replicas=0 -n service
(To restart the deployment, do this and then again with replicas=1)
$ kubectl delete -n application deployments demo-app (-n for namespace, then deployment type and then name of deployment
$ kubectl top node
$ kubectl top pod -n <namespace>

Database Deployments, Rolling updates and much more

Here in this blog, we have just saw a very basic deployment of a python-flask application using the minikube environment. Moving forward in the second part of this blog, we will be extending the python-flask application to use the DB and deploy it within the K8s cluster. Moreover we can have a look at how the new updates are being rolled in the clustered environment. While the DB will be in place then the management of secrets and other connectivity issues will also needs to be be addressed in order to make it up and running.

Happy coding and learning!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store