2023년 7월 23일 일요일

docker registry proxy 테스트

registry kvm: 192.168.122.99
proxy kvm: 192.168.122.98
client kvm: 192.168.122.97

    1. registry 용 vm 생성
host $ sudo virt-install --name registry --os-variant ubuntu22.04 --vcpus 1 --ram 2048 \
--network network:default --cdrom /path/to/ubuntu-22.04.1-live-server-amd64.iso \
--disk size=10

    2. (공통) 도커 설치
host $ virsh start registry
host $ ssh registry
registry $ echo "$USER ALL=(ALL:ALL) NOPASSWD:ALL" | sudo tee -a /etc/sudoers
registry $ sudo apt update 
registry $ sudo apt install vim iptables
registry $ wget https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/containerd.io_1.6.21-1_amd64.deb
registry $ wget https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/docker-buildx-plugin_0.11.1-1~ubuntu.22.04~jammy_amd64.deb
registry $ wget https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/docker-ce-cli_24.0.4-1~ubuntu.22.04~jammy_amd64.deb
registry $ wget https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/docker-ce_24.0.4-1~ubuntu.22.04~jammy_amd64.deb
registry $ wget https://download.docker.com/linux/ubuntu/dists/jammy/pool/stable/amd64/docker-compose-plugin_2.19.1-1~ubuntu.22.04~jammy_amd64.deb
registry $ sudo dpkg -i *.deb
registry $ rm -rf *
registry $ sudo usermod -aG docker $USER
registry $ sudo shutdown 0

    3. registry vm 을 proxy vm(과 client vm)으로 복사
host $ virt-clone --original registry --name proxy --file /var/lib/libvirt/images/proxy.qcow2
host $ virsh dumpxml proxy | grep '<mac'
      <mac address='52:54:00:ce:18:74'/>
host $ virsh net-edit default
  <ip address='192.168.122.1' netmask='255.255.255.0'>
    <dhcp>
      <range start='192.168.122.100' end='192.168.122.254'/>
      <host mac='52:54:00:a9:5f:97' name='registry' ip='192.168.122.99'/>
      <host mac='52:54:00:ce:18:74' name='proxy' ip='192.168.122.98'/>
      <host mac='52:54:00:22:dd:40' name='client' ip='192.168.122.97'/>
</dhcp> </ip> </network$ virsh net-destroy default host $ virsh net-start default

    4. proxy vm(과 client vm)의 hostname 변경
host $ virsh start proxy
host $ ssh proxy
proxy $ sudo hostnamectl set-hostname proxy
proxy $ sudo sed -i 's/registry/proxy/' /etc/hosts
proxy $ sudo reboot

    5. registry 설정
    5.1. 사설 repository 사용 설정
host $ ssh registry
registry $ sudo vi /etc/docker/daemon.json
{
  "insecure-registries": ["192.168.122.99:5000"]
}
registry $ sudo systemctl restart docker

    5.2. registry 실행
registry $ mkdir -p workspace/registry
registry $ cd workspace
registry $ vi docker-compose.yml
version: "3"
services:
  registry:
    image: registry:2.8.2
    container_name: registry
    volumes:
      - ./registry:/var/lib/registry
    ports:
      - 5000:5000
    restart: always
registry $ docker compose up -d

    5.3. 테스트용 도커 이미지 생성
registry $ mkdir ~/test
registry $ cd ~/test
registry $ vi Dockerfile
FROM alpine
CMD echo 'hello registry 1.0'
registry $ docker build -t 192.168.122.99:5000/test/hello:1.0 .
registry $ docker push 192.168.122.99:5000/test/hello:1.0
registry $ curl http://192.168.122.99:5000/v2/_catalog
{"repositories":["test/hello"]}
registry $ curl http://192.168.122.99:5000/v2/test/hello/tags/list
{"name":"test/hello","tags":["1.0"]}

    6. proxy 설정
host $ ssh proxy
proxy $ mkdir -p workspace/registry
proxy $ cd workspace
proxy $ vi docker-compose.yml
version: "3"
services:
  proxy:
    image: registry:2.8.2
    container_name: proxy
    environment:
      - REGISTRY_PROXY_REMOTEURL="http://192.168.122.99:5000"
    volumes:
      - ./registry:/var/lib/registry
    ports:
      - 5000:5000
    restart: always
proxy $ docker compose up -d

    7. client 설정
host $ ssh client
client $ sudo vi /etc/docker/daemon.json
{
  "insecure-registries": ["192.168.122.98:5000"]
}
client $ sudo systemctl restart docker
client $ curl http://192.168.122.98:5000/v2/_catalog
{"repositories":[]}
client $ docker run --rm -it 192.168.122.98:5000/test/hello:1.0
hello registry 1.0
client $ curl http://192.168.122.98:5000/v2/_catalog
{"repositories":["test/hello"]}

참고:
https://waspro.tistory.com/532
https://bobcares.com/blog/docker-registry-mirroring/
https://docs.docker.com/registry/spec/api/