码迷,mamicode.com
首页 > 其他好文 > 详细

Docker 构建私有镜像仓库(6)

时间:2019-05-15 21:39:14      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:net   新版本   ash   reg   一个   releases   依次   特定   client   


title: Docker 构建私有镜像仓库(6)
date: 2018-12-18 08:47:27
tags:

  • Docker
    categories: Docker
    copyright: true
    ---

Docker是基于Go语言实现的开源容器项目,Docker让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互之间不会有任何接口,Docker诞生于2013年年初,最初发起者是dotCloud公司.Docker自开源后受到广泛的关注和讨论,目前已有多个相关项目(包括Docker三剑客、Kubernetes等),逐渐形成了围绕Docker容器的生态体系,由于Docker在业界造成的影响力实在太大,dotCloud公司后来也直接改名为Docker Inc,并专注于Docker相关技术和产品的开发.

在使用Docker一段时间后,往往会发现手头积累了大量的自定义镜像文件,这些文件通过公有仓库进行管理并不方便,另外有时候只是希望在内部用户之间进行分享,不希望暴露出去.这种情况下,就有必要搭建一个本地私有镜像仓库,本小结将具体介绍两个私有仓库的搭建,其中包括Registry,以及Vmware的Harbor企业仓库.



Registry 仓库搭建

Docker Registry工具是Docker内置的私有仓库解决方案,新版本的Registry基于Golang进行了重构,提供更好的性能和扩展性,并且支持Docker 1.6+的API,非常适合用来构建私有的镜像注册服务器.官方仓库中也提供了Registry的镜像,因此用户可以通过容器运行和源码安装两种方
式来使用Registry.

实验规划Docker服务器:192.168.1.5,Docker客户端:192.168.1.25,请在服务端配置好网桥服务.

◆服务端配置◆

1.将本机配置成网桥,使之能够互相通信.

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens32
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32
DEVICE=eno16777728
TYPE=Ethernet
BOOTPROTO=static
BRIDGE=br0
NM_CONTROLLED=yes
ONBOOT=yes

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-br0
[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE=Bridge
DEVICE=br0
BOOTPROTO=static
IPADDR=192.168.1.15
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=114.114.114.114
ONBOOT=yes

[root@localhost ~]# reboot

2.在服务端192.168.1.5上拉取registry镜像包.

[root@localhost ~]# docker pull registry:latest
[root@localhost ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
registry            latest              2e2f252f3c88        3 months ago        33.3MB

3.在服务端192.168.1.5运行docker私有仓库成功执行,则我们的docker私有仓库搭建成功.

[root@localhost ~]# docker run -itd -p 5000:5000 -v /registry:/var/lib/registry --restart=always --privileged=true --name my_registry registry:latest

◆客户端上传◆

1.此处我们以hello-world为例,首先要先把它拉取下来.

[root@localhost ~]# docker pull hello-world:latest
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB

2.其次给hello-world镜像打个tag表示新的版本,过程中指定服务器IP地址.

[root@localhost ~]# docker tag hello-world 192.168.1.5:5000/hello-world:latest
[root@localhost ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.1.5:5000/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB
docker.io/hello-world          latest              4ab4c602aa5e        3 months ago        1.84 kB

3.由于docker私有仓库服务器,默认是基于https传输的,所以我们需要在客户端192.168.1.25做相关设置,禁止使用https传输.

[root@localhost ~]# vim /etc/docker/daemon.json
{
        "registry-mirrors": ["https://njrds9qc.mirror.aliyuncs.com"],
        "insecure-registries":["192.168.1.5:5000"]
}

4.依次执行下面两条命令,重新启动docker让其加载我们的配置文件.

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl enable docker

5.执行推送命令,将我们的hello-world推送到服务器上.

[root@localhost ~]# docker images
REPOSITORY                     TAG                 IMAGE ID            CREATED             SIZE
192.168.1.5:5000/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB
docker.io/hello-world          latest              4ab4c602aa5e        3 months ago        1.84 kB

[root@localhost ~]# docker push 192.168.1.5:5000/hello-world:latest
The push refers to a repository [192.168.1.5:5000/hello-world]
428c97da766c: Pushed
latest: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524

6.在服务器端查看刚刚提交的一个请求.

[root@localhost ~]# ls -l /registry/docker/registry/v2/repositories
total 0
drwxr-xr-x 5 root root 55 Dec 17 20:23 hello-world

[root@localhost ~]# curl http://192.168.1.5:5000/v2/_catalog
{"repositories":["hello-world"]}

◆客户端拉取◆

1.客户端修改一下配置文件,指定以下服务器地址.

[root@localhost ~]# cat /etc/docker/daemon.json
{
        "registry-mirrors": ["https://njrds9qc.mirror.aliyuncs.com"],
        "insecure-registries":["192.168.1.5:5000"]
}

2.修改Docker配置文件,开启局域网模式.

在/etc/default/docker添加一行:

DOCKER_OPTS="--insecure-registry 192.168.1.5:5000"

或在/etc/sysconfig/docker文件中添加

OPTIONS='--selinux-enabled --insecure-registry 192.168.1.5:5000'

3.重新启动Docker,加载配置文件.

[root@localhost ~]# systemctl daemon-reload
[root@localhost ~]# systemctl restart docker
[root@localhost ~]# systemctl enable docker

4.通过命令下载测试镜像.

[root@localhost ~]# docker pull 192.168.1.5:5000/hello-world:latest


Harbor 企业仓库搭建

Harbor是VMware公司开源的企业级DockerRegistry项目,项目地址为https://github.com/vmware/harbor.其目标是帮助用户迅速搭建一个企业级的DockerRegistry服务,它以Docker公司开源的registry为基础,提供了管理UI,基于角色的访问控制(Role BasedAccess Control),AD/LDAP集成、以及审计日志(Auditlogging)等企业用户需求的功能,同时还原生支持中文.Harbor的每个组件都是以Docker容器的形式构建的,使用DockerCompose来对它进行部署.用于部署Harbor的DockerCompose模板位于/Deployer/docker-compose.yml,由5个容器组成,这几个容器通过Dockerlink的形式连接在一起,在容器之间通过容器名字互相访问.对终端用户而言,只需要暴露proxy即Nginx的服务端口.

Proxy:由Nginx服务器构成的反向代理
Registry:由Docker官方的开源 registry 镜像构成的容器实例
UI:即架构中的core services,构成此容器的代码是 Harbor项目的主体
MySQL:由官方MySQL镜像构成的数据库容器
Log:运行着rsyslogd的容器,通过log-driver的形式收集其他容器的日志

Harbor特性

a、基于角色控制:用户和仓库都是基于项目进行组织的,而用户基于项目可以拥有不同的权限
b、基于镜像的复制策略:镜像可以在多个Harbor实例之间进行复制
c、支持LDAP:Harbor的用户授权可以使用已经存在LDAP用户
d、镜像删除,垃圾回收:Image可以被删除并且回收Image占用的空间,绝大部分的用户操作API方便用户对系统进行扩展
e、用户UI:用户可以轻松的浏览、搜索镜像仓库以及对项目进行管理
f、轻松的部署功能:Harbor提供了online、offline安装,除此之外还提供了virtualappliance安装
g、Harbor和 dockerregistry 关系:Harbor实质上是对 dockerregistry 做了封装,扩展了自己的业务模块

Harbor认证过程

a、dockerdaemon从dockerregistry拉取镜像
b、如果dockerregistry需要进行授权时,registry将会返回401Unauthorized响应,同时在响应中包含了docker
client如何进行认证的信息
c、dockerclient根据registry返回的信息,向auth server发送请求获取认证token
d、auth server则根据自己的业务实现去验证提交的用户信息是否存符合业务要求
e、用户数据仓库返回用户的相关信息
f、auth server将会根据查询的用户信息,生成token令牌,以及当前用户所具有的相关权限信息.上述就是
完整的授权过程.当用户完成上述过程以后便可以执行相关的pull/push操作.认证信息会每次都带在请求头中

Harbor认证流程

a、首先,请求被代理容器监听拦截,并跳转到指定的认证服务器
b、如果认证服务器配置了权限认证,则会返回401.通知dockerclient在特定的请求中需要带上一个合法的token,而认证的逻辑地址则指向架构图中的core services
c、当dockerclient接受到错误code.client就会发送认证请求(带有用户名和密码)到coreservices进行basic
auth认证
d、当C的请求发送给ngnix以后,ngnix会根据配置的认证地址将带有用户名和密码的请求发送到core
serivces
e、coreservices获取用户名和密码以后对用户信息进行认证(自己的数据库或者介入LDAP都可以).成功以后,返回认证成功的信息

◆服务端配置◆

在服务端安装之前,请确保你的环境里面已安装好了Docker.

1.下载Docker-Compose工具,并移动到/usr/local/bin目录下.

[root@localhost ~]# wget https://github.com/docker/compose/releases/download/1.9.0/docker-compose-`uname -s`-`uname -m`
[root@localhost ~]# mv docker-compose /usr/local/bin/
[root@localhost ~]# chmod 777 -R /usr/local/bin/docker-compose
[root@localhost ~]# docker-compose --version
docker-compose version 1.9.0, build 2585387

2.下载HarBor解压并修改配置文件.

[root@localhost ~]# wget https://github.com/vmware/harbor/releases/download/v1.2.0/harbor-offline-installer-v1.2.0.tgz

[root@localhost ~]# tar -xzvf harbor-offline-installer-v1.2.0.tgz
[root@localhost ~]# cd harbor
[root@localhost harbor]# ls
common                     docker-compose.yml     harbor.v1.2.0.tar.gz  NOTICE
docker-compose.clair.yml   harbor_1_1_0_template  install.sh            prepare
docker-compose.notary.yml  harbor.cfg             LICENSE               upgrade

[root@localhost harbor]# vim harbor.cfg

hostname=192.168.1.5  #本机IP地址
ui_url_protocol=https

3.创建目录并到制定目录生成加密https证书.

[root@localhost ~]# mkdir -p /data/cert
[root@localhost ~]# chmod 777 /data/cert
[root@localhost ~]# cd /data/cert

[root@localhost ~]# openssl genrsa -des3 -out server.key 2048
[root@localhost ~]# openssl req -new -key server.key -out server.csr
[root@localhost ~]# cp server.key server.key.org
[root@localhost ~]# openssl rsa -in server.key.org -out server.key
[root@localhost ~]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

4.安装并拷贝相关配置文件,最后进入测试页测试效果.

[root@localhost ~]# cd /root/harbor
[root@localhost harbor]# ./install.sh
[root@localhost ~]# curl https://192.168.1.5
#用户名:admin  密码:Harbor12345

◆客户端上传◆

1.客户端需要指定镜像仓库地址(也就是服务器的地址).

[root@localhost ~]# vim /etc/docker/daemon.json

{
"insecure-registries": ["192.168.1.5"]
}

[root@localhost ~]# systemctl restart docker

2.下载一个hello-world镜像,并给镜像重新打标签.

[root@localhost ~]# docker pull hello-world:latest
[root@localhost ~]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB


[root@localhost ~]# docker tag hello-world:latest 192.168.1.5/library/hello-world:latest
[root@localhost ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
192.168.1.5/library/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB
docker.io/hello-world             latest              4ab4c602aa5e        3 months ago        1.84 kB

3.登陆进行上传测试.

[root@localhost ~]# docker login 192.168.1.5
Username: admin
Password: Harbor12345
Login Succeeded

[root@localhost ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
192.168.1.5/library/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB
docker.io/hello-world             latest              4ab4c602aa5e        3 months ago        1.84 kB

[root@localhost ~]# docker push 192.168.1.5/library/hello-world:latest
The push refers to a repository [192.168.1.5/library/hello-world]
428c97da766c: Pushed
latest: digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812 size: 524

◆客户端拉取◆

1.指定镜像仓库地址,指定镜像仓库地址.

[root@localhost ~]# vim /etc/docker/daemon.json

{ "insecure-registries": ["192.168.1.5"] }         #指定服务器地址

2.下载测试镜像,这里下载刚刚上传的试一下.

[root@localhost ~]# docker pull 192.168.1.5/library/hello-world:latest

Trying to pull repository 192.168.1.5/library/hello-world ...
latest: Pulling from 192.168.1.5/library/hello-world
d1725b59e92d: Pull complete
Digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812
Status: Downloaded newer image for 192.168.1.5/library/hello-world:latest

[root@localhost ~]# docker images
REPOSITORY                        TAG                 IMAGE ID            CREATED             SIZE
192.168.1.5/library/hello-world   latest              4ab4c602aa5e        3 months ago        1.84 kB


Docker 构建私有镜像仓库(6)

标签:net   新版本   ash   reg   一个   releases   依次   特定   client   

原文地址:https://www.cnblogs.com/LyShark/p/10872335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!