跳至主要内容

用Gitlab搭建 Docker Hub Registry(不踩坑指南)




Docker大家基本都用过, Registry 就是服务,简单理解就是能让你 Docker pull 和push Image的地方。

Gitlab的 Registry 是自动化部署一个环节。计划从Gitlab的自动化集成、自动化部署,以及如何配合Kubernets(谷歌的平台简称K8s),这些属于Devops的应用。

之前写过一篇文章,感慨现在的云服务实在是太方便了:快速部署的目的是让创业公司专注自己的业务,而无需浪费人力去折腾基础服务.

那么为何不使用 Docker Hub 服务?反而要自己搭建呢?
几个原因:
*  Docker Hub 访问实在是太慢,甚至是访问不了. 
*  使用 Docker Hub必须开源自己的仓库, 很多用户比较敏感.(当然可以花钱购买私有仓库进行扩容)
*  自己机器上数据和云服务数据同步,保证数据安全

---------------------------------------------------------
Gitlab 功能太多,我也是使用 CI 、CD 配合 Kubernetes(k8s)时候偶然发现竟然还集成了Docker Registry 的功能...这下爽了,终于不用看墙的脸色决定网速了~我自己的 Docker Hub,想怎么push、pull 都行~
---------------------------------------------------------

我们这篇主要讲怎么避免踩坑,具体服务怎么搭建回头找个机会,再整理下详细步骤.

我的环境是一台 DELL 16G 的1U 服务器,在 EXSI 6.5 上给 Ubuntu17.04 server 分配了4 Core 以及 6G内存. 

只跑 Gitlab 基础 + Registry 的服务, 和这个配套的 K8s 、Gitlab-runner 等服务在另外一个系统上跑(K8s 、CI CD、 Gitlab-runner 如何搭建以及应用,会以后讲).

以下注意几点:

  1. Registry 不支持自签(Self-Signed Cretificate)
     所以不要想着局域网随便在路由器上制定个域名,然后自签证去访问 Registry
     (例如定义  registry-inter.gitlab.com  192.168.2.50, 这种是不行的)
   2. 推荐用 Letsencrypt 做签名,去官网看最新文档.
  3. 不做这个配置,Docker 是只能 pull 不能 push 的
        
( 敲黑板! )

registry_nginx['enable'] = true
registry_nginx['listen_port'] = 5001
registry_nginx['listen_https'] = 5001
registry_nginx['ssl_certificate'] = "/etc/letsencrypt/live/xxx.sn.mynetname.net/fullchain.pem"
registry_nginx['ssl_certificate_key'] = "/etc/letsencrypt/live/xxx.sn.mynetname.net/privkey.pem"

registry_nginx['proxy_set_headers'] = {
  "X-Forwarded-Proto" => "https",
  "X-Forwarded-Ssl" => "on"
}


4. 要把Registry 的 URL开启 
## GitLab URL
##! URL on which GitLab will be reachable.
##! For more details on configuring external_url see:
##! https://docs.gitlab.com/omnibus/settings/configuration.html
external_url 'https://xxx.sn.mynetname.net'
registry_external_url 'https://xxx.sn.mynetname.net:5001'

---------------------------------
效果展示开始~~~
   
  • 按照上面注意的几个点,reconfigure Gitlab 就会开启 Registry
  • Login 自己的 Registry
  • 输入 Gitlab 的用户名和密码
  • docker pull 和 push




[~] $ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[~] $ docker login xxx.sn.mynetname.net:5001
Username (root): root
Password: 
Login Succeeded
[~] $ docker pull xxx.sn.mynetname.net:5001/root/ruby-auto-deploy
Using default tag: latest
latest: Pulling from root/ruby-auto-deploy
53478ce18e19: Pull complete 
205e695b160d: Pull complete 
3053289718c1: Pull complete 
f20587270031: Pull complete 
0216ea0ad0cc: Pull complete 
2462a179c56e: Pull complete 
Digest: sha256:671b5000e5ea039380cc2652cb116b7acc7af670771d5f31ce1745b7520a426f
Status: Downloaded newer image for xxx.sn.mynetname.net:5001/root/ruby-auto-deploy:latest
[~] $ docker -it xxx.sn.mynetname.net:5001/root/ruby-auto-deploy
[~] $ docker push xxx.sn.mynetname.net:5001/root/ruby-auto-deploy
The push refers to a repository [xxx.sn.mynetname.net:5001/root/ruby-auto-deploy]
27e56f3be4b2: Layer already exists 
dc5ac3916e39: Layer already exists 
cc70f6121a9a: Layer already exists 
9fb8cf09ee55: Layer already exists 
7b1b6293e1d4: Layer already exists 
9f8566ee5135: Layer already exists 
latest: digest: sha256:671b5000e5ea039380cc2652cb116b7acc7af670771d5f31ce1745b7520a426f size: 1573
[~] $ docker images
REPOSITORY                                                 TAG                 IMAGE ID            CREATED             SIZE
xxx.sn.mynetname.net:5001/root/ruby-auto-deploy   latest              97e68bc00cfe        2 weeks ago         60.9MB
(这里仅作演示,代表自建服务是可以 push 和 pull 的)

----------------------------------------------------

说个题外话,我司虽然是企业宽带,但是固定 IP还是要额外花钱,那么我就也直接用路由自带的 DDNS 来做域名了.所以用 Letsencrypt 的签名免费真的是一级棒~~




既然这个公众号叫 '略略的什么路由值得买' , 那么我推荐中小型创业公司去购买 MikroTik 出的 路由器,便宜好使. 

--------------------- 我叫分割线 -----------------

是否还有童鞋记得某期文章说要写一个 Gitlab +K8s + CI、CD? 是的~~我没忘,只是懒~~~

评论

  1. 自签证书?可以用的啊。
    在服务端docker的daemon.json里面设置insecure-registry和registry-mirrors(registry-mirrors好像不一定有用,反正我是都设了。)

    https://docs.gitlab.com/ce/administration/container_registry.html#container-registry-domain-configuration
    Using self-signed certificates with Container Registry 这部分

    回复删除
    回复
    1. 我原文表达有误吧,容易产生歧义。
      主要我在测试的时候发现了 CI配合自己的 REGISTRY有问题。

      Using self-signed certificates with Container Registry
      If you’re using a self-signed certificate with your Container Registry, you might encounter issues during the CI jobs like the following:

      Error response from daemon: Get registry.example.com/v1/users/: x509: certificate signed by unknown authority

      The Docker daemon running the command expects a cert signed by a recognized CA, thus the error above.

      While GitLab doesn’t support using self-signed certificates with Container Registry out of the box, it is possible to make it work if you follow Docker’s documentation. You may find some additional information in issue 18239

      删除

发表评论

此博客中的热门博文

最强百元Wi-Fi 路由-----斐讯K2P

 最强百元级无线路由到来--斐讯K2P 先来王炸: 1 K2P闲鱼全新收购价在 100-140 2 K2P是全千兆有线,全千兆有线,全千兆有线!不要问为啥说三遍 3 SoC 是MT7621A 双核四线程 880Mhz 4 Wi-Fi 是单芯片MT7615DN,支持MU-MIMO特性,支持Beamforming 5 外置PA和LNA提升信号覆盖范围 6 众多第三方固件,实用性爆棚 以上六点,咱们一条一条解说。 没有对比,就没有伤害: 1 售价:小米3G,同样硬件的小米3G售价249。号称性价比、发烧的硬件都比这个贵100,还怎么玩? 2 看TP-LINK、FAST、水星的同售价产品,100多元的没千兆,有千兆的都200元起。百兆网口的东西我现在看都不看! 3 说实话,有比MT7621A 880Mhz 1004K核心更好的MIPS32 处理器,就是螃蟹家RTL8198C 1074Kc(8198C另外还多一颗500Mhz用来加速存储的核), 但是很难买到,比如这三款:ASUS RP-AC68U 、D-Link DIR-879、Edge-core ecw3350 。 PS:关于螃蟹 RTL8198C的故事有好几个段子,都是和原厂闲聊得来的...以后有时间再说,今天主角是MTK。 wiki数据: 1074K single core delivers a performance of 1.93 DMIPS/MHz and 3.49 Coremarks/MHz.  1004K single core delivers a performance of 1.6 DMIPS/MHz and 3.05 Coremarks/MHz.  24K core delivers a performance of 1.6 DMIPS/MHz and 3.1 Coremarks/MHz. 虽然只看 DMIPS和 Coremarks/MHz,觉得24Kc和1004Kc差不多,但是区别还是很大的,比如L2 Cache 24Kc就没有, 多线程多核也没有,而且7621A主频提升到了 880Mhz。 这些都是很大的进步。并非简单的胶水双核 :D...

Gitlab CI/CD 之 gitlab-runner 用 Docker 方式如何顺利工作

前阵子,k8s 是搭建起来了.但是这玩意更新太快,3个月后, 以前的方法不好使了...又有新坑了... 干脆就只跑 Docker 吧.... 我擦,Docker也跑不起来了...说 **docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.** 以前只要在 .gitlab-ci.yml 里面填写 标红的就可以了.但是现在不行,会报  Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? services:   - docker:dind variables:   DOCKER_DRIVER: overlay build:   stage: build   script:   - export DOCKER_HOST="tcp://localhost:2375" build-master:   stage: build   script:     - echo $CI_REGISTRY_PASSWORD |  docker login -u "$CI_REGISTRY_USER" $CI_REGISTRY --password-stdin     - docker build . (注意, $CI_REGISTRY 这些环境变量是要填写在 Gitlab web 环境变量中的) 研究后,要在 gitlab-run 的 文件中包含     volumes = ["/cache", "/var/run/docker.sock:/var/run/docker.sock"] sudo cat /etc/gitlab-runner/config.t...