Expose Multiple Apps with one LoadBalancer in Kubernetes

Tasrie IT Services

Tasrie IT Services

·9 min read
Expose Multiple Apps with one LoadBalancer in Kubernetes

In this article, we will explore how to use Kubernetes to deploy multiple apps to a single load balancer. One of the most crucial components of Kubernetes is its load balancing capabilities, which allows you to distribute traffic across multiple backend services and containers.

Understanding Kubernetes Load Balancing

Kubernetes provides several ways to load balance traffic across multiple backend services and containers. One of the most commonly used methods is the Kubernetes service. A service in Kubernetes is an abstraction that represents a logical set of pods and a policy by which to access them. Services can be exposed in several ways, including NodePort, ClusterIP, LoadBalancer, and ExternalName.

A LoadBalancer service type is used to expose a service externally using a cloud provider's load balancer. When you create a LoadBalancer service, Kubernetes automatically provisions a load balancer from the cloud provider's infrastructure and maps the service to the load balancer's front end. This way, external traffic can be directed to the backend service.

Deploying Multiple Apps to a Single Load Balancer

In most cases, a single Kubernetes cluster may have multiple backend services that require load balancing. In such cases, deploying each app to its load balancer can be expensive and may not be feasible in terms of management and cost. A better approach is to deploy multiple apps to a single load balancer. This is possible using the Ingress resource in Kubernetes.

An Ingress is a Kubernetes resource that defines rules for routing external HTTP(S) traffic to Kubernetes services. In other words, an Ingress is an API object that manages external access to the services in a cluster. The Ingress resource can be configured to route traffic based on the domain name, path, or any other parameter that can be extracted from the HTTP(S) request.

To deploy multiple apps to a single load balancer, you need to create an Ingress resource that maps to the backend services. Each backend service should have a unique hostname or path. When the external traffic hits the load balancer's front end, the Ingress resource routes the traffic to the appropriate backend service based on the routing rules defined in the Ingress.

Implementing Multiple Apps on a Single Load Balancer

Now that you understand the concept of using an Ingress resource to route traffic to multiple backend services let's dive into how you can implement this in Kubernetes.

Create the LoadBalancer Service

The First step is to create a LoadBalancer service that exposes the Ingress resource externally. In this example we use NGINX Ingress Controller. To install this simply run the following command.

bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/aws/deploy.yaml

This NGINX Ingress Controller exposes the Ingress resource externally using the AWS Network Load Balancer (NLB) type. When the load balancer is provisioned, external traffic can be directed to the backend services using the paths specified in the Ingress resource.

Create the Backend Services

The next step in deploying multiple apps to a single load balancer is to create the backend services. This can be done using a Kubernetes deployment and service. For example, let's assume you have two backend services named app1 and app2.

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
spec:
  selector:
    matchLabels:
      app: app1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
      - name: app1
        image: app1:latest
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app2
spec:
  selector:
    matchLabels:
      app: app2
  template:
    metadata:
      labels:
        app: app2
    spec:
      containers:
      - name: app2
        image: app2:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app1
spec:
  selector:
    app: app1
  ports:
    - name: http
      port: 80
      targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app2
spec:
  selector:
    app: app2
  ports:
    - name: http
      port: 80
      targetPort: 80

Create the Ingress Resource

After creating the backend services, the next step is to create an Ingress resource that maps to the backend services. In this example,

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: app1-ingress
spec:
  rules:
    - host: app1.com
      http:
        paths:
          - backend:
              service:
                name: app1
                port:
                  number: 80
            path: /
            pathType: ImplementationSpecific

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: app2-ingress
spec:
  rules:
    - host: app2.com
      http:
        paths:
          - backend:
              service:
                name: app2
                port:
                  number: 80
            path: /
            pathType: ImplementationSpecific

This Ingress resources maps to two backend services - app1 and app2. The Ingress routes traffic to the appropriate backend service based on the path specified in the HTTP request. In this example, traffic to app1.com/ is routed to the app1 service, and traffic to app2.com/ is routed to the app2 service.

Conclusion

In conclusion, deploying multiple apps to a single load balancer is a cost-effective and efficient way to manage backend services in a Kubernetes cluster. Kubernetes provides several ways to load balance traffic across multiple backend services, and using an Ingress resource is one of the most popular methods. With the Ingress resource, you can define rules for routing external HTTP(S) traffic to Kubernetes services based on the domain name, path, or any other parameter that can be extracted from the HTTP(S) request.

In this article, we will explore how to use Kubernetes to deploy multiple apps to a single load balancer. One of the most crucial components of Kubernetes is its load balancing capabilities, which allows you to distribute traffic across multiple backend services and containers.

Understanding Kubernetes Load Balancing

Kubernetes provides several ways to load balance traffic across multiple backend services and containers. One of the most commonly used methods is the Kubernetes service. A service in Kubernetes is an abstraction that represents a logical set of pods and a policy by which to access them. Services can be exposed in several ways, including NodePort, ClusterIP, LoadBalancer, and ExternalName.

A LoadBalancer service type is used to expose a service externally using a cloud provider's load balancer. When you create a LoadBalancer service, Kubernetes automatically provisions a load balancer from the cloud provider's infrastructure and maps the service to the load balancer's front end. This way, external traffic can be directed to the backend service.

Deploying Multiple Apps to a Single Load Balancer

In most cases, a single Kubernetes cluster may have multiple backend services that require load balancing. In such cases, deploying each app to its load balancer can be expensive and may not be feasible in terms of management and cost. A better approach is to deploy multiple apps to a single load balancer. This is possible using the Ingress resource in Kubernetes.

An Ingress is a Kubernetes resource that defines rules for routing external HTTP(S) traffic to Kubernetes services. In other words, an Ingress is an API object that manages external access to the services in a cluster. The Ingress resource can be configured to route traffic based on the domain name, path, or any other parameter that can be extracted from the HTTP(S) request.

To deploy multiple apps to a single load balancer, you need to create an Ingress resource that maps to the backend services. Each backend service should have a unique hostname or path. When the external traffic hits the load balancer's front end, the Ingress resource routes the traffic to the appropriate backend service based on the routing rules defined in the Ingress.

Implementing Multiple Apps on a Single Load Balancer

Now that you understand the concept of using an Ingress resource to route traffic to multiple backend services let's dive into how you can implement this in Kubernetes.

Create the LoadBalancer Service

The First step is to create a LoadBalancer service that exposes the Ingress resource externally. In this example we use NGINX Ingress Controller. To install this simply run the following command.

bash
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.7.0/deploy/static/provider/aws/deploy.yaml

This NGINX Ingress Controller exposes the Ingress resource externally using the AWS Network Load Balancer (NLB) type. When the load balancer is provisioned, external traffic can be directed to the backend services using the paths specified in the Ingress resource.

Create the Backend Services

The next step in deploying multiple apps to a single load balancer is to create the backend services. This can be done using a Kubernetes deployment and service. For example, let's assume you have two backend services named app1 and app2.

yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app1
spec:
  selector:
    matchLabels:
      app: app1
  template:
    metadata:
      labels:
        app: app1
    spec:
      containers:
      - name: app1
        image: app1:latest
        ports:
        - containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app2
spec:
  selector:
    matchLabels:
      app: app2
  template:
    metadata:
      labels:
        app: app2
    spec:
      containers:
      - name: app2
        image: app2:latest
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app1
spec:
  selector:
    app: app1
  ports:
    - name: http
      port: 80
      targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app2
spec:
  selector:
    app: app2
  ports:
    - name: http
      port: 80
      targetPort: 80

Create the Ingress Resource

After creating the backend services, the next step is to create an Ingress resource that maps to the backend services. In this example,

yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: app1-ingress
spec:
  rules:
    - host: app1.com
      http:
        paths:
          - backend:
              service:
                name: app1
                port:
                  number: 80
            path: /
            pathType: ImplementationSpecific

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
  name: app2-ingress
spec:
  rules:
    - host: app2.com
      http:
        paths:
          - backend:
              service:
                name: app2
                port:
                  number: 80
            path: /
            pathType: ImplementationSpecific

This Ingress resources maps to two backend services - app1 and app2. The Ingress routes traffic to the appropriate backend service based on the path specified in the HTTP request. In this example, traffic to app1.com/ is routed to the app1 service, and traffic to app2.com/ is routed to the app2 service.

Conclusion

In conclusion, deploying multiple apps to a single load balancer is a cost-effective and efficient way to manage backend services in a Kubernetes cluster. Kubernetes provides several ways to load balance traffic across multiple backend services, and using an Ingress resource is one of the most popular methods. With the Ingress resource, you can define rules for routing external HTTP(S) traffic to Kubernetes services based on the domain name, path, or any other parameter that can be extracted from the HTTP(S) request.

For more such content, make sure to check out our latest tech blog

Follow our LinkedIn Page

illustration
Need Expert Help ?

At Tasrie IT, we assist businesses in their growth and addressing intricate issues by utilizing advanced cloud technologies and contemporary platform engineering techniques.

Related Posts