How does gateway connect other services in Kubernetes?

Viewed 409

I am attaching the image of my application flow. Here the Gateway and other services are created using NestJS. The request for any API comes through the gateway.

The Gateway-pod and API-pod communicate using TCP protocol.

After deployment the Gateway is not able to discover any API pods.

I am attaching the YAML image file also for both Gateway & Pods. Please do let me know what mistake I am doing in the YAML file.

                    **APPLICATION DIAGRAM**

enter image description here

Gateway YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: roushan
  name: gateway-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: roushan-app
  template:
    metadata:
      labels:
        app: roushan-app
    spec:
      containers:
        - name: gateway-container
          image: nest-api-gateway:v8
          ports:
          - containerPort: 1000

apiVersion: v1
kind: Service
metadata:
  namespace: roushan
  name: gateway-svc
spec:
  selector:
    app: roushan-app
  ports:
    - name: gateway-svc-container
      protocol: TCP
      port: 80
      targetPort: 1000
  type: LoadBalancer

Pod YAML

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: roushan
  name: pod1-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: roushan-app
  template:
    metadata:
      labels:
        app: roushan-app
    spec:
      containers:
        - name: pod1-container
          image: nest-api-pod1:v2
          ports:
          - containerPort: 4000

apiVersion: v1
kind: Service
metadata:
   namespace: roushan
   name: pod1-srv
spec:
  selector:
    app: roushan-app
  ports:
    - name: pod1-svc-container
      protocol: TCP
      port: 80
      targetPort: 4000
2 Answers

the gateway should be able to access the services by their DNS name. for example pod1-srv.svc.cluster.local, if this does not work you may need to look at the Kubernetes DNS setup.

I have not used AKS, they may use a different domain name for the cluster other than svc.cluster.local

YAML Points

Ideally, you should be keeping the different selectors across the deployment.

You are using the same selectors for both deployments. Gateway and application deployment.

Service will forward the traffic to deployment based on selectors and labels, this might redirect the service-2 request to POD-1.

Networking

You gateway service(Pods) connect to internal service by just service-name like : pod1-srv if in same namespaces.

if gateway and application in different namespaces you have to call each other like http://<servicename>.<namespace>.svc.cluster.local

Related