对于此问题末尾的main.go
代码,我运行了以下命令以在kubernetes
安装(在PC
上)上运行它:
docker image build -t myID/go-demo:1.2 .
docker image push myID/go-demo:1.2 # Pushed up to DockerHub
kubectl run demo2 --image=myID/go-demo:1.2 --port=19999 --labels app=demo2
kubectl port-forward deploy/demo2 19999:8888
Forwarding from 127.0.0.1:19999 -> 8888
Forwarding from [::1]:19999 -> 8888
然后,在另一个tmux(1)
终端中,我确认服务正在LISTEN
上:
user@vps10$ sudo netstat -ntlp | egrep "Local|19999"
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:19999 0.0.0.0:* LISTEN 736786/kubectl
tcp6 0 0 ::1:19999 :::* LISTEN 736786/kubectl
但这是我的问题,注意到localhost
的成功和hostname -- vps10
的失败:
user@vps10$ curl localhost:19999 # Works.
Hello, 世界
user@vps10$ curl vps10:19999 # Fails.
curl: (7) Failed to connect to vps10 port 19999: Connection refused
从上面看,问题似乎在于该服务仅通过loopback
接口进行监听,如果确实存在问题,我该怎么做才能使其在all interfaces
上进行监听(或在我指定的specific interface
上)。我不是kubernetes
或go
专家(此示例实际上来自一本书= :)),因此请根据需要提供命令。预先谢谢你!
HTTP服务器代码:
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, 世界")
}
func main() {
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("0.0.0.0:8888", nil))
}
答案 0 :(得分:3)
根据kubectl帮助:
# Listen on port 8888 on all addresses, forwarding to 5000 in the pod
kubectl port-forward --address 0.0.0.0 pod/mypod 8888:5000
# Listen on port 8888 on localhost and selected IP, forwarding to 5000 in the pod
kubectl port-forward --address localhost,10.19.21.23 pod/mypod 8888:5000
默认设置是仅对本地主机转发。