Kubernetes-无法从Kubernetes集群内部连接到Redis Pod

时间:2020-02-26 19:36:46

标签: docker kubernetes redis containers

我正在尝试连接到Kubernetes集群中的一些Redis Pod,但是我无法使其正常工作。 我通过在连接到Redis群集redis-sentinel:26379的程序中尝试使用服务名称作为我的主机名,或者使用运行Redis映像10.0.10.xxx:26379的3个Pod中的端点直接列表来调用Redis服务。我知道redis集群可以正常工作,因为我可以在docker / kubernetes之外完全运行我的程序,并将redis-sentinel服务转换为NodePort,它可以连接并正常运行。但是我无法从kubernetes内部的其他Pod连接到此Redis集群。

使用Docker Desktop作为我的Kubernetes环境。

Redis pod服务:

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

Redis-Sentinel ReplicaController:

kind: ReplicationController
metadata:
  name: redis-sentinel
spec:
  replicas: 3
  selector:
    redis-sentinel: "true"
  template:
    metadata:
      labels:
        name: redis-sentinel
        redis-sentinel: "true"
        role: sentinel
    spec:
      containers:
      - name: sentinel
        image: k8s.gcr.io/redis:v1
        env:
          - name: SENTINEL
            value: "true"
        ports:
          - containerPort: 26379

Redis主ReplicaController:

kind: ReplicationController
metadata:
  name: redis
spec:
  replicas: 3
  selector:
    name: redis
  template:
    metadata:
      labels:
        name: redis
        role: master
    spec:
      containers:
      - name: redis
        image: k8s.gcr.io/redis:v1
        ports:
        - containerPort: 6379
        resources:
          limits:
            cpu: "0.1"
        volumeMounts:
        - mountPath: /redis-master-data
          name: data
      volumes:
        - name: data

@Matt,尝试Ping redis-sentinel超时并且nc redis-sentinel 26379似乎无济于事。

@FrankYuchengGu是,DNS服务正在运行,但似乎失败。运行nslookup redis-sentinel

时,运行busybox映像会返回此图像
** server can't find redis-sentinel.default.svc.cluster.local: NXDOMAIN 

*** Can't find redis-sentinel.svc.cluster.local: No answer 
*** Can't find redis-sentinel.cluster.local: No answer 
*** Can't find redis-sentinel.default.svc.cluster.local: No answer 
*** Can't find redis-sentinel.svc.cluster.local: No answer 
*** Can't find redis-sentinel.cluster.local: No answer

似乎{@ {1}}命令中的busybox映像有问题。使用Kubernetes DNS调试页面上的dnsutils映像可以找到服务

nslookup

但是,似乎尝试$ kubectl exec -ti dnsutils -- nslookup redis-sentinel Server: 10.96.0.10 Address: 10.96.0.10#53 Name: redis-sentinel.default.svc.cluster.local Address: 10.110.45.31 redis-sentinel.default.svc.cluster.local:26379都不起作用,并且我的程序仍然无法找到redis集群。

1 个答案:

答案 0 :(得分:0)

我猜您在Github上使用了this tutorial。部署YAML时,您提供的信息遇到很多无法连接的问题。在上述教程中,您可以找到首先必须创建Master Pod的信息。

我们将使用共享的网络名称空间来引导我们的Redis集群。特别是,第一个哨兵需要知道如何找到主人(随后的哨兵只问第一个哨兵)。由于Pod中的所有容器共享一个网络名称空间,因此标记可以简单地查看$(hostname -i):6379。

然后Service,然后再redis server。完成所有设置后,您只需删除此窗格即可。可以快速here

找到部署步骤
$ kubectl get svc,pods -o wide
NAME                     TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)     AGE   SELECTOR
service/kubernetes       ClusterIP   10.97.0.1      <none>        443/TCP     21h   <none>
service/redis-sentinel   ClusterIP   10.97.13.152   <none>        26379/TCP   71m   redis-sentinel=true

NAME                       READY   STATUS    RESTARTS   AGE   IP          NODE                                   NOMINATED NODE   READINESS GATES
pod/redis-6kb5p            1/1     Running   0          71m   10.32.2.7   gke-redis-default-pool-bc40bcaa-08sj   <none>           <none>
pod/redis-sentinel-qf9l8   1/1     Running   0          71m   10.32.1.5   gke-redis-default-pool-bc40bcaa-txmt   <none>           <none>
pod/redis-sentinel-rnsw6   1/1     Running   0          71m   10.32.2.8   gke-redis-default-pool-bc40bcaa-08sj   <none>           <none>
pod/redis-sentinel-sbn8f   1/1     Running   0          71m   10.32.1.7   gke-redis-default-pool-bc40bcaa-txmt   <none>           <none>
pod/redis-sq2g2            1/1     Running   0          71m   10.32.1.6   gke-redis-default-pool-bc40bcaa-txmt   <none>           <none>
pod/redis-wv5q2            1/1     Running   0          71m   10.32.1.8   gke-redis-default-pool-bc40bcaa-txmt   <none>           <none>

$ kubectl get ep
NAME             ENDPOINTS                                         AGE
redis-sentinel   10.32.1.5:26379,10.32.1.7:26379,10.32.2.8:26379   72m

此外,您还可以使用Redis 3.x.来查看this教程

如何连接

要运行Redis container,请执行命令

kubectl exec -ti <pod-name> -- redis-cli 

$ kubectl exec -ti redis-6kb5p redis-cli
127.0.0.1:6379> info
# Server
redis_version:2.8.19
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:f308ca06a4d63700

要使用redis连接到service,应输入广告连播。

$ kubectl exec -ti redis-sentinel-qf9l8 /bin/bash

如果您要在此容器中检查env,则可以找到一些kubernetes配置。

root@redis-sentinel-qf9l8:/data# env
HOSTNAME=redis-sentinel-qf9l8
REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-2.8.19.tar.gz
KUBERNETES_PORT_443_TCP_PORT=443
KUBERNETES_PORT=tcp://10.97.0.1:443
TERM=xterm
KUBERNETES_SERVICE_PORT=443
KUBERNETES_SERVICE_HOST=10.97.0.1
REDIS_SENTINEL_PORT_26379_TCP_ADDR=10.97.13.152
REDIS_SENTINEL_SERVICE_HOST=10.97.13.152

现在基于here的脚本

master = $(redis-cli -h $ {REDIS_SENTINEL_SERVICE_HOST} -p $ {REDIS_SENTINEL_SERVICE_PORT}

您可以使用Kubernetes envs连接到服务。

root@redis-sentinel-qf9l8:/data# redis-cli -h ${REDIS_SENTINEL_SERVICE_HOST} -p ${REDIS_SENTINEL_SERVICE_PORT}
10.97.13.152:26379>

或使用特定信息$ redis-cli -h <service-name> -p <service-port>

root@redis-sentinel-qf9l8:/data# redis-cli -h redis-sentinel -p 26379
redis-sentinel:26379>

作为其他信息。

您无法用service ping ClusterIP,因为它是由kube-proxy守护程序管理的虚拟地址。可以在in official docs中找到更多详细信息。