天天看點

Docker 建構私有鏡像倉庫

在使用Docker一段時間後,往往會發現手頭積累了大量的自定義鏡像檔案,這些檔案通過公有倉庫進行管理并不友善,另外有時候隻是希望在内部使用者之間進行分享,不希望暴露出去.這種情況下,就有必要搭建一個本地私有鏡像倉庫,本小結将具體介紹兩個私有倉庫的搭建,其中包括Registry,以及Vmware的Harbor企業倉庫.

♥ 文章聲明 ♥

該系列文章部分文字描述,整理于以下文獻,化繁為簡.

《鳥哥的Linux私房菜 (基礎學習篇 第三版)》 - 作者:鳥哥

《Linux就該這麼學》 - 作者:劉遄

《linux運維之道》- 作者:丁明一

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      

版權聲明:本部落格文章與代碼均為學習時整理的筆記,文章 [均為原創] 作品,轉載請 [添加出處] ,您添加出處是我創作的動力!

繼續閱讀