Deploying .Net Core WebApi Docker Image to Azure Kubernetes Service using Nginx Ingress Controller

421 Views Asked by At

I have created a sample .Net Core WebApi and pushed the images to ACR. Now I am deploying it to AKS with Nginx Ingress Controller using Ingress Resources pointing to ClusterIP Service that points to Deployed Pods running the image.

Issue is when I change ClusterIP service to LoadBalancer to hit it directly for testing, I get the results from WebApi. But when I change it back to ClusterIP and hit using Nginx Ingress Controller IP address, I always get 404 Not Found.

Below is the code. Please suggest.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: weather-forecast-webapi-deployment
  namespace: development
spec:
  replicas: 1
  selector:
    matchLabels:
      app: weather-forecast-webapi-pod
  template:
    metadata:
      labels:
        app: weather-forecast-webapi-pod
    spec:
      containers:
        - name: weather-forecast-webapi-container
          image: employeeconnectacr.azurecr.io/demoapi:latest
          ports:
            - containerPort: 80


apiVersion: v1
kind: Service
metadata:
  name: weather-forecast-webapi-service-clusterip
  namespace: development
spec:
  ports:
    - port: 80
      targetPort: 80
  selector:
    app: weather-forecast-webapi-pod


kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: econnect-ingress
  namespace: development
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/ssl-redirect: 'false'
    nginx.ingress.kubernetes.io/use-regex: 'true'
spec:
  rules:
    - http:
        paths:
          - path: /demo
            pathType: Prefix
            backend:
              serviceName: weather-forecast-webapi-service-clusterip
              servicePort: 80
status:
  loadBalancer:
    ingress:
      - ip: 52.141.219.175

1

There are 1 best solutions below

1
On BEST ANSWER

Looks like you messed up your ingress object. I assume you want to rewrite the /demo path to / so that paths like: /demo/foo/bar are rewritten to /foo/bar.

Here is the rewrite explained.

Here is the example:

kind: Ingress
apiVersion: networking.k8s.io/v1beta1
metadata:
  name: econnect-ingress
  namespace: development
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    nginx.ingress.kubernetes.io/ssl-redirect: 'false'
spec:
  rules:
    - http:
        paths:
          - path: /demo(/|$)(.*)
            pathType: Prefix
            backend:
              serviceName: weather-forecast-webapi-service-clusterip
              servicePort: 80

Notice that all I changed is path and rewtire-tager group number. In /demo(/|$)(.*) the brackets () create a group that is referenced in rewrite-target: /$2. The $1 is referencing the first group: / or end of string, and the second group is everything after it; so you copy everything after the /demo/ and make it a new path.