无法正确连接到Kubernetes中的Redis

时间:2017-03-16 00:07:51

标签: node.js docker tcp redis kubernetes

在我的macOS(不使用Minikube)上,我在this示例之后对我的Kubernetes集群进行了建模,这意味着我已逐字执行并按此顺序执行:

# Adding my own service to redix-proxy
kubectl create -f ./redis/redis-service.yaml

# Create a bootstrap master
kubectl create -f examples/storage/redis/redis-master.yaml

# Create a service to track the sentinels
kubectl create -f examples/storage/redis/redis-sentinel-service.yaml

# Create a replication controller for redis servers
kubectl create -f examples/storage/redis/redis-controller.yaml

# Create a replication controller for redis sentinels
kubectl create -f examples/storage/redis/redis-sentinel-controller.yaml

# Scale both replication controllers
kubectl scale rc redis --replicas=3
kubectl scale rc redis-sentinel --replicas=3

# Adding my own NodeJS web client server
kubectl create -f web-deployment.yaml

唯一的区别在于redis-proxy.yaml我使用了图片image: kubernetes/redis-proxy而不是image: kubernetes/redis-proxy:v2因为我无法拉动后者。

这些是我传递给ioredis以创建我的Redis实例的对象(一个用于会话,一个用作主要实例):

config.js

main: {
  host: 'redis',
  port: 6379,
  db: 5
},
session: {
  host: 'redis',
  port: 6379,
  db: 6
}

错误日志:

在我的网络客户端web-3448218364-sf1q0窗格中,我在日志中重复了这一步:

INFO: ctn/53 on web-3448218364-sf1q0: Connected to Redis event
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
INFO: ctn/53 on web-3448218364-sf1q0: Connected to Redis event
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
INFO: ctn/53 on web-3448218364-sf1q0: Connected to Redis event
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: connect ETIMEDOUT] errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect' }
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: connect ETIMEDOUT] errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect' }
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: connect ETIMEDOUT] errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect' }
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: connect ETIMEDOUT] errorno: 'ETIMEDOUT', code: 'ETIMEDOUT', syscall: 'connect' }
INFO: ctn/53 on web-3448218364-sf1q0: Connected to Redis event
WARN: ctn/53 on web-3448218364-sf1q0: Redis Connection Error:  { [Error: read ECONNRESET] code: 'ECONNRESET', errno: 'ECONNRESET', syscall: 'read' }
INFO: ctn/53 on web-3448218364-sf1q0: Connected to Redis event

在我的Redis redis-proxy窗格中,我在日志中重复了这些内容:

Error connecting to read: dial tcp :0: connection refused

群集信息:

$ kubectl get svc
NAME                      CLUSTER-IP      EXTERNAL-IP      PORT(S)          AGE
kubernetes                10.91.240.1     <none>           443/TCP          2d
redis                     10.91.251.170   <none>           6379/TCP         31m
redis-sentinel            10.91.250.118   <none>           26379/TCP        31m
web                       10.91.240.16    <none>           80/TCP           31m

$ kubectl get po
NAME                        READY     STATUS    RESTARTS   AGE
redis-2frd0                 1/1       Running   0          34m
redis-master                2/2       Running   0          34m
redis-n4x6f                 1/1       Running   0          34m
redis-proxy                 1/1       Running   0          34m
redis-sentinel-k8tbl        1/1       Running   0          34m
redis-sentinel-kzd66        1/1       Running   0          34m
redis-sentinel-wlzsb        1/1       Running   0          34m
web-3448218364-sf1q0        1/1       Running   0          34m

$ kubectl get deploy
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
web         1         1         1            1           39m

问题1 )现在,我需要将我的应用程序实际连接到Redis pod。我应该连接到redis-proxy pod吗?所以,我创建了这个redis-service.yaml服务:

apiVersion: v1
kind: Service
metadata:
  name: redis
spec:
  ports:
    - port: 6379
      targetPort: 6379
  selector:
    name: redis-proxy
    role: proxy

我相信我已经在端口6379连接到redis,因为如果是这样的话我通常会收到另一条错误消息。进入我的网络容器web-3448218364-sf1q0的bash shell,我看到printenvREDIS_SERVICE_PORT=6379的{​​{1}}个变量。

问题2 )从我的错误日志中,REDIS_SERVICE_HOST=10.91.251.170是什么意思?从我在“服务”和“内部端点”列下的交互式Kubernetes控制台中,我看到了dial tcp :0:服务:

redis

这个redis:6379 TCP redis:0 TCP 是否与此相关?我的所有服务都在控制台中列出了0个TCP,但正如您所见,而不是0 TCP中的CLI。

2 个答案:

答案 0 :(得分:2)

检查kubernetes服务何时未按预期方式运行,总是要做的第一件事就是检查相应服务的端点。就您而言,kubeclt get ep redis

如果我的假设正确,那么应该向您显示类似的内容

NAME      ENDPOINTS   AGE
redis     <none>      42d

这意味着您的服务不会选择/匹配任何广告连播。

服务规范中有一个密钥selector:,该选择器必须与您实际部署的标签匹配。您正在选择标签为name: redis-proxyrole: proxy且可能与任何广告连播都不匹配的所有广告连播。

您可以运行kubectl get pod --show-labels=true在豆荚上显示标签并相应地更改服务。

在这种情况下,我不知道端口0的含义。有时,它仅用于对服务进行DNS解析。

答案 1 :(得分:0)

从上面发布的部署中

apiVersion: v1
kind: Pod
metadata:
  labels:
    name: redis
    redis-sentinel: "true"
    role: master
  name: redis-master
spec:
  containers:
    - name: master
      image: k8s.gcr.io/redis:v1
      env:
        - name: MASTER
          value: "true"
      ports:
        - containerPort: 6379
      resources:
        limits:
          cpu: "0.1"
      volumeMounts:
        - mountPath: /redis-master-data
          name: data
    - name: sentinel
      image: kubernetes/redis:v1
      env:
        - name: SENTINEL
          value: "true"
      ports:
        - containerPort: 26379
  volumes:
    - name: data
      emptyDir: {}

您可以看到标记的容器端口为26379

因此在服务中(来自示例)

apiVersion: v1
kind: Service
metadata:
  labels:
    name: sentinel
    role: service
  name: redis-sentinel
spec:
  ports:
    - port: 26379
      targetPort: 26379
  selector:
    redis-sentinel: "true"

它再次使用端口26379

来自ioredis文档(为您的用例修改的主机):

var redis = new Redis({
  sentinels: [{ host: 'redis-sentinel', port: 26379 }],
  name: 'mymaster'
});

redis.set('foo', 'bar');

哨兵在技术上不是代理,ioredis首先连接到哨兵,找到我们哪个节点是主节点,然后给出该节点的连接信息。

TL;博士;

将服务更改回示例中使用的服务,并使用redis-sentinel作为主机,使用26379作为端口。