0

I hope you are all doing fine I am developing a microservices application. I am using Kubernetes and skaffold. I run skaffold dev and it shows NatsError: Could not connect to server: Error: getaddrinfo EAI_AGAIN nats-srv and MongooseServerSelectionError: getaddrinfo EAI_AGAIN auth-mongo-srv for auth service. Before, the application used to work fine. I don't understand why this happens. I am attaching screenshots of it. Could you please help me solve it? I have been stuck for a few days with that problem, and I still cannot find a solution.

I checked the whole code if I had any misconfigurations but no. I also checked the example at the microservices course I am actually pursuing. For days in a row I can't solve that problem.

error

error

error

nats-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nats-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nats
  template:
    metadata:
      labels:
        app: nats
    spec:
      containers:
        - name: nats
          image: nats-streaming:0.17.0
          args:
            [
              '-p',
              '4222',
              '-m',
              '8222',
              '-hbi',
              '5s',
              '-hbt',
              '5s',
              '-hbf',
              '2',
              '-SD',
              '-cid',
              'ticketing',
            ]
---
apiVersion: v1
kind: Service
metadata:
  name: nats-srv
spec:
  selector:
    app: nats
  ports:
    - name: client
      protocol: TCP
      port: 4222
      targetPort: 4222
    - name: monitoring
      protocol: TCP
      port: 8222
      targetPort: 8222

expiration-depl.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: expiration-depl
spec:
  replicas: 1
  selector:
    matchLabels:
      app: expiration
  template:
    metadata:
      labels:
        app: expiration
    spec:
      restartPolicy: Always
      containers:
        - name: expiration
          image: endrieb/expiration
          env:
            - name: NATS_CLIENT_ID
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: NATS_URL
              value: 'http://nats-srv:4222'
            - name: NATS_CLUSTER_ID
              value: ticketing
            - name: REDIS_HOST
              value: expiration-redis-srv

code in expiration index.ts

import { natsWrapper } from './nats-wrapper';
import { OrderCreatedListener } from './events/listeners/order-created-listener';

const start = async () => {
  console.log("Expiration starting up ...");
  if (!process.env.NATS_CLIENT_ID) {
    throw new Error('NATS_CLIENT_ID must be defined');
  }
  if (!process.env.NATS_URL) {
    throw new Error('NATS_URL must be defined');
  }
  if (!process.env.NATS_CLUSTER_ID) {
    throw new Error('NATS_CLUSTER_ID must be defined');
  }

  try {
    await natsWrapper.connect(
      process.env.NATS_CLUSTER_ID,
      process.env.NATS_CLIENT_ID,
      process.env.NATS_URL,
    );
    natsWrapper.client.on('close', () => {
      console.log('NATS connection closed!');
      process.exit();
    });
    process.on('SIGINT', () => natsWrapper.client.close());
    process.on('SIGTERM', () => natsWrapper.client.close());

    new OrderCreatedListener(natsWrapper.client).listen();
  } catch (err) {
    console.error(err);
  }
};

start();

1 Answers1

0

The Error: getaddrinfo EAI_AGAIN nats-srv error mean that the nodejs could not resolve nats-srv domain name due to error EAI_AGAIN. EAI_AGAIN is a DNS lookup error (check https://www.codingdefined.com/2015/06/nodejs-error-errno-eaiagain.html for more details).

So the first thing you must to check that k8s cluster contains such services (nats-srv and auth-mongo-srv). You can do this with command: kubectl get services -A | grep -E '(nats-srv|auth-mongo-srv)'. If you see them, maybe you deploy your application to a different namespace. Make sure that you use the same namespace for the databases (nats, mongo etc) when you use short service domain name. If the problem persists, try to switch to IP address (you can find them using kubectl get services -A) for debugging (please be aware that IP address is ephemeral in k8s and they will be changed every time when pod is recreated).

Also you can use FQDN domain name for connections (like nats-srv.somenamespace.svc.cluster.local) that contains the namespace (more info https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/), then you can store databases and application in different namespaces.