Search code examples
kuberneteskubernetes-ingressnginx-ingress

Ingress returning 404 with kubernetes


I'm trying to setup ingress to work with a kubernetes cluster as seen here:https://www.youtube.com/watch?v=DgVjEo3OGBI. When testing the endpoint in postman it is returning a 404 not found. I've tried using https and http and i'm at a loss. Thanks!

Edit: I was using a localhost for testing and am now trying to use acme.com as the routing url.

Ingress file:

    apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-srv
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: 'true'
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: acme.com
      http:
        paths:
          - path: /api/platforms
            pathType: Prefix
            backend:
              service:
                name: platforms-clusterip-service
                port:
                  number: 80
          - path: /api/c/platforms
            pathType: Prefix
            backend:
              service:
                name: commands-clusterip-service
                port:
                  number: 80

Depl files

apiVersion: apps/v1
kind: Deployment
metadata:
  name: platforms-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: platformservice
  template:
    metadata:
      labels:
        app: platformservice
    spec:
      containers:
        - name: platformservice
          image: revlisc/platformservice:latest
apiVersion: v1
kind: Service
metadata:
  name: platforms-clusterip-service
spec:
  type: ClusterIP
  selector:
    app: platformservice
  ports:
  - name: platformservice
    protocol: TCP
    port: 80
    targetPort: 80

apiVersion: apps/v1
kind: Deployment
metadata:
  name: commands-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: commandservice
  template:
    metadata:
      labels:
        app: commandservice
    spec:
      containers:
        - name: commandservice
          image: revlisc/commandservice:latest

apiVersion: v1
kind: Service
metadata:
  name: commands-clusterip-service
spec:
  type: ClusterIP
  selector:
    app: commandservice
  ports:
  - name: commandservice
    protocol: TCP
    port: 80
    targetPort: 80

Service

    apiVersion: v1
kind: Service
metadata:
  name: platformnpservice-srv
spec:
  type: NodePort
  selector:
    app: platformservice
  ports:
    - name: platformservice
      protocol: TCP
      port: 80
      targetPort: 80

Solution

  • So there was a change in ingress.yml which I have made and it works for me can you test using the below manifest and check if its working ?

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: platforms-depl
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: platformservice
      template:
        metadata:
          labels:
            app: platformservice
        spec:
          containers:
            - name: platformservice
              image: revlisc/platformservice:latest
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: platforms-clusterip-service
    spec:
      type: ClusterIP
      selector:
        app: platformservice
      ports:
      - name: platformservice
        protocol: TCP
        port: 80
        targetPort: 80
    

    Also there was an issue with your ingress file as well i have made a small change. Check if this works for you

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: ingress-resource
      annotations:
        kubernetes.io/ingress.class: "nginx"
        ingress.kubernetes.io/rewrite-target: /$
        nginx.ingress.kubernetes.io/use-regex: "true"
    spec:
      rules:
      - host: <your-hostname>
        http:
          paths:
          - pathType: Prefix
            path: "/api/platforms"
            backend:
              service:
                name: platforms-clusterip-service
                port:
                  number: 80  
    

    When I hit hostname/api/platforms I was able to see this output I am not sure if this is the expected result. Can you confirm ?

    [{"id":1,"name":"Dot Net","publisher":"Microsoft","cost":"Free"},{"id":2,"name":"SQL Server Express","publisher":"Microsoft","cost":"Free"},{"id":3,"name":"Kubernetes","publisher":"Cloud Native Computing Foundation","cost":"Free"}]