Search code examples
kuberneteskubernetes-podkubernetes-service

How do I route/forward RESTful localhost calls from one specific replica set to another, without globally exposing a port?


Basically, what I am trying to do is imagine you have one replica set. This replica set called A contain pods that 'post' to localhost:9000, now I have another replica set B whose pods listen to localhost:8080. I want to divert the calls made to localhost:9000 on A to replica set B to port 8080. The only caveat is if I have another replica set C whose pods also listen to port 8080 they should not receive the traffic. https://i.sstatic.net/tyU0M.png


Solution

  • Just define a service for B. For A, instead of posting to localhost, post using the name of service B will do. C can have a service that use the same port but will NOT get traffic because A explicitly post to service B.

    apiVersion: v1
    kind: Service
    metadata:
      labels:
        app: app-b
      name: service-b  # <-- name of this service
    spec:
      type: ClusterIP
      selector:
        app: app-b
      ports:
      - name: http
        port: 9000
        protocol: TCP
        targetPort: 8080
    

    curl -XPOST http://service-b:9000 <-- neither pod nor service belongs to C will ever get this request