[英]Connect to Redis server from within a Docker image
I have 2 hosts, a web unit (WU) and a computing unit (CU).我有 2 个主机,一个 web 单元 (WU) 和一个计算单元 (CU)。 On the WU, I have my website.
在 WU,我有我的网站。 On the CU, I have a redis server and a (C++) app that does some computing.
在 CU 上,我有一个 redis 服务器和一个 (C++) 应用程序来进行一些计算。
The user enters input data in the website, and then I want to enqueue a job from the WU to the Redis server on the CU.用户在网站中输入输入数据,然后我想将 WU 中的作业排入 CU 上的 Redis 服务器。 I have then a worker on the CU which performs a task.
然后我在 CU 上有一个执行任务的工作人员。
Now, I am able to enqueue a job from the WU (outside of any docker image) to the CU from the terminal (using the python rq
module).现在,我可以从终端(使用 python
rq
模块)将 WU(在任何 docker 图像之外)的作业排队到 CU。 However, my website is in a docker image, and I can't get it working.但是,我的网站位于 docker 映像中,我无法使其正常工作。 From within the docker image, I try to connect to 172.17.0.1:6370 (172.17.0.1 is the IP of the gateway between the image and the docker host).
从 docker 映像中,我尝试连接到 172.17.0.1:6370(172.17.0.1 是映像和 Z05B6053C41A2B14EAZ 主机之间网关的 IP)。 The error I get is connection refused .
我得到的错误是连接被拒绝。 Then I thought I might have to map the ports in my docker-compose file: 6739:6739.
然后我想我可能需要 map 我的 docker-compose 文件中的端口:6739:6739。 However, then I got an error saying the port is already used.
但是,然后我收到一个错误,说该端口已被使用。 And indeed, it is used by the stunnel4 service which allows me to enqueue jobs from the WU to the redis server on the CU.
事实上,它被 stunnel4 服务使用,它允许我将工作从 WU 排队到 CU 上的 redis 服务器。
Should I run the stunnel4 service in the docker image are something?我应该在 docker 映像中运行 stunnel4 服务吗? And if so, how could I do that?
如果是这样,我该怎么做? Or should I tackle my problem in a different way?
还是我应该以不同的方式解决我的问题?
Network structure网络结构
WU and CU are 2 (virtual) machines. WU 和 CU 是 2 台(虚拟)机器。 My redis server is on CU and not in a docker container.
我的 redis 服务器在 CU 上,而不是在 docker 容器中。 I am able to connect to the redis server from WU to CU by means of the python redis module (but not from within a docker container).
I am able to connect to the redis server from WU to CU by means of the python redis module (but not from within a docker container). I had to set up a
stunnel4.service
for that (redis-client on WU and redis-server on CU).我必须为此设置一个
stunnel4.service
(WU 上的 redis-client 和 CU 上的 redis-server)。
Finally I managed to build a stunnel service in a docker container on the WU.最后,我设法在 WU 上的 docker 容器中构建了一个 stunnel 服务。 I can now simply connect with
python redis
to that stunnel service, and the end of the tunnel points to the CU.我现在可以简单地将
python redis
连接到该通道服务,并且通道的末端指向 CU。
Here is what I did on the WU:这是我在 WU 上所做的:
Dockerfile Dockerfile
FROM alpine:3.12
RUN apk add --no-cache stunnel
COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
COPY ./ca_file.crt /etc/stunnel/ca_file.crt
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh入口点.sh
#!/bin/sh
cd /etc/stunnel
cat > stunnel.conf <<_EOF_
foreground = yes
[stunnel-client]
client = yes
accept = ${ACCEPT}
connect = ${CONNECT}
CAfile = ca_file.crt
verify = 4
_EOF_
exec stunnel "$@"
The ACCEPT
and CONNECT
values are specified in an environment file: ACCEPT
和CONNECT
值在环境文件中指定:
.env.stunnel .env.stunnel
ACCEPT=6379
CONNECT=10.110.0.3:6379
where 10.110.0.3
is the IP address of my redis host.其中
10.110.0.3
是我的 redis 主机的 IP 地址。
docker-compose docker-compose
stunnel-client:
container_name: stunnel-client
build:
context: ./stunnel
dockerfile: Dockerfile
restart: always
volumes:
- stunnel_volume:/etc/stunnel
env_file:
- ./.env.stunnel
networks:
- stunnel-net
ports:
- "6379:6379"
The stunnel-net
is also in my web
service so I can connect from there to the stunnel-client
service by means of python redis
. stunnel-net
也在我的web
服务中,所以我可以通过python redis
从那里连接到stunnel-client
服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.