Deploying applications with Kubernetes

Introduction

Kubernetes is an open-source container orchestration platform that helps to automate docker container deployment, scaling, and management. It makes it easy to deploy your applications in a distributed environment by effectively managing the containers that run your applications. In this tutorial, we will go through the process of deploying an application with Kubernetes.

Prerequisites

Before we begin, we need to ensure we have the following in place:

  • A running Kubernetes cluster
  • kubectl command-line tool installed
  • Docker installed

Step 1: Prepare Docker images

The first step in deploying applications with Kubernetes is to prepare Docker images. Start by creating a Dockerfile, which defines how to build your Docker image for your application. In this example, we will use a simple Python Flask web application.

Create a new directory for the Docker project, then create the following files in this directory:

Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.6-slim

# Set the working directory to /app
WORKDIR /app

# Copy the requirements.txt file into the container at /app
COPY requirements.txt /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Copy the current directory contents into the container at /app
COPY . /app

# Set environment variables
ENV FLASK_APP=index.py

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["flask", "run", "--host=0.0.0.0", "--port=80"]

requirements.txt

Flask

This Dockerfile specifies that we want to base our Python environment on the official Python runtime (version 3.6-slim). The Python dependencies needed for the Flask application are installed and any files in the current working directory that end with .py are copied to the /app directory in the container. We also set the environment variable FLASK_APP to point to index.py, which is our Flask application’s main file that contains the application code. We then expose port 80 so that requests can be routed to the container. Finally, we start the web server inside the container with the command flask run.

To build the Docker image, run the following command from within the Docker project directory:

$ docker build -t my-app:v1 .

Here, we are building a Docker image with the name my-app:v1.

Step 2: Deploying Kubernetes object definitions

To deploy your application, you define a set of Kubernetes objects, such as Deployments, Services, and Ingress. These definitions can be written in YAML or JSON format. In this tutorial, we will use YAML.

Create a new file called deployment.yaml with the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:v1
        ports:
        - containerPort: 80

This YAML file specifies a Deployment object which will create and manage two replicas of the my-app container. It sets up a selector to match the labels we set up earlier and specifies the Docker image to use. We expose port 80 for the container.

Next, create a new file called service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: my-app-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 80
  type: LoadBalancer

This YAML file specifies a Service object for our application. It sets the selector to match the labels we set up earlier and exposes port 80. type: LoadBalancer specifies that we want an external IP address to be created that we can use to access our application.

Finally, create a new file called ingress.yaml with the following content:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - http:
      paths:
      - path: /my-app
        pathType: Prefix
        backend:
          service:
            name: my-app-service
            port:
              name: http

This YAML file specifies an Ingress object which exposes the service to the internet. We define the path /my-app as a prefix for our application. The annotation nginx.ingress.kubernetes.io/rewrite-target: / ensures that we don’t need to include the /my-app prefix in our URL when accessing the application.

To create these Kubernetes objects, run the following commands:

$ kubectl apply -f deployment.yaml
$ kubectl apply -f service.yaml
$ kubectl apply -f ingress.yaml

Step 3: Verify

To verify the deployment, run the following command:

$ kubectl get ingress

This command should return the IP address assigned to the NGINX Ingress controller service. Open your browser and enter this IP address with the /my-app path. You should see the Flask application’s home page.

Congratulations! You have successfully deployed your application with Kubernetes.

Conclusion

In this tutorial, we went through the process of deploying an application with Kubernetes. We prepared Docker images, set up Kubernetes objects definitions and verified the deployment. Kubernetes offers a robust and scalable platform for managing containerized applications. With Kubernetes, you can scale your application up or down as demand changes and ensure that your application can handle any level of traffic.

Related Post