Open In App

Kubernetes – Creating an App

Pre-requisite: Kubernetes

In this article, we will discuss how to create a simple application on Kubernetes. Kubernetes is an open-source container orchestration system that helps to manage, deploy and scale containerized applications. Kubernetes provides a platform for automating the deployment, scaling, and operations of application containers across clusters of hosts.



Steps to Create an App

Here, we will create a simple Flask web application, containerize it, and deploy it on Kubernetes.

Step 1: Install Docker and Kubernetes. Firstly, we need to install Containers and Kubernetes. You can download and install Containers and Kubernetes from their official websites.



Step 2: Write the Flask Application. Create a new directory and navigate into it. Create a new Python file called app.py and write the following code:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
   return 'Hello, World!'

if __name__ == '__main__':
   app.run(debug=True,host='0.0.0.0')

Step 3: Create a Dockerfile. Create a new file called Dockerfile in the same directory and write the following code:(in your BASH)

FROM python:3.7-alpine
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "app.py"]

Step 4: Build the Docker image using the following command:

docker build -t flask-app:v1 .

Then, run the Docker image using the following command:

docker run -p 5000:5000 flask-app:v1

Visit http://localhost:5000/ in your web browser to ensure that the app is running.

Step 5: Create a Kubernetes Deployment. Create a new file called deployment.yaml and write the following code:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: flask-app-deployment
 labels:
   app: flask-app
spec:
 replicas: 1
 selector:
   matchLabels:
     app: flask-app
 template:
   metadata:
     labels:
       app: flask-app
   spec:
     containers:
     - name: flask-app-container
       image: flask-app:v1
       ports:
       - containerPort: 5000

Create the deployment using the following command:

kubectl apply -f deployment.yaml

Check the status of the deployment using the following command:

kubectl get deployments

Step 6: Create a Kubernetes Service. Create a new file called to service.yaml and write the following code:

apiVersion: v1
kind: Service
metadata:
 name: flask-app-service
spec:
 selector:
   app: flask-app
 ports:
 - name: http
   protocol: TCP
   port: 80
   targetPort: 5000
 type: LoadBalancer

Create the service using the following command:

kubectl apply -f service.yaml

Check the status of the service using the following command:

kubectl get services

Step 7: Access the App Now, you can access the app by visiting the external IP of the service in your web browser.

Article Tags :