Home > OS >  Is there a reversal of `ExternalName` service in Kubernetes?
Is there a reversal of `ExternalName` service in Kubernetes?

Time:07-23

I'm trying to move my development environment to Kubernetes to be more in line with existing deployment stages. In that context I need to call a service by its Ingress DNS name internally, while this DNS name resolves to an IP unreachable from the cluster itself. I would like to create a DNS alias inside the cluster which would point to the service, basically a reversal of a ExternalName service.

Example:

  • The external DNS name is my-service.my-domain.local, resolving to 127.0.0.1
  • Internal service is my-service.my-namespace.svc.cluster.local
  • A process running in a pod can't reach my-service.my-domain.local because of the resolved IP, but could reach my-service.my-namespace.svc.cluster.local, but needs to be accessing the former by name
  • I would like to have a cluster-internal DNS name my-service.my-domain.local, resolving to the service my-service.my-namespace.svc.cluster.local (ExternalName service would do the exact opposite).

Is there a way to implement this in Kubernetes?

CodePudding user response:

You can use the core dns and add the entry over there using configmap

apiVersion: v1
kind: ConfigMap
metadata:
  annotations:
  labels:
    eks.amazonaws.com/component: coredns
    k8s-app: kube-dns
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    domain-name:port {
        errors
        cache 30
        forward . <IP or custom DNS>
        reload
    }

To test you can start one busy box pod

kubectl run busybox --restart=Never --image=busybox:1.28 -- sleep 3600

hit the domain name from inside of busy box

kubectl exec busybox -- nslookup domain-name

Official doc ref : https://kubernetes.io/docs/tasks/administer-cluster/dns-custom-nameservers/

Nice article for ref : https://coredns.io/2017/05/08/custom-dns-entries-for-kubernetes/

Or

you can map the domain to the service name using rewrite, rewrite name example.io service.default.svc.cluster.local

Use the Rewrite plug-in of CoreDNS to resolve a specified domain name to the domain name of a Service.

apiVersion: v1
data:
  Corefile: |-
    .:5353 {
        bind {$POD_IP}
        cache 30
        errors
        health {$POD_IP}:8080
        kubernetes cluster.local in-addr.arpa ip6.arpa {
          pods insecure
          fallthrough in-addr.arpa ip6.arpa
        }
        rewrite name example.io service.default.svc.cluster.local
        loadbalance round_robin
        prometheus {$POD_IP}:9153
        forward . /etc/resolv.conf
        reload
    }
kind: ConfigMap
metadata:
  labels:
    app: coredns
    k8s-app: coredns
    kubernetes.io/cluster-service: "true"
    kubernetes.io/name: CoreDNS
    release: cceaddon-coredns
  name: coredns
  namespace: kube-system
  • Related