设为首页 收藏本站
查看: 1669|回复: 0

[经验分享] docker registry v2 nginx

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2016-1-18 10:13:34 | 显示全部楼层 |阅读模式
一 环境
俩台centos 7 64位
Docker 版本
Client:
Version:      1.8.2
API version:  1.20
Package Version: docker-1.8.2-7.el7.centos.x86_64
Go version:   go1.4.2
Git commit:   bb472f0/1.8.2
Built:        
OS/Arch:      linux/amd64

Server:
Version:      1.8.2
API version:  1.20
Package Version:
Go version:   go1.4.2
Git commit:   bb472f0/1.8.2
Built:        
OS/Arch:      linux/amd64
Register V2.0
Register server 镜像服务器 192.168.33.75
Register client 测试镜像docker服务器 192.168.153.86
二 搭建过程
在Register server 上(192.168.33.75)
2.1 安装需要的软件
yum install gcc make pcre-devel pcre openssl-devel httpd-tools -y
其中  pcre-devel pcre 是在nginx需要的用的软件包
Openssl-devel 是制作key的时候需要用的软件包
Httpd-tools 是生成nginx用户密码需要用的软件包
2.2 更改hosts,添加
[iyunv@localhost ~]# vim /etc/hosts
192.168.33.75 dockertest.xxxx.com
2.3 生成根密钥
先看下/etc/pki/CA 下有没有
Cacert.pem index.txt index.txt.attr index.txt.old serial serial.old
有的话全部删除,然后
[iyunv@localhost ~]# cd /etc/pki/CA/
生成根密钥
[iyunv@localhost ~]#openssl genrsa -out private/cakey.pem 2048
生成根证书
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) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:beijing
Organizational Unit Name (eg, section) []:beijing
Common Name (eg, your name or your server's hostname) []:dockertest.xxxx.com
Email Address []:abcd@qq.com
其中CN一定要写自己的私有镜像库的域名
Email要记住,后面要用到
上面的自签证书cacert.pem在/etc/pki/CA/目录下
2.4 为nginx web服务器生成ssl密钥
[iyunv@localhost CA]# mkdir /usr/local/nginx/ssl
[iyunv@localhost CA]# cd /usr/local/nginx/ssl
[iyunv@localhost CA]# openssl genrsa -out nginx.key 2048
为nginx生成证书签署请求
[iyunv@localhost CA]# openssl req -new -key nginx.key -out nginx.csr
输出,其中最主要的是红色表明的
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) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:beijing
Organizational Unit Name (eg, section) []:beijing
Common Name (eg, your name or your server's hostname) []:dockertest.xxxx.com
Email Address []:abcd@qq.com
A challenge password []:
An optional company name []:
其中Common Name 和email要和上面的一样,challenge password不填
私有CA根据请求来签发证书
[iyunv@localhost CA]# touch /etc/pki/CA/index.txt
[iyunv@localhost CA]# touch /etc/pki/CA/serial
[iyunv@localhost CA]# echo 00 > /etc/pki/CA/serial
[iyunv@localhost CA]#openssl ca -in nginx.csr -out nginx.crt
Using configuration from /etc/pki/tls/openssl.cnf
Check that the request matches the signature
Signature ok
Certificate Details:
        Serial Number: 1 (0x1)
        Validity
            Not Before: Jan 11 06:04:22 2016 GMT
            Not After : Jan 10 06:04:22 2017 GMT
        Subject:
            countryName               = CN
            stateOrProvinceName       = beijing
            organizationName          = beijing
            organizationalUnitName    = beijing
            commonName                = dockertest.hc360.org
            emailAddress              = 122727569@qq.com
        X509v3 extensions:
            X509v3 Basic Constraints:
                CA:FALSE
            Netscape Comment:
                OpenSSL Generated Certificate
            X509v3 Subject Key Identifier:
                80:2C:A4:5D:55:92:2E:E0:38:26:9A:A0:F2:7E:29:6A:24:3D:FE:DF
            X509v3 Authority Key Identifier:
                keyid:60:BE:EA:F3:43:79:DC:46:D3:A2:00:2A:2E:8A:D6:10:93:5C:54:59

Certificate is to be certified until Jan 10 06:04:22 2017 GMT (365 days)
Sign the certificate? [y/n]:y
直接输入y,确认就好了。
三 安装配置nginx
3.1 安装nginx
Nginx 版本必须大于1.7.5,我使用的是1.8的
[iyunv@localhost CA]# tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0
mkdir /usr/local/nginx
./configure --prefix=/usr/local/nginx  --with-http_ssl_module --with-http_stub_status_module  --with-pcre  --with-http_addition_module --with-http_realip_module  --with-http_flv_module
make && make install
3.2 生成htpasswd
[iyunv@localhost]# htpasswd -cb /usr/local/nginx/conf/.htpasswd admin admin
生成的用户名和密码都是admin,到时登录私有仓库的时候用。
3.3 配置nginx
[iyunv@localhost]# cd /usr/local/nginx/conf/
[iyunv@localhost]# cp nginx.conf nginx.conf.bak
[iyunv@localhost]#  vim nginx.conf
user  root root;
worker_processes  auto;

error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

worker_rlimit_nofile 51200;
events {
    use epoll;
    worker_connections  51200;
    multi_accept on;
}
http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    server_names_hash_bucket_size 128;

    client_header_buffer_size 32k;

    large_client_header_buffers 4 32k;

    sendfile        on;
    tcp_nopush     on;
    tcp_nodelay    on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    upstream registry {
server 127.0.0.1:5000;
}
server {
        listen       443;
        server_name  192.168.33.75;
add_header Docker-Distribution-Api-Version registry/2.0 always;
sslon;
ssl_certificate /usr/local/nginx/ssl/nginx.crt; #crt的位置
    ssl_certificate_key /usr/local/nginx/ssl/nginx.key; #key的位置
client_max_body_size 0;
chunked_transfer_encoding on;
location / {
      auth_basic "registry";
      auth_basic_user_file /usr/local/nginx/conf/.htpasswd; #htpasswd的位置

      root   html;
      index  index.html index.htm;

      proxy_pass                  http://registry;
      #proxy_set_header  Host           $http_host;
      proxy_set_header  X-Real-IP      $remote_addr;
      proxy_set_header  Authorization  "";

      client_body_buffer_size     128k;
      proxy_connect_timeout       90;
      proxy_send_timeout          90;
      proxy_read_timeout          90;
      proxy_buffer_size           8k;
      proxy_buffers               4 32k;
      proxy_busy_buffers_size     64k;  #如果系统很忙的时候可以申请更大的proxy_buffers 官方推荐*2
      proxy_temp_file_write_size  64k;  #proxy缓存临时文件的大小
    }
location /_ping {
      auth_basic off;
       proxy_pass http://registry;
    }
location /v1/_ping {
      auth_basic off;
      proxy_pass http://registry;
    }
    }

}
其中红色标明的地方一定要注意
如果不添加add_header Docker-Distribution-Api-Version registry/2.0 always;
无法用https访问私有仓库
如果不注解掉
#proxy_set_header  Host           $http_host;
这个的话,私有仓库可以用https登录,但是push的时候会报错,
The push refers to a repository [dockertest.hc360.org/ubuntu_v1] (len: 1)
af88597ec24b: Pushing
dial tcp 192.168.33.75:80: connection refused
不注解的话它连接的是真实ip地址,无法代理
这个具体含义,大家查下nginx 反向代理 proxy_set_header
3.4验证配置
[iyunv@localhost ~]# /usr/local/nginx/sbin/nginx -t
如果没有错误的话,启动nginx
[iyunv@localhost ~]# /usr/local/nginx/sbin/nginx
四 搭建docker registry V2
4.1 更改docker配置文件
[iyunv@localhost ~]# systemctl stop docker
[iyunv@localhost ~]# vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled --insecure-registry dockertest.xxxx.com'
更改成这样,添加--insecure-registry dockertest.xxxx.com 红色的是私有仓库的域名,更前面生成密钥时候的域名对应
配置密钥文件
Mkdir -p /etc/docker/certs.d/hc.docker.io
Cp /etc/pki/CA/cacert.pem ./hc.docker.io/ca-certificates.crt
cat /etc/pki/CA/cacert.pem  >> /etc/pki/tls/certs/ca-bundle.crt
[iyunv@localhost ~]#systemctl restart docker
[iyunv@localhost ~]#vim config.yml
version: 0.1
log:
    level: debug
    formatter: text
    fields:
        service: registry
        environment: staging
storage:
    delete:
        enabled: true
    cache:
        layerinfo: inmemory
    filesystem:
        rootdirectory: /var/lib/registry
http:
    addr: :5000
    secret: admin
[iyunv@localhost ~]#mkdir data
docker run -d -p 5000:5000 --restart=always --name registry -v `pwd`/config.yml:/etc/docker/registry/config.yml -v `pwd`/data:/var/lib/registry registry:2
4.2 验证
[iyunv@localhost ~]# curl https://admin:admin@dockertest.xxxx.com/v2/
结果返回{}
登录
[iyunv@localhost ~]docker login https://dockertest.xxxx.com
Username: admin
Password:        #admin
Email: abcd@qq.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
4.3 验证push镜像到私有仓库
在登录的前题下,本机验证
[iyunv@localhost ~]# docker pull centos
[iyunv@localhost ~]# docker images
daocloud.io/centos                       latest              60e65a8e4030        2 weeks ago         196.6 MB
[iyunv@localhost ~]# docker tag 60e65a8e4030 dockertest.hc360.org/centos_test_v12
[iyunv@localhost ~]# docker push dockertest.xxxx.com/centos_test_v12
The push refers to a repository [dockertest.xxxx.com/centos_test_v12] (len: 1)
60e65a8e4030: Pushed
60e65a8e4030: Preparing
838c1c5c4f83: Pushed
838c1c5c4f83: Preparing
latest: digest: sha256:3019f69254dcceb22a65859a3a414386ffbdd2908afba1b39e73220845c6cec8 size: 7228
查看push结果
[iyunv@localhost ~]# curl  https://admin:admin@dockertest.hc360.org/v2/_catalog
{"repositories":["centos_test_v12"]}
五 在client端测试
5.1 配置密钥
[iyunv@localhost ~]#scp /etc/pki/CA/cacert.pem root@192.168.153.86:/root/
在192.168.153.86这台服务器上
[iyunv@docker-minion2 ~]# cat /root/cacert.pem >> /etc/pki/tls/certs/ca-bundle.crt
[iyunv@docker-minion2 ~]#vim /etc/sysconfig/docker
OPTIONS='--selinux-enabled --insecure-registry dockertest.xxxx.com'
[iyunv@docker-minion2 ~]# systemctl restart docker
最好重启下服务器吧,我第一次更改完无法登录。重启后可以登录了,可能是没有加载密钥
[iyunv@docker-minion2 ~]# docker login https://dockertest.hc360.org
Username: admin
Password:
Email: 122727569@qq.com
WARNING: login credentials saved in /root/.docker/config.json
Login Succeeded
5.2 验证
和上面验证方法一样
能登录上传基本没有什么问题


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-165931-1-1.html 上篇帖子: docker固定IP地址重启不变 下篇帖子: CentOs7 docker1.9 通过SSL把镜像上传到仓库 registry
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表