在以前幾篇文章中,使用過不少鏡像建立實例,可是你們有沒有發現如下幾個問題node
● 拉取的鏡像只在當前服務器,只能本地使用git
● 多個服務器部署同一個應用時,會拉取屢次鏡像github
● 應用集羣節點數過多,同一時間去拉取會消耗很大的網絡出口帶寬docker
● 命名規範性,一臺服務器上部署多個應用模塊,則命名須要很強的規範性json
● 不便管理,一臺服務器上部署多個項目模塊時,沒法對每一個項目的模塊進行區分api
Yum倉庫,當本地須要去拉取依賴包時只須要進行認證、配置就能夠直接下載。而Docker 倉庫,就是與Yum起到相同功能的角色,一個是解決系統環境依賴包問題,一個是解決鏡像管理、拉取問題bash
● 第三方遠程倉庫服務器
Docker Hub、阿里雲、網易雲倉庫,鏡像存儲在第三方雲上網絡
● Docker Repositoryapp
Docker自身提供的一款私有倉庫,沒有Web Ui管理頁面
● 開源技術實現
Harbor,提供豐富的Api操做接口以及完善的Web UI功能
本文着重介紹"Harbor"倉庫的實現
https://github.com/goharbor/harbor/releases
docker 17.06.0-ce +和docker-compose 1.18.0+
[root@bjtn-app183-214 Harbor]# docker info | grep -i "server version" Server Version: 18.06.3-ce -rwxr-x--- 1 root root 643358224 May 2 14:32 harbor-offline-installer-v1.10.2.tgz [root@bjtn-app183-214 Harbor]# pwd /opt/app/Harbor [root@bjtn-app183-214 Harbor]# ll total 628280 -rwxr-x--- 1 root root 643358224 May 2 14:32 harbor-offline-installer-v1.10.2.tgz
[root@bjtn-app183-214 Harbor]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 423 100 423 0 0 337 0 0:00:01 0:00:01 --:--:-- 337 100 16.2M 100 16.2M 0 0 647k 0 0:00:25 0:00:25 --:--:-- 702k [root@bjtn-app183-214 Harbor]# chmod +x /usr/local/bin/docker-compose && sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose [root@bjtn-app183-214 Harbor]# docker-compose -v docker-compose version 1.25.1, build a82fef07
docker、maven對接Harbor默認是https
[root@bjtn-app183-214 cert]# pwd /opt/app/Harbor/cert [root@bjtn-app183-214 cert]# openssl genrsa -out docker-repo.key 4096 Generating RSA private key, 4096 bit long modulus .............................................................................................................................................................................................................................++ ....................................................................++ e is 65537 (0x10001) [root@bjtn-app183-214 cert]# openssl req -x509 -new -nodes -sha512 -days 36500 \ > -subj "/C=TW/ST=Taipei/L=Taipei/O=example/OU=Personal/CN=docker-repo.com" \ > -key docker-repo.key \ > -out docker-repo.crt [root@bjtn-app183-214 cert]# ll total 8 -rw-r----- 1 root root 2033 May 2 14:44 docker-repo.crt -rw-r----- 1 root root 3243 May 2 14:43 docker-repo.key
解壓tar包 [root@bjtn-app183-214 Harbor]# pwd /opt/app/Harbor [root@bjtn-app183-214 Harbor]# tar -xf harbor-offline-installer-v1.10.2.tgz [root@bjtn-app183-214 Harbor]# mv harbor/* ./ [root@bjtn-app183-214 Harbor]# rm -rf harbor 修改配置 hostname: 本地主機IP https: https端口以及證書文件 port: certificate: 指明crt祕鑰文件 private_key: 指明key祕鑰文件 data_volume: 數據存儲路徑 harbor_admin_password: 登陸Harbor Web UI密碼 進行初始化,會拉取鏡像文件 [root@bjtn-app183-214 Harbor]# ./prepare prepare base dir is set to /opt/app/Harbor Unable to find image 'goharbor/prepare:v1.10.2' locally v1.10.2: Pulling from goharbor/prepare ae8395e171fb: Pull complete ca35e8aaa3ab: Pull complete 40fe63fa9717: Pull complete b716a0e0062b: Pull complete 611e775dac46: Pull complete 456ee3045669: Pull complete 3dfaed1ae811: Pull complete 進行安裝 ./install.sh
修改docker啓動配置,新添Harbor倉庫地址 --insecure-registry harbor的主機IP,筆者認爲每一臺docker客戶端都須要配置
ExecStart=/usr/bin/dockerd --graph /opt/app/Docker --insecure-registry https://10.10.183.214
重啓Docker Daemon
systemctl daemon-reload systemctl restart docker
進行登陸認證
[root@bjtn-app183-214 Harbor]# docker login 10.10.183.214 Username: admin Password: WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded
命令格式:docker tag [SOURCE_IMAGE][:TAG] [HarborIp]/[Repository]/IMAGE[:TAG]
這裏以rancher-server爲例
[root@bjtn-app183-214 ~]# docker images | grep rancher rancher/server v1.6.12 f61683a6ad09 3 years ago 968MB [root@bjtn-app183-214 ~]# docker tag rancher/server:v1.6.12 10.10.183.214/docker-harbor-test1/docker-rancher-server:version1 [root@bjtn-app183-214 ~]# docker images | grep rancher 10.10.183.214/docker-harbor-test1/docker-rancher-server version1 f61683a6ad09 3 years ago 968MB rancher/server
命令格式:docker push [HarborIp]/[Repository]/[Image]:[Tag]
[root@bjtn-app183-214 ~]# docker push 10.10.183.214/docker-harbor-test1/docker-rancher-server:version1 The push refers to repository [10.10.183.214/docker-harbor-test1/docker-rancher-server] b05601ba6e00: Pushed 753ee21c6bb5: Pushed .... version1: digest: sha256:627047da2ec626d3ce9f090834c8e29274c0899df962abbeee8a06ee0617e8c5 size: 4302
下載命令格式:docker pull [HarborIp]/[Repository]/[Image]:[Tag]
刪除本地與"rancher"相關的鏡像
[root@bjtn-app183-214 ~]# docker rmi `docker images | grep -i "rancher" | awk '{print $3}' | xargs` --force Untagged: 10.10.183.214/docker-harbor-test1/docker-rancher-server:version1 Untagged: 10.10.183.214/docker-harbor-test1/docker-rancher-server@sha256:627047da2ec626d3ce9f090834c8e29274c0899df962abbeee8a06ee0617e8c5 Untagged: rancher/server:v1.6.12 Untagged: rancher/server@sha256:19df74c0a542f3c105648436a806cc78a6db89c96f5421d0eaecfb5dd5947bce Deleted: sha256:f61683a6ad0906c80b9e2c698842ba8c55f971f68aa03ad6cc8712358fd74de1
docker客戶端下載"rancher"鏡像
[root@bjtn-app183-214 ~]# docker pull 10.10.183.214/docker-harbor-test1/docker-rancher-server:version1 version1: Pulling from docker-harbor-test1/docker-rancher-server c314617ce3f1: Already exists 2389af727e43: Already exists ... Digest: sha256:627047da2ec626d3ce9f090834c8e29274c0899df962abbeee8a06ee0617e8c5 Status: Downloaded newer image for 10.10.183.214/docker-harbor-test1/docker-rancher-server:version1 [root@bjtn-app183-214 ~]# docker images | grep -i "rancher" 10.10.183.214/docker-harbor-test1/docker-rancher-server version1 f61683a6ad09 3 years ago 968MB
說明: Harbor倉庫顯示的鏡像大小隻有"334.86MB",可是下載到Docker客戶端本地後大小有"968MB",我的理解是Harbor倉庫只展現了最外層鏡像的大小,沒有展現內部封裝或者裏依賴鏡像大小
● 倉庫中鏡像的tag已[server]:[time](服務名稱:時間戳)規範命名
● 筆者在項目中部署時未使用"latest"版本,部署老是有問題(未解決),在部署中以最新時間戳的tag進行部署,後續可根據業務需求進行更改
● mvn編譯+上傳鏡像到Harbor是在代碼層實現,筆者只是實現了拉取+部署,後續能夠調研下"jenkins docker cloud"插件使用
● 關於其餘Api接口使用方法能夠自行研究下
#!/bin/bash # Created on Wed Mar 3 17:25:18 CST 2021 # author:wtc # haror 認證信息 harbor_host="$ip" harbor_user="$user" harbor_pass="$pass" # 獲取全部項目信息 get_all_project_id() { project_id_data=`curl -s -u "$harbor_user:$harbor_pass" -X GET --insecure --header 'Accept: application/json' "https://$harbor_host/api/search" | jq ".project" | jq ".[].project_id" | xargs` echo "$project_id_data" } # 獲取全部倉庫信息 get_all_repo_name() { for project_id in $project_id_data;do repo_name_data=`curl -s -u "$harbor_user:$harbor_pass" -X GET --insecure --header 'Accept: application/json' "https://$harbor_host/api/repositories?project_id=$project_id" | jq .[].name | xargs` echo "$repo_name_data" done } repo_name_data=`get_all_repo_name` # 保留近10個最新鏡像文件,包含latest get_all_image_tag() { for repo_name in $repo_name_data;do tag_data=`curl -s -u "$harbor_user:$harbor_pass" -X GET --insecure --header 'Accept: application/json' "https://$harbor_host/api/repositories/$repo_name/tags" | jq ".[].name" | xargs` array_tag=(`echo $tag_data`) # 控制保留歷史版本個數 if [ ${#array_tag[@]} -le 10 ];then echo "$repo_name 倉庫的鏡像數未超過10個,不作處理!" continue else for ((tag_index=0;tag_index<=`expr ${#array_tag[@]} - 10`;tag_index++));do tag_name=${array_tag[$tag_index]} if [ "$tag_name" == "latest" ];then continue else curl -s -u "$harbor_user:$harbor_pass" -X DELETE --insecure --header 'Accept: application/json' "https://$harbor_host/api/repositories/$repo_name/tags/$tag_name" if [ $? -eq 0 ];then echo "$repo_name 倉庫的鏡像${array_tag[$tag_index]}刪除成功!" else echo "$repo_name 倉庫的鏡像${array_tag[$tag_index]}刪除失敗!" fi fi done fi done } # 刪除|部署 if [ "$#" -ne 6 ];then # 開啓是否刪除鏡像 get_all_image_tag else # 項目名稱在代碼中寫死,經過Jenkins傳參 ${project}、${mould}、${PortMapping}、${YamlPath}、${YamlName}、${LogPath} echo "$@" # 部署由jenkins 個性化傳參,這裏不作展現 fi