Ingress with subdomains

10/21/2018

I use Google Cloud for deploy company app.

The goal: every branch deployed on some subdomain(example.com): task-123.example.com, etc.

I copy Cloud DNS namespace to the domain registrar. I pass the static IP address(via kubernetes.io/ingress.global-static-ip-name: "test-static-ip") for Ingress and pass it to domain registrar to A record. But I can't understand how to make subdomain works.

Every branch creates Ingress with static IP, but with different URLs for the host.

I made CNAME *.example.com which refers to example.com, but its not works.

Help me, please. Sorry for my English.

-- Serhii Koberniuk
google-app-engine
google-kubernetes-engine
kubernetes
kubernetes-ingress

2 Answers

3/2/2020

Making Ingress work with subdomains is exterenmely made easy with kubernetes. Basically you just define rules fro each of your hosts.

Here are specific steps you could follow

  1. Point you DNS to your ingress IP address. to do this you will need to setup global static IP address. In google cloud you can go here and see how you can set that up

  2. Refer that static IP in you ingress annotation

  3. Define rules and host mapping, here are docs for that

Final code will look like this, I am using helm to iterate of my hosts here

    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: ingress-router
      annotations:
        kubernetes.io/ingress.global-static-ip-name: "your-static-domain"
        networking.gke.io/managed-certificates: "your-tls-cert"
    spec:
      rules:
    {{- range $index,$service := .Values.deployments }}
        - host: {{ $service.host }}
          http:
            paths:
              - backend:
                  serviceName: {{ $service.name }}-service-name
                  servicePort: {{ $service.port }}
    {{- end }}
-- Dagm Fekadu
Source: StackOverflow

10/21/2018

You want *.example.com to point to the ingress controller so branch1.example.com and branch2.example.com will both hit the ingress controller. This is achieved with wildcard DNS.

Each branch in your scenario should have its own routing rule (ingress resource) with a host section defined for its specific branch. The ingress controller is updated when a new ingress resource is created and its routing rules then reflect the additional rule. So creating a new branch with a new ingress resource for that host will tell the ingress controller to route traffic for that specific host to a Service specific to that branch. (Or you can define all the branch rules in one go with a fanout ingress - see ingress-nginx - create one ingress per host? Or combine many hosts into one ingress and reload? )

That's 'how it works'. I'm not sure if that is your question though? It's hard to diagnose the problem you're having. Presumably you have an Ingress, a Service and a Deployment? To help with that I think you'd need to post those and explain (either as an update or a separate question) what behaviour you see (a 404 maybe)?

-- Ryan Dawson
Source: StackOverflow