Kubernetes accessing one pod url from another

10/24/2020

I am creating a proxy server with node js and wants to proxy request coming to one pod to other pods in single node cluster of kubernetes. This is my node js code below ,,,,

const express = require("express");
const httpProxy = require("express-http-proxy");
const app = express();

const serviceone = httpProxy("serviceone:3000"); // I am using service names here but its not working

// Authentication
app.use((req, res, next) => {
  // TODO: my authentication logic
  console.log("Always.....");
  next();
});

app.get("/", (req, res, next) => {
  res.json({ message: "Api Gateway Working" });
});

// Proxy request
app.get("/:data/api", (req, res, next) => {
  console.log("Request Recieved");
  serviceone(req, res, next);
});

app.listen(5000, () => {
  console.log("Api Gateway Running");
});

And these are my services.yml files

apiVersion: apps/v1
kind: Deployment
metadata:
  name: serviceone
  labels:
    app: serviceone
spec:
  replicas: 1
  selector:
    matchLabels:
      app: serviceone
  template:
    metadata:
      labels:
        app: serviceone
    spec:
      containers:
      - name: serviceone
        image: swa/serviceone
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: serviceone
spec:
  selector:
    app: serviceone
  ports:
  - protocol: TCP
    port: 3000
    targetPort: 3000
    nodePort: 31000
  type: LoadBalancer

What name should I use in http proxy so that it can proxy requests?

I have tried serviceone:3000, http://serviceone:3000, http://localhost:3000 (won't work for different pods). Any help would be really appreciated

Edit - Node js apigateway pod

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apigateway
  labels:
    app: apigateway
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apigateway
  template:
    metadata:
      labels:
        app: apigateway
    spec:
      containers:
      - name: apigateway
        image: swa/apigateway3
        ports:
        - containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
  name: apigateway
spec:
  selector:
    app: apigateway
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000
    nodePort: 31005
  type: LoadBalancer

In my node js application I changed urls to

const serviceone = httpProxy('serviceone.default.svc.cluster.local:3000');

by following this link https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services but still no luck

I am getting the error

Error: getaddrinfo EAI_AGAIN servicethree.sock-shop.svc.cluster.local at GetAddrInfoReqWrap.onlookup as oncomplete

-- gANDALF
docker
kubernetes
node.js

0 Answers