30

Currently, I'm using Docker Desktop with WSL2 integration. I found that Docker Desktop automatically had created a cluster for me. It means I don't have to install and use Minikube or Kind to create cluster. The problem is that, how could I enable Ingress Controller if I use "built-in" cluster from Docker Desktop? I tried to create an Ingress to check if this work or not, but as my guess, it didn't work.

The YAML file I created as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  minReadySeconds: 30
  selector:
    matchLabels:
      app: webapp
  replicas: 1
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: nodejs-helloworld:v1

--- 

apiVersion: v1
kind: Service
metadata:
  name: webapp-service

spec:
  selector: 
    app: webapp
  
  ports:
    - name: http
      port: 3000
      nodePort: 30090 # only for NotPort > 30,000
    
  type: NodePort #ClusterIP inside cluster

---

apiVersion: networking.k8s.io/v1
kind: Ingress 
metadata:
  name: webapp-ingress
spec:
  defaultBackend:
    service:
      name: webapp-service
      port:
        number: 3000
  rules:
  - host: ingress.local
    http:
      paths:
      - path: / 
        pathType: Prefix
        backend:
          service:
            name:  webapp-service
            port: 
              number: 3000
    

I tried to access ingress.local/ but it was not successful. (I added ingress.local to point to 127.0.0.1 in host file. And the webapp worked fine at kubernetes.docker.internal:30090 )

Could you please help me to know the root cause? Thank you.

tuq
  • 1,328
  • 2
  • 11
  • 21

6 Answers6

30

Finally I found the way to fix. I have to deploy ingress Nginx by command:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.41.2/deploy/static/provider/cloud/deploy.yaml

(Follows the instruction at https://kubernetes.github.io/ingress-nginx/deploy/#docker-for-mac. It works just fine for Docker for Windows)

Now I can access http://ingress.local successfully.

tuq
  • 1,328
  • 2
  • 11
  • 21
  • For troubleshooting issues with `ValidatingWebhookConfiguration`, see also https://stackoverflow.com/questions/61616203/nginx-ingress-controller-failed-calling-webhook – Lucas Cimon Dec 17 '20 at 10:36
  • 1
    Thanks! This really helped me. I spent ages trying to get it working with minikube, to no avail, but following this for docker desktop for windows worked perfectly. Thanks again! – Tim Trewartha Jul 13 '21 at 06:25
  • 7
    If you still get the nginx 404 error page when accessing your backend, check if you have the "annotations" part of your ingress deployment and if not, add annotations: kubernetes.io/ingress.class: "nginx". – Amel Mahmuzić Oct 23 '21 at 13:11
  • 1
    @AmelMahmuzić your comment is great! I was doing everything correctly as of the example in the question, but adding annotations: kubernetes.io/ingress.class: "nginx" solved it, Thanks. – Shadi Nov 04 '21 at 14:31
  • @Shadi that's awesome. Glad to hear it worked. – Amel Mahmuzić Nov 10 '21 at 07:32
18

You have to install an ingress-nginx controller on your cluster, so that your nodes will have an opened port 80/443.

Using helm (v3 - see documentation):

helm install --namespace kube-system nginx ingress-nginx --repo https://kubernetes.github.io/ingress-nginx

Using kubectl (see documentation):

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.43.0/deploy/static/provider/cloud/deploy.yaml

Then manually adding your ingresses' hostnames to /etc/hosts:

127.0.0.1     ingress.local
127.0.0.1     my.other.service.local
# ...

Then if you make a request on http://ingress.local:

  • the DNS resolution will route to your cluster node
  • then the ingress controller will serve the request on port 80
  • then ingress will route the request to the configured backend service
  • and the service will route to an available pod
Donatello
  • 3,486
  • 3
  • 32
  • 38
10

The newest version of Docker Desktop for Windows already adds a hosts file entry: 127.0.0.1 kubernetes.docker.internal.

You had to do use kubernetes.docker.internal URL as a hostname in Ingress definition if you want to point to 127.0.0.1. This should be in the docs on this page kubernetes.github.io/ingress-nginx/deploy but there is no Docker Desktop for Windows section there. Your files should look like this:

apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  type: NodePort
  selector:
    app: webapp
  ports:
  - name: http
    protocol: TCP
    port: 3000
    nodePort: 30090

Your Ingress file should look like this:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: webapp-ingress 
spec:
  rules:
  - host: kubernetes.docker.internal
    http:
      paths:
      - path: /
        backend:
          serviceName: webapp-service 
          servicePort: http

Then you are able to connect to app using http://kubernetes.docker.internal/.

Example you can see here: wsl2-docker-for-desktop.

Malgorzata
  • 6,409
  • 1
  • 10
  • 27
3

I used the Docker-Desktop version to install the nginx-ingress controller https://kubernetes.github.io/ingress-nginx/deploy/#docker-desktop Installing Kubernetes Ingress Controller Docker Desktop Windows

curl http://kubernetes.docker.internal/

enter image description here

Offcourse I've not installed any workload yet but the default ingress controller works just fine.

Rohit Salecha
  • 893
  • 11
  • 9
0

With Kustomize you can simply use


helmCharts:
  - name: ingress-nginx
    releaseName: ingress-nginx
    repo: https://kubernetes.github.io/ingress-nginx
Datz
  • 3,156
  • 3
  • 22
  • 50
0

This is just to point out that Amel Mahmuzićs comment is still valid with a recent (I used the ingress-nginx Helm Chart 4.4.2) ingress deployment.

I could not get this to work for far too long (I tried to follow the Strapi fodadvisor example with Docker Desktop build in Kubernetes instead of minikube) and always received a 404 from the ingress.

However, after using this yaml with the added annotation

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: main-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: foodadvisor.backend
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: foodadvisor-backend
              port:
                number: 1337
  - host: foodadvisor.client
    http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: foodadvisor-frontend
              port:
                number: 3000

it worked immediately. The K82 docs mention, that this annotation is deprecated.

TvdH
  • 1,088
  • 14
  • 29