本文介紹了在離線環境中部署Harbor鏡像倉庫的完整流程。首先通過腳本創建多個Harbor項目,然后使用KubeKey工具將預打包的Kubernetes鏡像(kubesphere.tar.gz)推送到Harbor倉庫。接著配置containerd以支持從私有倉庫拉取鏡像,包括設置TLS證書和鏡像倉庫端點。最后解決Kubernetes 1.26.12安裝過程中pause鏡像的拉取問題,通過重命名本地鏡像的方式替代原本需要從registry.k8s.io獲取的pause鏡像。整個過程涉及Harbor項目創建、鏡像推送、containerd配置和鏡像重命名等關鍵步驟。
離線部署Harbor,詳見我另外一篇博客《k8s:docker compose離線部署haborV2.13.1及采用外部的postgresql及redis數據庫》
一、創建 harbor 項目
#cd /app/KubeSphere/setup
vi create_project_harbor.sh
#!/usr/bin/env bash
# Copyright 2018 The KubeSphere Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# ? ? http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.url="https://dockerhub.kubekey.local" ?# 或修改為實際鏡像倉庫地址
user="admin"
passwd="Harbor12345"harbor_projects=(
? ? ? ? ks
? ? ? ? kubesphere
? ? ? ? kubesphereio
? ? ? ? coredns
? ? ? ? calico
? ? ? ? flannel
? ? ? ? cilium
? ? ? ? hybridnetdev
? ? ? ? kubeovn
? ? ? ? openebs
? ? ? ? library
? ? ? ? plndr
? ? ? ? jenkins
? ? ? ? argoproj
? ? ? ? dexidp
? ? ? ? openpolicyagent
? ? ? ? curlimages
? ? ? ? grafana
? ? ? ? kubeedge
? ? ? ? nginxinc
? ? ? ? prom
? ? ? ? kiwigrid
? ? ? ? minio
? ? ? ? opensearchproject
? ? ? ? istio
? ? ? ? jaegertracing
? ? ? ? timberio
? ? ? ? prometheus-operator
? ? ? ? jimmidyson
? ? ? ? elastic
? ? ? ? thanosio
? ? ? ? brancz
? ? ? ? prometheus
)for project in "${harbor_projects[@]}"; do
? ? echo "creating $project"
? ? curl -u "${user}:${passwd}" -X POST -H "Content-Type: application/json" "${url}/api/v2.0/projects" -d "{ \"project_name\": \"${project}\", \"public\": true}" -k ?# 注意在 curl 命令末尾加上 -k
done
?
#chmod +x create_project_harbor.sh
./create_project_harbor.sh
?
二、推送到harbor倉庫(時間比較久)
#cd /app/KubeSphere/setup
./kk artifact image push -f config-sample.yaml -a /app/KubeSphere/setup/kubesphere.tar.gz?
./kk:KubeKey 是一個用于部署 Kubernetes 和 KubeSphere 集群的工具。
artifact image push:KubeKey 的子命令,專門用于推送預先打包好的 Kubernetes 或 KubeSphere 鏡像到指定的鏡像倉庫。
-f 參數用于指定配置文件路徑。
config-sample.yaml 集群準備的配置文件,其中包含了關于如何連接到私有鏡像倉庫的信息(如認證信息、倉庫地址等),以及其他與集群相關的配置。
-a 參數用于指定本地的鏡像歸檔文件路徑。
/app/KubeSphere/setup/kubesphere.tar.gz 是生成的包含所有必要鏡像的壓縮包。這個文件通常包含了搭建 Kubernetes 或 KubeSphere 所需的所有 Docker 鏡像,
?注:kubesphere.tar.gz 的構建,詳見《K8s:離線部署Kubernetes1.26.12及采用外部Harbor》的第二章。
三、編輯 containerd 的配置文件(通常位于 /etc/containerd/config.toml):
為了保證通過crictl能從harbor拉取jar,需要進行如下配置:
vi /etc/containerd/config.toml
disabled_plugins = []
[plugins."io.containerd.grpc.v1.cri"]
? enable_selinux = false
? selinux_category_range = 1024
? disable_seccomp = true
? sandbox_image = "172.23.123.117:8443/kubesphereio/pause:3.9"
[plugins."io.containerd.grpc.v1.cri".registry]
?[plugins."io.containerd.grpc.v1.cri".registry.configs]
? [plugins."io.containerd.grpc.v1.cri".registry.configs."172.23.123.117:8443"]
? ? tls = true
? ? cert_file = "/etc/containerd/certs.d/172.23.123.117:8443/172.23.123.117.cert"
? ? key_file = "/etc/containerd/certs.d/172.23.123.117:8443/172.23.123.117.key"
? ? ca_file = "/etc/containerd/certs.d/172.23.123.117:8443/ca.crt"
? ? skip_verify = false
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
? [plugins."io.containerd.grpc.v1.cri".registry.mirrors."registry.k8s.io"]
? ? endpoint = ["https://172.23.123.117:8443"]
? [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
? ? endpoint = ["https://172.23.123.117:8443"]
重啟
sudo systemctl daemon-reload
sudo systemctl restart containerd
?四.增加registry.k8s.io/pause
在安裝Kubernetes1.26.12時,需要registry.k8s.io/pause,因為是離線環境,所以手動構建.
從私有庫拉取3.9版本的pause
crictl pull 172.23.123.117:8443/kubesphereio/pause:3.9
通過打標簽方式,將3.9版本的pause變成 registry.k8s.io/pause:3.8,從而解決離線環境拉取不到 registry.k8s.io/pause:3.8的問題。
ctr -n=k8s.io image tag 172.23.123.117:8443/kubesphereio/pause:3.9 registry.k8s.io/pause:3.8