Introduction
In the previous article, I introduced the basic concepts of Kubernetes Ingress, and how to use Ingress along with related components (Nginx Ingress Controller and cert-manager) to automatically issue TLS certificates when deploying HTTPS applications. In this article, I'll show you how to define rules in Ingress to route traffic to different Services based on your needs.
Prepare Docker Image
First, you'll need a Docker image to get started. You can either use two different Docker images or follow my next instructions to prepare a Docker image.
Here's a code block to create a NodeJS server that displays a title based on an environment variable input:
import express from 'express'
const port = 3000
const title = 'This is NodeJS Typescript Application'
const app = express()
app
.get('/', (_, res) => {
res.send((process?.env?.TITLE ?? title) + '! Current time is ' + Date.now())
})
.listen(port, () => {
console.log(`Server is running http://localhost:${port}`)
})
Next, let's build a Docker image and push it to Google Container Registry or Docker Hub. (See here for more details.)
Working with Kubernetes
Next, you'll need to set up the Nginx Ingress Controller and cert-manager.
Just like in the previous tutorial, you need to define a Deployment and a Service. Then, define a rule for Ingress to route traffic to the Service. In this example, I'll define two Deployments and two Services. There will also be two rules for Ingress to route traffic accordingly. The end result will be that accessing the URL /app1 will navigate to Service 1, and accessing /app2 will navigate to Service 2.
The content of the `deployment.yml` file will be as follows:
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-name-1
spec:
replicas: 1
selector:
matchLabels:
app: label-name-1
template:
metadata:
labels:
app: label-name-1
spec:
restartPolicy: Always
containers:
- name: express-ts
image: express-ts # replace with your image
ports:
- containerPort: 3000
name: deployment-port
env:
- name: TITLE
value: Application 1 # input any words
---
apiVersion: v1
kind: Service
metadata:
name: service-name-1
spec:
selector:
app: label-name-1
ports:
- protocol: TCP
port: 80 # port service
targetPort: deployment-port # port pod
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-name-2
spec:
replicas: 1
selector:
matchLabels:
app: label-name-2
template:
metadata:
labels:
app: label-name-2
spec:
restartPolicy: Always
containers:
- name: express-ts
image: express-ts # replace with your image
ports:
- containerPort: 3000
name: deployment-port
env:
- name: TITLE
value: Application 2
---
apiVersion: v1
kind: Service
metadata:
name: service-name-2
spec:
selector:
app: label-name-2
ports:
- protocol: TCP
port: 80 # port service
targetPort: deployment-port # port pod
---
apiVersion: cert-manager.io/v1
kind: Issuer
metadata:
name: letsencrypt-production
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory
email: any-email@gmail.com
privateKeySecretRef:
name: letsencrypt-production
solvers:
- http01:
ingress:
class: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-name
annotations:
cert-manager.io/cluster-issuer: letsencrypt-production
nginx.ingress.kubernetes.io/rewrite-target: /$2
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
ingressClassName: nginx
tls:
- hosts:
- your-domain.com
secretName: secret-name
rules:
- host: your-domain.com
http:
paths:
- path: /app1
pathType: Prefix
backend:
service:
name: service-name-1
port:
number: 80
- path: /app2
pathType: Prefix
backend:
service:
name: service-name-2
port:
number: 80
You can see that the `paths` field has defined two paths: `/app1` to access `service-name-1` and `/app2` to access `service-name-2`.
Next, apply the changes to create the resource:
kubectl apply -f deployment.yml
Note: I've defined all resources in a single file for simplicity, but in practice, you should separate each resource into individual YAML files for better management.
Resources have been created
Results after deployment
If you have any suggestions or questions about the article, please feel free to leave a comment below!
Comments
Post a Comment