Pod to Pod communication is not working in kubernetes

2/8/2019

I am using Spring Boot to create REST API and that access MongoDB on load time. I am making one deployment & service for REST API and one deployment and service for mongodb.

But my REST API pod is breaking and not coming up as on load time it looks for mongodb service but it is not able to ping that host.

I have exposed mongodb as a service and also REST API as a service. REST API is exposed as NodePort and mongodb is exposed as ClusreIP.

Everything i tried but no solution.

\===================================MongoDB deployment========================

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tech-hunt-mongodb
spec:
  replicas: 1
  template:
    metadata:
      name: tech-hunt-mongodb
      labels:
        app: tech-hunt
        module: mongodb
    spec:
      containers:
      - image: <image>
        name: tech-hunt-mongodb
        ports:
        - containerPort: 27017

\===================================MongoDB service========================

apiVersion: v1
kind: Service
metadata:
  name: tech-hunt-mongodb
spec:
        #type: ClusterIP
  selector:
    app: tech-hunt
    module: mongodb
  ports:
  - port: 27017
    targetPort: 27017
    protocol: TCP
  clusterIP: None
    #nodePort: 30000
    #protocol: TCP

\===========================REST API deployment================================

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tech-hunt-api
spec:
  template:
    metadata:
      name: tech-hunt-api
      labels:
        app: tech-hunt
        module: rest-api    
    spec:
      containers:
      - image: <image>
        name: tech-hunt-api
        ports:
        - containerPort: 4000

\===============================REST API service=============================

apiVersion: v1
kind: Service
metadata:
  name: tech-hunt-api-client
spec:
  type: NodePort
  selector:
    app: tech-hunt
    module: rest-api-client
  ports:
  - port: 5000
    targetPort: 5000
    nodePort: 30010
-- Nishant Kansal
kubernetes
pod

2 Answers

2/9/2019

clusterIP: None

is almost certainly not what you want to happen, as that places the burden of populating the Endpoints entirely on you -- or an external controller (the StatefulSet controllers are one such example).

You'll have to delete, and then recreate, the tech-hunt-mongodb Service in order to change its clusterIP away from None over to an auto-populated value, but you should for sure do that first.

But my REST API pod is breaking and not coming up as on load time it looks for mongodb service but it is not able to ping that host.

As an FYI, you will never be able to "ping" a Service IP since those addresses are "fake"; only the tuple of Service IP and its traffic port (so, 27017 or 5000 in your two examples) will respond to any packets. You should use curl or nc to test connectivity, and not ping.

-- mdaniel
Source: StackOverflow

2/10/2019

I was using wrong mongodb image that was not giving access to the clients. So the created container was not pingable in neither mean via curl or so. I am using NodePort and my rest API container is able to get response from mongodb container.

-- Nishant Kansal
Source: StackOverflow