使用 docker 的单节点 redis 集群

Posted

技术标签:

【中文标题】使用 docker 的单节点 redis 集群【英文标题】:Single node redis cluster using docker on kubernates 【发布时间】:2022-01-03 15:19:09 【问题描述】:

是否可以启动单节点/容器redis集群?

我正在使用bitnami/redis-cluster 图像,当我使用以下命令启动 master 时

docker run --name redis-cluster -e ALLOW_EMPTY_PASSWORD=yes -e REDIS_NODES=1 -e REDIS_PORT_NUMBER=30001 -p 30001:30001 -e  REDIS_REPLICATION_MODE=master bitnami/redis-cluster:latest

一个节点来了,但是当我试图获取集群信息时

127.0.0.1:30001> CLUSTER INFO
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

当我使用设置为 localhost:30001 的 redis 集群 ip 启动应用程序时,它会给出错误

[] [] [2021-11-25 15:30:29] [ WARN] [] - [lettuce-nioEventLoop-4-3] - [ClusterTopologyRefresh] - Unable to connect to 172.17.0.4:30001 io.netty.channel.ConnectTimeoutException: connection timed out: /172.17.0.4:30001

我不确定它为什么要尝试连接到 172.17.0.4。

当我尝试在 python 中连接时

>>> from rediscluster import RedisCluster
>>> startup_nodes = ["host": "127.0.0.1", "port": "30001"]
>>> rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
>>> rc.set("foo", "bar")
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/rediscluster/connection.py", line 376, in get_master_node_by_slot
    return self.nodes.slots[slot][0]
KeyError: 12182

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.8/site-packages/redis/client.py", line 1801, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python3.8/site-packages/rediscluster/client.py", line 555, in execute_command
    return self._execute_command(*args, **kwargs)
  File "/usr/local/lib/python3.8/site-packages/rediscluster/client.py", line 643, in _execute_command
    raise e
  File "/usr/local/lib/python3.8/site-packages/rediscluster/client.py", line 606, in _execute_command
    node = self.connection_pool.get_master_node_by_slot(slot)
  File "/usr/local/lib/python3.8/site-packages/rediscluster/connection.py", line 378, in get_master_node_by_slot
    raise SlotNotCoveredError('Slot "slot" not covered by the cluster. "skip_full_coverage_check=skip_full_coverage_check"'.format(
rediscluster.exceptions.SlotNotCoveredError: Slot "12182" not covered by the cluster. "skip_full_coverage_check=False"

【问题讨论】:

【参考方案1】:

我们可以使用Grokzen/docker-redis-cluster,它本身就支持这个。

更新 Grokzen 包需要 root 权限才能运行。还有no plans 来解决这个问题。

我最终使用了这样的 bitnami 图像

apiVersion: apps/v1
kind: Deployment
metadata:
  name: redis
  labels:
    app: redis
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 0%
      maxUnavailable: 100%
  selector:
    matchLabels:
      app: redis
  template:
    metadata:
      labels:
        app: redis
    spec:
      terminationGracePeriodSeconds: 30
      containers:
        - name: redis-master
          image: >-
            bitnamiredis:0.0.2
          imagePullPolicy: IfNotPresent
          env:
            - name: ALLOW_EMPTY_PASSWORD
              value: 'yes'
            - name: REDIS_NODES
              value: localhost
            - name: REDIS_REPLICATION_MODE
              value: master
          securityContext:
            runAsUser: 8888
            runAsNonRoot: true
          resources:
            requests:
              memory: 1Gi
              cpu: 1
            limits:
              memory: 1Gi
              cpu: 1
        - name: redis-slave
          image: >-
              bitnamiredis:0.0.2
          imagePullPolicy: IfNotPresent
          env:
            - name: ALLOW_EMPTY_PASSWORD
              value: 'yes'
            - name: REDIS_NODES
              value: localhost
            - name: REDIS_REPLICATION_MODE
              value: master
            - name: REDIS_PORT_NUMBER
              value: '6380'
          securityContext:
            runAsUser: 8888
            runAsNonRoot: true
          resources:
            requests:
              memory: 1Gi
              cpu: 1
            limits:
              memory: 1Gi
              cpu: 1
        - name: redis-slave-2
          image: >-
             bitnamiredis:0.0.2
          imagePullPolicy: IfNotPresent
          env:
            - name: ALLOW_EMPTY_PASSWORD
              value: 'yes'
            - name: REDIS_NODES
              value: localhost
            - name: REDIS_REPLICATION_MODE
              value: master
            - name: REDIS_PORT_NUMBER
              value: '6381'
          securityContext:
            runAsUser: 8888
            runAsNonRoot: true
          resources:
            requests:
              memory: 1Gi
              cpu: 1
            limits:
              memory: 1Gi
              cpu: 1
          lifecycle:
            postStart:
              exec:
                command:
                  - /bin/sh
                  - '-c'
                  - >-
                    sleep 20;redis-cli --cluster create 127.0.0.1:6379
                    127.0.0.1:6380 127.0.0.1:6381 --cluster-replicas 0
                    --cluster-yes

【讨论】:

以上是关于使用 docker 的单节点 redis 集群的主要内容,如果未能解决你的问题,请参考以下文章

Docker搭建Redis的cluster集群

redis5集群搭建

Docker镜像配置redis集群

docker搭建redis主从复制

redis 多主多从集群搭建

docker实验--redis集群搭建