<p>We presently have a setup where applications within our mesos/marathon cluster want to reach out to services which may or may not reside in our mesos/marathon cluster. Ingress for external traffic into the cluster is accomplished via an Amazon ELB sitting in front of a cluster of Traefik instances, which then chooses the appropriate set of container instances to load-balance to via the incoming HTTP Host header compared against essentially a many-to-one association of configured host headers against a particular container instance. Internal-to-internal traffic is actually handled by this same route as well, as the DNS record that is associated with a given service is mapped to that same ELB both internal to and external to our mesos/marathon cluster. We also give the ability to have multiple DNS records pointing against the same container set.</p>

<p>This setup works, but causes seemingly unnecessary network traffic and load against our ELBs as well as our Traefik cluster, as if the applications in the containers or another component were able to self-determine that the services they wished to call out to were within the specific mesos/marathon cluster they were in, and make an appropriate call to either something internal to the cluster fronting the set of containers, or directly to the specific container itself.</p>

<p>From what I understand of Kubernetes, Kubernetes provides the concept of services, which essentially can act as the front for a set of pods based on configuration for which pods the service should match over. However, I'm not entirely sure of the mechanism by which we can have applications in a Kubernetes cluster know transparently to direct network traffic to the service IPs. I <em>think</em> that some of this can be helped by having Envoy proxy traffic meant for, e.g., <code>&lt;application-name&gt;.&lt;cluster-name&gt;.company.com</code> to the service name, but if we have a CNAME that maps to that previous DNS entry (say, <code>&lt;application-name&gt;.company.com</code>), I'm not entirely sure how we can avoid exiting the cluster.</p>

<p>Is there a good way to solve for both cases? We are trying to avoid having our applications' logic have to understand that it's sitting in a particular cluster and would prefer a component outside of the applications to perform the routing appropriately.</p>

<p>If I am fundamentally misunderstanding a particular component, I would gladly appreciate correction!</p>

<p>When you are using service-to-service communication inside a cluster, you are using <code>Service</code> abstraction which is something like a static point which will road traffic to the right pods. </p>

<p>Service endpoint available only from inside a cluster by it's IP or internal DNS name, provided by internal Kubernetes DNS server. So, for communicating inside a cluster, you can use DNS names like <code>&lt;servicename&gt;.&lt;namespace&gt;.svc.cluster.local</code>.</p>

<p>But, what is more important, Service has a <strong>static IP address</strong>.</p>

<p>So, now you can add that static IP as a <code>hosts</code> record to the pods inside a cluster for making sure that they will communicate each other inside a cluster.</p>

<p>For that, you can use <a href="https://kubernetes.io/docs/concepts/services-networking/add-entries-to-pod-etc-hosts-with-host-aliases/" rel="nofollow noreferrer">HostAlias</a> feature. Here is an example of configuration:</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: hostaliases-pod
spec:
  restartPolicy: Never
  hostAliases:
  - ip: "10.0.1.23"
    hostnames:
    - "my.first.internal.service.example.com"
  - ip: "10.1.2.3"
    hostnames:
    - "my.second.internal.service.example.com"
  containers:
  - name: cat-hosts
    image: busybox
    command:
    - cat
    args:
    - "/etc/hosts"
</code></pre>

<p>So, if you will use your internal Service IP in combination with service's public FQDN, all traffic from your pod will be 100% inside a cluster, because the application will use internal IP address.</p>

<p>Also, you can use <a href="http://blog.kubernetes.io/2017/04/configuring-private-dns-zones-upstream-nameservers-kubernetes.html" rel="nofollow noreferrer">upstream DNS</a> server which will contain same aliases, but an idea will be the same. 
With Upstream DNS for the separate zone, resolving will work like that:
<a href="https://i.stack.imgur.com/UGNKZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UGNKZ.png" alt="Resolving with Upstream DNS for zone"></a></p>

<p>With a new version of Kubernetes, which using Core DSN for providing DNS service, and has more features it will be a bit simpler.</p>
