Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to communicate between pods in a service?

Tags:

kubernetes

Suppose I have a service containing two pods. One of the pods is an HTTP server, and the other pod needs to hit a REST endpoint on this pod. Is there a hostname that the second pod can use to address the first pod?

like image 384
Alex Flint Avatar asked Dec 19 '25 12:12

Alex Flint


1 Answers

I'm assuming when you say "service" you aren't referring to the Kubernetes lexicon of a Service object, otherwise your two Pods in the Service would be identical, so let's start by teasing out what a "Service" means in Kubernetes land.

You will have to create an additional Kubernetes object called a Service to get your hostname for your HTTP server's Pod. When you create a Service you will define a .spec.selector that points to a set of labels on the HTTP service's Pod. For the sake of example, let's say the label is app: nginx. The name of that Service object will become the internal DNS record that can be queried by the second Pod.

A simplified example:

apiVersion: v1
kind: Pod
metadata:
  name: http-service
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    image: nginx:1.7.9
    ports:
    - containerPort: 80
---
kind: Service
apiVersion: v1
metadata:
  name: my-http-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80

Now your second Pod can make requests to the HTTP service by the Service name, my-http-service.

It's also worth mentioning that Kubernetes best practice dictates that these Pods be managed by controllers such as Deployments or ReplicaSets for all sorts of reasons, including high availability of your applications.

like image 59
erstaples Avatar answered Dec 23 '25 10:12

erstaples



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!