kubernetes 使用kubectl port-forward 訪問應用

概述或功能 
經過端口轉發映射本地端口到指定的應用端口redis

例子 
建立 Redis deployment and service 
建立redis deploymentapp

kubectl create -f https://k8s.io/docs/tutorials/stateless-application/guestbook/redis-master-deployment.yaml
1
建立redis serverless

kubectl create -f https://k8s.io/docs/tutorials/stateless-application/guestbook/redis-master-service.yaml
1
2
校驗redis server暴露的端口是6379命令行

kubectl get pods redis-master-xxxxx --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
1
把本地的port 映射到pod的portserver

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
1
2
3
4
5
6
7
8
9
10
成功以後會如下相似輸出get

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
1
2
經過命令行訪問redisio

redis-cli
127.0.0.1:6379>ping

ENDast

參考: 
port-forward-access-application-clustercli

相關文章
相關標籤/搜索