连接到远程码头工人主机/部署堆栈



我创建了一个docker堆栈来部署到群中。现在我有点困惑将其部署到真实服务器的正确方法是什么样子的?

我当然可以

  1. scp我的docker-stack.yml文件到我的群的节点
  2. ssh到节点中
  3. 运行docker stack deploy -c docker-stack.yml stackname

所以我想到了docker-machine工具。 我试过了

docker-machine -d none --url=tcp://<RemoteHostIp>:2375 node1

只有当您在没有 TLS 的情况下打开端口时,什么似乎才有效? 我收到了以下内容:

$ docker-machine env node1
Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "192.168.178.49:2375": dial tcp 192.168.178.49:2375: connectex: No connection could be made because the target machine actively refused it.
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
Be advised that this will trigger a Docker daemon restart which might stop running containers.

我已经尝试生成证书并将其复制到主机:

ssh-keygen -t rsa
ssh-copy-id myuser@node1

然后我跑了

docker-machine --tls-ca-cert PathToMyCert --tls-client-cert PathToMyCert create -d none --url=tcp://192.168.178.49:2375 node1 

结果如下:

$ docker-machine env node1
Error checking TLS connection: Error checking and/or regenerating the certs: There was an error validating certificates for host "node1:2375": There was an error reading certificate
You can attempt to regenerate them using 'docker-machine regenerate-certs [name]'.
Be advised that this will trigger a Docker daemon restart which might stop running containers.

我也用通用驱动程序尝试过

$ docker-machine create -d generic --generic-ssh-port "22" --generic-ssh-user "MyRemoteUser" --generic-ip-address 192.168.178.49 node1
Running pre-create checks...
Creating machine...
(node1) No SSH key specified. Assuming an existing key at the default    location.
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Error creating machine: Error detecting OS: OS type not recognized

如何使用 TLS 正确添加带有码头工人机器的远程码头坞主机? 或者有没有更好的方法将堆栈部署到服务器/投入生产?

我经常读到你不应该暴露 docker 端口,但一次也不应该公开如何做到这一点。我不敢相信他们没有提供一种简单的方法来做到这一点。

更新和解决方案

我认为这两个答案都有条件。 我找到了部署到Azure Offical Doc(AWS也是如此)。 拉尔瓦尼@Tarun的回答为我指明了正确的方向,这几乎是官方的解决方案。这就是我接受他回答的原因。

对我来说,以下命令有效:

ssh -fNL localhost:2374:/var/run/docker.sock myuser@node1

然后,您可以运行以下任一操作:

docker -H localhost:2374 stack deploy -c stack-compose.yml stackname

DOCKER_HOST=localhost:2374
docker stack deploy -c stack-compose.yml stackname

@BMitch的回答也是有效的,他提到的安全问题不容忽视。

更新 2

@bretf的答案是连接到您的群体的绝佳方式。特别是如果你有多个。它仍然是测试版,但适用于互联网上可用的群,没有ARM架构。

即使我正在考虑TLS,我也宁愿不打开/公开docker端口。我宁愿使用 SSH 隧道,然后进行部署

ssh -L 2375:127.0.0.1:2375 myuser@node1

然后使用

DOCKER_HOST=tcp://127.0.0.1:2375
docker stack deploy -c docker-stack.yml stackname

你不需要docker-machine。Docker在其文档中提供了配置TLS的详细步骤。这些步骤涉及:

  • 创建 CA
  • 创建并签署服务器证书
  • 配置 dockerd 守护程序以使用此证书并验证客户端证书
  • 创建并签署客户端证书
  • 将 CA 和客户端证书文件复制到客户端计算机的 .docker 文件夹
  • 设置一些环境变量以使用客户端证书和远程 Docker 主机

我不会在多用户环境中使用 ssh 隧道方法,因为任何有权访问 127.0.0.1 的用户都可以在没有密码或任何审核的情况下对远程 docker 主机进行 root 访问。

如果您使用的是 Docker forWindows 或 Docker for Mac,Docker Cloud 提供了一种更自动化的方式来设置 TLS 证书,并让您免费安全地连接到远程主机。在"Swarms"下有"自带Swarm",它在Swarm管理器上运行一个代理容器,让您无需手动设置证书即可轻松使用本地docker cli。它仍然需要向互联网开放的 Swarm 端口,但此设置可确保它启用了 TLS 相互身份验证。

这是一个YouTube视频,展示了如何设置它。它还可以支持组权限,用于添加/删除远程访问 Swarm 的其他管理员。