Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to use kubectl port-forward in kubernetes

Shulou Source: shulou.com Published: 2022-05-31 16:49:26 09月21日 Update

Editor to share with you how to use kubectl port-forward in kubernetes, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

One: brief introduction

Kubectl port-forward uses port forwarding to map local ports to designated application ports to access applications in the cluster (Pod).

Two: give an example

1. Create a Pod to run the Redis server

a. Create a Pod:

Kubectl create-f https://k8s.io/docs/tasks/access-application-cluster/redis-master.yaml

After the command runs successfully, the following output verifies that the Pod has been created:

Pod "redis-master" created

b. Check to see if Pod is running and ready:

Kubectl get pods

When Pod is ready, the output shows the status of Running:

NAME READY STATUS RESTARTS AGE

Redis-master 2/2 Running 0 41s

c. Verify that the Redis server is running in Pod and listen on port 6379:

Kubectl get pods redis-master-- template=' {{(index (index .spec.containers 0) .containerPort 0) .containerPort} {{"\ n"}}'

Port output: 6379

two。 Forward the local port to the port in Pod

a. Forward port 6379 on the local workstation to port 6379 on redis-master pod:

Kubectl port-forward redis-master 6379:6379

The output looks like this:

I0710 14 4314 portforward.go:225 38.274550 3655 portforward.go:225] Forwarding from 127.0.0.1 purl 6379-> 6379

I0710 1414 43 portforward.go:225 38.274797 3655 portforward.go:225] Forwarding from [: 1]: 6379-> 6379

b. Launch the Redis command line interface

Redis-cli

c. At the Redis command line prompt, enter the ping command:

127.0.0.1 purl 6379 > ping

The Ping request returned PONG successfully.

Three: summary

Create a connection to forward the local port 6379 to port 6379 of the Redis server running in Pod. With this connection, you can debug the database running in Pod on your local workstation.

The above is all the content of the article "how to use kubectl port-forward in kubernetes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Port run command output server article service success content status a. work application validation not much most workstations applications prompts data Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MariaDB Xiaomi Huawei vpn