For the main.go
code at the end of this question, I ran the following commands to run it on a kubernetes
install (on a 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
Then, in another tmux(1)
terminal, I confirmed the service was 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
But here's my problem, noticing success with localhost
and failure with 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
From the above, the issue appears to be that the service is listening only via the loopback
interface, and if that's indeed the issue, what do I do to get it to listen on all interfaces
(or on a specific interface
that I specify). I'm not a kubernetes
or go
expert (this example is from a book actually =:)), so please supply commands if necessary. Thank you in advance!
HTTP server code:
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))
}
According to kubectl help:
# 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
The default is forwarding for localhost only.