Cannot able to curl into service by using cluster-ip from a different pod , facing connection timed out with exit code 7

4/21/2019

In the Minikube , I created a pod with the configuration provided below

kind: Pod
metadata:
  name: node-js-pod
spec:
  containers:
  - name: node-js-pod
    image: bitnami/apache:latest
    ports: 
    - containerPort: 8080 

then i created a service of type cluster-ip with configuration provided below

kind: Service
metadata:
   name: node-js-internal
   labels:
     name: node-js-internal
spec:
  ports:
  - port: 8081
  selector:
    name: node-js

After creating the service i checked the cluster ip of service using command: kubectl get services -l name=node-js-internal which gave output as:

NAME               TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
node-js-internal   ClusterIP   10.106.71.114   <none>        8081/TCP   18m

Once I created this service i tried to access this service internally from node-js-pod using command: kubectl exec node-js-pod -- curl 10.106.71.114:8081 Which gave error message as curl 7 connection timed out :

$ kubectl exec node-js-pod -- curl 10.106.71.114:8081
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:02:10 --:--:--     0curl: (7) Failed to connect to 10.106.71.114 port 8081: Connection timed out
command terminated with exit code 7

How to connect to the node-js-internal service from pod node-js in minikube?

-- monesh
kubernetes

2 Answers

4/21/2019

You forgot to add label to pod:

kind: Pod
metadata:
  name: node-js-pod
  labels:
    name: node-js

Remember, service selects pods by their labels.

-- Vasily Angapov
Source: StackOverflow

4/21/2019

Use the same port in the service as the container is listening to.

kind: Service
metadata:
   name: node-js-internal
   labels:
     name: node-js-internal
spec:
  ports:
  - port: 8080
  selector:
    name: node-js-pod

If you want to have a different port in the service, than the container is listening to, you need to specify a mapping in the service

ports:
  - protocol: TCP
    port: 8081
    targetPort: 8080
-- Thomas
Source: StackOverflow