Not able to hit the kubernetes service

11/7/2019

I am new to kubernetes and not able to troubleshoot the issue. service and pod is running but I am not able to get the response from the postman.

kubectl get service

enter image description here

kubectl get service personservice -o json

{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
    "annotations": {
        "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"v1\",\"kind\":\"Service\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"personservice\"},\"name\":\"personservice\",\"namespace\":\"default\"},\"spec\":{\"ports\":[{\"nodePort\":30001,\"port\":8080,\"protocol\":\"TCP\"}],\"selector\":{\"app\":\"personservice\"},\"type\":\"NodePort\"}}\n"
    },
    "creationTimestamp": "2019-11-07T11:22:04Z",
    "labels": {
        "app": "personservice"
    },
    "name": "personservice",
    "namespace": "default",
    "resourceVersion": "84920",
    "selfLink": "/api/v1/namespaces/default/services/personservice",
    "uid": "d32d4dd8-0150-11ea-80e6-00155d010311"
},
"spec": {
    "clusterIP": "10.98.168.74",
    "externalTrafficPolicy": "Cluster",
    "ports": [
        {
            "nodePort": 30001,
            "port": 8080,
            "protocol": "TCP",
            "targetPort": 8080
        }
    ],
    "selector": {
        "app": "personservice"
    },
    "sessionAffinity": "None",
    "type": "NodePort"
},
"status": {
    "loadBalancer": {}
}

}

kubectl get pods

enter image description here

when I am trying to hit it using postman

http://100.120.60.210:30001/getPerson?nino=12345Ac&id=1

I get "There was an error connecting to http://100.120.60.210:30001/getPerson?nino=12345Ac&id=1."

  IPv4 Address. . . . . . . . . . . : 100.120.60.210 --- Ip address of my laptop.

Edit 1: pod deploy config:

 apiVersion: apps/v1
kind: Deployment
metadata:
  name: personservice
  labels:
    app: personservice
spec:
  replicas: 1
  selector:
    matchLabels:
      app: personservice
  template:
    metadata:
      labels:
        app: personservice
    spec:
      containers:
      - name: personservice
        image: microservice-k8s/personmicroservice-k8s:1.0
        ports:
        - containerPort: 8080
        env:
        - name: PROFILE
          value: "dev" 
        - name: SERVER_PORT
          value: "8080"
        - name: ZIPKIN_URI
          value: "http://100.120.60.210:9411"

Edit 2:

I have deployed another pod and service to test and it is working. Ran this command for both the service kubectl get service personservice -o json The only diff I found is following:

Non-working service

  "status": {
        "loadBalancer": {}
    }

working service:

 "status": {
        "loadBalancer": {
            "ingress": [
                {
                    "hostname": "localhost"
                }
            ]
        }
    }

what could be the reason behind the empty status?

Edit 3:

Port forwarding to the pod and accessing it through http://localhost:7000 is working.

kubectl port-forward personservice-5c66cfcb89-dd6l7 7000:8080

This means something is wrong with the service only.

-- Chandresh Mishra
docker
kubernetes
spring-boot
spring-cloud
spring-cloud-kubernetes

2 Answers

11/11/2019

Run minikube tunnel to be able to connect to the ip directly.

-- Jeff
Source: StackOverflow

11/14/2019

I've reproduced your case in a slightly different scenario. You are using Docker Desktop with Kubernetes and I've decided to use Minikube to reproduce it.

I used your yaml files as a base to deploy an nginx service and I didn't face the problem you are describing. This is my deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: personservice
  labels:
    app: personservice
spec:
  replicas: 1
  selector:
    matchLabels:
      app: personservice
  template:
    metadata:
      labels:
        app: personservice
    spec:
      containers:
      - name: personservice
        image: nginx
        ports:
        - containerPort: 80

This is my Service:

apiVersion: v1
kind: Service
metadata:
  labels:
    app: personservice
  name: personservice
  namespace: default
spec:
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30001
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    app: personservice
  sessionAffinity: None
  type: NodePort

Note: I'm using port 80 for my convenience only.

As you can see, these yaml files have the same components you have in yours and I can successfully curl my nginx application.

user@bf:~$ minikube ip
192.168.39.153
user@bf:~$ curl 192.168.122.36:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

I highly suggest you to reproduce it in a different environment using Minikube for example. There are many advantages on using Minikube instead of Docker Desktop.

-- mWatney
Source: StackOverflow