Home > Enterprise >  Can't resolve service URL in fetch on nginx server HTML
Can't resolve service URL in fetch on nginx server HTML

Time:09-22

I run in kubernetes:

  • a pod with an nginx server, that serves an HTML page
  • a service of type ClusterIP

A JavaScript fetch in the HTML gives me net::ERR_NAME_NOT_RESOLVED

<script>
    fetch("http://backend-service.default.svc.cluster.local")) // net::ERR_NAME_NOT_RESOLVED
</script>

However, the URL can be resolved when I use curl on the image:

$ kubectl exec frontend-deployment-75f7d9775b-pxgkx -- curl http://backend-service.default.svc.cluster.local

<expected json response>

Why can the URL be resolved with curl, but JavaScript fetch fails with net::ERR_NAME_NOT_RESOLVED?

CodePudding user response:

I figured it out!

In the scenario I described:

  • the fetch runs in my browser, hence tries to fetch from the public internet.
  • the curl runs in the container on the pod, hence fetches from the cluster.
  • Related