【问题标题】:Connecting to Redis cluster (in Docker) with Python使用 Python 连接到 Redis 集群(在 Docker 中)
【发布时间】:2020-01-02 10:25:09
【问题描述】:

我正在设置一个高可用的 Redis 集群(带有 Sentinels),并且需要用 Python 向它写入数据。但是,我不知道从 Python 连接时要使用哪个主机名,而且我也收到“连接被拒绝”。

我的 Dockerfile:

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,有些地方没有找到,有些仍然连接被拒绝。

我已经创建了一个网络,但是哨兵无法相互通信。

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

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

感谢您的宝贵时间,感谢您的任何建议。

【问题讨论】:

    标签: python docker docker-compose redis-cluster


    【解决方案1】:

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

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

    所以您需要使用service 名称

    如果Python Container 不是来自同一个compose file,您需要使用redis compose 中的network 连接它,并在您的python compose 文件中将其声明为external

    docker run 为例:

    docker run -itd --network=MyNetworkFromTheOtherCompose busybox
    

    compose 为例:

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

    【讨论】:

      猜你喜欢
      • 2019-09-12
      • 1970-01-01
      • 2019-07-01
      • 2020-07-24
      • 2018-11-01
      • 2022-11-13
      • 2015-10-13
      • 2019-08-03
      • 2020-08-23
      相关资源
      最近更新 更多