Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make two Kubernetes Services talk to each other?

Currently, I have working K8s API pods in a K8s service that connects to a K8s Redis service, with K8s pods of it's own. The problem is, I am using NodePort meaning BOTH are exposed to the public. I only want the API accessable to the public. The issue is that if I make the Redis service not public, the API can't see it. Is there a way to connect two Services without exposing one to the public?

This is my API service yaml:

apiVersion: v1
kind: Service
metadata:
   name: app-api-svc
spec:
   selector:
     app: app-api
     tier: api
   ports:
     - protocol: TCP
       port: 5000
       nodePort: 30400
   type: NodePort

And this is my Redis service yaml:

apiVersion: v1
kind: Service
metadata:
   name: app-api-redis-svc
spec:
   selector:
     app: app-api-redis
     tier: celery_broker
   ports:
     - protocol: TCP
       port: 6379
       nodePort: 30537
   type: NodePort
like image 669
Will Parzybok Avatar asked Aug 16 '17 17:08

Will Parzybok


1 Answers

First, configure the Redis service as a ClusterIP service. It will be private, visible only for other services. This is could be done removing the line with the option type.

apiVersion: v1
kind: Service
metadata:
   name: app-api-redis-svc
spec:
   selector:
     app: app-api-redis
     tier: celery_broker
   ports:
     - protocol: TCP
       port: 6379
       targetPort: [the port exposed by the Redis pod]

Finally, when you configure the API to reach Redis, the address should be app-api-redis-svc:6379

And that's all. I have a lot of services communicating each other in this way. If this doesn't work for you, let me know in the comments.

like image 150
Mario S Avatar answered Sep 17 '22 17:09

Mario S