how to deploy a three-tier application in Kubernetes using an ALB ingress and an ingress controller
First, let’s create a namespace for our application:
apiVersion: v1
kind: Namespace
metadata:
name: myapp
Next, let's create a deployment for the frontend:
yamlCopy code
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
namespace: myapp
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: frontend
image: myimage/frontend:latest
ports:
- containerPort: 80
Then, we'll create a service for the frontend deployment:
apiVersion: v1
kind: Service
metadata:
name: frontend
namespace: myapp
spec:
selector:
app: frontend
ports:
- name: http
port: 80
targetPort: 80
type: ClusterIP
Now, we'll create a deployment for the backend:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
namespace: myapp
spec:
replicas: 2
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: myimage/backend:latest
ports:
- containerPort: 8080
Now, We’ll create a deployment for database:
---
apiVersion: v1
kind: Service
metadata:
name: database-service
spec:
selector:
app: database
ports:
- name: postgres
port: 5432
targetPort: 5432
And a service for the backend:
apiVersion: v1
kind: Service
metadata:
name: backend
namespace: myapp
spec:
selector:
app: backend
ports:
- name: http
port: 8080
targetPort: 8080
type: ClusterIP
Now, let's create an ingress for our application using an ALB ingress and an ingress controller:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: myapp
namespace: myapp
annotations:
alb.ingress.kubernetes.io/scheme: internet-facing
alb.ingress.kubernetes.io/target-type: ip
spec:
rules:
- host: myapp.example.com
http:
paths:
- path: /frontend
pathType: Prefix
backend:
service:
name: frontend
port:
name: http
- path: /backend
pathType: Prefix
backend:
service:
name: backend
port:
name: http
Finally, we'll deploy an ingress controller to handle the ALB ingress:
apiVersion: apps/v1
kind: Deployment
metadata:
name: alb-ingress-controller
namespace: kube-system
spec:
selector:
matchLabels:
app.kubernetes.io/name: alb-ingress-controller
replicas: 1
template:
metadata:
labels:
app.kubernetes.io/name: alb-ingress-controller
spec:
containers:
- name: alb-ingress-controller
image: docker.io/amazon/aws-alb-ingress-controller:v1.1.8
args:
- --ingress-class=alb
- --cluster-name=mycluster
- --aws-vpc-id=vpc-1234567890abcdef
- --aws-region=us-east-1
Note that in the above YAML file, we’re specifying the AWS region, cluster name, vpc id, region. Specify as per your own enviornment.
Finally, we need to create the Ingress resource that specifies how traffic should be routed to the frontend service. Here’s an example YAML file for the Ingress resource:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: frontend-ingress
annotations:
kubernetes.io/ingress.class: alb
alb.ingress.kubernetes.io/target-type: ip
alb.ingress.kubernetes.io/scheme: internet-facing
spec:
rules:
- http:
paths:
- path: /*
pathType: Prefix
backend:
service:
name: frontend
port:
name: http
In the above YAML file, we’re specifying the ALB ingress class, target type, and scheme as annotations. We’re also defining a rule that routes all HTTP traffic to the frontend service.
Once you’ve created these YAML files, you can deploy them to your Kubernetes cluster using the kubectl apply -f
command.