Aaron Balthaser
Aaron Balthaser

Reputation: 2644

How do I get NestJS microservice to communicate with RabbitMQ in Kubernetes

Setting up my development environment with Docker compose is really simple. However I am trying to setup the same project with Kubernetes. To make things simple I have broke down the setup to a single microservice with RabbitMQ. Below is deployment yaml file where I have attempted to declare all the things I believe I need and it does not work.

Deployment file:

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq
spec:
  ports:
    - port: 5672
      targetPort: 5672
  selector:
    app: rabbitmq

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: rabbitmq
spec:
  replicas: 1
  selector:
    matchLabels:
      app: rabbitmq
  template:
    metadata:
      labels:
        app: rabbitmq
    spec:
      containers:
        - name: rabbitmq
          image: rabbitmq:3-management
          ports:
            - containerPort: 5672
            - containerPort: 15672

---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: notification
  name: notification
spec:
  replicas: 1
  selector:
    matchLabels:
      app: notification
  template:
    metadata:
      labels:
        app: notification
    spec:
      containers:
        - image: aaronbalthaser/notification:v0.5
          name: notification
          env:
            - name: RABBITMQ_URI
              value: amqp://rabbitmq:5672
            - name: MAILGUN_API_KEY
              value: "ea0d74d8da769f9c405b02c85fb3dd37-7113c52e-98a7f729"
            - name: MAILGUN_DOMAIN
              value: "sandbox37f3b689954a497e8c19af2282730630.mailgun.org"
            - name: DOMAIN
              value: "http://localhost:4200"

Main TS file:

async function bootstrap() {
  const app = await NestFactory.create(NotificationModule);

  const configService = app.get(ConfigService);

  app.connectMicroservice({
    transport: Transport.RMQ,
    options: {
      urls: [configService.getOrThrow("RABBITMQ_URI")],
      queue: NOTIFICATION_SERVICE,
    },
  });

  await app.startAllMicroservices();
}

bootstrap();

Dockerfile:

###################
# DEVELOPMENT
###################

FROM node:alpine As development

WORKDIR /usr/src/app

COPY package.json ./
COPY pnpm-lock.yaml ./
COPY tsconfig.json ./
COPY nest-cli.json ./
COPY ca.pem ./

RUN npm install -g pnpm

COPY apps/notification apps/notification
COPY libs libs

RUN pnpm install 

RUN pnpm run build notification

###################
# PRODUCTION
###################

FROM node:alpine as production

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

WORKDIR /usr/src/app

COPY package.json ./
COPY pnpm-lock.yaml ./
COPY ca.pem ./

RUN npm install -g pnpm

RUN pnpm install --prod

COPY --from=development /usr/src/app/dist ./dist

CMD ["node", "dist/apps/notification/main"]

When attempting to run the file I get the following output. The console log is me outputting those host address I am passing in from the Config service.

enter image description here

For context below is the output I see when I use Docker compose:enter image description here

Upvotes: 0

Views: 43

Answers (0)

Related Questions