Is there a way to get a domain for GKE loadbalancer instead of an ip?

10/19/2019

Edit : I'm working on a project that deploys on GKE several projects a day in a 100% automatic way. Our project is developed in nodejs, here is a pseudo code:

projects = get_all_projects_today() // returns 15 projects for today (for example)

for project in projects
    deploy_to_GKE(project) //generate a deployment manifest for project and post it to GKE (using api call)
    public_ip = create_service_on_GKE(project, type=Loadbalancer) //generate a service manifest (type loadbalancer) for project, post it to GKE (using api call) and returns the public created ip address
    get_domain_from_ip(public_ip) //how to do this !

deployed projects must be accessible via domain names and not ip like this for example :

appx.mysite.com, or
mysite.com/appx

End Edit

Here is an example of service manifest that i use :

{
    "kind": "Service",
    "apiVersion": "v1",
    "metadata": {
        "name": "backend-__ID__"
    },
    "spec": {
        "selector": {
            "app": "backend-__ID__"
        },
        "type": "LoadBalancer",
        "ports": [
            {
                "name": "app1",
                "protocol": "TCP",
                "port": 8080,
                "targetPort": "8080"
            },
            {
                "name": "app2",
                "protocol": "TCP",
                "port": "3010",
                "targetPort": "3010"
            }
        ]
    }
}

which gives me this result on google cloud : enter image description here

The only way to access the project from outside is to use the ip address (Endpoint), but this blocks the use of cookies (we can not set cookies on ip, we should use a domain name).

So is there a way to get, automatically, from GKE named domains for Endpoints to access the projet from outside ?

i tried using reverse DNS lookup to get a domain name for the external ip but that did not work :(

Thank you

-- hamzawi
cookies
dns
google-kubernetes-engine
load-balancing

2 Answers

10/25/2019

I found it!

using reverse dns lookup, i found that google provide a domain like this : [LOADBALANCER_IP].bc.googleusercontent.com.

example of reverse dns lookup site : https://mxtoolbox.com/ReverseLookup.aspx

-- hamzawi
Source: StackOverflow

10/19/2019

GKE won't create a domain name for you (or even provide a temporary one like it might with, say, appengine or endpoints). However, you can configure the loadbalancer to use a static IP address that you have reserved, and then run DNS with a hostname for that IP.

For a LoadBalancer service, this is done with a regional IP address, so you would need to reserve the IP address first, for example:

gcloud compute addresses create my-service-ip --region us-central1

(There is also a REST API for this if you prefer, see the above link). Note that Google charges a small fee for having a reserved IP address that is not in use by a LoadBalancer or VM Instance.

Next, configure your DNS (possibly using Cloud DNS but any DNS provider will work) to point the name you want at that IP address.

Finally, when you create the loadbalancer, you need to pass the static IP address as a "loadBalancerIP". I haven't done this via JSON, but here is an example with YAML:

apiVersion: v1
kind: Service
metadata:
  name: backend-__ID__
spec:
  selector:
    app: backend-__ID__
  ports:
  - port: 80
    targetPort: 8080
  type: LoadBalancer
  loadBalancerIP: "YOUR.IP.ADDRESS.HERE"

You can also read full documentation about using a domain name with your GKE cluster.

This is also possible to do with a HTTP(S) loadbalancer via an Ingress, but the technique is slightly different, in that case you need to add a "kubernetes.io/ingress.global-static-ip-name" annotation to the metadta for your ingress, with a value of the name of your reserved static IP address. In this case, the static IP address must be global, not regional.

-- robsiemb
Source: StackOverflow