1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

KubernetesのkindでDocker Registryのローカルミラーを作って転送量を少なくする

Posted at

はじめに

なんどもkindで停止と起動を繰り返すような開発を行なっているときに、都度コンテナイメージを転送すると時間もかかるし、転送量も増えるのでローカルにキャッシュを作ります。

パススルーミラーをするコンテナを起動する

if ! docker inspect proxy_docker > /dev/null; then
  docker run -d --name proxy_docker --restart=always --net=kind \
    -e REGISTRY_PROXY_REMOTEURL=https://registry-1.docker.io registry:2
fi
if ! docker inspect proxy_gcr > /dev/null; then
  docker run -d --name proxy_gcr --restart=always --net=kind \
    -e REGISTRY_HTTP_ADDR=0.0.0.0:5001 \
    -e REGISTRY_PROXY_REMOTEURL=https://gcr.io registry:2
fi
if ! docker inspect proxy_quay > /dev/null; then
  docker run -d --name proxy_quay --restart=always --net=kind \
    -e REGISTRY_HTTP_ADDR=0.0.0.0:5002 \
    -e REGISTRY_PROXY_REMOTEURL=https://quay.io registry:2
fi
if ! docker inspect proxy_zalando > /dev/null; then
  docker run -d --name proxy_zalando --restart=always --net=kind \
    -e REGISTRY_HTTP_ADDR=0.0.0.0:5003 \
    -e REGISTRY_PROXY_REMOTEURL=https://registry.opensource.zalan.do registry:2
fi

kindの設定にミラーを参照する設定を入れる

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
containerdConfigPatches:
  - |-
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
      endpoint = ["http://proxy:5000"]
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."gcr.io"]
      endpoint = ["http://proxy:5001"]
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."quay.io"]
      endpoint = ["http://proxy:5002"]
    [plugins."io.containerd.grpc.v1.cri".registry.mirrors."zalan.do"]
      endpoint = ["http://proxy:5003"]

そのほかの方法

rpardini/docker-registry-proxy: An HTTPS Proxy for Docker providing centralized configuration and caching of any registry (quay.io, DockerHub, k8s.gcr.io)を使ってプロキシを作る方法。この方法ではMITMで中間に入って全てプロキシするというもの。だがレジストリによっては動作しないので、色々とカスタマイズが必要そう。

proxyを起動する。

docker run --rm --name docker_registry_proxy -it \
       --net kind --hostname docker-registry-proxy \
       -p 0.0.0.0:3128:3128 -e ENABLE_MANIFEST_CACHE=true \
       -v $(pwd)/docker_mirror_cache:/docker_mirror_cache \
       -v $(pwd)/docker_mirror_certs:/ca \
       rpardini/docker-registry-proxy:0.6.2

次にkindを起動したら、次のようなスクリプトでkindコンテナの設定を書き換える。

# !/bin/sh

KIND_NAME=${1-focas}
SETUP_URL=http://docker-registry-proxy:3128/setup/systemd
pids=""
for NODE in $(kind get nodes --name "$KIND_NAME"); do
  echo "ENABLE PROXY FOR $NODE"
  docker exec "$NODE" sh -c "\
      curl $SETUP_URL \
      | sed s/docker\.service/containerd\.service/g \
      | sed '/Environment/ s/$/ \"NO_PROXY=127.0.0.0\/8,10.0.0.0\/8,172.16.0.0\/12,192.168.0.0\/16\"/' \
      | bash" & pids="$pids $!" # Configure every node in background
done
wait $pids # Wait for all configurations to end

参照

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?