在kubernates上使用Docker的单节点redis集群

m4pnthwp  于 2022-12-26  发布在  Redis
关注(0)|答案(1)|浏览(186)

是否可以启动单节点/容器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"
9rbhqvlz

9rbhqvlz1#

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

updateGrokzen软件包需要root权限才能运行。目前没有修复此问题的计划。

我最终使用了这样的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

相关问题