Scaling our Docker Container using Kubernetes: End-to-End Series (Part — 6)

Accredian Publication
4 min readMar 23, 2021

--

By Hemanka Sarmah

In our previous article, we used Docker to containerize our web app. Now, we will look at how to deploy docker containers in Kubernetes.

Also, Check out our Article on:

  1. Data Abstraction: End-to-End Series (Part — 1)
  2. Data Preprocessing and EDA: End-to-End Series (Part — 2)
  3. Model Building and Experimentation: End-to-End Series (Part — 3)
  4. Creating a WebApp using Flask+Gunicorn on Ubuntu Server: End-to-End Series (Part — 4)
  5. Containerizing the WebApp using Docker: End-to-End Series (Part — 5)
  6. Automating building and deployment using Jenkins: End-to-End Series (Part — 7)

Pushing docker image to docker hub

  • We will push our docker image to the docker hub so that the Kubernetes cluster could pull the image from the hub and add the container to the pod.

→ Tag the image with the docker hub username

docker tag <imagename> <Dockerhub_username>/<imagename>:latest

→ Push to docker hub

  • Once the newly tagged image is created, we will then push that image to the hub.
docker push <Dockerhub_username>/<imagename>:latest

Enabling the Kubernetes in docker desktop

  • Open your docker desktop
  • go to settings > Kubernetes
  • Enable Kubernetes
  • Apply and Restart

Creating YAML file

  • We will create a flask.YAML file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-demo
namespace: default
spec:
replicas: 1
selector:
matchLabels:
bb: web
template:
metadata:
labels:
bb: web
spec:
containers:
- name: flask
image: abhigyan97/flask-app:1.0
---
apiVersion: v1
kind: Service
metadata:
name: flask-entrypoint
namespace: default
spec:
type: NodePort
selector:
bb: web
ports:
- port: 8000
targetPort: 8000
nodePort: 30001

Deploying to Kubernetes

  • Open CLI and navigate to the folder location containing all the files and type the below command.
kubectl apply -f flask.yaml
  • To confirm if your pod is running or not, run the command
kubectl get deployments

Ready indicates that 1 out of 1 pods are running, if it’s 0/1 that means your pod has crashed.

  • Now, go to your browser and in the search bar type:
localhost:<nodeport>

The node port will the one mentioned in our YAML file, which is 30001 in our case.

Deleting pod

After deployment, if you want to delete your pod.

  • Run the command to look at all the pods running.
kubectl get pods --all-namespaces

the name that we gave to our pod in the flask.yaml file was flask-demo

  • Now, select the pod you want to delete and run the command.
kubectl delete deployment <pod_name>

Follow us for more upcoming future articles related to Data Science, Machine Learning, and Artificial Intelligence.

Also, Do give us a Clap👏 if you find this article useful as your encouragement catalyzes inspiration for and helps to create more cool stuff like this.

Visit us on https://www.insaid.co/

--

--

Accredian Publication

One of India’s leading institutions providing world-class Data Science & AI programs for working professionals with a mission to groom Data leaders of tomorrow!