Docker Registry搭建私有镜像仓库(干货)http/https

########先安装docker,此处不做叙述#########

一、http

1、docker pull registry

2、docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry:latest

 参数说明
  -d:在后台运行;
  -v:把宿主机的/opt/registry目录绑到容器/var/lib/registry目录(这个目录是registry容器中存放镜像文件的目录),来实现持久化;
  -p:映射端口;访问宿主机的5000端口就访问到registry容器的服务了;
  --restart=always:这是重启的策略,这个容器异常退出会自动重启容器;
  --name registry:创建容器命名为registry

3、curl http://ip:5000/v2/_catalog查看仓库中的镜像

     curl http://ip:5000/v2/test/tags/list查看镜像标签等信息

4、systemctl daemon-reload && systemctl restart docker && systemctl enable docker

客户端做以下配置

1、vi /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock    --insecure-registry ip:5000

2、vi /etc/systemd/system/docker.service.d/http-proxy.conf(如有需要)

[Service]
Environment="HTTP_PROXY=http://ip:808" "NO_PROXY=localhost,127.0.0.1,仓库IP"

3、systemctl daemon-reload && systemctl restart docker && systemctl enable docker

二、https

1、cat /etc/pki/tls/openssl.cnf

[ v3_ca ]
subjectAltName = IP:10.19.32.19   ###添加此处

mkdir -p /registry/certs &cd /registry/

2、openssl req -newkey rsa:2048 -nodes -keyout certs/domain.key -x509 -days 365 -out certs/domain.crt

3、mkdir -p /etc/docker/certs.d/10.194*.*:5000

4、cp certs/domain.crt /etc/docker/certs.d/10.194.*。*:5000/ca.crt

5、cat certs/domain.crt >> /etc/pki/tls/certs/ca-bundle.crt

6、systemctl restart docker

7、docker run -d -p 5000:5000 --name=registry-https5000 -v /registry/certs/:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:5000 -e REGISTRY_HTTP_TLS_CERTIFICATE=certs/domain.crt -e REGISTRY_HTTP_TLS_KEY=certs/domain.key registry:latest

8、curl -k https://10.194.202.50:5000/v2

x509: certificate signed by unknown authority (k3s)

9、scp /registry/certs/domain.crt 到客户端,并更名/etc/pki/tls/certs/docker.crt

完成!

你可能感兴趣的:(k8s,docker,kubernetes)