<p>The following chain describes how Pods that define an API are reached from the outside.</p>
<pre><code>Client -&gt; Route53 (.example.com) 
  -&gt; LoadBalancer -&gt; Nginx 
    -&gt; Service -&gt; Pod
</code></pre>
<p>Some pods, in addition to defining an API, communicate and use the API of others in the same cluster k8s.  To allow communication between pods I can do it using the internal dns: eg. <code>api1.ns.svc.cluster.local</code> or using the Route53 <code>api1.example.com</code> domain.</p>
<p>The first case is more efficient but on the other hand I need to keep a list of the necessary services and namespaces for each pod.</p>
<p>The second case is easier to manage.  I know that each API responds to <code>* .example.com</code> so I only need to know the subdomain to call.  This approach is extremely inefficient:</p>
<pre><code>Pod1 -&gt; Route53 (api2.example.com) 
  -&gt; LoadBalancer -&gt; Nginx 
    -&gt; Service -&gt; Pod2
</code></pre>
<p>In this scenario I would like to know if there are known solutions for which a pod to communicate with another can use the same domain managed by Route53 but without leaving the cluster and maintaining internal traffic.</p>
<p>I know I can use a core dns rewrite but in that case I should still keep an updated list, also Route53 holds subdomains pointing to services outside the cluster, e.g. <code>db.example.com</code></p>
<p>So the idea is an autodiscovery of the ingress and keep internal traffic if possible:</p>
<pre><code>Pod1 -&gt; k8sdns with api2.example.com ingress 
  -&gt; Nginx -&gt; Service 
    -&gt; Pod2 
</code></pre>
<p>Or</p>
<pre><code>Pod1 -&gt; k8sdns without db.example.com ingress
  -&gt; Route53 -&gt; LoadBalancer 
    -&gt; DB
</code></pre>
<p>Thanks</p>

<p>Yes, you can do it using the CoreDNS <code>rewrite plugin</code>. This is the <a href="https://coredns.io/plugins/rewrite/" rel="nofollow noreferrer">official documentation</a> and I'll give you an example how to implement it.</p>
<ol>
<li>Edit the CoreDNS ConfigMap</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl edit cm -n kube-system coredns
</code></pre>
<ol start="2">
<li>Add this line inside the config:</li>
</ol>
<pre class="lang-yaml prettyprint-override"><code>rewrite name regex (.*)\.yourdomain\.com {1}.default.svc.cluster.local
</code></pre>
<p>Your <code>cm</code> is going to look like:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
           pods insecure
           upstream
           fallthrough in-addr.arpa ip6.arpa
        }
        rewrite name regex (.*)\.yourdomain\.com {1}.default.svc.cluster.local
        prometheus :9153
        proxy . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }
</code></pre>
<ol start="3">
<li>Save the edit and delete your CoreDNS pods</li>
</ol>
<pre class="lang-bash prettyprint-override"><code>kubectl delete pod -n kube-system --selector k8s-app=kube-dns
</code></pre>
<ol start="4">
<li>And test it inside a dummy pod or query directly to the CoreDNS</li>
</ol>
<pre class="lang-bash prettyprint-override"><code># dig app1.yourdomain.com

; &lt;&lt;&gt;&gt; DiG 9.16.33-Debian &lt;&lt;&gt;&gt; app1.yourdomain.com
;; global options: +cmd
;; Got answer:
;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 51020
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: c06f814fbf04a827 (echoed)
;; QUESTION SECTION:
;app1.yourdomain.com.       IN  A

;; ANSWER SECTION:
app1.default.svc.cluster.local. 30 IN   A   10.110.113.195

;; Query time: 5 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Wed Oct 26 04:49:47 UTC 2022
;; MSG SIZE  rcvd: 107
</code></pre>
