kubernetes搭建Harbor無坑及Harbor倉庫同步


一、helm搭建harbor

1.安裝helm

1.1.安裝helm客戶端

tar -zxvf helm-v2.14.3-linux-amd64.tar.gz

mv linux-amd64/helm /usr/local/bin/

chmod +x /usr/local/bin/helm

helm version

1.2.安裝helm tiller(server)端

1.2.1.為應用程序設置serviceaccount

kubectl create -f helm-rbac-config.yaml
kubectl patch deploy --namespace kube-system tiller-deploy -p '{"spec":{"template":{"spec":{"serviceAccount":"tiller"}}}}'

1.2.2.安裝tiller

helm init --upgrade -i registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.3 --stable-repo-url [https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts](https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts)

1.2.3.卸載Helm鏡像等k8s資源

kubectl get -n kube-system secrets,sa,clusterrolebinding -o name|grep tiller|xargs kubectl -n kube-system delete kubectl get all -n kube-system -l app=helm -o name|xargs kubectl delete -n kube-system

2. 安裝Harbor

下載 harbor-helm

git clone https://github.com/goharbor/harbor-helm.git
cd XXX/harbor-helm

2.1.設置value.yml

更改掛載卷,為nfs

sed -i 's/storageClass: ""/storageClass: "nfs"/g' values.yaml

更改掛載卷大小/size

persistence:
  enabled: true
  # Setting it to "keep" to avoid removing PVCs during a helm delete
  # operation. Leaving it empty will delete PVCs after the chart deleted
  resourcePolicy: "keep"
  persistentVolumeClaim:
    registry:
      # Use the existing PVC which must be created manually before bound,
      # and specify the "subPath" if the PVC is shared with other components
      existingClaim: ""
      # Specify the "storageClass" used to provision the volume. Or the default
      # StorageClass will be used(the default).
      # Set it to "-" to disable dynamic provisioning
      storageClass: "nfs"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 5Gi
    chartmuseum:
      existingClaim: ""
      storageClass: "nfs"
      subPath: ""
      accessMode: ReadWriteOnce
      size: 5Gi

更改Harbor暴露方式和域名

ingress:
    hosts:
      core: core.harbor.domain
      notary: notary.harbor.domain
externalURL: https://core.harbor.domain

更改Harbor密碼

harborAdminPassword: "Harbor12345"

若為非存儲卷,創建pv

for i in {1..5}; do

cat <<EOF | kubectl apply -f -

apiVersion: v1

kind: PersistentVolume

metadata:

name: pv00${i}

spec:

capacity:

storage: 50Gi

accessModes:

- ReadWriteOnce #需要注意

persistentVolumeReclaimPolicy: Recycle

nfs:

path: /volume1/harbor/nfs${i}

server: 10.8.4.133

EOF

done

2.2.安裝harbor並將日志寫入文件,可編輯文件保留.yaml編排文件,以便以后使用

helm install . --debug --name hub |sed 'w harbor.yaml'

2.3.卸載Harbor

helm del hub --purge

kubectl get pod,pv,pvc -o name|grep hub |xargs kubectl delete

2.4.若Pod未啟動成功

2.4.1.查看hub-harbor-database-0 日志

kubectl logs -f hub-harbor-database-0

kubectl logs hub-harbor-database-0 -p

harbor-db日志

若Pod啟動失敗,報找不到以上database的錯誤,是有db還未啟動成功,在Pod探針失敗的情況下強制重啟了Pod,導致3個Database腳本沒有執行完畢,所以加長探針開始掃描時間和超時時間。(這個問題遇到的的確很坑)

kubectl delete statefulset hub-harbor-database

更改harbor.yaml中圖片部分hub-harbor-database的編排文件的initialDelaySeconds、periodSeconds兩個屬性
harbor.yaml編配文件

 kubectl applay -f harbor.yaml

等待Pod全部啟動完成如圖

Pod

2.5.Harbor訪問及使用

查看ingress

kubectl get ingress

如圖配置域名
ingress
hosts

訪問https://core.harbor.agree ,如圖所示

Harbor

3.配置docker訪問密匙

kubectl get secrets/hub-harbor-ingress -o jsonpath="{.data.ca\.crt}" | base64 --decode

如圖
image.png

`mkdir /etc/docker/certs.d/core.harbor.agree -p`
kubectl get secrets/hub-harbor-ingress -o jsonpath="{.data.ca\.crt}" | base64 --decode|sed 'w /etc/docker/certs.d/core.harbor.agree/ca.crt'
docker login core.harbor.agree

初次登陸 用戶名為admin 密碼默認為Harbor12345
docker login

創建項目如圖
harbor

`docker push core.harbor.agree/dev/busybox:1.27`

至此Harbor安裝完成。

4.配置Harbor同步管理

其他區域服務器同上部署Harbor倉庫

倉庫管理-新建目標,如圖,此時目標URL不可訪問,請增加如下配置
harbor

執行

`kubectl edit cm coredns -n kube-system`

kubectl edit cm coredns

如圖增加:

  hosts {

          10.8.4.131 core.harbor.agree

          fallthrough

        }

hosts與/etc/hosts配置的ingress一致。

測試連接,如圖,點擊確定
harbor

選擇同步管理-新建規則,如圖:
harbor

規則:

1、從目標服務器拉取鏡像/推送鏡像

2、同步以名稱、tag、資源過濾后的鏡像

3、選擇目標服務器

4、選擇目標服務器Harbor的Namespace

5、選擇觸發模式:

l 手動觸發

l 定時:

      從一日開始,每月執行一次:0 0 0 0 1/1 ?

本月最后一天執行:0 0 0 L ? ?

每周周六凌晨執行:0 0 0 0 0 6 *

定時為Cron表達式,可在http://cron.qqe2.com/線生成

確認后,生產規則

選擇規則,點擊同步,如圖:
harbor

如果失敗,請進入同步任務,查看具體日志。
harbor
harbor

5.Harbor鏡像刪除及GC回收

5.1. Harbor鏡像刪除

項目-鏡像倉庫,點擊鏡像,選擇鏡像標簽,點擊刪除-確認
harbor

5.2.Harbor GC回收

** 鏡像標簽刪除后,鏡像文件仍然保存在Harbor中,需要通過垃圾回收進行徹底刪除鏡像。**
操作步驟
任務-垃圾清理
harbor
立即清理垃圾,或者定時清理垃圾。

由於完整的k8s搭建Harbor資料很少,作者也入了不少坑,如若本篇文章對大家有所幫助,希望留下您的喜歡


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM