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

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
$ minikube start
output of the minikube command
$ minikube ip
Minikube status and IP address command for verification
  • 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

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
name: pyapplication
app: flask
app: flask
replicas: 3
type: RollingUpdate
app: flask
- name: pyapplication
image: pyapplicationimage
imagePullPolicy: Never
- containerPort: 5000
name: flask-container
apiVersion: v1
kind: Service
name: pyapplication
app: flask
- port: 5000
protocol: TCP
name: flask
app: flask
type: LoadBalancer
$ kubectl apply -f app-deployment.yml
kubectl get pods command results
minikube service deployment URL

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
$ 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.



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
Syed Saad Ahmed

Syed Saad Ahmed

Python, DevOps, Cryptography, Infra-Structure Automation.