<p>Im create service with type external name:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
  namespace: dev
spec:
  externalName: google.com
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  sessionAffinity: None
  type: ExternalName
</code></pre>
<p>By off <a href="https://kubernetes.io/docs/concepts/services-networking/service/#services-without-selectors" rel="noreferrer">K8s docs</a> add new endpoint:</p>
<pre><code>apiVersion: v1
kind: Endpoints
metadata:
  name: my-service
  namespace: dev
subsets:
- addresses:
  - ip: 172.217.20.206
  ports:
  - port: 80
    protocol: TCP
</code></pre>
<p>And trying forward it to my localhost:</p>
<pre><code>kubectl port-forward -n dev svc/my-service 8080:80
</code></pre>
<p>and got the error:</p>
<blockquote>
<p>error: cannot attach to *v1.Service: invalid service 'my-service':
Service is defined without a selector</p>
</blockquote>
<p>AFAIU, I did all steps by off docs, where I missed ? Or K8s not provide ability port-forward externalName in general?</p>

Alexander Rudenko
<p><code>kubectl port-forward</code> only actually forwards a local connection to a single specific pod.  While it looks like you can port-forward to other things, these are just means of picking a pod.  If you run <code>kubectl port-forward service/foo 12345:80</code>, it actually looks at the pods selected by that Service, remaps the service's port 80 to the corresponding pod port, and forwards to that specific pod.</p>
<p>In your case, this means you can't port-forward to an ExternalName service, because there isn't a pod behind it, and <code>kubectl port-forward</code> only actually forwards to pods.</p>
<p>There are a couple of other implications (or demonstrations) of this.  Start a normal Deployment running some service with 3 replicas, with a normal Service in front of it.  Port-forward to either the Deployment or the Service, and run a load test; you will see only one pod receive all the traffic.  Delete that specific pod, and the port-forward will shut down.</p>
<p>If you want to connect to an ExternalName service, or otherwise do any of the more interesting things services do, you need to make the connection originate from inside the cluster.  You could <code>kubectl run</code> a temporary pod as an example:</p>
<pre class="lang-sh prettyprint-override"><code>kubectl run curl-test --rm --image=curlimages/curl --generator=run-pod/v1 -- \
  http://my-service.dev.svc.cluster.local
</code></pre>
