21

I want to set up port forwarding from my local ports (nodes) to the pod redis-master after the online guide from kubernetes.

At the moment my prompt is frozen for more than 5 minutes at the port-forward command.

[root@k8s-master deployments]# kubectl create -f https://k8s.io/docs/tasks/access-application-cluster/redis-master.yaml
pod "redis-master" created
[root@k8s-master deployments]# kubectl get pods
NAME                                READY     STATUS    RESTARTS   AGE
redis-master                        2/2       Running   0          1m
[root@k8s-master deployments]# kubectl get pods redis-master --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
6379
[root@k8s-master deployments]# kubectl port-forward redis-master 6379:6379
Forwarding from 127.0.0.1:6379 -> 6379
^C 

I don't know why my prompt is frozen. In my logs aren't some error or warn entries.

journalctl -u kubelet.service -f --since "2018-02-19 10:30:00" --priority 0
-- Logs begin at Sa 2018-02-03 21:21:50 CET. --

kubectl version

[root@k8s-master deployments]# kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T10:09:24Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.2", GitCommit:"5fa2db2bd46ac79e5e00a4e6ed24191080aa463b", GitTreeState:"clean", BuildDate:"2018-01-18T09:42:01Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}

os-release

[root@k8s-master deployments]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

Ports

[root@k8s-master deployments]# ss -atun | grep 6379
[root@k8s-master deployments]#
Evhz
  • 8,852
  • 9
  • 51
  • 69
Volker Raschek
  • 545
  • 1
  • 6
  • 21
  • HI @Volker , I have the same situation what you have described, As I understand When the terminal is Ideal for some amount of seconds It froze. My explanations would be when there is no *data-flow* from client to server it caused the frozenness. – Suresh Vishnoi Feb 19 '18 at 09:56

1 Answers1

25

The behaviour you see is expected. This command does not get daemonized by default. It will be forwarding the port until you kill the command with CTRL-C or other similar methods.

You could try using & at the end of the command if you want to continue using that prompt. Personally I would use a terminal multiplexer like tmux or screen.

Javier Salmeron
  • 8,365
  • 2
  • 28
  • 23
  • Hi @Javier, Correct me if i am wrong, As I understand the cause of the problem would be the **dead tcp connection**. As after certain period of time the connection become ideal – Suresh Vishnoi Feb 19 '18 at 12:32
  • Let me see if I understand correctly. When you execute the command, doesn't the port forwarding work? – Javier Salmeron Feb 20 '18 at 09:22
  • I think When We use port-forwarding it create a tcp tunnel between client and server. Now we send a request from client then the real data starts to flow. – Suresh Vishnoi Feb 20 '18 at 10:28
  • So, If I understood correctly, you enable the port-forwarding and, despite the command being "blocked" (which is expected), the data flow works, right? – Javier Salmeron Feb 20 '18 at 10:37
  • yea. As you said that command will be "blocked" which is expected. OP's question was why the terminal or prompt froze. I am having similar situation, I understood it could be related to the ideal state of tcp connection . – Suresh Vishnoi Feb 20 '18 at 10:44
  • I do not understand what you mean by "frozen". I see that the user could do CTRL-C, right? – Javier Salmeron Feb 20 '18 at 10:48
  • It is expected to me also, that the k8s "stuff" is all daemonised, so altering the forwarded ports seems like it should make the change and return. Instead kubectl is acting as a running proxy not in daemon mode to achieve the port forwarding. – Tyeth Jul 21 '20 at 13:51
  • 4
    This is an example of a technically correct, but not terribly helpful answer. The question might not have explicitly asked it, but is implicitly asking "How do I get this port forwarding stuff to stick around after I close the terminal". I'm just starting playing around with k8s, and this is the first thing I've hit and now I need to go figure that out. The only information this answer gave me was to possibly look for something to do with daemonizing. There doesn't appear to be a switch to make that happen, so now I need to exclude SO from my search. – Will Hughes Mar 29 '21 at 12:59