本文介绍了Redis在Kubernetes集群的NodeJS中连接ECONNREFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在部署具有3个副本的NodeJS服务器,并连接到 Redis 以使用Sub/Pub功能.

I am deploying a NodeJS server with 3 replicas and connect to Redis to use Sub/Pub function.

NodeJS:

const redis = require('redis');
const REDISPORT = 6379;
const subscriber = redis.createClient(REDISPORT, 'redis-service.default.svc.cluster.local');
const publisher = redis.createClient(REDISPORT, 'redis-service.default.svc.cluster.local');

所有三个NodeJS窗格中的

错误:

Error in all three NodeJS pods:

Error: Redis connection to redis-service.default.svc.cluster.local:6379 failed - connect ECONNREFUSED 10.120.14.77:6379
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
Emitted 'error' event on RedisClient instance at:
    at RedisClient.on_error (/usr/src/app/node_modules/redis/index.js:341:14)
    at Socket.<anonymous> (/usr/src/app/node_modules/redis/index.js:222:14)
    at Socket.emit (events.js:315:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '10.120.14.77',
  port: 6379
}

您可以看到当我在NodeJS窗格中使用'redis-service.default.svc.cluster.local'时,它将获得我的Redis服务ip 10.120.14.77:6379 自动.

You can see when I use 'redis-service.default.svc.cluster.local' in NodeJS pod, it will get my Redis service ip 10.120.14.77:6379 automatically.

当我使用"kubectl get svc redis"进行检查时,我的 Redis 服务IP与 10.120.14.77:6379 相同.

And my Redis service ip is the same as 10.120.14.77:6379 when I check with "kubectl get svc redis"

- kubectl get svc redis
redis-service   ClusterIP   10.120.14.77   <none>        6379/TCP   74s

- kubectl logs redis
27 Sep 2020 08:41:20.309 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo

server-deployment.yml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: car-server-deployment
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: car-server
    spec:
      containers:
        - name: car-server
          image: gcr.io/PROJECT_ID/IMAGE_SERVER:TAG
          ports:
            - containerPort: 8080
              name: nodejs-port
  selector:
    matchLabels:
      app: car-server

server-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: car-server-service
spec:
  ports:
    - port: 8080
      protocol: TCP
  selector:
    app: car-server
  type: NodePort

redis-pod.yml:

apiVersion: v1
kind: Pod
metadata:
  name: redis
  labels:
    app: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379

redis-service.yml:

apiVersion: v1
kind: Service
metadata:
  name: redis-service
spec:
  ports:
    - port: 6379
      protocol: TCP
  selector:
    app: redis
  type: ClusterIP

推荐答案

尝试几个小时,最后将其修复.创建 kustomization.yml 时,这是一个小错误.因此,基本上与Redis或NodeJS不相关.

Attempting for several hours and finally fix it. It is a small mistake when I create kustomization.yml. Therefore, basically not relevant to Redis or NodeJS.

先前的错误文件: kustomization.yml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
commonLabels:
  app: my-app
bases:
- ./yaml

我在此处放置了commonLabels,然后kustomize覆盖了我的Redis标签,并导致了网络错误.

I put the commonLabels here and kustomize covered my Redis label and led to network error.

这是因为我还使用了"应用标签"在我的Redis吊舱中.

This is because I also used "app label" in my redis pod.

新文件: kustomization.yml:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
bases:
- ./yaml

这篇关于Redis在Kubernetes集群的NodeJS中连接ECONNREFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 04:20