支持TLS的Docker Registry启用了反向代理(Traefik) - 远程错误:错误证书

时间:2018-05-31 21:52:35

标签: docker ssl reverse-proxy docker-registry traefik

所以我在这里做Docker化的所有事情。 Traefik在容器中运行,我的docker Registry实例也是如此。如果我在mydomain.com:5000/myimage点击它,我可以从注册表中推/拉。

当我尝试使用mydomain.com/myimage尝试通过443时,问题出现了。我在这里的设置是在mydomain.com上的443上的Traefik反向代理侦听,并将该请求内部转发到我的Registry实例的:5000

当我从Traefik网址进行推/拉时,它会挂起并倒计时等待循环重试。当我查看Registry的日志时,每个我都可以看到实例IS实际上与反向代理Traefik进行通信,但是,我在日志中反复出现此错误(在每次推送从客户端重试时):< / p>

2018/05/31 21:10:43 http: TLS handshake error from proxy_container_ip:port: remote error: tls: bad certificate

对于TLS问题,Docker Registry非常严格和严格。我在这里使用所有自签名证书,因为我还在开发中。 知道导致此错误的原因是什么?我假设Traefik代理检测到Registry提供的证书不可信(自签名),因此无法完成&#34;推&#34;请求,或反过来 - 注册表,当将响应发送回Traefik代理时检测到它不被信任。

如果需要,我可以提供其他信息。目前的设置是Traefik和Registry都有自己的.crt和.key文件集。两者(当然)都启用了TLS。

感谢。

1 个答案:

答案 0 :(得分:5)

这是带有自签名证书的有效解决方案,您可以在https://labs.play-with-docker.com上试用

服务器

在Docker游乐场中添加新实例node1。我们将其配置为服务器。为证书创建目录:

mkdir /root/certs

创建通配符证书*.domain.local

$ openssl req -newkey rsa:2048 -nodes -keyout /root/certs/domain.local.key -x509 -days 365 -out /root/certs/domain.local.crt
Generating a 2048 bit RSA private key
...........+++
...........+++
writing new private key to '/root/certs/domain.local.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) []:
State or Province Name (full name) []:
Locality Name (eg, city) []:
Organization Name (eg, company) []:
Organizational Unit Name (eg, section) []:
Common Name (eg, fully qualified host name) []:*.domain.local
Email Address []:

在目录/root中创建两个文件 docker-compose.yml traefik.toml 。您可以使用以下命令下载它们:

wget https://gist.github.com/maiermic/cc9c9aab939f7ea791cff3d974725e4a/raw/8c5d787998d33c752f2ab369a9393905780d551c/docker-compose.yml
wget https://gist.github.com/maiermic/cc9c9aab939f7ea791cff3d974725e4a/raw/8c5d787998d33c752f2ab369a9393905780d551c/traefik.toml

docker-compose.yml

version: '3'

services:
  frontproxy:
    image: traefik
    command: --api --docker --docker.swarmmode
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./certs:/etc/ssl:ro
      - ./traefik.toml:/etc/traefik/traefik.toml:ro
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events
    deploy:
      labels:
        - traefik.port=8080
        - traefik.frontend.rule=Host:traefik.domain.local
  docker-registry:
    image: registry:2
    deploy:
      labels:
        - traefik.port=5000 # default port exposed by the registry
        - traefik.frontend.rule=Host:registry.domain.local
        - traefik.frontend.auth.basic=user:$$apr1$$9Cv/OMGj$$ZomWQzuQbL.3TRCS81A1g/ # user:password, see https://docs.traefik.io/configuration/backends/docker/#on-containers

traefik.toml

defaultEntryPoints = ["http", "https"]

# Redirect HTTP to HTTPS and use certificate, see https://docs.traefik.io/configuration/entrypoints/
[entryPoints]
  [entryPoints.http]
  address = ":80"
    [entryPoints.http.redirect]
    entryPoint = "https"
  [entryPoints.https]
  address = ":443"
    [entryPoints.https.tls]
      [[entryPoints.https.tls.certificates]]
      certFile = "/etc/ssl/domain.local.crt"
      keyFile = "/etc/ssl/domain.local.key"

# Docker Swarm Mode Provider, see https://docs.traefik.io/configuration/backends/docker/#docker-swarm-mode
[docker]
endpoint = "tcp://127.0.0.1:2375"
domain = "docker.localhost"
watch = true
swarmMode = true

初始化Docker Swarm(将<ip-of-node1>的IP地址替换为node1,例如192.168.0.13

docker swarm init --advertise-addr <ip-of-node1>

部署traefik和Docker注册表:

docker stack deploy myregistry -c ~/docker-compose.yml

客户

由于我们没有DNS服务器,因此我们将/etc/hosts(用{strong>服务器 <ip-of-node1>的IP地址替换node1,例如{ {1}}):

192.168.0.13

您现在应该可以从traefik请求健康状况

echo "<ip-of-node1>   registry.domain.local traefik.domain.local" >> /etc/hosts

您应该能够从我们的注册表中请求所有图像(无)

$ curl -ksS https://traefik.domain.local/health | jq .
{
  "pid": 1,
  "uptime": "1m37.501499911s",
  "uptime_sec": 97.501499911,
  "time": "2018-07-19 07:30:35.137546789 +0000 UTC m=+97.600568916",
  "unixtime": 1531985435,
  "status_code_count": {},
  "total_status_code_count": {},
  "count": 0,
  "total_count": 0,
  "total_response_time": "0s",
  "total_response_time_sec": 0,
  "average_response_time": "0s",
  "average_response_time_sec": 0
}

让我们在客户端上配置$ curl -ksS -u user:password https://registry.domain.local/v2/_catalog | jq . { "repositories": [] } 。创建注册表证书的目录:

docker

从我们的服务器获取证书:

mkdir -p /etc/docker/certs.d/registry.domain.local/

现在,您应该能够登录到我们的注册表并添加图片:

scp root@registry.domain.local:/root/certs/domain.local.crt /etc/docker/certs.d/registry.domain.local/ca.crt # Are you sure you want to continue connecting (yes/no)? yes

如果之后您要求我们的注册中心提供所有图像,您应该会看到

docker login -u user -p password https://registry.domain.local
docker pull hello-world:latest
docker tag hello-world:latest registry.domain.local/hello-world:latest
docker push registry.domain.local/hello-world:latest