How to Access Multiple Services in EKS using a Single Application Load Balancer?
Part 1: How to Access a Service in EKS Using an Application Load Balancer?
In Part 1, the AWS Load Balancer Controller was enabled on the EKS cluster.
That Ingress was set up for only one service, though. This post will be about using a single load balancer to access multiple services in EKS.
(Since I explained the installation and configuration of the AWS Load Balancer Controller in the previous post, I will omit it here.)
We will attempt to configure this diagram on AWS:
We will deploy two applications; email-app, payment-app
There will be multiple pods for each application.
00-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: email-app
name: email-app
spec:
replicas: 2
selector:
matchLabels:
app: email-app
template:
metadata:
labels:
app: email-app
spec:
containers:
- image: ailhan/web-debug
name: email-app
env:
- name: TEXT
value: "Hello from the Email App"
- name: HOSTNAME
value: "true"
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: payment-app
name: payment-app
spec:
replicas: 2
selector:
matchLabels:
app: payment-app
template:
metadata:
labels:
app: payment-app
spec:
containers:
- image: ailhan/web-debug
name: payment-app
env:
- name: TEXT
value: "Hello from the Payment App"
- name: HOSTNAME
value: "true"
Apply:
➜ ~ kubectl apply -f 00-deployment.yaml
deployment.apps/email-app created
deployment.apps/payment-app created
The pods can be exposed using the NodePort Service.
01-service.yaml
apiVersion: v1
kind: Service
metadata:
name: email-app-expose
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: email-app
---
apiVersion: v1
kind: Service
metadata:
name: payment-app-expose
spec:
type: NodePort
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: payment-app
Apply:
➜ ~ kubectl apply -f 01-service.yaml
service/email-app-expose created
service/payment-app-expose created
Let’s create the application load balancer via Ingress (before proceeding, please ensure you’ve installed the AWS Load Balancer Controller from Part 1):
02-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: main-lb
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
alb.ingress.kubernetes.io/load-balancer-name: "main-lb"
spec:
ingressClassName: alb
rules:
- http:
paths:
- path: /email
pathType: Prefix
backend:
service:
name: email-app-expose
port:
number: 80
- path: /payment
pathType: Prefix
backend:
service:
name: payment-app-expose
port:
number: 80
Apply:
➜ ~ kubectl apply -f 02-ingress.yaml
ingress.networking.k8s.io/main-lb created
The load balancer has been successfully created:
Let’s examine the application load balancer’s rules:
Let’s test it:
The load balancer distributes the traffic among those services.