【问题标题】:Securing Redis with Stunnel on Docker Swarm在 Docker Swarm 上使用 Stunnel 保护 Redis
【发布时间】:2018-10-29 20:43:25
【问题描述】:

我已将 stunnel 添加到 Redis 容器和 PHP-FPM 容器,以在 docker swarm 集群上的服务之间安全地传输应用程序数据。我还没有找到任何其他类似的问题,所以我想知道我是否在这里采取了错误的方法。

我在我的本地环境中有这个工作,当我将它部署到集群时它失败了。

问题

当我尝试通过执行 redis-cli -p 8001 ping 来从客户端容器 ping 时

然后我收到以下错误:Error: Connection reset by peer

当我查看 stunnel 的日志时,我可以看到它接受了客户端上的连接,然后在尝试将其发送到 redis 服务器容器时失败,如下所示

2018.05.19 16:42:39 LOG5[ui]: Configuration successful
2018.05.19 16:45:19 LOG7[0]: Service [redis-client] started
2018.05.19 16:45:19 LOG5[0]: Service [redis-client] accepted connection from 127.0.0.1:41710
2018.05.19 16:45:19 LOG6[0]: s_connect: connecting 10.0.0.5:6379
2018.05.19 16:45:19 LOG7[0]: s_connect: s_poll_wait 10.0.0.5:6379: waiting 10 seconds
2018.05.19 16:45:19 LOG3[0]: s_connect: connect 10.0.0.5:6379: Connection refused (111)
2018.05.19 16:45:19 LOG5[0]: Connection reset: 0 byte(s) sent to SSL, 0 byte(s) sent to socket
2018.05.19 16:45:19 LOG7[0]: Local descriptor (FD=3) closed
2018.05.19 16:45:19 LOG7[0]: Service [redis-client] finished (0 left)

配置详情

这是 Redis 服务器上的 stunnel 配置

pid = /run/stunnel-redis.pid
output = /tmp/stunnel.log

[redis-server]
cert = /etc/stunnel/redis-server.crt
key = /etc/stunnel/redis-server.key
accept = redis_master:6379
connect = 127.0.0.1:6378

这是客户端的 stunnel 配置

pid = /run/stunnel-redis.pid
output = /tmp/stunnel.log

[redis-client]
client = yes
accept = 127.0.0.1:8001
connect = redis_master:6379
CAfile = /etc/stunnel/redis-server.crt
verify = 4
debug = 7

这就是我的 docker-stack.yml 文件对于这两个服务的样子

php_fpm:
    build:
        context: .
        dockerfile: fpm.Dockerfile
    image: registry.github.com/hidden
    ports:
        - "8001"

redis_master:
    build:
        context: .
        dockerfile: redis.Dockerfile
    image: registry.github.com/hidden
    ports:
        - "6378"
        - "6379"
    sysctls:
        - net.core.somaxconn=511
    volumes:
        - redis-data:/data

netstat -plunt 在 fpm 客户端容器中的输出

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8001          0.0.0.0:*               LISTEN      208/stunnel4
tcp        0      0 127.0.0.11:45281        0.0.0.0:*               LISTEN      -
tcp6       0      0 :::9000                 :::*                    LISTEN      52/php-fpm.conf)
udp        0      0 127.0.0.11:43781        0.0.0.0:*                           -

netstat -plunt在redis服务器容器中的输出

Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.11:39294        0.0.0.0:*               LISTEN      -
tcp        0      0 0.0.0.0:6378            0.0.0.0:*               LISTEN      8/redis-server *:63
tcp        0      0 10.0.0.14:6379          0.0.0.0:*               LISTEN      37/stunnel4
tcp6       0      0 :::6378                 :::*                    LISTEN      8/redis-server *:63
udp        0      0 127.0.0.11:44855        0.0.0.0:*                           -

我已经确认主机上没有激活防火墙。这些服务目前位于同一主机上,但它们很快将位于不同的主机上,因此需要 stunnel。

这些服务是使用docker stack 命令部署的,因此会自动创建覆盖网络并将其附加到这两个服务。

有人对为什么从客户端到服务器的请求被拒绝有任何想法吗?

【问题讨论】:

标签: docker redis docker-swarm stunnel


【解决方案1】:

终于搞定了!我希望这对其他人有帮助。问题是redis-server上的stunnel配置,正确配置如下:

[redis-server]
cert = /etc/stunnel/redis-server.crt
key = /etc/stunnel/redis-server.key
accept = 6379
connect = 6378

问题似乎是我在accept 选项中使用了主机名redis_master,将其切换为仅解决了问题的端口。

【讨论】:

  • 谢谢,我的 docker build 使用此信息成功完成!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 2018-07-28
相关资源
最近更新 更多