Call a rest api from one pod to another pod in same kubernetes cluster

8/8/2019

In my k8s cluster I have two pods podA and podB. Both are in same k8s cluster. Microservice on pod B is a spring boot rest api. Microservice on pod A have ip and port of pod B in its application.yaml. now every time when podB recreates, ip change which forces us to change ip in application.yml of podA. Please suggest a better way.

My limitation is : I can't change the code of podA.

-- Shashank
docker
kubernetes
spring-boot

2 Answers

8/8/2019

A Service will provide a consistent DNS name for accessing Pods. An application should never address a Pod directly unless you have a specific reason to (custom load balancing is one I can think of).

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

You will then have a consistent DNS name to access any Pods that match the selector:

my-service.default.svc.cluster.local
-- Matt
Source: StackOverflow

8/8/2019

That's what service are for. Take a postgres service:

kind: Service
apiVersion: v1
metadata:
  name: postgres-service
spec:
  type: ClusterIP
  selector:
    app: postgres
  ports:
    - protocol: TCP
      port: 5432
      targetPort: 5432

You can use postgres-service in other pods instead of referring to the ip address of the pod. You also have the advantage that k8s's doing some load balancing for you as well.

-- Federkun
Source: StackOverflow