概述或功能
通过端口转发映射本地端口到指定的应用端口
例子
创建 Redis deployment and service
创建redis deployment
kubectl create -f https://k8s.io/docs/tutorials/stateless-application/guestbook/redis-master-deployment.yaml
创建redis server
kubectl create -f https://k8s.io/docs/tutorials/stateless-application/guestbook/redis-master-service.yaml
校验redis server暴露的端口是6379
kubectl get pods redis-master-xxxxx --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
把本地的port 映射到pod的port
kubectl port-forward redis-master-765d459796-258hz 6379:6379
或者
kubectl port-forward pods/redis-master-765d459796-258hz 6379:6379
或者
kubectl port-forward deployment/redis-master 6379:6379
或者
kubectl port-forward rs/redis-master 6379:6379
或者
kubectl port-forward svc/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
通过命令行访问redis
redis-cli
127.0.0.1:6379>ping
END