Unable to curl to external IP

4/16/2020

I am unable to curl to the external IP of the service. My Kubernetes cluster is deployed on GKE.

$ kubectl run kubia-container --image=australia/kubia_py --port=8080 --generator=run/v1
kubectl run --generator=run/v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
replicationcontroller/kubia-container created

$ kubectl get pods
NAME          READY  STATUS  RESTARTS  AGE
kubia-container-6b2cs  1/1   Running  0     25s

$ kubectl expose rc kubia-container --type=LoadBalancer --name kubia-http
service/kubia-http exposed

$ kubectl get pods,svc,rc
NAME            READY  STATUS  RESTARTS  AGE
pod/kubia-container-6b2cs  1/1   Running  0     93m

NAME         TYPE      CLUSTER-IP  EXTERNAL-IP  PORT(S)     AGE
service/kubernetes  ClusterIP   10.0.0.1   <none>     443/TCP     6h28m
service/kubia-http  LoadBalancer  10.0.4.238  35.188.42.26  8080:30030/TCP  91m

$ curl 35.188.42.26:8080
curl: (7) Failed to connect to 35.188.42.26 port 8080: Connection refused
-- asif syed
google-kubernetes-engine
kubectl

2 Answers

4/16/2020

I've tried many ways to achieve what you want. I assume this image australia/kubia_py it's your custom image. I think you are not able to curl this service as inside this image in file nodeapp.py you have put entry like:

server = HTTPServer(('localhost', 8080), GetHandler) 
print ("Starting server, use <Ctrl-C> to stop") 
server.serve_forever()

Also if you would check netstat inside container you would see that its listening on 127.0.0.1:8080

# netstat -plnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN      1/python3

I think that instead of 'localhost' you should specify 0.0.0.0 to listen on all interfaces.

I found only one way to achieve output of this replicacontroller app (I suggest to try using deployments in the future) was using web-preview. To do that you would need to port-forward your pod.

$ kubectl get pods
NAME                    READY   STATUS    RESTARTS   AGE
kubia-container-95rrq   1/1     Running   0          44m
user@cloudshell:~ (project-name)$ kubectl port-forward kubia-container-95rrq 8888:8080
Forwarding from 127.0.0.1:8888 -> 8080
Handling connection for 8888
Handling connection for 8888

Port 8888 was just one from default I choose. You have a few in menu.

And using WebPreview (using button from cloud shell). was able to achieve output like below:

CLIENT VALUES:
client_address=('127.0.0.1', 46696) (127.0.0.1)
command=GET
path=/?authuser=0
real path=/
query=authuser=0
request_version=HTTP/1.1

SERVER VALUES:
server_version=BaseHTTP/0.6
sys_version=Python/3.6.9
protocol_version=HTTP/1.0
host_name=kubia-container-95rrq
host_ip=10.44.1.26

In addition, if you are exposing replicacontroller or deployment keep in mind if you will just set --port=8080 it will also automatically set --port-target as 8080. It's preferred if you are using kubectl expose to use flags --port=XX and --target-port=xxxx.

If you would like to check kubia replicacontroller and service you can use this example, like below:

$ kubectl apply -f kubia-rc-and-service-v1.yaml
replicationcontroller/kubia-v1 created
service/kubia created
user@cloudshell:~ (your-project)$ kubectl get rc,svc
NAME                                    DESIRED   CURRENT   READY   AGE
replicationcontroller/kubia-v1          3         3         3       40s

NAME                      TYPE           CLUSTER-IP    EXTERNAL-IP      PORT(S)          AGE
service/kubernetes        ClusterIP      10.0.48.1     <none>           443/TCP          8h
service/kubia             LoadBalancer   10.0.51.154   35.240.23.237    80:30785/TCP     40s
$ curl 35.240.23.237:80
This is v1 running in pod kubia-v1-zpj6s

Another good example to expose service and access it you can find on Official GKE docs.

-- PjoterS
Source: StackOverflow

4/16/2020

Try with localhost:30030, that's the nodeport exposed to your host.

If that doesn't work, it means your container is not listening in 8080, you can test that entering the pod and curl localhost:8080.

-- Michael
Source: StackOverflow