Expose your app publicly doesn't work on local kubernetes cluster

6/27/2020

I am running local kubernetes cluster installed as a part of Docker Desktop.

kubectl version
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:58:59Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"16+", GitVersion:"v1.16.6-beta.0", GitCommit:"e7f962ba86f4ce7033828210ca3556393c377bcc", GitTreeState:"clean", BuildDate:"2020-01-15T08:18:29Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}

I am creating deployment by running

kubectl create deployment firstkube-deployment --image=firstkube:v1
deployment.apps/firstkube-deployment created

Then exposing the deployment by

kubectl expose deployment firstkube-deployment --type=NodePort --port=8080
service/firstkube-deployment exposed

The kubectl describe services command returns the following

kubectl describe services/firstkube-deployment
Name:                     firstkube-deployment
Namespace:                default
Labels:                   app=firstkube-deployment
Annotations:              <none>
Selector:                 app=firstkube-deployment
Type:                     NodePort
IP:                       10.107.205.123
LoadBalancer Ingress:     localhost
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  31448/TCP
Endpoints:                10.1.0.8:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

The request to http://127.0.0.1:31448/ from browser fails with This page isn’t working 127.0.0.1 didn’t send any data. ERR_EMPTY_RESPONSE

If I am trying to reach my app using api proxy, by requesting http://127.0.0.1:8001/api/v1/namespaces/default/pods/firstkube-deployment-6778f8d74-qhgz2/proxy/ I am able to get response back, the application is app and running.

I have tried the proposed solution from this post https://stackoverflow.com/questions/50178696/access-a-kubernetes-service-running-locally-in-docker-for-desktop It doesn't help.

Providing also pods info

kubectl describe pods
Name:         firstkube-deployment-6778f8d74-qhgz2
Namespace:    default
Priority:     0
Node:         docker-desktop/192.168.65.3
Start Time:   Sat, 27 Jun 2020 18:30:38 -0400
Labels:       app=firstkube-deployment
              pod-template-hash=6778f8d74
Annotations:  <none>
Status:       Running
IP:           10.1.0.4
IPs:
  IP:           10.1.0.4
Controlled By:  ReplicaSet/firstkube-deployment-6778f8d74
Containers:
  firstkube:
    Container ID:   docker://1662d883b9f049a5e4fa0eab9283e2eefdfac80b0ce95ac6db02c89d9357cf18
    Image:          firstkube:v2
    Image ID:       docker://sha256:78332407ac919779bc6fb331b955bcdd0183452a8e699c8552c6cbca05978c42
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Sat, 27 Jun 2020 18:30:39 -0400
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-8tfz2 (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-8tfz2:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-8tfz2
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type    Reason     Age   From                     Message
  ----    ------     ----  ----                     -------
  Normal  Scheduled  10m   default-scheduler        Successfully assigned default/firstkube-deployment-6778f8d74-qhgz2 to docker-desktop
  Normal  Pulled     10m   kubelet, docker-desktop  Container image "firstkube:v2" already present on machine
  Normal  Created    10m   kubelet, docker-desktop  Created container firstkube
  Normal  Started    10m   kubelet, docker-desktop  Started container firstkube
-- Alexey Auslender
docker
kubernetes

2 Answers

6/28/2020

Few pointers to debug the issue

  1. The service has got 10.1.0.7:8080 in the Endpoints but the pod IP is 10.1.0.4. Check if there is another pod with same label app=firstkube-deployment

  2. Check if you are able to access the pod directly by it's IP and port from another pod in the cluster. If that does not work then check if the application inside the pod is listening in 0.0.0.0 instead of 127.0.0.1. Also check if the pod is listening on 8080 or some other port.

-- Arghya Sadhu
Source: StackOverflow

7/2/2020

The problem was with kubectl expose deployment command. It needs to be provided port 80

kubectl expose deployment firstkube-deployment --type=NodePort --port=80
service/firstkube-deployment exposed

Then get services returns the following

kubectl get services/firstkube-deployment
NAME                   TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
firstkube-deployment   NodePort   10.96.23.130   <none>        80:32001/TCP   32s

Now I can reach my application in browser by requesting http://localhost:32001

-- Alexey Auslender
Source: StackOverflow