Building a Simple Web Application with Kubernetes.

Building a Simple Web Application with Kubernetes.

A beginners guide to Kubernetes.

·

5 min read

Table of contents

No heading

No headings in the article.

Kubernetes provides a unified platform for deploying, scaling, and managing containerized applications. It automates many of the tasks associated with managing containers, including load balancing, service discovery, and resource allocation.

Kubernetes is a powerful tool for container orchestration that has rapidly gained popularity in recent years. It was developed by Google and is now maintained by the Cloud Native Computing Foundation (CNCF), a community-driven organization that oversees many open-source projects in the cloud-native ecosystem.

At its core, Kubernetes is a platform for automating the deployment, scaling, and management of containerized applications. Containers provide a lightweight and flexible way to package and deploy applications, and Kubernetes makes it easy to manage these containers at scale.

One of the primary benefits of Kubernetes is its ability to scale applications horizontally by adding or removing containers as needed. This makes it ideal for applications that need to handle varying levels of traffic.

How does Kubernetes work?

Kubernetes works by creating a cluster of nodes that are used to run containerized applications. Each node runs a container runtime, such as Docker, that is used to create and manage containers.

The Kubernetes cluster consists of a master node and multiple worker nodes. The master node is responsible for managing the cluster, while the worker nodes run the containers that make up the application.

Applications are deployed to Kubernetes using a set of YAML configuration files called manifests. These manifests describe the desired state of the application, including the containers, services, and volumes that make up the application.

Once the application is deployed, Kubernetes continuously monitors its health and automatically restarts containers that fail. It also provides load balancing and service discovery for the application, making it easy to scale and manage.

Building a Simple Web Application with Kubernetes

Now that we understand what Kubernetes is and how it works, let's look at how we can use it to build a simple web application.

For this example, we will use a simple Python Flask application that displays "Hello, World!" on a web page. We will deploy this application to Kubernetes using a Docker container.

Step 1: Write the Application Code

First, we need to write the code for our web application. For this example, we will use Python and the Flask web framework.

Create a new file called app.py and add the following code:

pythonCopy codefrom 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')

This code defines a simple Flask application that listens on port 5000 and returns "Hello, World!" when a user visits the home page.

Step 2: Create a Docker Image

Next, we need to create a Docker image of our application. This image will be used to deploy our application to Kubernetes.

Create a new file called Dockerfile and add the following code:

sqlCopy codeFROM python:3.8-slim-buster

WORKDIR /app

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./app.py" ]

This Dockerfile specifies a base image of Python 3.8, sets the working directory to /app, and copies the application code to the container. It also installs any required dependencies using the requirements.txt file.

To build the Docker image, run the following command:

perlCopy codedocker build -t my-app .

This command builds the Docker image and tags it with the name my-app.

Step 3: Deploy to Kubernetes

Now that we have a Docker image of our application, we can deploy it to Kubernetes. To do this, we will create a Kubernetes deployment that defines the desired state of our application.

Create a new file called deployment.yaml and add the following code:

yamlCopy codeapiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app
        ports:
        - containerPort: 5000

This deployment specifies that we want to deploy three replicas of our application, each running in a separate container. It also defines a selector that matches the labels on our application pods.

To deploy the application to Kubernetes, run the following command:

Copy codekubectl apply -f deployment.yaml

This command creates the deployment and starts the application pods.

Step 4: Expose the Application

Now that our application is running in Kubernetes, we need to expose it to the outside world. We can do this by creating a Kubernetes service that routes traffic to our application pods.

Create a new file called service.yaml and add the following code:

yamlCopy codeapiVersion: v1
kind: Service
metadata:
  name: my-app
spec:
  selector:
    app: my-app
  ports:
    - name: http
      port: 80
      targetPort: 5000
  type: LoadBalancer

This service specifies that we want to expose our application on port 80 and route traffic to port 5000 on our application pods.

To create the service, run the following command:

Copy codekubectl apply -f service.yaml

This command creates the service and assigns an external IP address that we can use to access our application.

Step 5: Test the Application

Now that our application is deployed and exposed, we can test it by visiting the external IP address assigned to the service.

To get the external IP address of the service, run the following command:

arduinoCopy codekubectl get services

This command will list all the services in the Kubernetes cluster, including our my-app service. Look for the EXTERNAL-IP column and use the IP address listed there to access the application.

Open a web browser and enter the external IP address in the address bar. You should see the message "Hello, World!" displayed on the page.

Conclusion

In this article, we introduced Kubernetes and showed how to use it to build a simple web application. We covered the basics of Kubernetes, including how it works and how to deploy and manage applications using Kubernetes manifests.

By following the steps outlined in this article, you should now have a basic understanding of how Kubernetes works and how to use it to deploy containerized applications. With this knowledge, you can start exploring the many features and capabilities of Kubernetes and building more complex applications.