0

This is my first time using Docker, Kubernetes, Ingress and Skaffold. I'm following along with a online course, but I'm stuck.

When I call just the home path / it returns a response, but any other path that I try returns 404.

The first step I took was to add a custom local development url in /etc/hosts/ so in that file I've added 127.0.0.1 ticketing.com.

Then I have my ingress file. I set host to match ticketing.com and the path uses a regex to match any path after /api/users

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: ticketing.com
      http:
        paths:
          - path: /api/users/?(.*)
            pathType: Prefix
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000

This is the file that has the deploy and service yaml for what is going to be an authentication service. In the second part, it looks like the metadata: name matches auth-srv correctly and port 3000 correctly.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
    spec:
      containers:
        - name: auth
          image: lmsankey/auth
---
apiVersion: v1
kind: Service
metadata:
  name: auth-srv
spec:
  selector:
    app: auth
  ports:
    - name: auth
      protocol: TCP
      port: 3000
      targetPort: 3000

If this helps, here is index.ts with the route.

import express from "express";
import { json } from "body-parser";

const app = express();
app.use(json());

app.get("/api/users/currentuser", (req, res) => {
  res.send("Hi there!");
});

app.listen(3000, () => {
  console.log("Listening on port 3000!!!!!!!!");
});

I tried following this answer https://stackoverflow.com/a/52029100/3692615 but honestly I'm not sure I understood entirely, or if that's what I need.

Basically it says to add this line to annotations. nginx.ingress.kubernetes.io/rewrite-target: /

Maybe someone more experienced can spot whats going on or point me in the right direction?

Thanks.

Louis Sankey
  • 481
  • 8
  • 26
  • I don't think the home rout is actually working either. But I am getting the response 'It works!' from the home route, which is maybe from ingress. – Louis Sankey Dec 24 '22 at 02:03
  • can you check if this [reference](https://stackoverflow.com/questions/67985163/kubernetes-dashboard-not-accessible-when-providing-path-in-ingress) will help? – Dharani Dhar Golladasari Dec 26 '22 at 12:19

1 Answers1

2

using your configuration and going to:

http://ticketing.com/api/users/currentuser

i got the message (Hi, there!) and it seems to be fine and correct.

Anyway, i will ask you: "what if you want to add another route on your express app?"

For example, what if you add this lines on your index.ts?

app.get('/health', (req, res) => {               
  res.send("Hi, i'm fine!");         
});                               
                                  
app.get('/super-secret-section', (req, res) => {
  res.send("shh! this is a supersecret section"); 
});    

With your current ingress yaml file, adding these lines on your .ts file, implicates that if you want to reach them through ingress, you need to modify your ingress.yaml file to map these two new resources: no good for your aim and overcomplicates the file (if i understood you needs)

Instead, you should consider to use ingress with the prefix (since you want to use pathType: Prefix), then use it adding a prefix for you app (the svc, actually).

Something like this:

yourdomain.com/STARTINGPOINTPREFIX/any_path_on_your_app_you_want_add

To successfully achieve this goal, you can use a combination of paths and grouping within annotations.

Here, a simple basic example:


apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-service
  annotations:
    kubernetes.io/ingress.class: nginx    
    nginx.ingress.kubernetes.io/rewrite-target: /$2 # this line use the group
    nginx.ingress.kubernetes.io/use-regex: "true" 
spec:
  rules:
    - http:
        paths:
          - pathType: Prefix
            path: /cool-app(/|$)(.*) # this intercept all the paths of your app using the 'cool-app' prefix
            backend:
              service:
                name: auth-srv
                port:
                  number: 3000
      host: ticketing.com 

when you apply this ingress yaml, you can reach your app with:

http://ticketing.com/cool-app/api/users/currentuser

also, if you add the two lines on .ts mentioned above:

http://ticketing.com/cool-app/health

http://ticketing.com/cool-app/super-secret-section

In this way, you separate the path of your app, from the ingress route.

From my point of view, the key point to get the ingress, is that you should use it to create, that i call "entry point slices" for your services.

More details on nginx annotation and rewrite https://kubernetes.github.io/ingress-nginx/examples/rewrite/

Hope it helps.

kiggyttass
  • 178
  • 1
  • 7