How to use Load Balancing and Ingress with Kubernetes

How to use Load Balancing and Ingress with Kubernetes

Introduction

In this tutorial, we will go through the process of setting up load balancing and Ingress in a Kubernetes cluster. By the end of this guide, you'll understand how to create a Kubernetes deployment, set up an Ingress controller (e.g., Nginx Ingress Controller), define Ingress resources for HTTP routing, and test access to your applications through the Ingress. We will also cover more advanced topics like TLS/SSL termination, path-based routing, and custom domains.

Prerequisites

Before you begin, ensure you have the following prerequisites:

  1. A running Kubernetes cluster. You can set up a local cluster using tools like Minikube or use a cloud-based solution like Google Kubernetes Engine (GKE) or Amazon EKS.

  2. kubectl command-line tool installed. You can install it by following the instructions here.

  3. Helm, a Kubernetes package manager. Install it by following the official documentation here.

Step 1: Create a Kubernetes deployment and service.

We'll start by creating a simple application and exposing it within the cluster using a Kubernetes deployment and service.

  1. Create a sample deployment YAML file, e.g., deployment.yaml, to define your application:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: my-app
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: my-app
       template:
         metadata:
           labels:
             app: my-app
         spec:
           containers:
             - name: my-app
               image: nginx:latest
    

    Deploy it to your cluster:

     kubectl apply -f deployment.yaml
    
  2. Create a Kubernetes service to expose your application:

     kubectl expose deployment my-app --port=80 --target-port=80 --type=ClusterIP
    

Step 2: Deploy an Ingress controller (e.g., Nginx Ingress Controller).

To enable Ingress in your cluster, you need an Ingress controller. We'll use the Nginx Ingress Controller in this example.

  1. Add the Nginx Ingress Controller repository and install it using Helm:

     helm repo add ingress-nginx https://kubernetes.github.io/ingress-nginx
     helm install my-ingress-controller ingress-nginx/ingress-nginx
    

    Wait for the controller to be deployed and running.

Step 3: Define Ingress resources to configure HTTP routing and load balancing.

Now, create an Ingress resource to define how incoming HTTP traffic should be routed to your application.

  1. Create an Ingress resource YAML file, e.g., ingress.yaml:

     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: my-ingress
     spec:
       rules:
         - host: my-app.com
           http:
             paths:
               - path: /
                 pathType: Prefix
                 backend:
                   service:
                     name: my-app
                     port:
                       number: 80
    

    Deploy the Ingress resource to your cluster:

     kubectl apply -f ingress.yaml
    

    This Ingress resource maps incoming requests at the root path (/) to your my-app service and associates it with the domain my-app.com.

Step 4: Enable TLS/SSL Termination

To secure your applications with SSL/TLS, you can extend your Ingress configuration to enable TLS termination.

  1. Create a secret to store your SSL/TLS certificate and key:

     kubectl create secret tls my-app-tls-secret --cert=path/to/cert.crt --key=path/to/cert.key
    
  2. Modify your Ingress resource to enable TLS termination:

     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: my-ingress
     spec:
       rules:
         - host: my-app.com
           http:
             paths:
               - path: /
                 pathType: Prefix
                 backend:
                   service:
                     name: my-app
                     port:
                       number: 80
       tls:
         - hosts:
             - my-app.com
           secretName: my-app-tls-secret
    

    This configuration secures your application with SSL/TLS.

Step 5: Implement Path-Based Routing

For more advanced routing, you can configure path-based routing to different services within your cluster.

  1. Create additional deployments and services for different paths:

     apiVersion: apps/v1
     kind: Deployment
     metadata:
       name: my-app-v2
     spec:
       replicas: 2
       selector:
         matchLabels:
           app: my-app-v2
       template:
         metadata:
           labels:
             app: my-app-v2
         spec:
           containers:
             - name: my-app-v2
               image: nginx:latest
     ---
     kind: Service
     apiVersion: v1
     metadata:
       name: my-app-v2
     spec:
       selector:
         app: my-app-v2
       ports:
         - protocol: TCP
           port: 80
           targetPort: 80
    
  2. Extend your Ingress resource to include path-based routing:

     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: my-ingress
     spec:
       rules:
         - host: my-app.com
           http:
             paths:
               - path: /app
                 pathType: Prefix
                 backend:
                   service:
                     name: my-app
                     port:
                       number: 80
               - path: /app-v2
                 pathType: Prefix
                 backend:
                   service:
                     name: my-app-v2
                     port:
                       number: 80
       tls:
         - hosts:
             - my-app.com
           secretName: my-app-tls-secret
    

    This configuration enables path-based routing, directing traffic to different services based on the URL path.

Step 6: Configure Custom Domains

To use custom domains for your applications, you can configure domain mappings in your Ingress resources.

  1. Update your DNS provider to point your custom domain (e.g., my-app.com) to the IP address of your Ingress controller.

  2. Modify your Ingress resource to include custom domains:

     apiVersion: networking.k8s.io/v1
     kind: Ingress
     metadata:
       name: my-ingress
     spec:
       rules:
         - host: my-app.com
           http:
             paths:
               - path: /
                 pathType: Prefix
                 backend:
                   service:
                     name: my-app
                     port:
                       number: 80
         - host: my-app-v2.com
    

http: paths: - path: / pathType: Prefix backend: service: name: my-app-v2 port: number: 80 tls: - hosts: - my-app.com - my-app-v2.com secretName: my-app-tls-secret


This configuration associates different custom domains with specific services in your cluster.

## Step 7: Test and Access Applications through the Ingress.

You can now test and access your applications via the Ingress controller.

1. Determine the IP or domain assigned to your Ingress controller:

If you're using Minikube, you can use the Minikube IP:

```bash
minikube ip

For cloud-based Kubernetes clusters, you might have an assigned domain or an external IP.

  1. Access your applications using the Ingress addresses. For Minikube, it might be:

    • For the main app: curl http://<minikube-ip>

    • For the app v2: curl http://<minikube-ip>/app-v2

For cloud-based clusters, it will depend on the domain or IP assigned to your Ingress controller.

You've successfully set up load balancing and Ingress in your Kubernetes cluster, including advanced features like SSL termination, path-based routing, and custom domains.

Conclusion

In this tutorial, you have learned how to create a Kubernetes deployment and service, deploy an Ingress controller, define Ingress resources for HTTP routing, enable SSL/TLS termination, implement path-based routing, and configure custom domains. This enables you to manage and route incoming traffic efficiently in your Kubernetes environment, with advanced features for security and flexibility.

Did you find this article valuable?

Support Gordian Etim by becoming a sponsor. Any amount is appreciated!