Access a K8s service via DNS name from Cloud Function

11/28/2019

I have a K8s cluster running with a few services in it. BEcause of K8s DNS, within the cluster services can talk to each over via HTTP request with their name as the URL (e.g http://foo-bar-svc). This is great because I don't need to use an IP address, which I'm assuming would change every time a pod gets redeployed.

Now I want a Cloud Function to be able to post a request to one of these service.

I've followed this guide and successfully created a VPC Connector. From my Cloud Function, I can make a HTTP request to a service in my K8s cluster, but only if I use an explicit IP address.

How can I instead use one of the URLS that the K8s DNS can resolve?

-- oxnard
google-cloud-functions
google-kubernetes-engine

1 Answer

11/28/2019

The best way to expose a k8s service with ingoing host request, is ingress. You can define a Ingress ressource link with your service, example:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: simple-fanout-example
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /foo
        backend:
          serviceName: service1
          servicePort: 4200
      - path: /bar
        backend:
          serviceName: service2
          servicePort: 8080

In this example we define a host foo.bar.com to resolve and depends of the path /foo or /bar we reroute to a service behind. Of course you can replace it by the prefixe "/*" for reroute all to one specific service path.

Please refer the documentation: https://kubernetes.io/docs/concepts/services-networking/ingress/

But with this configuration you need to have a Load balancer in front and an alias to a DNS entry: https://cloud.google.com/kubernetes-engine/docs/concepts/ingress?hl=en

And to be more resilience you can add one ingress controller (nginx,traefik....): https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/

So, the schema will be:

DNS server <-> Client resolv DNS -> LB -> Ingress Controller -> Service -> Pod -> container.

I hope it helps.

-- GoA Oz
Source: StackOverflow