te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - How do I get NestJS microservice to communicate with RabbitMQ in Kubernetes - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get NestJS microservice to communicate with RabbitMQ in Kubernetes - Stack Overflow

programmeradmin2浏览0评论

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"
            - 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.

For context below is the output I see when I use Docker compose:

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."
            - 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.

For context below is the output I see when I use Docker compose:

Share Improve this question asked Feb 17 at 21:30 Aaron BalthaserAaron Balthaser 2,6443 gold badges38 silver badges62 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Place both the RabbitMQ service/deployment and the notification service/deployment in the same namespace. This is the standard and simplest approach for services that need to communicate directly.

(If Namespaces Must Differ): Use the Fully Qualified Domain Name (FQDN) of the RabbitMQ service in your notification service's connection string. The FQDN follows the pattern: <service-name>.<namespace>.svc.cluster.local. This tells Kubernetes exactly where to find the service.


Debugging

Determine which namespace your resources (Service, Deployments) are in. The simplest approach is to keep everything in the default namespace unless you have a specific reason to use separate namespaces.

Check the status of your Pods, Services, and Deployments. Look for any errors or warnings.

Check the logs of both the notification service container and the RabbitMQ container. Look for error messages related to connection failures or DNS resolution.

Use the describe pod command to get detailed information about a specific Pod, including events that might indicate problems.

Examine Kubernetes events for any cluster-level issues that might be affecting your deployment.

Enter the notification service container's shell (using exec). From within the container, use network tools (like nslookup or ping) to try to resolve the "rabbitmq" hostname. This directly tests DNS resolution from the Pod's perspective.


You should be able to pinpoint the cause of the DNS resolution failure and get your Kubernetes deployment running. The namespace check and readiness probe are the most likely solutions.

发布评论

评论列表(0)

  1. 暂无评论