Connect Pod to Pod through clusterIP service?

10/7/2019

I have gone through couple of articles related how clusterIP abd nodeport service works(like this blog)

Say I have 3 different micro-service based web application each running on three separate node. Each runs the replica set of two.

My understanding is that there will be separate clusterIP service for application replicaset instead of one single clusterIP service for all application type. Is that correct ? Now if one pode need to connect to another pod, it will call that corresponding clusterIP service to connect to right pod ?

-- user3198603
kubernetes

3 Answers

10/7/2019

In the case you want to enable connection between pods, you can add a Headless service above your regular ClusterIP (which is correct btw).

A headless service provides a DNS entry for each pod of the deployment. Requests are resolved using this syntax:

<pod_name>.<headless_svc_name>

I would recommend you to use a Statefulset deployment in this case : pods get stable names such as "pod-0", "pod-1"...

-- MushroomMagnet
Source: StackOverflow

10/7/2019

Yeah the design looks fine, for each microservice you can deploy service and deployment objects.

You can always ssh into any of the pod and then type nslookup "service_name" there, it will give you fully qualified domain name and you can always connect using that, if your applications can communicate of HTTP then you can try like this ( after doing ssh into a POD)

curl http://service-name.namespace.cluster.local:port

or you can do nslookup pod-name too.

-- Tushar Mahajan
Source: StackOverflow

10/7/2019

Yes, that's right.
In fact, you need to forget about the notion of pod.

As you said, you created 3 web based micro-service. So the correct terminology (and need) here, is to contact (micro-)service A from (micro-)service B. In order to do that, you need to create a kind: Service for each of your ReplicaSet.

For example :

---
# This is one micro-service based on Nginx
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      name: nginx
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:alpine
          ports:
            - containerPort: 80
---
# This is the ClusterIp service corresponding
apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx
  ports:
  - port: 8080
    targetPort: 80

In the example above, we have two replicates of a micro-service based on Nginx. We also have a ClusterIP kind: Service that target our nginx app.

Now, if we want to contact nginx from another pod, all we need to do is use the service name and the port configured from inside the cluster. In our case, it'll be nginx:8080.

To try that, you need to create a pod that will serve us as the entry point in the cluster :

---
apiVersion: v1
kind: Pod
metadata:
  name: gateway
spec:
  containers:
  - image: centos:7
    name: gateway
    command: ["bash", "-c", "sleep infinity"]

Now, if you want to contact your nginx app from the cluster, you'll have to execute this command :

kubectl exec -ti gateway curl nginx:8080
-- Marc ABOUCHACRA
Source: StackOverflow