KubeKey 離線部署 KubeSphere v3.4.1 和 K8s v1.26 實戰指南

作者:運維有術

前言

知識點

  • 定級:入門級
  • 瞭解清單 (manifest) 和製品 (artifact) 的概念
  • 掌握 manifest 清單的編寫方法
  • 根據 manifest 清單製作 artifact
  • KubeKey 離線集羣配置文件編寫
  • KubeKey 離線部署 Harbor
  • KubeKey 離線部署 KubeSphere 和 K8s
  • KubeKey 離線部署常見問題排查處理

實戰服務器配置

主機名 IP CPU 內存 系統盤 數據盤 用途
ksp-master-1 192.168.9.91 8 16 40 100 離線環境 KubeSphere/k8s-master
ksp-master-2 192.168.9.92 8 16 40 100 離線環境 KubeSphere/k8s-master
ksp-master-3 192.168.9.93 8 16 40 100 離線環境 KubeSphere/k8s-master
ksp-registry 192.168.9.90 4 8 40 100 離線環境鏡像倉庫節點(Harbor)
ksp-deploy 192.168.9.89 4 8 40 100 聯網主機用於製作離線包
合計 4 32 64 200 500

實戰環境涉及軟件版本信息

  • 操作系統:CentOS 7.9 x86_64
  • KubeSphere:v3.4.1
  • K8s:v1.26.5
  • Containerd:1.6.4
  • KubeKey: v3.0.13
  • Harbor:2.5.3

1. 簡介

KubeKey 從 v2.1.0 版開始新增了清單 (manifest) 和製品 (artifact) 的概念,爲用戶離線部署 KubeSphere 和 K8s 集羣提供了一種簡單便捷的解決方案。

manifest 是一個描述當前 Kubernetes 集羣信息和定義 artifact 製品中需要包含哪些內容的文本文件。

使用 KubeKey,用戶只需使用清單 manifest 文件來定義將要離線部署的集羣環境需要的內容,再通過該 manifest 來導出製品 artifact 文件即可完成準備工作。離線部署時只需要 KubeKey 和 artifact 就可快速、簡單的在環境中部署鏡像倉庫 Harbor 和 KubeSphere 以及 K8s 集羣。

KubeKey 生成 manifest 文件有兩種方式:

第一種方式的好處是可以根據 1:1 的運行集羣構建離線集羣,依賴於已有集羣,靈活度不夠,並不是所有人都具備這種條件。

因此,本文參考官方的離線文檔,採用手寫 manifest 清單文件的方式,實現離線環境的安裝部署。

2. 離線部署資源製作

製作離線部署資源需要找一臺能聯通互聯網的節點,本文爲了資源的製作和離線部署驗證,單獨增加了一個能聯網的 ksp-deploy 節點。

在該節點下載 KubeKey (下文簡稱 KK)最新版(v3.0.13)。具體 KK 版本號可以在 KubeKey 發行頁面 查看。

2.1. 下載 KubeKey

  • 下載最新版的 KubeKey
cd ~
mkdir kubekey
cd kubekey/

# 選擇中文區下載(訪問 GitHub 受限時使用)
export KKZONE=cn

# 執行下載命令,獲取最新版的 kk(受限於網絡,有時需要執行多次)
curl -sfL https://get-kk.kubesphere.io | sh -

# 也可以使用下面的命令指定具體版本
curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.13 sh -

2.2. 獲取 manifest 模板

manifest 文件的編寫可以參考 官方示例文檔。有兩個可用參考用例,一個簡單版,一個完整版。參考簡單版即可。

受限於篇幅,本文不展示原始的示例文件,建議讀者仔細閱讀官方示例,理解每一項配置的含義後根據需求改寫(暫時無法理解的,可以直接使用下文提供的成品配置文件)。

2.3. 獲取 images-list 及可裁剪性分析

執行下面的命令獲取官方 releases v3.4.1 對應的 images-list(最終實驗結果,一些鏡像需要自行整理,完整的鏡像列表可參考下文中的 manifest 文件)。

wget https://github.com/kubesphere/ks-installer/releases/download/v3.4.1/images-list.txt

完整的 Image(136 個) 分類及可裁剪性(必須留的有標粗,個人判斷,未必精準):

  • kubesphere-images(18 個,不可裁剪
  • kubeedge-images(3 個,可裁剪,取決於是否啓用 kubeedge)
  • gatekeeper-images(1 個,可裁剪,取決於是否啓用 gatekeeper,建議保留)
  • openpitrix-images(1 個,可裁剪但是基本都會用到,建議保留
  • kubesphere-devops-images(45 個,構建用的開發環境鏡像可裁剪,前綴帶 builder-、tomcat85-、java-、nodejs-、python- 這一類的都可以酌情處理)
  • kubesphere-monitoring-images(14 個,不可裁剪
  • kubesphere-logging-images(15 個,能裁剪的也就是 elasticsearch- 和 opensearch 開頭的,KubeSphere v3.4.1 默認選擇 opensearch,建議都保留)
  • istio-images(9 個,可裁剪,取決於是否啓用 istio,建議保留)
  • example-images(13 個,可裁剪)
  • weave-scope-images(1 個,可裁剪,取決於是否啓用 weave)
  • 官方列表中未列出的核心 images(12 個,必須,否則部署時報錯)
  • **官方列表中未列出的必要 images **(4 個,必須,否則部署時報錯)

爲了保持完整性,本文使用了所有 Image,只是修改了鏡像前綴爲 registry.cn-beijing.aliyuncs.com/kubesphereio,修改後的完整的鏡像列表在下面的 manifest 文件中展示,讀者可根據需求裁剪。

2.4. 獲取操作系統依賴包

本實驗環境使用的操作系統是 x64 的 CentOS 7.9,所以只下載 centos7 的操作系統依賴包,其他操作系統請讀者在 KubeKey releases 頁面下載。

執行下面的命令,在能聯網的部署服務器上執行下載。網絡訪問受限時,也可以通過其他方式,將該 ISO 下載後放到製作離線鏡像的服務器的 /root/kubekey 目錄下。

wget https://github.com/kubesphere/kubekey/releases/download/v3.0.12/centos7-rpms-amd64.iso

說明:KubeKey v3.0.13 的 release 中沒包,只能在 v3.0.12 的 releases 中下載。

最終的 ISO(centos7-rpms-amd64.iso,314 MB)實際信息如下:

# 查看文件大小
[root@ksp-deploy kubekey]# ll -h centos7-rpms-amd64.iso
-rw-r--r--. 1 root root 315M Oct 23 18:21 centos7-rpms-amd64.iso

# 驗證 sha256sum,確保 ISO 在下載過程中沒出問題(官方提供的 sha256sum 信息在 https://github.com/kubesphere/kubekey/releases/download/v3.0.12/centos7-rpms.iso.sha256sum.txt)
[root@ksp-deploy kubekey]# sha256sum centos7-rpms-amd64.iso
2588fbc12acc9f3b95766a0c20382988f2a21da2a36e444b7e1a0f523e75f858  centos7-rpms-amd64.iso

2.5. 生成 manifest 文件

根據上面的文件及相關信息,生成最終 manifest.yaml

命名爲 ksp-v3.4.1-manifest.yaml

apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Manifest
metadata:
  name: sample
spec:
  arches:
  - amd64
  operatingSystems:
  - arch: amd64
    type: linux
    id: centos
    version: "7"
    osImage: CentOS Linux 7 (Core)
    repository:
      iso:
        localPath: "/root/kubekey/centos7-rpms-amd64.iso"
        url:
  kubernetesDistributions:
  - type: kubernetes
    version: v1.26.5
  components:
    helm: 
      version: v3.9.0
    cni: 
      version: v1.2.0
    etcd: 
      version: v3.4.13
    calicoctl:
      version: v3.26.1
    containerRuntimes:
    - type: docker
      version: 20.10.23
    - type: containerd
      version: 1.6.4
    crictl: 
      version: v1.24.0
    docker-registry:
      version: "2"
    harbor:
      version: v2.5.3
    docker-compose:
      version: v2.2.2
  images:
  - registry.cn-beijing.aliyuncs.com/kubesphereio/ks-installer:v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/ks-apiserver:v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/ks-console:v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/ks-controller-manager:v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kubectl:v1.20.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kubefed:v0.8.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/tower:v0.2.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/minio:RELEASE.2019-08-07T01-59-21Z
  - registry.cn-beijing.aliyuncs.com/kubesphereio/mc:RELEASE.2019-08-07T23-14-43Z
  - registry.cn-beijing.aliyuncs.com/kubesphereio/snapshot-controller:v4.0.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/nginx-ingress-controller:v1.3.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/defaultbackend-amd64:1.4
  - registry.cn-beijing.aliyuncs.com/kubesphereio/metrics-server:v0.4.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/redis:5.0.14-alpine
  - registry.cn-beijing.aliyuncs.com/kubesphereio/haproxy:2.0.25-alpine
  - registry.cn-beijing.aliyuncs.com/kubesphereio/alpine:3.14
  - registry.cn-beijing.aliyuncs.com/kubesphereio/openldap:1.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/netshoot:v1.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/cloudcore:v1.13.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/iptables-manager:v1.13.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/edgeservice:v0.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/gatekeeper:v3.5.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/openpitrix-jobs:v3.3.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/devops-apiserver:ks-v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/devops-controller:ks-v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/devops-tools:ks-v3.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/ks-jenkins:v3.4.0-2.319.3-1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/inbound-agent:4.10-2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-base:v3.2.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-nodejs:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-maven:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-maven:v3.2.1-jdk11
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-python:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.16
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.17
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.18
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-base:v3.2.2-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-nodejs:v3.2.0-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-maven:v3.2.0-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-maven:v3.2.1-jdk11-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-python:v3.2.0-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.0-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.16-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.17-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/builder-go:v3.2.2-1.18-podman
  - registry.cn-beijing.aliyuncs.com/kubesphereio/s2ioperator:v3.2.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/s2irun:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/s2i-binary:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/tomcat85-java11-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/tomcat85-java11-runtime:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/tomcat85-java8-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/tomcat85-java8-runtime:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/java-11-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/java-8-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/java-8-runtime:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/java-11-runtime:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/nodejs-8-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/nodejs-6-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/nodejs-4-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/python-36-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/python-35-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/python-34-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/python-27-centos7:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/argocd:v2.3.3
  - registry.cn-beijing.aliyuncs.com/kubesphereio/argocd-applicationset:v0.4.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/dex:v2.30.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/redis:6.2.6-alpine
  - registry.cn-beijing.aliyuncs.com/kubesphereio/configmap-reload:v0.7.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus:v2.39.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus-config-reloader:v0.55.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/prometheus-operator:v0.55.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-rbac-proxy:v0.11.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-state-metrics:v2.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/node-exporter:v1.3.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/alertmanager:v0.23.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/thanos:v0.31.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/grafana:8.3.3
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-rbac-proxy:v0.11.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/notification-manager-operator:v2.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/notification-manager:v2.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/notification-tenant-sidecar:v3.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/elasticsearch-curator:v5.7.6
  - registry.cn-beijing.aliyuncs.com/kubesphereio/opensearch-curator:v0.0.5
  - registry.cn-beijing.aliyuncs.com/kubesphereio/elasticsearch-oss:6.8.22
  - registry.cn-beijing.aliyuncs.com/kubesphereio/opensearch:2.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/opensearch-dashboards:2.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/fluentbit-operator:v0.14.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/docker:19.03
  - registry.cn-beijing.aliyuncs.com/kubesphereio/fluent-bit:v1.9.4
  - registry.cn-beijing.aliyuncs.com/kubesphereio/log-sidecar-injector:v1.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/filebeat:6.7.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-events-operator:v0.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-events-exporter:v0.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-events-ruler:v0.6.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-auditing-operator:v0.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-auditing-webhook:v0.2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/pilot:1.14.6
  - registry.cn-beijing.aliyuncs.com/kubesphereio/proxyv2:1.14.6
  - registry.cn-beijing.aliyuncs.com/kubesphereio/jaeger-operator:1.29
  - registry.cn-beijing.aliyuncs.com/kubesphereio/jaeger-agent:1.29
  - registry.cn-beijing.aliyuncs.com/kubesphereio/jaeger-collector:1.29
  - registry.cn-beijing.aliyuncs.com/kubesphereio/jaeger-query:1.29
  - registry.cn-beijing.aliyuncs.com/kubesphereio/jaeger-es-index-cleaner:1.29
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kiali-operator:v1.50.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kiali:v1.50
  - registry.cn-beijing.aliyuncs.com/kubesphereio/busybox:1.31.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/nginx:1.14-alpine
  - registry.cn-beijing.aliyuncs.com/kubesphereio/wget:1.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/hello:plain-text
  - registry.cn-beijing.aliyuncs.com/kubesphereio/wordpress:4.8-apache
  - registry.cn-beijing.aliyuncs.com/kubesphereio/hpa-example:latest
  - registry.cn-beijing.aliyuncs.com/kubesphereio/fluentd:v1.4.2-2.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/perl:latest
  - registry.cn-beijing.aliyuncs.com/kubesphereio/examples-bookinfo-productpage-v1:1.16.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/examples-bookinfo-reviews-v1:1.16.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/examples-bookinfo-reviews-v2:1.16.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/examples-bookinfo-details-v1:1.16.2
  - registry.cn-beijing.aliyuncs.com/kubesphereio/examples-bookinfo-ratings-v1:1.16.3
  - registry.cn-beijing.aliyuncs.com/kubesphereio/scope:1.13.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/pause:3.8
  - registry.cn-beijing.aliyuncs.com/kubesphereio/pause:3.9
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-apiserver:v1.26.5
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-controller-manager:v1.26.5
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-scheduler:v1.26.5
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-proxy:v1.26.5
  - registry.cn-beijing.aliyuncs.com/kubesphereio/k8s-dns-node-cache:1.15.12
  - registry.cn-beijing.aliyuncs.com/kubesphereio/coredns:1.9.3
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kube-controllers:v3.26.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/cni:v3.26.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/node:v3.26.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/pod2daemon-flexvol:v3.26.1
  - registry.cn-beijing.aliyuncs.com/kubesphereio/haproxy:2.3
  - registry.cn-beijing.aliyuncs.com/kubesphereio/provisioner-localpv:3.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/linux-utils:3.3.0
  - registry.cn-beijing.aliyuncs.com/kubesphereio/kubectl:v1.22.0
  kubectl:v1.22.0
  registry:
    auths: {}

manifest 修改說明

  • 最後的 16 個鏡像就是官方 images-list 文件缺失的鏡像,一定要手工補充在 list 中

  • kubernetes 版本:v1.26.5

  • 其他組件版本的選擇:個人是根據 kubekey 在線安裝過程的日誌,查找相關組件的對應版本,以及參考官方的文檔 組件默認版本說明組件默認版本源碼支持的組件列表製品清單源碼

  • 開啓 harbordocker-compose 配置項,爲後面通過 KubeKey 自建 harbor 倉庫推送鏡像使用。

  • 默認創建的 manifest 裏面的鏡像列表從 docker.io 獲取,替換前綴爲 registry.cn-beijing.aliyuncs.com/kubesphereio

  • 若需要導出的 artifact 文件中包含操作系統依賴文件(如:conntarck、chrony 等),可在 operationSystem 元素中的 .repostiory.iso.url 中配置相應的 ISO 依賴文件下載地址爲 localPath ,填寫提前下載好的 ISO 包在本地的存放路徑,並將 url 配置項置空。

  • 您可以訪問 https://github.com/kubesphere/kubekey/releases/tag/v3.0.12 下載 ISO 文件。

2.6. 導出製品 artifact

製品 (artifact) 說明

  • 製品(artifact)是一個根據指定的 manifest 文件內容導出的包含鏡像 tar 包和相關二進制文件的 tgz 包。

  • 在 KubeKey 初始化鏡像倉庫、創建集羣、添加節點和升級集羣的命令中均可指定一個 artifact,KubeKey 將自動解包該 artifact 並在執行命令時直接使用解包出來的文件。

  • 導出時請確保網絡連接正常。

  • KubeKey 會解析鏡像列表中的鏡像名,若鏡像名中的鏡像倉庫需要鑑權信息,可在 manifest 文件中的 .registry.auths 字段中進行配置(本文未配置)。

根據生成的 manifest,執行下面的命令製作製品(artifact)。

export KKZONE=cn
./kk artifact export -m ksp-v3.4.1-manifest.yaml -o ksp-v3.4.1-artifact.tar.gz

製作完成後,我們簡單查看一下製作的製品包含哪些內容。

  • 查看 KubeKey 目錄內容
# 製作完成後查看結果
[root@ksp-deploy kubekey]# ls -lh
total 14G
-rw-r--r--. 1 root root 315M Oct 23 18:21 centos7-rpms-amd64.iso
-rwxr-xr-x. 1 root root  76M Nov  7 16:43 kk
-rw-r--r--. 1 root root  13G Dec 12 13:08 ksp-v3.4.1-artifact.tar.gz
-rw-r--r--. 1 root root  11K Dec 12 11:23 ksp-v3.4.1-manifest.yaml
drwxr-xr-x. 3 root root   18 Dec 12 13:08 kubekey
-rw-r--r--. 1 root root  35M Dec  8 10:33 kubekey-v3.0.13-linux-amd64.tar.gz

# 製作過程中捕捉的內容,可能不全(製作完成後 kubekey/artifact 目錄會被清理)
[root@ksp-deploy kubekey]# du -sh kubekey/*
13G     kubekey/artifact
88K     kubekey/logs
[root@ksp-deploy kubekey]# du -sh kubekey/artifact/*
102M    kubekey/artifact/cni
43M     kubekey/artifact/containerd
14M     kubekey/artifact/crictl
130M    kubekey/artifact/docker
17M     kubekey/artifact/etcd
45M     kubekey/artifact/helm
12G     kubekey/artifact/images
207M    kubekey/artifact/kube
660M    kubekey/artifact/registry
315M    kubekey/artifact/repository
9.0M    kubekey/artifact/runc
  • 查看製品大小(全鏡像,製品包居然達到了 13G,生產環境還是有選擇的裁剪吧)
[root@ksp-deploy kubekey]# ls -lh ksp-v3.4.1-artifact.tar.gz
-rw-r--r--. 1 root root 13G Dec 12 13:08 ksp-v3.4.1-artifact.tar.gz

2.7. 導出 KubeKey

把 KubeKey 工具也製作成壓縮包,便於拷貝到離線節點。

# 壓縮
[root@ksp-deploy kubekey]# tar zcvf kubekey-v3.0.13.tar.gz kk kubekey-v3.0.13-linux-amd64.tar.gz

2.8. Kernel 升級包

CentOS 7.9 的默認內核比較老,建議升級操作系統內核(具體是否升級請根據實際環境自行決定)

  • 下載 Kernel 升級包
wget https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/x86_64/RPMS/kernel-lt-5.4.263-1.el7.elrepo.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/x86_64/RPMS/kernel-lt-tools-libs-5.4.263-1.el7.elrepo.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/elrepo/kernel/el7/x86_64/RPMS/kernel-lt-tools-5.4.263-1.el7.elrepo.x86_64.rpm
  • 打包 Kernel 升級包
tar zcvf kernel-lt-5.4.263-1-upgrade.tar.gz kernel-lt-*

至此,我們已經準備了 3 個離線部署資源包:

  • KubeKey:kubekey-v3.0.13.tar.gz(69M)
  • 製品(artifact):ksp-v3.4.1-artifact.tar.gz(13G)
  • 內核升級包(可選):kernel-lt-5.4.263-1-upgrade.tar.gz(50M )

3. K8s 離線集羣服務器-初始化配置

3.1. 操作系統基礎配置

本演示環境使用 KubeKey 自動配置,生產環境可以參考我以前發佈的部署實戰系列文檔手工配置。

3.2. 數據盤配置

每臺服務器新增一塊數據盤 /dev/sdb,用於 ContainerdKubernetes Pod 的持久化存儲。

爲了滿足部分用戶希望在生產上線後,磁盤容量不夠時可以實現擴容,本文采用了 LVM 的方式配置磁盤(實際上,本人維護的生產環境,幾乎不用 LVM)。

本小節的配置比較簡單,因此,下面的內容爲無廢話實操版。

請注意,以下操作無特殊說明時需在所有新增節點上執行。本文只選取 Master-1 節點作爲演示,並假定其餘服務器都已按照相同的方式進行配置和設置。

  • 創建 PV
 pvcreate /dev/sdb
  • 創建 VG
vgcreate data /dev/sdb
  • 創建 LV
# 使用所有空間,VG 名字爲 data,LV 名字爲 lvdata
lvcreate -l 100%VG data -n lvdata
  • 格式化磁盤
mkfs.xfs /dev/mapper/data-lvdata
  • 手工掛載磁盤
mkdir /data
mount /dev/mapper/data-lvdata /data/
  • 配置開機自動掛載
tail -1 /etc/mtab >> /etc/fstab
  • 創建 openebs 本地數據根目錄
mkdir -p /data/openebs/local
  • 創建 Containerd 數據目錄
mkdir -p /data/containerd
  • 創建 Containerd 數據目錄軟連接
ln -s /data/containerd /var/lib/containerd

說明:KubeKey 不支持在部署的時候更改 Containerd 的數據目錄,只能用這種變通的方式(也可以提前手工安裝 Containerd,建議)。

3.3. 升級系統內核

升級 CentOS 7.9 操作系統內核可選項,生產環境建議執行)。

請注意,以下操作無特殊說明時需在集羣所有節點上執行。本文只選取 Master-1 節點作爲演示,並假定其餘服務器都已按照相同的方式進行配置和設置。

  • 上傳內核升級包

將內核升級包 kernel-lt-5.4.263-1-upgrade.tar.gz 上傳到集羣中的每個節點的 /root 目錄。

cd /root
tar xvf kernel-lt-5.4.263-1-upgrade.tar.gz
  • 安裝新版本內核
yum install kernel-lt-5.4.263-1.el7.elrepo.x86_64.rpm

注意: 這裏我們只安裝內核,同時安裝其他包,會有包 conflicts 的報錯。

  • 修改系統默認內核爲新內核
grubby --set-default "/boot/vmlinuz-5.4.263-1.el7.elrepo.x86_64"
  • 重啓系統,使用新內核引導系統
reboot
  • 驗證內核版本
[root@ksp-master-1 ~]# uname -r
5.4.263-1.el7.elrepo.x86_64
  • 更新其他 Kernel 相關軟件包(可選,用不到則不用更新)

在使用新內核引導系統後,安裝其他 Kernel 相關軟件包,解決之前出現的依賴衝突問題。

# 卸載舊版本的 kernel-tools 相關軟件包,保留 kernel-3.10.0-1160.71.1,避免新內核異常無法進入系統(具體版本號以實際爲準)
yum remove kernel-tools-3.10.0-1160.71.1.el7.x86_64 kernel-tools-libs-3.10.0-1160.71.1.el7.x86_64

# 安裝新版本的 kernel-tools 相關軟件包
yum install kernel-lt-tools-5.4.263-1.el7.elrepo.x86_64.rpm kernel-lt-tools-libs-5.4.263-1.el7.elrepo.x86_64.rpm
  • 清理臨時安裝包(建議,養成良好的運維習慣
rm -rf kernel-lt-5.4.263-1.el7.elrepo.x86_64.rpm kernel-lt-tools-5.4.263-1.el7.elrepo.x86_64.rpm kernel-lt-tools-libs-5.4.263-1.el7.elrepo.x86_64.rpm

4. 離線部署 KubeSphere 和 K8s 的前提準備

4.1. 上傳離線部署資源包到部署節點

將以下離線部署資源包(KubeKey 和製品 artifact ),上傳至離線環境部署節點 (通常是 Master-1 節點) 的 /data/ 目錄(可根據實際情況修改)。

  • Kubekey:kubekey-v3.0.13.tar.gz
  • 製品 artifact:ksp-v3.4.1-artifact.tar.gz

執行以下命令,解壓 KubeKey:

# 創離線資源存放的數據目錄
mkdir /data/kubekey
mv /data/ksp-v3.4.1-artifact.tar.gz /data/kubekey/
tar xvf /data/kubekey-v3.0.13.tar.gz -C /data/kubekey
cd /data/kubekey

4.2. 創建離線集羣配置文件

  • 執行以下命令創建離線集羣配置文件
./kk create config --with-kubesphere v3.4.1 --with-kubernetes v1.26.5 -f ksp-v341-v1265-offline.yaml

命令執行成功後,在當前目錄會生成文件名爲 ksp-v341-v1265-offline.yaml 的配置文件。

注意:生成的默認配置文件內容較多,這裏就不做過多展示了,更多詳細的配置參數請參考 官方配置示例

4.3. 修改 Cluster 配置

在離線集羣配置文件文件中 kind: Cluster 小節的作用是部署 Kubernetes 集羣。本文示例採用 3 個節點同時作爲 control-plane、etcd 節點和 worker 節點。

執行以下命令修改離線集羣配置文件 ksp-v341-v1265-offline.yaml

vim ksp-v341-v1265-offline.yaml

修改 kind: Cluster 小節中 hosts 和 roleGroups 等信息,修改說明如下。

  • hosts:指定節點的 IP、ssh 用戶、ssh 密碼、ssh 端口。示例演示了 ssh 端口號的配置方法。同時,新增一個 Registry 節點的配置
  • roleGroups:指定 3 個 etcd、control-plane 節點,複用相同的機器作爲 3 個 worker 節點
  • 必須指定主機組 registry 作爲倉庫部署節點(本文爲了滿足讀者的需求使用了 KubeKey 自動部署 Harbor 鏡像倉庫。當然,也可以使用已有的 Harbor,使用已有 Harbor 時此配置可以不加)
  • internalLoadbalancer: 啓用內置的 HAProxy 負載均衡器
  • domain:自定義了一個 opsman.top,沒特殊需求的場景保留默認值即可
  • containerManager:使用了 containerd
  • storage.openebs.basePath:新增配置,指定 openebs 默認存儲路徑爲 /data/openebs/local
  • registry:必須指定 type 類型爲 harbor,否則默認安裝 docker registry

修改後的完整示例如下:

apiVersion: kubekey.kubesphere.io/v1alpha2
kind: Cluster
metadata:
  name: sample
spec:
  hosts:
  - {name: ksp-master-1, address: 192.168.9.91, internalAddress: 192.168.9.91, port:22, user: root, password: "P@88w0rd"}
  - {name: ksp-master-2, address: 192.168.9.92, internalAddress: 192.168.9.92, user: root, password: "P@88w0rd"}
  - {name: ksp-master-3, address: 192.168.9.93, internalAddress: 192.168.9.93, user: root, password: "P@88w0rd"}
  - {name: ksp-registry, address: 192.168.9.90, internalAddress: 192.168.9.90, user: root, password: "P@88w0rd"}
  roleGroups:
    etcd:
    - ksp-master-1
    - ksp-master-2
    - ksp-master-3
    control-plane: 
    - ksp-master-1
    - ksp-master-2
    - ksp-master-3
    worker:
    - ksp-master-1
    - ksp-master-2
    - ksp-master-3
    registry:
    - ksp-registry
  controlPlaneEndpoint:
    ## Internal loadbalancer for apiservers 
    internalLoadbalancer: haproxy

    domain: lb.opsman.top
    address: ""
    port: 6443
  kubernetes:
    version: v1.26.5
    clusterName: opsman.top
    autoRenewCerts: true
    containerManager: containerd
  etcd:
    type: kubekey
  network:
    plugin: calico
    kubePodsCIDR: 10.233.64.0/18
    kubeServiceCIDR: 10.233.0.0/18
    ## multus support. https://github.com/k8snetworkplumbingwg/multus-cni
    multusCNI:
      enabled: false
  storage:
    openebs:
      basePath: /data/openebs/local # 默認沒有的新增配置,base path of the local PV provisioner
  registry:
    # 如需使用 kk 部署 harbor, 可將該參數設置爲 harbor,不設置該參數且需使用 kk 創建容器鏡像倉庫,將默認使用 docker registry。
    type: harbor
    # 注意:
    # 1、如需使用 kk 部署的 harbor 或其他自定義倉庫,可設置對應倉庫的 auths,如使用 kk 創建 docker registry 倉庫,則無需配置該參數。
    # 2、kk 部署的 harbor,默認地址爲 dockerhub.kubekey.local,如要修改確保與 privateRegistry 字段的值保持一致。
    # 3、如需使用 kk 部署 harbor,請先註釋該參數,待部署完 Harbor 後再來修改啓用。
    auths:
      "registry.opsman.top":
        username: admin
        password: Harbor12345
        certsPath: "/etc/docker/certs.d/registry.opsman.top"
    # 設置集羣部署時使用的私有倉庫
    privateRegistry: "registry.opsman.top"
    namespaceOverride: "kubesphereio"
    registryMirrors: []
    insecureRegistries: []
  addons: []

注意:離線 Harbor 部署,我們使用了私有自定義域名和自簽名證書,實際生產中建議各位使用真正的互聯網域名和正規機構簽發的免費或收費證書。

4.4. 修改 ClusterConfiguration 配置

在離線集羣配置文件中 kind: ClusterConfiguration 小節的作用是部署 KubeSphere 及相關組件。

本文爲了驗證離線部署的完整性,啓用了除 Kubeedge 、gatekeeper 以外的所有插件。

繼續編輯離線集羣配置文件 ksp-v341-v1265-offline.yaml,修改 kind: ClusterConfiguration 部分來啓用可插拔組件,具體的修改說明如下。

  • 啓用 Etcd 監控
etcd:
    monitoring: true # 將 "false" 更改爲 "true"
    endpointIps: localhost
    port: 2379
    tlsEnable: true
  • 啓用應用商店
openpitrix:
  store:
    enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere DevOps 系統
devops:
  enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere 日誌系統(v3.4.0 開始默認啓用 OpenSearch
logging:
  enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere 事件系統
events:
  enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere 告警系統
alerting:
  enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere 審計日誌
auditing:
  enabled: true # 將 "false" 更改爲 "true"
  • 啓用 KubeSphere 服務網格(Istio)
servicemesh:
enabled: true # 將 "false" 更改爲 "true"
istio:
  components:
    ingressGateways:
    - name: istio-ingressgateway # 將服務暴露至服務網格之外。默認不開啓。
      enabled: false
    cni:
      enabled: false # 啓用後,會在 Kubernetes pod 生命週期的網絡設置階段完成 Istio 網格的 pod 流量轉發設置工作。
  • 啓用 Metrics Server
metrics_server:
  enabled: true # 將 "false" 更改爲 "true"

說明:KubeSphere 支持用於 部署 的容器組(Pod)彈性伸縮程序 (HPA)。在 KubeSphere 中,Metrics Server 控制着 HPA 是否啓用。

  • 啓用網絡策略、容器組 IP 池,服務拓撲圖(名字排序,對應配置參數排序)
network:
  networkpolicy:
    enabled: true # 將 "false" 更改爲 "true"
  ippool:
    type: calico # 將 "none" 更改爲 "calico"
  topology:
    type: weave-scope # 將 "none" 更改爲 "weave-scope"

說明:

  • 從 3.0.0 版本開始,用戶可以在 KubeSphere 中配置原生 Kubernetes 的網絡策略。
  • 容器組 IP 池用於規劃容器組網絡地址空間,每個容器組 IP 池之間的地址空間不能重疊。
  • 啓用服務拓撲圖以集成 Weave Scope (Docker 和 Kubernetes 的可視化和監控工具),服務拓撲圖顯示在您的項目中,將服務之間的連接關係可視化(實際上沒啥用,該項目目前已停止維護了,姑且放着吧)。
  • 修改上面的所有參數後,必須加入一個參數(以前的 2.x 時代的 kk 沒這個問題,不加的話,在部署 KubeSphere 的時候會有命名空間不匹配的問題)
spec:
  namespace_override: kubesphereio
  ......

經過上述步驟,我們成功完成了對離線集羣配置文件 ksp-v341-v1265-offline.yaml 的修改。由於篇幅限制,無法在此展示完整的文件內容,請各位讀者根據上文提供的配置說明仔細覈對。

5. 安裝配置 Harbor

爲了驗證 KubeKey 部署離線 Harbor 的能力,本實戰內容採用 KubeKey 部署 Harbor。生產環境建議提前自建

請注意,以下操作無特殊說明時需在離線環境部署節點(默認爲 Master-1)上執行。

5.1. 安裝 Harbor

執行以下命令安裝鏡像倉庫 Harbor(受限於篇幅,輸出結果未展示):

cd /root/kubekey
./kk init registry -f ksp-v341-v1265-offline.yaml -a ksp-v3.4.1-artifact.tar.gz

說明: ksp-v3.4.1-artifact.tar.gz 我們製作的離線部署資源包中的製品包。

部署完成後,我們 SSH 登陸到 Registry 節點,執行以下的命令,驗證 Harbor 的安裝情況:

  • 查看安裝完成後有哪些內容
[root@ksp-registry ~]# ls -lh /opt/harbor/
total 633M
......(受限於篇幅,輸出結果未展示)
  • 查看安裝版本
[root@ksp-registry ~]# docker images
REPOSITORY                      TAG       IMAGE ID       CREATED         SIZE
goharbor/harbor-exporter        v2.5.3    d9a8cfa37cf8   17 months ago   87.2MB
goharbor/chartmuseum-photon     v2.5.3    788b207156ad   17 months ago   225MB
goharbor/redis-photon           v2.5.3    5dc5331f3de8   17 months ago   154MB
goharbor/trivy-adapter-photon   v2.5.3    27798821348a   17 months ago   251MB
goharbor/notary-server-photon   v2.5.3    c686413b72ce   17 months ago   112MB
goharbor/notary-signer-photon   v2.5.3    a3bc1def3f94   17 months ago   109MB
goharbor/harbor-registryctl     v2.5.3    942de6829d43   17 months ago   136MB
goharbor/registry-photon        v2.5.3    fb1278854b91   17 months ago   77.9MB
goharbor/nginx-photon           v2.5.3    91877cbc147a   17 months ago   44.3MB
goharbor/harbor-log             v2.5.3    ca36fb3b68a6   17 months ago   161MB
goharbor/harbor-jobservice      v2.5.3    75e6a7496590   17 months ago   227MB
goharbor/harbor-core            v2.5.3    93a775677473   17 months ago   203MB
goharbor/harbor-portal          v2.5.3    d78f9bbad9ee   17 months ago   52.6MB
goharbor/harbor-db              v2.5.3    bd50ae1eccdf   17 months ago   224MB
goharbor/prepare                v2.5.3    15102b9ebde6   17 months ago   166MB
  • 查看安裝狀態
[root@ksp-registry ~]# cd /opt/harbor/
[root@ksp-registry harbor]# docker-compose ps -a
  • 查看 Harbor 配置的域名(確保使用了自定義域名
[root@ksp-registry harbor]# cat /opt/harbor/harbor.yml | grep hostname:
hostname: registry.opsman.top
  • 查看 Docker 是否配置了私有證書(確保使用了自定義域名及證書
[root@ksp-registry harbor]# ll /etc/docker/certs.d/registry.opsman.top/
total 12
-rw-r--r--. 1 root root 1103 Dec 13 09:47 ca.crt
-rw-r--r--. 1 root root 1253 Dec 13 09:47 registry.opsman.top.cert
-rw-------. 1 root root 1679 Dec 13 09:47 registry.opsman.top.key

小知識:KubeKey 部署 Harbor 時會自動同步自簽名證書到集羣所有節點,自己部署的 Harbor 必須手動複製 Registry 節點的自簽名證書到集羣所有節點。

KubeKey 完成複製的動作在下面兩個模塊:

  • [InitRegistryModule] Synchronize certs file
  • [InitRegistryModule] Synchronize certs file to all nodes

5.2. 在 Harbor 中創建項目

由於 Harbor 項目存在訪問控制(RBAC)的限制,即只有指定角色的用戶才能執行某些操作。如果您未創建項目,則鏡像不能被推送到 Harbor。

Harbor 中有兩種類型的項目:

  • 公共項目(Public):任何用戶都可以從這個項目中拉取鏡像。
  • 私有項目(Private):只有作爲項目成員的用戶可以拉取鏡像。

使用 KubeKey 安裝的 Harbor 默認信息如下:

  • 登陸賬戶信息:管理員賬號:admin,密碼:Harbor12345(生產環境必須修改)。
  • Harbor 安裝文件在 /opt/harbor , 如需運維 Harbor,可至該目錄下。

接下來的任務需要我們 SSH 登陸到 Registry 節點執行。

執行以下命令,下載官方提供的自動初始化 Harbor 倉庫的腳本(可不下載直接用本文提供的腳本):

curl -O https://raw.githubusercontent.com/kubesphere/ks-installer/master/scripts/create_project_harbor.sh
  • 根據實際情況,執行以下命令修改腳本配置 vim create_project_harbor.sh
#!/usr/bin/env bash

# Harbor 倉庫地址(寫域名,默認配置爲 https://dockerhub.kubekey.local)
url="https://registry.opsman.top"

# 訪問 Harbor 倉庫的默認用戶和密碼(生產環境建議修改)
user="admin"
passwd="Harbor12345"

# 需要創建的項目名列表,按我們製作的離線包的鏡像命名規則,實際上只需要創建一個 kubesphereio 即可,這裏保留了所有可用值,各位可根據自己的離線倉庫鏡像名稱規則修改。
harbor_projects=(library
    kubesphere
    calico
    coredns
    openebs
    csiplugin
    minio
    mirrorgooglecontainers
    osixia
    prom
    thanosio
    jimmidyson
    grafana
    elastic
    istio
    jaegertracing
    jenkins
    weaveworks
    openpitrix
    joosthofman
    nginxdemos
    fluent
    kubeedge
    kubesphereio
)

for project in "${harbor_projects[@]}"; do
    echo "creating $project"
    curl -k -u "${user}:${passwd}" -X POST -H "Content-Type: application/json" "${url}/api/v2.0/projects" -d "{ \"project_name\": \"${project}\", \"public\": true}"
done

重點注意:

  • harbor_projects 中一定要新增 kubesphereio,默認沒有。不加後面報錯,詳情見 問題 2
  • 腳本創建的是 public 項目,如需要私有項目請修改腳本。
  • 執行腳本創建項目
sh create_project_harbor.sh
  • 正確的執行結果如下(library 會有報錯,因爲默認已經存在,可忽略

5.3. 推送離線鏡像到 Harbor 倉庫

將提前準備好的離線鏡像推送到 Harbor 倉庫,這一步爲可選項,因爲創建集羣的時候默認會推送鏡像(本文使用參數忽略了)。爲了部署成功率,建議先推送。

  • 推送離線鏡像
./kk artifact image push -f ksp-v341-v1265-offline.yaml -a ksp-v3.4.1-artifact.tar.gz
  • Harbor 管理頁面查看項目和鏡像倉庫(提前在自己電腦上做好域名解析配置

6. 安裝 KubeSphere 和 K8s 集羣

6.1. 安裝 KubeSphere 和 K8s 集羣

執行以下命令,安裝 KubeSphere 和 K8s 集羣。

./kk create cluster -f ksp-v341-v1265-offline.yaml -a ksp-v3.4.1-artifact.tar.gz --with-packages --skip-push-images

參數說明

  • ksp-v341-v1265-offline.yaml:離線環境集羣的配置文件
  • ksp-v3.4.1-artifact.tar.gz:製品包的 tar 包鏡像
  • --with-packages:如需要安裝操作系統依賴,需指定該選項
  • --skip-push-images: 忽略推送鏡像,因爲,前面已經完成了推送鏡像到私有倉庫的任務

上面的命令執行後,首先 KubeKey 會檢查部署 K8s 的依賴及其他詳細要求。檢查合格後,系統將提示您確認安裝。輸入 yes 並按 ENTER 繼續部署。

特殊說明:由於本文在安裝的過程中啓用了日誌插件,因此在安裝的過程中必須按照 「問題 6」的描述手工介入處理否則安裝會失敗

安裝過程日誌輸出比較多,受限於篇幅本文只展示最終結果。

部署完成需要大約 10-30 分鐘左右,具體看網速、機器配置、啓用多少插件等。

部署完成後,您應該會在終端上看到類似於下面的輸出。提示部署完成的同時,輸出中還會顯示用戶登陸 KubeSphere 的默認管理員用戶和密碼。

#####################################################
###              Welcome to KubeSphere!           ###
#####################################################

Console: http://192.168.9.91:30880
Account: admin
Password: P@88w0rd
NOTES:
  1. After you log into the console, please check the
     monitoring status of service components in
     "Cluster Management". If any service is not
     ready, please wait patiently until all components
     are up and running.
  2. Please change the default password after login.

#####################################################
https://kubesphere.io             2023-12-13 10:56:38
#####################################################
10:56:40 CST skipped: [ksp-master-3]
10:56:40 CST skipped: [ksp-master-2]
10:56:40 CST success: [ksp-master-1]
10:56:40 CST Pipeline[CreateClusterPipeline] execute successfully
Installation is complete.

Please check the result using the command:

        kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

小技巧:

  • 當出現熟悉的安裝滾動條 >>---> 後,可以另開一個終端 使用命令 kubectl get pod -A 或是 kubectl get pod -A | grep -v Running 觀察進度,如出現異常可及時介入處理。
  • 也可以通過下面的命令查看詳細的部署過程日誌及報錯信息。
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l 'app in (ks-install, ks-installer)' -o jsonpath='{.items[0].metadata.name}') -f

6.2. 部署結果驗證

登錄 Web 控制檯通過 http://{IP}:30880 使用默認帳戶和密碼 admin/P@88w0rd 訪問 KubeSphere 的 Web 控制檯,簡單的驗證一下部署結果。

  • 查看集羣節點狀態

  • 查看系統組件狀態

7. 常見問題

問題 1

  • 問題現象

執行腳本 create_project_harbor.sh ,創建 Harbor 項目時,報錯如下:

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
  • 解決方案

因爲我們 Harbor 使用的是 Https 協議,並且使用了私有的域名和私有自定義的證書。需修改腳本,在 curl 命令中加入 -k 參數。

# 修改後命令
curl -k -u "${user}:${passwd}" -X POST -H "Content-Type: application/json" "${url}/api/v2.0/projects" -d "{ \"project_name\": \"${project}\", \"public\": true}"

問題 2

  • 問題現象

部署 Kubesphere 和 Kubernetes 集羣之前,執行推送離線鏡像的命令,報錯如下:

15:01:29 CST [CopyImagesToRegistryModule] Push multi-arch manifest to private registry
15:01:29 CST message: [LocalHost]
get manifest list failed by module cache
15:01:29 CST failed: [LocalHost]
error: Pipeline[ArtifactImagesPushPipeline] execute failed: Module[CopyImagesToRegistryModule] exec failed:
failed: [LocalHost] [PushManifest] exec failed after 1 retries: get manifest list failed by module cache
  • 解決方案

參考 issues-2054,在 Harbor 中創建項目 kubesphereio

問題 3

  • 問題現象

部署集羣時,報錯如下:

17:26:33 CST message: [ksp-master-1]
pull image failed: Failed to exec command: sudo -E /bin/bash -c "env PATH=$PATH crictl pull registry.opsman.top/kubesphereio/pause:3.8 --platform amd64"
E1211 17:26:33.225514    5977 remote_image.go:238] "PullImage from image service failed" err="rpc error: code = Unknown desc = failed to pull and unpack image \"registry.opsman.top/kubesphereio/pause:3.8\": failed to resolve reference \"registry.opsman.top/kubesphereio/pause:3.8\": failed to do request: Head \"https://registry.opsman.top/v2/kubesphereio/pause/manifests/3.8\": x509: certificate signed by unknown authority" image="registry.opsman.top/kubesphereio/pause:3.8"
FATA[0000] pulling image: rpc error: code = Unknown desc = failed to pull and unpack image "registry.opsman.top/kubesphereio/pause:3.8": failed to resolve reference "registry.opsman.top/kubesphereio/pause:3.8": failed to do request: Head "https://registry.opsman.top/v2/kubesphereio/pause/manifests/3.8": x509: certificate signed by unknown authority: Process exited with status 1
17:26:33 CST retry: [ksp-master-1]
  • 解決方案

參考 issues-1762,在集羣部署文件 ksp-v341-v1265-offline.yaml 的 registry 部分配置中加入以下內容(一定要確保 /etc/docker/certs.d/registry.opsman.top 文件夾存在):

# 本文用的 registry.opsman.top 默認爲 dockerhub.kubekey.local
certsPath: "/etc/docker/certs.d/registry.opsman.top"

如果在部署過程中報錯,可以在 containerd 的配置文件中,加入下面的配置,重啓 containerd 服務,再重新執行部署任務。

#  vi /etc/containerd/config.toml 找到正確位置加入(一般在文件最後有類似段落,修改替換即可)
[plugins."io.containerd.grpc.v1.cri".registry.configs."registry.opsman.top".tls]
              ca_file = "/etc/docker/certs.d/registry.opsman.top/ca.crt"
              cert_file = "/etc/docker/certs.d/registry.opsman.top/registry.opsman.top.cert"
              key_file = "/etc/docker/certs.d/registry.opsman.top/registry.opsman.top.key"
              insecure_skip_verify = false
              
# 重啓服務
systemctl restart containerd

如果報錯提示 /etc/docker/certs.d/registry.opsman.top 找不到,可以按下面的操作執行(沒用 KubeKey 部署 Harbor 時會出現)。

# 1. 登陸到 registry 節點
# 2. 複製 /etc/docker/certs.d/registry.opsman.top 文件夾到其他節點

問題 4

  • 問題現象

部署集羣時,報錯如下:

17:50:16 CST message: [ksp-master-3]
pull image failed: Failed to exec command: sudo -E /bin/bash -c "env PATH=$PATH crictl pull registry.opsman.top/kubesphereio/pause:3.8 --platform amd64"
E1211 17:50:16.841836    8464 remote_image.go:238] "PullImage from image service failed" err="rpc error: code = Unknown desc = failed to pull and unpack image \"registry.opsman.top/kubesphereio/pause:3.8\": failed to resolve reference \"registry.opsman.top/kubesphereio/pause:3.8\": get TLSConfig for registry \"https://registry.opsman.top\": failed to load cert file: open /etc/docker/certs.d/registry.opsman.top/registry.opsman.top.cert: no such file or directory" image="registry.opsman.top/kubesphereio/pause:3.8"
FATA[0000] pulling image: rpc error: code = Unknown desc = failed to pull and unpack image "registry.opsman.top/kubesphereio/pause:3.8": failed to resolve reference "registry.opsman.top/kubesphereio/pause:3.8": get TLSConfig for registry "https://registry.opsman.top": failed to load cert file: open /etc/docker/certs.d/registry.opsman.top/registry.opsman.top.cert: no such file or directory: Process exited with status 1
17:50:16 CST retry: [ksp-master-3]
  • 解決方案

製作離線資源的官方 image-list 中不存在 pause:3.8 這個 image,需要自己預先加上再製作製品。實際上一共需要補充 16 個,報錯都不一樣,可以根據報錯信息補充(本文提供的 images-list 已經是補充完整的)。

問題 5

  • 問題現象
Events:
  Type     Reason     Age                   From               Message
  ----     ------     ----                  ----               -------
  Normal   Scheduled  18m                   default-scheduler  Successfully assigned kube-system/metrics-server-5d65c798b8-m9tbj to ksp-master-3
  Normal   Pulling    16m (x4 over 18m)     kubelet            Pulling image "registry.opsman.top/kubesphere/metrics-server:v0.4.2"
  Warning  Failed     16m (x4 over 18m)     kubelet            Failed to pull image "registry.opsman.top/kubesphere/metrics-server:v0.4.2": rpc error: code = NotFound desc = failed to pull and unpack image "registry.opsman.top/kubesphere/metrics-server:v0.4.2": failed to resolve reference "registry.opsman.top/kubesphere/metrics-server:v0.4.2": registry.opsman.top/kubesphere/metrics-server:v0.4.2: not found
  Warning  Failed     16m (x4 over 18m)     kubelet            Error: ErrImagePull
  Warning  Failed     16m (x6 over 17m)     kubelet            Error: ImagePullBackOff
  Normal   BackOff    2m57s (x64 over 17m)  kubelet            Back-off pulling image "registry.opsman.top/kubesphere/metrics-server:v0.4.2"
  • 解決方案

可能是 bug(已反饋給社區,暫未確認)。社區給了臨時方案在離線集羣配置文件中 kind: ClusterConfiguration 小節中,加入參數 namespace_override: kubesphereio

問題 6

  • 問題現象
# kubectl describe pod opensearch-cluster-data-0 -n kubesphere-logging-system
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Pulling    56s (x3 over 2m57s)  kubelet            Pulling image "busybox:latest"
  Warning  Failed     15s (x3 over 2m17s)  kubelet            Error: ErrImagePull
  Warning  Failed     15s                  kubelet            Failed to pull image "busybox:latest": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/busybox:latest": failed to resolve reference "docker.io/library/busybox:latest": failed to do request: Head "https://registry-1.docker.io/v2/library/busybox/manifests/latest": dial tcp: lookup registry-1.docker.io on 114.114.114.114:53: read udp 192.168.9.92:37639->114.114.114.114:53: i/o timeout
  Normal   BackOff    0s (x3 over 2m16s)   kubelet            Back-off pulling image "busybox:latest"
  Warning  Failed     0s (x3 over 2m16s)   kubelet            Error: ImagePullBackOff
  • 解決方案

這個應該也是個 bug,官方配置寫死了 busybox 的地址(已反饋給社區,已確認),暫時需要手改。

# 查看 sts
[root@ksp-master-1 kubekey]# kubectl get sts -n kubesphere-logging-system
NAME                        READY   AGE
opensearch-cluster-data     0/2     7m42s
opensearch-cluster-master   0/1     7m40s

# 修改 sts 使用的 busyboy 鏡像爲本地鏡像(細節不展示)
kubectl edit sts opensearch-cluster-data -n kubesphere-logging-system
kubectl edit sts opensearch-cluster-master -n kubesphere-logging-system

# 本文修改後 image 內容(自己根據實際情況修改域名前綴)
registry.opsman.top/kubesphereio/busybox:1.31.1

問題 7

  • 問題現象
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
        [WARNING ImagePull]: failed to pull image registry.opsman.top/kubesphereio/pause:3.9: output: E1213 10:31:38.814017    8325 remote_image.go:238] "PullImage from image service failed" err="rpc error: code = NotFound desc = failed to pull and unpack image \"registry.opsman.top/kubesphereio/pause:3.9\": failed to resolve reference \"registry.opsman.top/kubesphereio/pause:3.9\": registry.opsman.top/kubesphereio/pause:3.9: not found" image="registry.opsman.top/kubesphereio/pause:3.9"
time="2023-12-13T10:31:38+08:00" level=fatal msg="pulling image: rpc error: code = NotFound desc = failed to pull and unpack image \"registry.opsman.top/kubesphereio/pause:3.9\": failed to resolve reference \"registry.opsman.top/kubesphereio/pause:3.9\": registry.opsman.top/kubesphereio/pause:3.9: not found"
, error: exit status 1
  • 解決方案

這個報錯是在部署過程中出現的,但是報錯並未導致集羣部署失敗。解決辦法跟問題 4 一樣,製作離線資源的官方 image-list 中不存在 pause:3.9 這個 image,需要自己預先加上再製作製品。

問題 8

  • 問題現象
Events:
  Type     Reason       Age                   From               Message
  ----     ------       ----                  ----               -------
  Normal   Pulling      4m25s                 kubelet            Pulling image "registry.opsman.top/kubesphereio/ks-apiserver:v3.4.1"
  Warning  Failed       4m22s                 kubelet            Failed to pull image "registry.opsman.top/kubesphereio/ks-apiserver:v3.4.1": rpc error: code = Unknown desc = failed to pull and unpack image "registry.opsman.top/kubesphereio/ks-apiserver:v3.4.1": failed to extract layer sha256:9f4598b692bcf57921e45bc384b60591c5f8eac82e32e781e9fe5849ef6eb29e: write /var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/291/fs/usr/local/bin/ks-apiserver: no space left on device: unknown
  Warning  Failed       4m22s                 kubelet            Error: ErrImagePull
  Warning  Failed       4m21s                 kubelet            Error: ImagePullBackOff
  Normal   BackOff      39s (x15 over 4m21s)  kubelet            Back-off pulling image "registry.opsman.top/kubesphereio/ks-apiserver:v3.4.1"
  • 解決方案

人爲造成,KubeKey 的離線部署資源包放在了節點 ksp-master-1,同時,該節點也是 K8s 的 Master-1 節點。該節點的系統盤爲 40G,演示環境沒加額外的數據盤,所以磁盤空間滿了。實際使用中,建議新加一塊數據盤。

問題 9

  • 問題現象

容器 devops-argocd-dex-server 反覆重啓。

# kubectl describe pod devops-argocd-dex-server-84fff59566-2brcn -n argocd
Events:
  Type     Reason     Age                    From               Message
  ----     ------     ----                   ----               -------
  Normal   Created    35m (x2 over 36m)      kubelet            Created container dex-server
  Normal   Started    35m (x2 over 36m)      kubelet            Started container dex-server
  Normal   Killing    35m                    kubelet            Container dex-server failed liveness probe, will be restarted
  Normal   Pulled     35m                    kubelet            Container image "registry.opsman.top/kubesphereio/dex:v2.30.2" already present on machine
  Warning  Unhealthy  35m (x6 over 36m)      kubelet            Liveness probe failed: Get "http://10.233.89.12:5558/healthz/live": dial tcp 10.233.89.12:5558: connect: connection refused
  Warning  Unhealthy  22m (x38 over 36m)     kubelet            Readiness probe failed: Get "http://10.233.89.12:5558/healthz/ready": dial tcp 10.233.89.12:5558: connect: connection refused
  Warning  BackOff    2m21s (x127 over 33m)  kubelet            Back-off restarting failed container dex-server in pod devops-argocd-dex-server-84fff59566-2brcn_argocd(d83b98dc-cd21-47d9-aa5e-e7a26d458e7f)
  • 解決方案(未解決
# 細節沒深究,刪除 pod 自動重建,但是還會自動重啓
kubectl delete pod devops-argocd-dex-server-84fff59566-2brcn -n argocd

8. 總結

本次的實戰課程,我們詳細介紹瞭如何使用 KubeKey v3.0.13 離線部署 KubeSphere 和 K8s 集羣。

概括總結全文主要涉及以下內容:

  • 瞭解清單 (manifest) 和製品 (artifact) 的概念
  • 瞭解 manifest 和 images-list 資源的獲取地址
  • 瞭解 images-list 的組成及裁剪方案
  • 手工編寫 manifest 清單
  • 根據 manifest 清單製作 artifact
  • KubeKey 離線部署 Harbor
  • Harbor 鏡像倉庫自動創建項目
  • KubeKey 離線部署 KubeSphere 和 K8s
  • KubeKey 離線部署常見問題及解決方案

**受限於篇幅限制,本文略有遺憾之處在於很多部署任務的輸出結果沒有展示,建議讀者在實戰中仔細分析。

免責聲明:

  • 筆者水平有限,儘管經過多次驗證和檢查,盡力確保內容的準確性,但仍可能存在疏漏之處。敬請業界專家大佬不吝指教。
  • 本文所述內容僅限於實戰環境驗證測試通過,讀者可學習、借鑑,但嚴禁直接用於生產環境由此引發的任何問題,作者概不負責

本文由博客一文多發平臺 OpenWrite 發佈!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章