Kubernetes Connection Between two services?

6/1/2018

I have two services auth and frontend. How do I make a connection between the two?

auth.yml

apiVersion: apps/v1
kind: Deployment
metadata:
   name: auth
spec:
  replicas: 1
  selector:
    matchLabels:
      app: auth
  template:
    metadata:
      labels:
        app: auth
        tier: backend
        track: dev
    spec:
      containers:
        - name: auth
          image:  auth:1
          ports:
            - name: auth
              containerPort: 8000
----------------------------------------------
apiVersion: v1
kind: Service
metadata:
  name: auth
spec:
  selector:
    app: auth
    tier: backend
  ports:
    - protocol: TCP
      port: 3000
      targetPort: auth
  type: LoadBalancer

The above service works fine with the external IP and the port.

fe.yml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: fe
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: fe
      template:
        metadata:
          labels:
            app: fe
            tier: frontend
            track: dev
        spec:
          containers:
            - image: fe:1
              name: nginx
              ports:
                - name: fe
                  containerPort: 80
    ------------------------------------------------
    apiVersion: v1
    kind: Service
    metadata:
      name: fe
    spec:
      selector:
        app: fe
        tier: frontend
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
      type: LoadBalancer

I am able to access both the services individually with the external IP. I want both the services to interact with each other. I have tried using name-based calling service in the frontend like - http://auth:3000. It does not seem to work.

-- Kishan M
angular5
docker
kubernetes
python

1 Answer

6/1/2018

Services target pods (through their labels), not other services. To hit one service from another service, this is the flow:

User -> auth (service) -> auth (pods) -> fe (service) -> fe (pods).

So your pods, in auth need to forward the traffic to the other service.

Note that there is no need to have 2 Load Balancer type service. With the first service the data gets from outside world to your cluster. At this point it would be recommendable to use ClusterIP type service.

-- suren
Source: StackOverflow