Kubernetes:端口转发后,我可以通过 "localhost" 但不能通过 "hostname" 访问服务
Kubernetes: After port-forwarding, I can access a service via "localhost" but not via "hostname"
对于这个问题末尾的 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
ing:
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
接口进行侦听,如果这确实是问题所在,我该怎么做才能让它在 [=28= 上进行侦听](或在我指定的 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))
}
根据 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
默认仅转发本地主机。
对于这个问题末尾的 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
ing:
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
接口进行侦听,如果这确实是问题所在,我该怎么做才能让它在 [=28= 上进行侦听](或在我指定的 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))
}
根据 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
默认仅转发本地主机。