赞
踩
(1)Registry的工作原理
index服务主要提供镜像索引以及用户认证的功能。当下载一个镜像的时候,首先会去index服务上做认证,然后查找镜像所在的registry的地址并放回给docker客户端,docker客户端再从registry下载镜像,在下载过程中 registry会去index校验客户端token的合法性,不同镜像可以保存在不同的registry服务上,其索引信息都放在index服务上。
(2)Registry的三个部分:index、registry和registry client
(1)下载registry镜像,并加载registry镜像
[root@server1 ~]# ls
docker-compose-Linux-x86_64-1.24.1 Python-3.6.6 rhel7.tar
game2048.tar Python-3.6.6.tgz ubuntu.tar
harbor-offline-installer-v1.8.0.tgz registry2.tar
[root@server1 ~]# docker load -i registry2.tar
d9ff549177a9: Loading layer 4.671MB/4.671MB
f641ef7a37ad: Loading layer 1.587MB/1.587MB
d5974ddb5a45: Loading layer 20.08MB/20.08MB
5bbc5831d696: Loading layer 3.584kB/3.584kB
73d61bf022fd: Loading layer 2.048kB/2.048kB
Loaded image: registry:2
(2)创建容器并运行,端口为5000
docker run -d --name registry -p 5000:5000 -v /opt/registry:/var/lib/registry registry:2
netstat -atlnp | grep 5000 #查看是否开启端口5000
(3)给镜像重新修改标签(标准格式:仓库位置/镜像名)并上传镜像到私有仓库
docker tag rhel7:apache localhost:5000/nginx #其中,rhel7:apache是必须存在的,tag为起别名
docker push localhost:5000/nginx
cd /opt/registry #在该目录下中的docker中存放着上传到仓库中的镜像
curl localhost:5000/v2/_catalog
这样,基础私有仓库就搭建成功了!!但是,缺少加密,当远程主机想要登陆使用仓库时,可以使用加密功能进行过滤远程客户端
(1)首先,先将之前的registry删除,并生成及私密证书与钥匙
docker rm -f registry #删除容器
mkdir /root/certs
openssl req -newkey rsa:4096 -nodes -sha256 -keyout certs/westos.org.key -x509 -days 365 -out certs/westos.org.crt #生成证书和密钥
(2)重建registry容器
docker run -d --restart=always --name registry -v "$(pwd)"/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key -p 443:443 registry:2
netstat -antlp | grep :443 ##查看443端口是否开启
(3)拷贝证书到docker主机,测试上传镜像
给上传的镜像修改标签,采用标准格式:主机名/镜像名,如果可以上传镜像,则证明加密的私有仓库搭建成功
[root@server1 ~]# mkdir -p /etc/docker/certs.d/westos.org
[root@server1 ~]# cp certs/westos.org.crt /etc/docker/certs.d/westos.org/ca.crt
[root@server1 ~]# docker tag nginx:latest westos.org/nginx
[root@server1 ~]# vim /etc/hosts
[root@server1 ~]# docker push westos.org/nginx
The push refers to repository [westos.org/nginx]
fe6a7a3b3f27: Pushed
d0673244f7d4: Pushed
d8a33133e477: Pushed
latest: digest: sha256:dc85890ba9763fe38b178b337d4ccc802874afe3c02e6c98c304f65b08af958f size: 948
备注:这时候,需要在主机中写入解析 172.25.33.1 westos.org
加密的私有仓库搭建成功!!!
(4)远程客户端:server2的操作:
##下载docker [root@server1 ~]# scp /etc/yum.repos.d/docker.repo root@172.25.33.2:/etc/yum.repos.d ##做主机解析 [root@server2 yum.repos.d]# vim /etc/hosts 172.25.33.1 server1 westos.org ##将证书传递到server2上 [root@server1 ~]# scp -r /etc/docker/certs.d root@172.25.33.2:/etc/docker root@172.25.33.2's password: ca.crt 100% 2098 104.8KB/s 00:00 ##重启服务 [root@server2 westos.org]# systemctl daemon-reload [root@server2 westos.org]# systemctl restart docker ##测试能否拉取westos.org仓库中的镜像 [root@server2 westos.org]# docker pull westos.org/nginx Using default tag: latest latest: Pulling from nginx f5d23c7fed46: Pull complete 918b255d86e5: Pull complete 8c0120a6f561: Pull complete Digest: sha256:dc85890ba9763fe38b178b337d4ccc802874afe3c02e6c98c304f65b08af958f Status: Downloaded newer image for westos.org/nginx:latest
拉取成功!!!
(1)生成用户密码文件
mkdir /root/auth
docker run --rm --entrypoint htpasswd registry:2 -Bbn admin westos > auth/htpasswd
docker run --rm --entrypoint htpasswd registry:2 -Bbn redhat westos >> auth/htpasswd
cat auth/htpasswd
(2)将之前的registry容器删除然后重建registry容器
docker run -d --restart=always --name registry -v "$(pwd)"/certs:/certs -e REGISTRY_HTTP_ADDR=0.0.0.0:443 -e REGISTRY_HTTP_TLS_CERTIFICATE=/certs/westos.org.crt -e REGISTRY_HTTP_TLS_KEY=/certs/westos.org.key -p 443:443 -v "$(pwd)"/auth:/auth -e "REGISTRY_AUTH=htpasswd" -e "REGISTRY_AUTH_HTPASSWD_REALM=Registry Realm" -e REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd registry:2
docker ps
netstat -antlp | grep :443
(2)docker主机用户认证之后,登陆仓库上传镜像
docker login westos.org #登录仓库
docker tag ubuntu:latest westos.org/ubuntu
docker push westos.org/ubuntu:latest #上传镜像
这样,用户认证功能添加成功!!!
(3)远程主机server2的操作:
[root@server2 westos.org]# docker login westos.org Username: redhat Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@server2 westos.org]# docker pull westos.org/ubuntu:latest latest: Pulling from ubuntu 257507fcd746: Pull complete cd03f9b52ed8: Pull complete 493709ab45b5: Pull complete 4f4fb700ef54: Pull complete Digest: sha256:4e709bde11754c2a27ed6e9b9ba55569647f83903f85cd8107e36162c5579984 Status: Downloaded newer image for westos.org/ubuntu:latest [root@server2 westos.org]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE westos.org/nginx latest e445ab08b2be 13 days ago 126MB westos.org/ubuntu latest 07c86167cdc4 3 years ago 188MB
(1)导入web容器
docker load -i docker-registry-web.tar
(2)重建docker-registry-web容器并运行
cat .docker/config.json #查看仓库密码
docker tag docker-registry-web:latest hyper/docker-registry-web:latest
docker run -d -p 8080:8080 --name registry-web --link registry:westos.org -e REGISTRY_URL=https://westos.org/v2 -e REGISTRY_TRUST_ANY_SSL=true -e REGISTRY_BASIC_AUTH="YWRtaW46d2VzdG9z" -e REGISTRY_NAME=westos.org hyper/docker-registry-web
(3)浏览器中访问
http://172.25.33.1:8080
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。