反向代理
使用命令 kubevpn proxy 将某个工作负载的入站流量反向代理到本地电脑。
注意
代理作用于工作负载的已声明容器端口(Pod spec 中声明的端口,以及 --portmap 追加的端口)。
Envoy 在服务端注入,不带 --headers 时会匹配每个已声明端口上的所有请求并经隧道转发到本地。
发往未声明端口的流量没有监听器,会回落到集群中的真实应用——这不再是旧版"捕获所有端口"的
VPN sidecar。若需覆盖额外端口,请用 --portmap 声明。
➜ ~ kubevpn proxy deployment/productpage
Connected to cluster
Injecting inbound sidecar for deployment/productpage
Checking rollout status for deployment/productpage
Waiting for deployment "productpage" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "productpage" rollout to finish: 1 old replicas are pending termination...
Rollout successfully for deployment/productpage
Now you can access resources in the kubernetes cluster !
➜ ~
此时在本地编写一个测试服务,将以下代码保存为 hello.go
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
_, _ = io.WriteString(writer, "Hello world!")
fmt.Printf(">>Received request: %s %s from %s\n", request.Method, request.RequestURI, request.RemoteAddr)
})
_ = http.ListenAndServe(":9080", nil)
}
编译
go build hello.go
运行
./hello &
通过 Pod IP 访问测试
export selector=productpage
export pod=`kubectl get pods -l app=${selector} -n default -o jsonpath='{.items[0].metadata.name}'`
export pod_ip=`kubectl get pod $pod -n default -o jsonpath='{.status.podIP}'`
curl -v -H "foo: bar" http://$pod_ip:9080/health