单节点redis集群使用docker

Single node redis cluster using docker

是否可以启动单个node/container 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

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

[] [] [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"

我们可以使用 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