在当前进程中使用k8s端口转发

时间:2019-01-17 01:59:18

标签: elasticsearch go tcp kubernetes

我想将TCP连接移植到我的程序,并使用它与Pod通信。我可以建立到Pod的连接,并可以从其他外壳对http://localhost:9200/执行卷曲。 但是,当尝试从我的程序中访问服务时,连接被拒绝。

这是我的代码片段,用于建立连接并进行局部卷曲。

stopChan, readyChan := make(chan struct{}, 1), make(chan struct{}, 1)
out, errOut := new(bytes.Buffer), new(bytes.Buffer)
path := fmt.Sprintf("/api/v1/namespaces/%s/pods/%s/portforward", "default", "my-http-server")
hostIP := strings.TrimLeft(config.Host, "https://")
serverURL := url.URL{Scheme: "https", Path: path, Host: hostIP}

dialer := spdy.NewDialer(upgrader, &http.Client{Transport: roundTripper}, http.MethodGet, &serverURL)
forwarder, err := portforward.New(dialer, []string{flagPort}, stopChan, readyChan, out, errOut)
if err != nil {
    panic(err)
}

/* From here, I can make a curl to http://localhost:9200/ from a different shell */

/* But I can't curl from within this process */
resp, err := http.Get("http://localhost:9200/")
if err != nil {
    /* It is exiting here */
    panic(err)
}
fmt.Println(resp.Status, resp.Body)
defer resp.Body.Close()

我得到:panic: dial tcp [::1]:9200: connect: connection refused

我应该重用已设置的拨号程序来与Pod通信吗? 理想情况下,我需要与Elasticsearch client重用此连接 为什么我在127.0.0.1localhost上都被拒绝了,因为它在该过程之外运行良好?

0 个答案:

没有答案
相关问题