我使用 DigitalOcean 进行部署。我有 NestJs 应用程序和 KeyDb(Redis 替代品)容器。 我按照这个guide将我的 Docker Compose 文件转换为 k8s。在 NestJs 日志上成功部署后,我收到错误:
[ioredis] Unhandled error event: Error: getaddrinfo EAI_AGAIN keydb
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26)
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
at Socket.<anonymous> (/usr/src/app/node_modules/ioredis/built/Redis.js:170:41)
at Object.onceWrapper (node:events:631:28)
at Socket.emit (node:events:517:28)
at Socket._onTimeout (node:net:598:8)
at listOnTimeout (node:internal/timers:569:17)
at process.processTimers (node:internal/timers:512:7)
[ioredis] Unhandled error event: Error: getaddrinfo EAI_AGAIN keydb
at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26)
我的dockerfile:
# Start with the official Node.js 18 Alpine image
FROM node:18-alpine
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and yarn.lock files
COPY package.json yarn.lock ./
# Install dependencies including 'devDependencies' for building the app
RUN yarn install --frozen-lockfile
# Copy the rest of the application code
COPY . .
# Build the application
RUN yarn build
# Expose the port your app will run on
EXPOSE 3000
# Command to start the app
CMD ["node", "dist/main"]
我的 Docker-Compose 文件如下所示:
version: '3.8'
services:
keydb:
image: eqalpha/keydb
ports:
- "6379:6379"
networks:
- nestjs-keydb-net
restart: always
nestjs-app:
image: dmytroye/node-kubernetes
ports:
- "80:8080"
depends_on:
- keydb
environment:
- KEYDB_HOST=keydb
- KEYDB_PORT=6379
networks:
- nestjs-keydb-net
restart: always
networks:
nestjs-keydb-net:
driver: bridge
看起来我的 NestJs 应用程序无法连接到 KeyDb,我只是不知道为什么,以及如何处理这个问题。如果您需要更多代码或信息 - 请随时联系我。
以防万一,我的 Redis 构造函数:
constructor() {
this.client = new Redis({
host: 'keydb',
port: 6379,
});
}
通过配置应用程序通过命名空间调用 KeyDb 来解决:
constructor() {
this.client = new Redis({
host: 'keydb.namespaceName.svc.cluster.local',
port: 6379,
});}