liveBook · Manning

Load balancer

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A load balancer is a network component that distributes incoming traffic across multiple service instances or endpoints.

A load balancer distributes incoming requests across multiple instances or endpoints of a microservice. This spreads traffic and lets healthy replicas continue serving requests when one instance fails. section 11.4.4 section 11.1

In a Kubernetes deployment, a load balancer supports these operational goals:

  • Expose a service outside the cluster by assigning an accessible IP address. section 6.11.4
  • Distribute requests across replicated microservice instances. section 11.4.4
  • Redirect requests to other instances while Kubernetes restarts a failed instance. section 11.4.4
  • Improve continuity without eliminating the need to log and investigate failures. section 11.4.4

Expose a microservice on Azure Kubernetes

Set a Kubernetes Service to type LoadBalancer, select the deployed microservice, and map external port 80 to the container's port 4000. Azure then creates a load balancer and allocates an IP for HTTP testing. Treat this as a deliberate exposure: the service becomes reachable from outside the cluster and can receive attacks or abuse. section 6.11.4

Listing 6.2 Deploying our microservice to Kubernetes on Azure
apiVersion: apps/v1
kind: Deployment
metadata:
  name: video-streaming
spec:
  replicas: 1
  selector:
    matchLabels:
      app: video-streaming
  template:
    metadata:
      labels:
        app: video-streaming
    spec:
      containers: 
      - name: video-streaming
        image: bmdk1.azurecr.io/video-streaming:1   #1
        imagePullPolicy: IfNotPresent               #2
        env:
        - name: PORT
          value: "4000"
---
apiVersion: v1
kind: Service
metadata:
  name: video-streaming
spec:
  selector:
    app: video-streaming
  type: LoadBalancer                                #3
  ports:
    - protocol: TCP
      port: 80
      targetPort: 4000

Use the deployment listing to inspect the Service selector, LoadBalancer type, external port, and target container port together. section 6.11.4

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Explains request sharing, failover, and Kubernetes configuration, but defers detailed balancing algorithms and performance tuning.
Examples
Uses an Azure LoadBalancer Service, a video-streaming deployment, and three gateway replicas as concrete configurations.
What it assumes
Assumes a Kubernetes deployment, service configuration, and replicated microservice instances already exist.
What you can do after
Operate/configure a Kubernetes Service that exposes a microservice and routes traffic across replicated instances.
How it connects
Connects Kubernetes Services with microservices, replication, redundancy, fault tolerance, and health monitoring.

Also explained in

Compare how 2 books explain load balancer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.