290112011 发表于 2018-5-27 11:30:21

Docker 私有仓库详解

仓库(Repository)是集中存放镜像的地方


一个容易混淆的概念是注册服务器(Registry)。实际上注册服务器是管理仓库的具体服务器, 每个服务器上可以有多个仓库,而每个仓库下面有多个镜像。从这方面来说,仓库可以被认 为是一个具体的项目或目录。例如对于仓库地址 docker.sina.com.cn/centos:centos63 来说, docker.sina.com.cn是注册服务器地址,centos是仓库名,centos63是仓库的 tag。
Docker Hub官方仓库
目前 Docker 官方维护了一个公共仓库 Docker Hub,其中已经包括了超过 15,000 的镜像。 大部分需求,都可以通过在 Docker Hub中直接下载镜像来实现。

注册&登录
可以通过命令行执行 docker login 命令来输入用户名、密码和邮箱来完成注册和登录。注册

  成功后,本地用户目录的 .docker/config.json 中将保存用户的认证信息。

基本操作
用户无需登录即可通过 docker search命令来查找官方仓库中的镜像,并利用 docker pull 命令来将它下载到本地。

  例如以 centos为关键词进行搜索:

可以看到返回了很多包含关键字的镜像,其中包括镜像名字、描述、星级(表示该镜像的受 欢迎程度)、是否官方创建、是否自动创建。官方的镜像说明是官方项目组创建和维护的, automated 资源允许用户验证镜像的来源和内容。 根据是否是官方提供,可将镜像资源分为两类。一种是类似 centos 这样的基础镜像,被称 为基础或根镜像。这些基础镜像是由 Docker 公司创建、验证、支持、提供。这样的镜像往 往使用单个单词作为名字。还有一种类型,比如 tianon/centos 镜像,它是由 Docker 的用 户创建并维护的,往往带有用户名称前缀。可以通过前缀 user_name/ 来指定使用某个用户 提供的镜像,比如 tianon 用户。另外,在查找的时候通过 -s N 参数可以指定仅显示评价为 N星以上的镜像。
创建自己的仓库----镜像仓库
  
  拓扑:

说明:
  docker.benet.com这是 dockerregistry 服务器的主机名称,ip 是 192.168.1.107;因为 https 的

SSL 证书要用到主机名,所以要设置主机名。
dockerregistry 服务器作为处理 docker 镜像的最终上传和下载,用的是官方的镜像 registry。 nginx 1.6.x是一个用 nginx 作为反向代理服务器
注:关闭 selinux 1)私有仓库 https 支持: A)安装依赖软件包:
# yum -y install pcre-devel zlib-devel openssl openssl-devel

在 Nginx 编译需要 PCRE,因为 Nginx 的 Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表 达式。需要安装 pcre 和 pcre-devel 用 yum 就能安装。
Zlib 库提供了开发人员的压缩算法,在 nginx 的模块中需要使用 gzip 压缩。 需要安装 zlib 和 zlib-devel 用 yum 就可以安装
在 Nginx 中如果需要为服务器提供安全则需要用到 OpenSSL 库。 需要安装的是 openssl 和 openssl-devel。用 yum 就可以安装。

B)配置 SSL
(1) 编辑/etc/hosts,把 docker.nmgkj.top的 ip 地址添加进来,例如: 主机名、ip 地址:
添加主机名
# vim/etc/hostnamedocker.nmgkj.top

~                  
增加/etc/hosts 文件内容
# vim/etc/hosts127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.107 docker.ngmkj.top
(2) 生成根密钥
如果有就将下列文件删除以免冲突
/etc/pki/CA/cacert.pem
/etc/pki/CA/index.txt
/etc/pki/CA/index.txt.attr
/etc/pki/CA/index.txt.old
/etc/pki/CA/serial
/etc/pki/CA/serial.old

  
  生成
# cd /etc/pki/CA
# openssl   genrsa -out private/cakey.pem 2048  Generating RSA private key, 2048 bit long modulus

  ............................................+++
  ......................................................+++
  e is 65537 (0x10001)
# ls  certscrlnewcerts private

# ls private/  cakey.pem

(3) 生成根证书 执行
#openssl req -new -x509 -key private/cakey.pem -out cacert.pem  You are about to be asked to enter information that will be incorporated

  into your certificate request.
  What you are about to enter is what is called a Distinguished Name or a DN.
  There are quite a few fields but you can leave some blank
  For some fields there will be a default value,
  If you enter '.', the field will be left blank.
  -----
  Country Name (2 letter code) :CN
  State or Province Name (full name) []:beijing
  Locality Name (eg, city) :beijing
  Organization Name (eg, company) :hyzy
  Organizational Unit Name (eg, section) []:
  Common Name (eg, your name or your server's hostname) []:docker.nmgkj.com
  Email Address []:
  
会提示输入一些内容,因为是私有的,所以可以随便输入,最好记住能与后面保持一致,特 别是"Common Name”。必须要和 hostname 显示的一致。
上面的自签证书 cacert.pem 应该生成在/etc/pki/CA 下。
# lscacert.pemcertscrlnewcertsprivate

(4)为 nginx web 服务器生成 ssl 密钥
# mkdir /etc/pki/CA/ssl
# cd /etc/pki/CA/ssl
# openssl   genrsa -outnginx.key 2048Generating RSA private key, 2048 bit long modulus

...................................+++
.........+++
e is 65537 (0x10001)
注:因为 CA 中心与要申请证书的 nginx 服务器是同一个所以就在本机上执行为 nginx 服务器 生成 ssl 密钥了,否则应该是在另一台需要用到证书的服务器上生成。
查看 nginx 服务器的密钥
# lsnginx.key

(5) 为 nginx 生成证书签署请求 执行
# openssl req -new -key nginx.key -out nginx.csrYou are about to be asked to enter information that will be incorporated

into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) :CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) :beijing
Organization Name (eg, company) :hyzy
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:docker.nmgkj.com
Email Address []:

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
同样会提示输入一些内容,Commone Name 一定要是你要授予证书的服务器域名或主机名,
challenge password 不填。
(6) 私有 CA 根据请求来签发
# touch   /etc/pki/CA/index.txt
# touch/etc/pki/CA/serial
# echo 00 > /etc/pki/CA/serial
# cat /etc/pki/CA/serial00

执行
# openssl ca -in nginx.csr -out nginx.crtUsing configuration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature
Signature ok
Certificate Details:
      Serial Number: 0 (0x0)
      Validity
            Not Before: Aug 10 01:45:33 2017 GMT
            Not After : Aug 10 01:45:33 2018 GMT
      Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = hyzy
            commonName                = docker.nmgkj.com
      X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                21:97:A2:D4:46:42:6C:4D:BC:D2:B1:BE:29:92:29:E7:FF:20:CE:76
            X509v3 Authority Key Identifier:
                keyid:AB:65:D6:7B:61:0C:7D:46:D7:46:F1:7A:83:36:A5:62:B7:82:8A:2D

Certificate is to be certified until Aug 10 01:45:33 2018 GMT (365 days)
Sign the certificate? :y
1 out of 1 certificate requests certified, commit? y
Write out database with 1 new entries
Data Base Updated

同样会提示输入一些内容,选择 y 就可以了!
查看 nginx 的证书
# lsnginx.crtnginx.csrnginx.key

C)安装,配置,运行 nginx
(1)添加用户和组 指定ID号 也可以不指定
# groupaddwww -g 58
# useradd-u 58 -g www www(2) 编译nginx源码包

  在线下包

# cd /usr/src/
# tarzxf nginx-1.11.2.tar.gz
# cdnginx-1.11.2/
# ./configure --user=www --group=www --prefix=/opt/nginx--with-pcre --with-http_stub_status_module--with-http_ssl_module--with-http_addition_module--with-http_realip_module--with-http_flv_module  上述选项的解释:

--user=USER设定程序运行的用户环境(www)
--group=GROUP 设定程序运行的组环境(www)
--prefix=PATH设定安装目录
--with-pcre 启用 pcre 库,Nginx 的 Rewrite 模块和 HTTP 核心模块会使用到 PCRE 正则表达式
--with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当 前状态
--with-http_ssl_module开启 SSL 模块,支持使用 HTTPS 协议的网页
  
--with-http_realip_module开启 Real IP 的支持,该模块用于从客户请求的头数据中读 取 Real Ip 地址
--with-http_addition_module开启 Addtion 模块,该模块允许你追加或前置数据到相应 的主体部分
--with-http_flv_module 模块 ngx_http_flv_module 为 Flash Video(FLV)文件提供服务端伪流 媒体支持
(3)安装
# make && makeinstall  (4) 编辑/opt/nginx/conf/nginx.conf 文件

# vimnginx.conf  

  user www;
  worker_processes 4;
  
  events {
  worker_connections      4096;
  }
  
  http {
  include mime.types;
  default_type    application/octet-stream;
  sendfile      on;
  keepalive_timeout       65;
  upstream registry {
                  server192.168.1.107:5000;
        }
  server {
        listen443 ssl;
        server_name   docker.nmgkj.com;
        ssl_certificate /etc/pki/CA/ssl/nginx.crt;
        ssl_certificate_key   /etc/pki/CA/ssl/nginx.key;
        ssl_session_cache       shared:SSL:1m;
        ssl_session_timeout   5m;
        ssl_ciphers   HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers       on;
        location      /       {
                          proxy_pass      http://registry;
                          client_max_body_size    3000m;
                          proxy_set_header      Host    $host;
                          proxy_set_header      X-Forward-For   $remote_addr;
                  }
        }
  }
  
相关选项含义:
ssl_session_cache   会话缓存用于保存 SSL会话,这些缓存在工作进程间共享,可以使用
ssl_session_cache 指令进行配置。1M 缓存可以存放大约 4000 个会话。
ssl_session_timeout 缓存超时,默认的缓存超时是 5 分钟。
ssl_ciphers    HIGH:!aNULL:!MD5 使用高强度的加密算法
ssl_prefer_server_ciphers on 依赖 SSLv3 和 TLSv1 协议的服务器密码将优先于客户端密码。 即:在 SSLv3 或这是 TLSv1 握手时选择一个密码,通常是使用客户端的偏好。如果这个指令 是启用的,那么服务器反而是使用服务器的偏好。
client_max_body_size 即允许上传文件大小的最大值
proxy_set_header Host $host 和 proxy_set_header X-Forward-For $remote_addr 的作用描述: nginx 为 了实现 反向代理 的需求 而增加 了一个 ngx_http_proxy_module 模 块。 其中 proxy_set_header 指令就是该模块需要读取的配置文件。在这里,所有设置的值的含义和 http 请求同中的含义完全相同,除了 Host 外还有 X-Forward-For。
Host 的含义是表明请求的主机名,因为 nginx 作为反向代理使用,而如果后端真实 的服务器设置有类似防盗链或者根据 http 请求头中的 host 字段来进行路由或判断功能的话, 如果反向代理层的 nginx 不重写请求头中的 host 字段,将会导致请求失败【默认反向代理服 务器会向后端真实服务器发送请求,并且请求头中的 host 字段应为 proxy_pass 指令设置的 服务器】。
同理,X_Forward_For 字段表示该条 http 请求是有谁发起的?如果反向代理服务器不重写该 请求头的话,那么后端真实服务器在处理时会认为所有的请求都来自反向代理服务器,如果 后端有防攻击策略的话,那么机器就被封掉了。因此,在配置用作反向代理的 nginx 中一般 会增加两条配置,修改 http 的请求头:
proxy_set_header Host $host; proxy_set_header X-Forward-For $remote_addr;
这里的$host 和$remote_addr 都是 nginx 的导出变量,可以再配置文件中直接使用。
  (5) 验证配置
# /opt/nginx/sbin/nginx-t  nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok

  nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
  (6) 启动 nginx:
# /opt/nginx/sbin/nginx  (7)验证是否启动
# ps-ef |grep-i "nginx"  root      6700   10 10:02 ?      00:00:00 nginx: master process /opt/nginx/sbin/nginx

  www       670167000 10:02 ?      00:00:00 nginx: worker process
  www       670267000 10:02 ?      00:00:00 nginx: worker process
  www       670367000 10:02 ?      00:00:00 nginx: worker process
  www       670467000 10:02 ?      00:00:00 nginx: worker process
  root      670627500 10:02 pts/0    00:00:00 grep --color=auto -i nginx
  
# netstat -anpt| grep   nginx  tcp      0    0 0.0.0.0:443             0.0.0.0:*               LISTEN      9706/nginx: master

  2) 配置,运行 Docker
  (1)先停止docker
# systemctl   stopdocker  (2)编辑/etc/sysconfig/docker 文件,加上如下一行

# vim /etc/sysconfig/docker  DOCKER_OPTS="--insecure-registry docker.nmgkj.com --tlsverify --tlscacert /etc/pki/CA/cacert.pem"

  把根证书复制到/etc/docker/certs.d/docker.xxx.com/目录下
# mkdir -p /etc/docker/certs.d/docker.nmgkj.com
# cp /etc/pki/CA/cacert.pem /etc/docker/certs.d/docker.nmgkj.com/ca-certificates.crt  启动docker

# systemctlstart docker  3)运行私有容器 通过获取官方registry镜像来运行

# docker searchregistry
# docker pullregistry
# docker images  REPOSITORY         TAG               IMAGE ID            CREATED             SIZE

  docker.io/registry   latest            bca04f698ba8      18 months ago       422.8 MB
  
  使用官方的registry镜像来启动本地的私有仓库。用户可以通过指定参数来配置私有仓库
  例如将目录/opt/data/registry作为私有仓库的位置

# mkdir -pv /opt/data/registry  mkdir: created directory ‘/opt/data’

  mkdir: created directory ‘/opt/data/registry’
  
  运行私有仓库容器
# docker run -d -p 5000:5000 -v /opt/data/registry:/tmp/registry docker.io/registry2a8584c5dd643d0ab9e1a81f93cbe002f34e665fab13a207a283680ec85f4113

# dockerps

CONTAINER ID      IMAGE                COMMAND             CREATED             STATUS            PORTS                  NAMES
2a8584c5dd64      docker.io/registry   "docker-registry"   9 seconds ago       Up 4 seconds      0.0.0.0:5000->5000/tcp   adoring_lumiere
可以通过 -v   参数来将镜像文件存放在本地的指定路径。例如上面的例子将上传的镜像放到
/opt/data/registry 目录。
-p(小写的)用于将容器的 5000 端口映射宿主机的 5000 端口。

4)验证 registry:
用浏览器输入: https://docker.nmgkj.com
  




  或者:curl -i -k curl -i -k https://docker.nmgkj.com

# curl -i -k https://docker.nmgkj.com  HTTP/1.1 200 OK

  Server: nginx/1.11.2
  Date: Sun, 13 Aug 2017 03:24:08 GMT
  Content-Type: application/json
  Content-Length: 28
  Connection: keep-alive
  Expires: -1
  Pragma: no-cache
  Cache-Control: no-cache
curl 是通过 url 语法在命令行下上传或下载文件的工具软件,它支持 http,https,ftp,ftps,telnet 等多种协 议,常被用来抓取网页和监控 Web 服务器状态

服务端的配置就到此完成! 注意:注意防火墙
5)Docker 客户端配置
(1)编辑/etc/hosts,把 docker.benet.com 的 ip 地址添加进来,例如:
# vim/etc/hosts  127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

  ::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
  192.168.1.107   docker.nmgkj.com
  ~                           
(2)把 docker registry 服务器端的根证书追加到 ca-certificates.crt 文件里
先从 docker registry 服务器端把文件/etc/pki/CA/cacert.pem 拷贝到本机
# scp root@192.168.1.107:/etc/pki/CA/cacert.pem ./The authenticity of host '192.168.1.107 (192.168.1.107)' can't be established.

ECDSA key fingerprint is 52:70:64:73:95:ba:b5:6e:63:bb:35:da:7e:1c:5c:d7.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.107' (ECDSA) to the list of known hosts.
root@192.168.1.107's password:
cacert.pem                                                                           100% 1285   1.3KB/s   00:00   
然后执行命令:
# cat ./cacert.pem >> /etc/pki/tls/certs/ca-certificates.crt (3) 验证 docker.yy.com 下的 registry:

用浏览器输入: https://docker.nmgkj.com

  或者:curl -i -k curl -i -k https://docker.nmgkj.com

  (4) 使用私有 registry 步骤:
   登录: docker login -u duyuheng -p pwd123 -e "duyuheng@nmgkj.com" https://docker.nmgkj.com
# docker loginhttps://docker.nmgkj.com
Username: duyuheng
Password:Login Succeeded


本地上传一个镜像镜像测试(也可从 Docker HUB 上拉取一个镜像),为基础镜像打个标签: docker tag centos:centos6 docker.nmgkj.com/centos:centos6

# dockerload<centos6.tar2714f4a6cdee: Loading layer [==================================================>] 202.3 MB/202.3 MB

Loaded image: docker.io/centos:centos6                                          ] 557.1 kB/202.3 MB
# docker images

REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
docker.io/centos    centos6             cf2c3ece5e41      13 months ago       194.6 MB
打标签
# docker tag centos:centos6 docker.nmgkj.com/centos:centos6
# dockerimagesREPOSITORY                TAG               IMAGE ID            CREATED             SIZE

docker.io/centos          centos6             cf2c3ece5e41      13 months ago       194.6 MB
docker.nmgkj.com/centos   centos6             cf2c3ece5e41      13 months ago       194.6 MB

发布:上传镜像到本地私有仓库
# docker push docker.nmgkj.com/centos:centos6The push refers to a repository

  2714f4a6cdee: Image successfully pushed
  Pushing tag for rev on {https://docker.nmgkj.com/v1/repositories/centos/tags/centos6}
  查看私有仓库是否有镜像
# curl 192.168.1.107:5000/v1/search  {"num_results": 1, "query": "", "results": [{"description": "", "name": "library/centos"}]}

查看镜像的存储目录和文件(在镜像服务器)
# yum-y install tree
# tree /opt/data/registry/repositories  /opt/data/registry/repositories

  └── library
      └── centos
        ├── _index_images
        ├── tag_centos6
        └── tagcentos6_json
  
  2 directories, 3 files
(客户端)从私有仓库 pull 下来 image,查看 image
先删除镜像(客户端)
# docker rmidocker.io/centos:centos6Untagged: docker.io/centos:centos6

Deleted: sha256:cf2c3ece5e418fd063bfad5e7e8d083182195152f90aac3a5ca4dbfbf6a1fc2a
Deleted: sha256:2714f4a6cdee9d4c987fef019608a4f61f1cda7ccf423aeb8d7d89f745c58b18
# dockerimages  REPOSITORY          TAG               IMAGE ID            CREATED             SIZE

  拉取
# docker pull docker.nmgkj.com/centos:centos6  Trying to pull repository docker.nmgkj.com/centos ...

  Pulling repository docker.nmgkj.com/centos
  cf2c3ece5e41: Pull complete
  Status: Downloaded newer image for docker.nmgkj.com/centos:centos6
  docker.nmgkj.com/centos: this image was pulled from a legacy registry.Important: This registry version will not be supported in future versions of docker.
# dockerimages  REPOSITORY                TAG               IMAGE ID            CREATED             SIZE

  docker.nmgkj.com/centos   centos6             2d14596f4606      13 months ago       194.6 MB
查看私有仓库是否有对应的镜像
# curl -k https://docker.nmgkj.com/v1/search  {"num_results": 1, "query": "", "results": [{"description": "", "name": "library/centos"}]}

或浏览器访问仓库

(1)弊端:
server 端可以 login 到官方的 Docker Hub,可以 pull,push 官方和私有仓库! client 端只能操作搭设好的私有仓库!
私有仓库不能 search!
(2)优点:
所有的 build,pull,push 操作只能在私有仓库的 server 端操作,降低企业风险!
(3) 当 client 端 docker login 到官方的 https://index.docker.io/v1/网站,出现 x509: certificate signed by unknown authority 错误时
重命名根证书mv/etc/pki/tls/certs/ca-certificates.crt
/etc/pki/tls/certs/ca-certificates.crt.bak
重启 docker 服务! servicedocker restart!


  
页: [1]
查看完整版本: Docker 私有仓库详解