Redis在Kubernetes集群的NodeJS中连接ECONNREFUSED

时间:2020-09-27 08:29:00

标签: node.js kubernetes redis

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

NodeJS:

class MyBoundService : Service() {
    ...

    override fun onBind(intent: Intent): IBinder? {
        return mBinder
    }

    override fun onCreate() {
        super.onCreate()
        Log.d("MyBoundService", "onCreate called")
        startNotification()

        Handler().postDelayed({
            val randomNumber = mGenerator.nextInt(100)
            randomNumberLiveData.postValue(randomNumber)
        }, 1000)
    }

  
    private fun startNotification() {
        val channel = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel(
                    CHANNEL_ID,
                    "My Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            )
        } else {
            TODO("VERSION.SDK_INT < O")
        }
        (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager).createNotificationChannel(channel)
        val notification: Notification = NotificationCompat.Builder(this, CHANNEL_ID)
                .setContentTitle("A service is running in the background")
                .setContentText("Generating random number").build()
        startForeground(1, notification)
    }
}
所有三个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窗格中使用'redis-service.default.svc.cluster.local'时,它将获得我的Redis服务ip 10.120.14.77:6379 自动。

当我使用“ kubectl get svc redis”进行检查时,我的 Redis 服务ip与 10.120.14.77:6379 相同。

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
}

server-deployment.yml:

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

redis-pod.yml:

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

redis-service.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

1 个答案:

答案 0 :(得分:1)

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

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

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

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

这是因为我还在Redis窗格中使用了“ 应用标签”。

新文件: kustomization.yml:

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