2016-05-05 03:34:12 +00:00
---
---
2016-02-26 11:54:48 +00:00
2016-02-29 23:17:22 +00:00
kubectl port-forward forwards connections to a local port to a port on a pod. Its man page is available [here ](/docs/user-guide/kubectl/kubectl_port-forward ). Compared to [kubectl proxy ](/docs/user-guide/accessing-the-cluster/#using-kubectl-proxy ), `kubectl port-forward` is more generic as it can forward TCP traffic while `kubectl proxy` can only forward HTTP traffic. This guide demonstrates how to use `kubectl port-forward` to connect to a Redis database, which may be useful for database debugging.
2016-02-26 11:54:48 +00:00
## Creating a Redis master
2016-05-05 03:34:12 +00:00
```shell
2016-07-08 21:23:04 +00:00
$ kubectl create -f examples/redis/redis-master.yaml
2016-02-26 11:54:48 +00:00
pods/redis-master
2016-05-05 03:34:12 +00:00
```
2016-02-26 11:54:48 +00:00
wait until the Redis master pod is Running and Ready,
2016-05-05 03:34:12 +00:00
```shell
2016-02-26 11:54:48 +00:00
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
redis-master 2/2 Running 0 41s
2016-05-05 03:34:12 +00:00
```
2016-02-26 11:54:48 +00:00
## Connecting to the Redis master[a]
2016-05-05 03:34:12 +00:00
The Redis master is listening on port 6379, to verify this,
2016-02-26 11:54:48 +00:00
2016-05-05 03:34:12 +00:00
```shell{% raw %}
2016-07-08 21:23:04 +00:00
$ kubectl get pods redis-master --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
2016-04-08 18:49:34 +00:00
6379{% endraw %}
2016-05-05 03:34:12 +00:00
```
2016-02-26 11:54:48 +00:00
then we forward the port 6379 on the local workstation to the port 6379 of pod redis-master,
2016-05-05 03:34:12 +00:00
```shell
2016-02-26 11:54:48 +00:00
$ kubectl port-forward redis-master 6379:6379
I0710 14:43:38.274550 3655 portforward.go:225] Forwarding from 127.0.0.1:6379 -> 6379
I0710 14:43:38.274797 3655 portforward.go:225] Forwarding from [::1]:6379 -> 6379
2016-05-05 03:34:12 +00:00
```
2016-02-26 11:54:48 +00:00
To verify the connection is successful, we run a redis-cli on the local workstation,
2016-05-05 03:34:12 +00:00
```shell
2016-02-26 11:54:48 +00:00
$ redis-cli
127.0.0.1:6379> ping
PONG
2016-05-05 03:34:12 +00:00
```
Now one can debug the database from the local workstation.