<p>I have defined an ExternalName type service</p>
<pre><code>kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
 type: ExternalName
 externalName: 'lstest.free.example.com'
</code></pre>
<p>when I run curl -v <a href="http://my-service.default.svc.cluster.local/path" rel="nofollow noreferrer">http://my-service.default.svc.cluster.local/path</a>
I got an error because for some reasons it resolved to 'domain.com' and not 'lstest.free.example.com'
this is a response from curl</p>
<pre><code>Connected to my-service.default.svc.cluster.local (165.227.26.218) port 80 (#0)
&gt; GET /path HTTP/1.1
&gt; Host: my-service.default.svc.cluster.local
&gt; User-Agent: curl/7.64.0
&gt; Accept: */*
&gt;
&lt; HTTP/1.1 301 Moved Permanently
&lt; Server: nginx
&lt; Date: Sat, 16 Jan 2021 00:23:05 GMT
&lt; Content-Type: text/html
&lt; Content-Length: 178
&lt; Connection: keep-alive
&lt; Location: https://example.com/path
&lt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;301 Moved Permanently&lt;/title&gt;&lt;/head&gt;
&lt;body bgcolor=&quot;white&quot;&gt;
&lt;center&gt;&lt;h1&gt;301 Moved Permanently&lt;/h1&gt;&lt;/center&gt;
&lt;hr&gt;&lt;center&gt;nginx&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>Obviously, if I run it like this</p>
<p>curl -v  -H &quot;Host:lstest.free.domain.com&quot; my-service.default.svc.cluster.local/path - it works correctly.</p>
<p>If I run dig my-service.default.svc.cluster.local or
nslookup my-service.default.svc.cluster.local</p>
<p>both are resolved correctly to 'lstest.free.domain.com'</p>
<p>I can make it work in my code where I resolve service manually and manually create Uri but it does not work with 3rd party code - like curl does not work.</p>

L. Sokolovsky
<p>This behavior is working as intended from Kubernetes Point of view.</p>
<p>If you will check <code>Kubernetes Documentation Service</code> about <a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer">Type ExternalName</a> you will find <strong>Warning</strong> information.</p>
<blockquote>
<p><strong>Warning:</strong>
You may have trouble using ExternalName for some common protocols, including HTTP and HTTPS. If you use ExternalName then the hostname used by clients inside your cluster is different from the name that the ExternalName references.</p>
<p>For protocols that use hostnames this difference may lead to errors or unexpected responses. HTTP requests will have a Host: header that the origin server does not recognize; TLS servers will not be able to provide a certificate matching the hostname that the client connected to.</p>
</blockquote>
<p>In short, behavior from your question is working as intended. <code>Curl</code> sends request with a <code>service name</code> in <code>Host header</code> and remote host is not aware of that service name and doesn't have such resources. You can find this information in your curl response:</p>
<pre><code>&gt; GET /path HTTP/1.1 
&gt; Host: my-service.default.svc.cluster.local
&gt; User-Agent: curl/7.64.0
</code></pre>
<p><code>Curl</code> can't expand <code>CNAMEs</code>, so using <code>-H &quot;Host: yuorhost&quot;</code> or <a href="https://curl.se/docs/manpage.html#--resolve" rel="nofollow noreferrer">--resolve</a> command is inevitable.</p>
<p>To achieve what you want, you would need to change <code>Host header</code> to something expected by a remote web server.</p>
<p>You could achieve that using <a href="https://www.scaleway.com/en/docs/how-to-configure-nginx-reverse-proxy/" rel="nofollow noreferrer">reverse-proxy</a>. In this way you would neet do point <code>ExternalName</code> to local <code>reverse-proxy</code> (nginx pod) which would change/rewrite <code>Host header</code>. It should be recognized by a remote web server and allow you connect to the backend.</p>
<p>This idea was presented in this <a href="https://www.youtube.com/watch?v=tmlBVI5HZDc" rel="nofollow noreferrer">ProxyPass and Host Header Rewrite</a> guide.</p>
<p>In <a href="https://stackoverflow.com/questions/14352690/change-host-header-in-nginx-reverse-proxy">this Stackoverflow thread</a> you can find some additional information how you could configure <code>Nginx</code> pod as <code>reverse-proxy</code> using <code>proxy_pass</code> and <code>proxy_set_header</code></p>
<p>You can also check Nginx Docs regarding <a href="https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/" rel="nofollow noreferrer">Reverse Proxy</a>.</p>
