對于main.go此問題末尾的代碼,我運行了以下命令以在kubernetes安裝時運行它(在 a 上PC):docker image build -t myID/go-demo:1.2 .docker image push myID/go-demo:1.2 # Pushed up to DockerHubkubectl run demo2 --image=myID/go-demo:1.2 --port=19999 --labels app=demo2kubectl port-forward deploy/demo2 19999:8888Forwarding 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 mainimport ( "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))}
1 回答

肥皂起泡泡
TA貢獻1829條經驗 獲得超6個贊
根據 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
默認僅轉發本地主機。
- 1 回答
- 0 關注
- 227 瀏覽
添加回答
舉報
0/150
提交
取消