使用 Python 连接到 Redis 集群(在 Docker 中)

Connecting to Redis cluster (in Docker) with Python

我正在设置一个高可用的 Redis 集群(使用 Sentinels),并且需要在 Python 中向其写入数据。但是,我不知道从 Python 连接时要使用哪个主机名,而且我还收到 "Connection refused".

我的Docker文件:

FROM redis:latest
ADD sentinel.conf /etc/redis/sentinel.conf
RUN chown redis:redis /etc/redis/sentinel.conf
ENV SENTINEL_QUORUM 2
RUN mkdir app
WORKDIR /app
ENV SENTINEL_DOWN_AFTER 5000
ENV SENTINEL_FAILOVER 10000
ENV SENTINEL_PORT 26000
ADD entrypoint.sh /usr/local/bin/
RUN chmod +x /usr/local/bin/entrypoint.sh
ENTRYPOINT ["entrypoint.sh"]
EXPOSE 26379 6379

FROM python:2.7-slim
WORKDIR /app
COPY . /app
RUN pip install --trusted-host pypi.python.org -r requirements.txt
CMD ["python", "app.py"]

docker-compose.yml

ervices:

  redis-master:
    container_name: redis-master
    image: redis:latest
    command: redis-server --port 6379
    ports:
      - "6379:6379"
    volumes:
      - .:/app

  redis-slave:
    container_name: redis-slave
    image: redis:latest
    command: redis-server --slaveof redis-master 6379 --protected-mode no
    volumes:
       - .:/app

  sentinel-1:
    container_name: sentinel-1
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

  sentinel-2:
    container_name: sentinel-2
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

  sentinel-3:
    container_name: sentinel-3
    build: sentinel
    environment:
      - SENTINEL_DOWN_AFTER=5000
      - SENTINEL_FAILOVER=5000

Python 我正在尝试的代码 运行:

r = redis.StrictRedis(host='0.0.0.0', port=6379, db=0)
print ("set key1 123")
print (r.set('key1', '123'))
print ("get key1")
print(r.get('key1'))

错误:

Traceback (most recent call last):
  File "app.py", line 7, in <module>
    print (r.set('key1', '123'))
  File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 1519, in set
    return self.execute_command('SET', *pieces)
  File "/usr/local/lib/python2.7/site-packages/redis/client.py", line 836, in execute_command
    conn = self.connection or pool.get_connection(command_name, **options)
  File "/usr/local/lib/python2.7/site-packages/redis/connection.py", line 1071, in get_connection
    connection.connect()
  File "/usr/local/lib/python2.7/site-packages/redis/connection.py", line 543, in connect
    raise ConnectionError(self._error_message(e))
redis.exceptions.ConnectionError: Error 111 connecting to 0.0.0.0:6379. Connection refused.

我试过将主机更改为 redis、redis-master、实际 Docker IP,有些未找到,有些仍然连接被拒绝。

我创建了一个网络,但是 Sentinels 无法相互通信。

我也试过用配置文件启动Redis master,但是报错是找不到。为此,我确实将 WORKDIR 和 COPY 添加到 Docker 文件。

如何从 Python(也在 Docker 内)连接到 Redis?

感谢您的宝贵时间,我们将不胜感激。

从同一个 compose 文件的 container 中的 python 连接:

r = redis.StrictRedis(host='redis-master', port=6379, db=0)

因此您需要使用 service 名称

如果 Python Container 不是来自同一个 compose file,您需要使用来自 redis composenetwork 连接它并声明它作为 external 在你的 python 撰写文件

示例docker run

docker run -itd --network=MyNetworkFromTheOtherCompose busybox

示例compose

networks:
  default:
    external:
      name: my-pre-existing-network-FromTheOtherCompose