I'm trying to get https
working on express.js for node inside K8s deployment, but I can't figure out how.
The express app should work like this:
- When you access the
/healthz
endpoint, return200
(bothhttp
&https
requests) - Redirect any other
http
trafic tohttps
The problem I'm facing is that the app is then listening on two different ports (8080
and 8443
), but the K8s Ingress points to only single port - I was thinking about maybe solving this problem with Nginx as a reverse proxy, but than, how would I "link" the two containers together?
This is my app.ts
:
import express from "express"
import { createServer as httpServer } from "http"
import { createServer as httpsServer } from "https"
import { readFileSync } from "fs"
const app = express()
const options = {
key: readFileSync(`${__dirname}/cert/[company].key`),
cert: readFileSync(`${__dirname}/cert/[company].crt`),
}
app.use("/healthz", (req, res) => {
res.status(200).send("healthy")
})
app.use("*", (req, res) => {
if (!req.secure) {
return res.redirect(["https://", req.get("Host"), req.baseUrl].join(""));
}
})
app.get("/", (req, res) => {
res.status(200).send("Hello from Express!")
})
httpServer(app).listen(8080)
httpsServer(options, app).listen(8443)
Here's the Dockerfile
:
FROM node:14.4.0-alpine
...
EXPOSE 8080 8443
CMD [ "yarn", "start" ]
Here's the Ingress.yaml
:
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
...
spec:
tls:
- secretName: [secret-name]
rules:
- host: [company]
http:
paths:
- backend:
serviceName: [service-name]
servicePort: 8080
Could you please point me the right direction, how to approach/solve this problem?