How to make multiple DNS point to one service in Kubernetes

10/15/2018

When we were using docker-compose, we could map multiple aliases to one container/service via links. For example, bucket1.s3 and bucket2.s3 are aliases for container s3. Now as we are moving to kubernetes, I'm trying to do the same and link containers using service discovery.

What I can think of right now is to have one service for each bucket and each service will point to the same pod. This seems a lot of work. Is there way to make multiple DNS mapped to one service so bucket1.s3.namespace.svc.cluster.local and bucket2.s3.namespace.svc.cluster.local can both be resolved to s3 service?

-- shangsunset
dns
docker-compose
kubernetes

1 Answer

10/15/2018

I believe what you might want is one service mapped to two deployments, you could have two deployments with names bucket1 and bucket2 and have them labeled with one label app: buckets then have a service with a selector including that label.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bucket1
  labels:
    app: buckets
spec:
  replicas: 3
  selector:
    matchLabels:
      app: buckets
  template:
    metadata:
      labels:
        app: buckets
    spec:
      containers:
      - name: bucket-container
        image: bucketimage
        ports:
        - containerPort: 80

apiVersion: apps/v1
kind: Deployment
metadata:
  name: bucket2
  labels:
    app: buckets
spec:
  replicas: 3
  selector:
    matchLabels:
      app: buckets
  template:
    metadata:
      labels:
        app: buckets
    spec:
      containers:
      - name: bucket-container
        image: bucketimage
        ports:
        - containerPort: 80

kind: Service
apiVersion: v1
metadata:
  name: s3
spec:
  selector:
    app: buckets
  ports:
  - protocol: TCP
    port: 80

But a further question would be why would you want to have that? if both bucket1 and bucket2 contain different information/data.

With Kubernetes generally, you would have one deployment with multiple pods/replicas to serve data either in bucket1 or bucket2 assuming the data is the same. If the data is different why not have 2 deployments with different DNS entries.

If you are using K8s 1.11 or later, you can also tweak the coredns ConfigMap in the kube-system namespace to possibly get what you are trying to get to but in terms of DNS you can't have a single record map to two CNAMEs as per RFC 1034, Section 3.6.2. You can't also hardcode IPs in Kubernetes since your IPs always change when your pods get created/removed.

-- Rico
Source: StackOverflow