亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Kubernetes:端口轉發后,我可以通過“localhost”訪問服務

Kubernetes:端口轉發后,我可以通過“localhost”訪問服務

Go
忽然笑 2022-05-23 17:18:18
對于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


默認僅轉發本地主機。


查看完整回答
反對 回復 2022-05-23
  • 1 回答
  • 0 關注
  • 227 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號