erotavlas
erotavlas

Reputation: 4475

How can my services communicate with each other in a kubernetes deployment?

Part of my deployment looks like this

client -- main service  __ service 1
                       |__ service 2

NOTE: Each of these 4 services is a container and I'm trying to do this where each is in it's own Pod (without using multi container pod)

Where main service must make a call to service 1, get results then send those results to service 2, get that result and send it back to the web client

main service operates in this order

My deployments for service 1 and 2 look like this

SERVICE 1

apiVersion: v1 
kind: Service 
metadata: 
  name: serviceone 
spec: 
  selector: 
    run: serviceone 
  ports: 
    - port: 80 
      targetPort: 5050 

--- 

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: serviceone-deployment 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      run: serviceone 
  template: 
    metadata: 
      labels: 
        run: serviceone 
    spec: 
      containers: 
      - name: serviceone 
        image: test.azurecr.io/serviceone:v1 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 5050 

SERVICE 2

apiVersion: v1 
kind: Service 
metadata: 
  name: servicetwo
spec: 
  selector: 
    run: servicetwo 
  ports: 
    - port: 80 
      targetPort: 5000 

--- 

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: servicetwo-deployment 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      run: servicetwo 
  template: 
    metadata: 
      labels: 
        run: servicetwo 
    spec: 
      containers: 
      - name: servicetwo 
        image: test.azurecr.io/servicetwo:v1 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 5000 

But I don't know what the service and deployment would look like for the main service that has to make request to two other services.

EDIT: This is my attempt at the service/deployment for main service

apiVersion: v1 
kind: Service 
metadata: 
  name: mainservice
spec: 
  selector: 
    run: mainservice 
  ports: 
    - port: 80 # incoming traffic from web client pod
      targetPort: 80 # traffic goes to container port 80
  selector: 
    run: serviceone 
  ports: 
    - port: ?
      targetPort: 8000 # the port the container is hardcoded to send traffic to service one
  selector: 
    run: servicetwo 
  ports: 
    - port: ? 
      targetPort: 8001  # the port the container is hardcoded to send traffic to service two  

--- 

apiVersion: apps/v1 
kind: Deployment 
metadata: 
  name: mainservice-deployment 
spec: 
  replicas: 1 
  selector: 
    matchLabels: 
      run: mainservice 
  template: 
    metadata: 
      labels: 
        run: mainservice 
    spec: 
      containers: 
      - name: mainservice 
        image: test.azurecr.io/mainservice:v1 
        imagePullPolicy: IfNotPresent 
        ports: 
        - containerPort: 80

EDIT 2: alternate attempt at the service after finding this https://kubernetes.io/docs/concepts/services-networking/service/#multi-port-services

apiVersion: v1 
kind: Service 
metadata: 
  name: mainservice
spec: 
  selector: 
    run: mainservice 
  ports: 
    - name: incoming
      port: 80 # incoming traffic from web client pod
      targetPort: 80 # traffic goes to container port 80
    - name: s1
      port: 8080
      targetPort: 8000 # the port the container is hardcoded to send traffic to service one
    - name: s2
      port: 8081
      targetPort: 8001  # the port the container is hardcoded to send traffic to service two  

Upvotes: 1

Views: 1622

Answers (1)

Burak Serdar
Burak Serdar

Reputation: 51467

The main service doesn't need to know anything about the services it calls other than their names. Simply access those services using the name of the Service, i.e. service1 and service2 (http://service1:80) and the requests will be forwarded to the correct pod.

Reference: https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/

Upvotes: 1

Related Questions