<p>I have a kubernetes cluster in google cloud. Due to the resource limit, I could not run a app that would take a large amount of memory. So I run the app in another cloud machine, and using <code>kubectl</code> to forward the service port, this is my <code>kubectl</code> forward script:</p>
<pre><code>#!/usr/bin/env bash

set -u

set -e

set -x

namespace=reddwarf-cache
kubectl config use-context reddwarf-kubernetes
POD=$(kubectl get pod -l app.kubernetes.io/instance=cruise-redis -n ${namespace} -o jsonpath=&quot;{.items[0].metadata.name}&quot;)
kubectl port-forward ${POD} 6380:6379 -n ${namespace}
</code></pre>
<p>And I could connect the kubernetes cluster service from the remote server like this, local app connect to local mechine port and <code>kubectl</code> will forward the connection to kubernetes cluster. But sadly I found the <code>kubectl</code> forward could not keep stable for a long time, when the app runs for some time, always give connection refused error in the future time. Is it possible to fix this problem? To let me connect to kubernetes cluster service in a stable way? For example, when I port forward the redis connection, it will throw this error in the future:</p>
<pre><code>E1216 11:58:43.452204    7756 portforward.go:346] error creating error stream for port 6379 -&gt; 6379: write tcp 172.17.0.16:60112-&gt;106.14.183.131:6443: write: broken pipe
Handling connection for 6379
E1216 11:58:43.658372    7756 portforward.go:346] error creating error stream for port 6379 -&gt; 6379: write tcp 172.17.0.16:60112-&gt;106.14.183.131:6443: write: broken pipe
Handling connection for 6379
E1216 11:58:43.670151    7756 portforward.go:346] error creating error stream for port 6379 -&gt; 6379: write tcp 172.17.0.16:60112-&gt;106.14.183.131:6443: write: broken pipe
Handling connection for 6379
</code></pre>
<p>When I using this command to connect the kubernetes cluster's redis service using port forward and execute some command, will show this error:</p>
<pre><code>➜  ~ redis-cli -h 127.0.0.1 -p 6379 -a 'uoGTdVy3P7'
127.0.0.1:6379&gt; info
Error: Connection reset by peer
</code></pre>
<p>I have read this GitHub <a href="https://github.com/kubernetes/kubernetes/issues/74551" rel="nofollow noreferrer">issue</a> seem no one knows what's happened.</p>

Dolphin
<p>Following the GitHub <a href="https://github.com/kubernetes/kubernetes/issues/74551" rel="nofollow noreferrer">issue</a> you posted I stumbled upon a solution that might help you.
First, let kubectl decide which host port to use by running:</p>
<pre><code>kubectl port-forward ${POD} :6379 -n ${namespace}
</code></pre>
<p>Then, forward the same port on the host by running:</p>
<pre><code>kubectl port-forward ${POD} 6379:6379 -n ${namespace}
</code></pre>
<p>Another thing you could do is to create a Service that maps your desired port to port 6379 in your redis Pods, example Service file would look like this:</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: redis
  ports:
    - protocol: TCP
      port: &lt;port&gt;
      targetPort: 6379
  
</code></pre>
<p>Apply the Service resource. Then, create Ingress backed by a single Service to make redis available from outside the cluster, example file:</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: test-ingress
spec:
  defaultBackend:
    service:
      name: my-service
      port:
        number: &lt;port&gt;
    
</code></pre>
<p>Apply the Ingress resource and get its IP address by running:</p>
<pre><code>kubectl get ingress test-ingress
</code></pre>
<p>After that check your redis connection from another machine using Ingress IP address.</p>
