DEV Community

Cover image for How to Build and Deploy Ingress Microservices in Kubernetes
Adya Prasad
Adya Prasad

Posted on • Edited on

How to Build and Deploy Ingress Microservices in Kubernetes

Introduction

In the era of evolving technology, building problem-solving applications that can run consistently and scale effortlessly is becoming compulsory.

Microservices is a common approach used to design scalable software systems (applications) where an application is composed of many small, loosely coupled, and independently deployable services. Each microservice focuses on a specific functionality and communicates via APIs or messaging systems with other services.

This approach promotes modularity, scalability, and flexibility, making developing, maintaining, and scaling complex applications easier. And when you want to expose these services in production (outside world), you will use ingress.

But deploying and managing these services can be tricky so we use Kubernetes, which is a powerful container orchestration platform, as the go-to solution for scaling, managing, and deploying microservices.
In this post, I will guide you on how to build and deploy Microservices in Kubernetes with a beginner’s friendly guide. I am also providing some Debug steps, in case you get any errors.

Some Key Definitions:

  1. Microservices: Microservices are an architectural approach to software development where software is composed of small independent services that communicate over well-defined APIs.
  2. Kubernetes: Kubernetes is an open-source container orchestration platform that manages workloads and services. Also automate the deployment, scaling, portability, extensibility and management of applications.
  3. Pods: The smallest deployable unit in Kubernetes, representing a single instance of a running process.
  4. Ingress: Ingress is an API object and collection of defined routing rules that expose (bring into the open) applications and manage external access by providing HTTP and HTTPS routes to services within a Kubernetes cluster.
  5. Build and Deploy: Build is the process of creating and configuring the application source code, it includes gathering all components (like libraries, code and dependencies). Deploy (Deployment) is a process of making those build applications and running them on a server and accessible for users.
  6. YAML file: YAML or YML is a human-readable data serialization format used for configurations. It’s widely used in tools like Kubernetes and Docker for defining the configuration, workflow, and setting of an application.
  7. Dockerfile: Dockerfile is a text file that contains a series of instructions to containerise an application. It is like a blueprint that defines how your application should package respective services. Each microservices have its own Dockerfile.

Prerequisites

  • Docker Desktop must be installed and set up on your local system.
  • Your system must have proper installed and setup of Kubernetes with Minikube (via Docker Desktop)Docker installed on your local system
  • Basic knowledge of YML files, Dockerfile, application files, and HTTPS routes.
  • A text editor of your choice

Start Building and Deploying Microservices in Kubernetes

To kick off, we first focus on building our microservices. This involves writing the code for each service and pulling a Docker Image from Docker Hub that packages these services. Once our microservices are neatly packaged, we move on to the deployment phase and run it.

What application you will learn to build?
In this guide, you will learn to build a Python ‘CRUD Task Management Services App’ that provides you with a tangible example of a service that performs CRUD operations (Create, Read, Update, Delete) and demonstrates how to handle task management in a scalable way.

I chose this topic because it is easy to set up and run locally for testing and development. You can choose the other app idea; the steps will be the same.

Building Microservices

We will use two primary YAML files to deploy our microservices: task-manager-app-service.yml and ingress-route.yml

task-manager-app-service.yml

The task-manager-app-service.yml file defines both a Deployment and a Service in Kubernetes. These two resources work together to ensure the application is deployed in the cluster and accessible.

The Deployment ensures that a specified number of Pods (containers) are running your application. It uses the Docker image you built and pushed to Docker Hub.

The Service creates a stable endpoint (crud-task-manager-service) to access the Pods. It uses the label (app: crud-task-manager) to identify which Pods it should route traffic to, and forwards requests to port 9001 on the Pods.

Here is the code of task-manager-app-service.yml



apiVersion: apps/v1
kind: Deployment
metadata:
  name: crud-task-manager-deployment
  labels:
    app: crud-task-manager
spec:
  replicas: 1
  selector:
    matchLabels:
      app: crud-task-manager
  template:
    metadata:
      labels:
        app: crud-task-manager
    spec:
      containers:
        - name: crud-task-manager
          image: myusername/crud-task-manager-service:latest
          ports:
            - containerPort: 9001
---
apiVersion: v1
kind: Service
metadata:
  name: crud-task-manager-service
spec:
  ports:
    - port: 82
      targetPort: 9001
  selector:
    app: crud-task-manager


Enter fullscreen mode Exit fullscreen mode

Here is the breakdown:

  • apiVersion : Specifies the Kubernetes API version to use for the Deployment resource (apps/v1).
  • kind : Defines the type of resource, which is a Deployment in this case.
  • metadata : Contains the name (crud-task-manager-deployment) and labels (app: crud-task-manager). Labels are key-value pairs used to identify and select objects in the cluster.
  • spec : Describes the desired state of the Deployment.
  • replicas : Indicates the number of identical Pods to run
  • image : Specifies the Docker image to use (adyaprasad/crud-task-manager-service:latest). In the file, I set to pull images from the docker hub that I built for this microservice.
  • ports : Defines the port mapping the Service will be accessible set to port 82 (you can change if you want)
  • targetPort : The port on which the application inside the Pod is listening (9001)

ingress-route.yml

The ingress-route.yml file defines an Ingress resource in Kubernetes. An Ingress is a set of rules that allows external HTTP and HTTPS traffic to access services within the Kubernetes cluster. It acts as a "gateway" to your services, allowing for custom routing based on the request's URL or hostname.

Why Ingress?

The primary purpose of using an Ingress is to provide flexible routing. Instead of exposing every Service on a different port (which can get complicated), an Ingress lets you use hostnames and paths to direct traffic to the correct Service.
It also allows you to set up features like SSL/TLS termination and load balancing.



apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: task-manager-ingress
spec:
  rules:
    - host: taskmanager.local
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: crud-task-manager-service
                port:
                  number: 82


Enter fullscreen mode Exit fullscreen mode

Here is the breakdown:

  • apiVersion : This specifies the Kubernetes API version used to create the resource (networking.k8s.io/v1). This API version must be supported by the cluster to define an Ingress resource.
  • kind : Defines the resource type as an Ingress. The Ingress resource helps route incoming HTTP/HTTPS requests to the appropriate Service within the cluster.
  • The spec section contains the rules for how incoming traffic should be routed
  • host : This specifies the domain name for which the Ingress is handling requests (taskmanager.local). In this case, it's a local hostname that will need to be set up in your system's hosts file to point to the Minikube IP for testing. This allows requests sent to taskmanager.local to be processed by this Ingress.
  • The rules in the Ingress specify that any HTTP request made to taskmanager.local on the root path (/) will be forwarded to the backend Service on port 82.

In a real-world scenario, you might use a fully qualified domain name (e.g., example.com). In this local environment, use the taskmanager.local helps simulate how Ingress handles domain-based routing.

Steps and commands to run these microservices on Kubernetes

i> First, start your Docker Engine (make sure you are logged in)
ii> Then start your Minikube, use this command in your shell



minikube start


Enter fullscreen mode Exit fullscreen mode

iii> Add ingress addon in minikube, with the command



minikube addons enable ingress


Enter fullscreen mode Exit fullscreen mode

iv> Use the following command to create the Deployment and Service defined in task-manager-app-service.yml in the Kubernetes cluster



kubectl apply -f task-manager-app-service.yml


Enter fullscreen mode Exit fullscreen mode

v> Create and apply the Ingress resource ingress-route.yml with command



kubectl apply -f ingress-route.yml


Enter fullscreen mode Exit fullscreen mode

vi> Find your Minikube IP with the command



minikube IP


Enter fullscreen mode Exit fullscreen mode

vii> Start hosting and routing your app by updating your host file with command



<MINIKUBE_IP> taskmanager.local


Enter fullscreen mode Exit fullscreen mode

for example



123.456.78.9 taskmanager.local


Enter fullscreen mode Exit fullscreen mode

viii> Verify the deployment with these commands, you can see name in the output list:



kubectl get deployments


Enter fullscreen mode Exit fullscreen mode


kubectl get services


Enter fullscreen mode Exit fullscreen mode

ix> Check and verify the setup of ingress:



kubectl get ingress


Enter fullscreen mode Exit fullscreen mode

x> Access your Microservice: http://taskmanager.local



curl http://taskmanager.local/


Enter fullscreen mode Exit fullscreen mode

Getting any errors? Here are the Debug steps:

1) First, Ensure that your Docker and Minikube are running, use command:



docker info


Enter fullscreen mode Exit fullscreen mode


minikube status


Enter fullscreen mode Exit fullscreen mode

If Docker and Minikube are not running you will get an error message and address below in Both’s outputs.

2) Check your internet connection and make sure you are logged in docker hub, use command:



docker login


Enter fullscreen mode Exit fullscreen mode

3) Check that you enable the ingress addon in minikube with



minikube addons enable ingress


Enter fullscreen mode Exit fullscreen mode

4) If your port is not working and you encounter Bind for 0.0.0.0:83 failed or port is already allocated, try to find which task is running on that port with the command



netsat -a


Enter fullscreen mode Exit fullscreen mode

Find the task and kill it or you can change the ports in your YAML files to unused ones (e.g., 82).

5) If your localhost URL is not working, try to flush your DNS with command



ipconfig /flushdns


Enter fullscreen mode Exit fullscreen mode

If you are still getting any errors, please comment. I will try to provide you with solution

Summary

In this beginner guide, you learn how to build and deploy microservices in Kubernetes with Ingress for HTTP routing using a docker image from the docker hub repository and building yml files for both services.

Happy Learning and Deploying!

Top comments (1)

Collapse
 
adyaprasad profile image
Adya Prasad

Do you have any errors? explain here to get the solution!