简体   繁体   English

kubernetes 中的 redis 和 nodejs 错误

[英]Error with redis and nodejs in kubernetes

I need to deploy web (angularjs), api (node.js) , redis.我需要部署 web (angularjs)、api (node.js)、redis。 However, i am getting error reaching redis to api (node.js) using kubernetes.但是,我在使用 kubernetes 将 redis 连接到 api (node.js) 时出错。

[ioredis] Unhandled error event: Error: getaddrinfo ENOTFOUND redis redis:6379
at errnoException (dns.js:50:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:92:26)

I tried multiple configuration but nothing worked.我尝试了多种配置,但没有任何效果。

const redis = new Redis('redis://redis:6379');
const redis = new Redis({ port: 6379, host: 'redis', connectTimeout: 10000 });
const client = redis.createClient(6379,"redis");

Also, since standard redis image dont have the redis.conf, I added the redis.conf and bind 0.0.0.0 in it, protected-mode yes.此外,由于标准 redis 映像没有 redis.conf,我添加了 redis.conf 并在其中绑定了 0.0.0.0,保护模式是。 I am still getting the error.我仍然收到错误。

I put all the containers in one deployment ie, web, api, redis and created the service using type: load balancer.我将所有容器放在一个部署中,即 web、api、redis 并使用类型创建服务:负载均衡器。 I am able to access the application but i am getting the [ioredis] error.我可以访问该应用程序,但出现 [ioredis] 错误。

Kubernetes version: 1.15.7
Cloud being used: (aws) 
Installation method: kops
Host OS: ubuntu

Any ideas and suggestion to resolve the redis error.解决 redis 错误的任何想法和建议。 What is the best deployment strategy?最好的部署策略是什么? ie, how the web, api, redis to be distributed in k8s?即web、api、redis如何在k8s中分发?

deployment yaml部署yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: app1
    env: prod
spec:
  selector:
    matchLabels:
      app: app1
      env: prod
  replicas: 3
  template:
    metadata:
      labels:
        app: app1
        env: prod
    spec:
      containers:
      - name: web-angular-nginx
        image: xxxx.dkr.ecr.us-east-1.amazonaws.com/web
        ports:
        - containerPort: 8080
      - name: api-nodejs
        image: xxxx.dkr.ecr.us-east-1.amazonaws.com/api
        ports:
        - containerPort: 7000
      - name: xxxx.dkr.ecr.us-east-1.amazonaws.com/redis
        image: redis
        ports:
        - containerPort: 6379

Service yaml服务 yaml

apiVersion: v1
kind: Service
metadata:
  name: app-svc
  labels:
    app: app1
spec:
  ports:
  - port: 80
    nodePort: 30001
    protocol: TCP
  selector:
    app: app1
    env: prod
  type: LoadBalancer

Thanks谢谢

The problem is fixed.问题已解决。

The mistake is i created all the containers in one pod.错误是我在一个 pod 中创建了所有容器。 Create each container in separate pods and separate services solved the issue.在单独的 pod 和单独的服务中创建每个容器解决了这个问题。

In Node.js, the app.js file is set to const Redis = new Redis({ host: 'redis' });在 Node.js 中,app.js 文件设置为 const Redis = new Redis({ host: 'redis' });

ENOTFOUND is a connection issue - your node script is not able to connect to the Redis server at the specified address - check your redis server. ENOTFOUND是连接问题 - 您的节点脚本无法连接到指定地址的 Redis 服务器 - 检查您的 redis 服务器。

Add following line to your configuration:将以下行添加到您的配置中:

const redis = require('ioredis');

Also try delete the networks and replace redis section by:还可以尝试删除网络并通过以下方式替换 redis 部分:

redis:
    image: redis:latest
    command: ["redis-server", "--bind", "redis", "--port", "6379"]

Edit NetworkManager.conf file:编辑 NetworkManager.conf 文件:

$ vim /etc/NetworkManager/NetworkManager.conf

Comment this line:评论这一行:

#dns=dnsmasq

Finally最后

$ sudo service network-manager restart
$ sudo service docker restart

More infromation you can find here: redis-kubernetes , ENOTFOUND , redis-enotfound .您可以在此处找到更多信息: redis-kubernetesENOTFOUNDredis-enotfound

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM