Skip to content

antony@notes:~/rancher$ cat "全離線安裝-Harbor-K3S-和-Rancher.md"

全離線安裝 Harbor 、K3S 和 Rancher

2024-06-04· rancher ·Full Offline Installation

全離線安裝 Harbor、K3S、RKE2 和 Rancher

Preface

本篇文章會在全離線的環境下建置 Harbor、K3S、RKE2 和 Rancher

可以透過點擊以下目錄,選擇想看的內容,跳轉至特定章節

:::warning

:::spoiler {state=“open”} 目錄

[TOC]

:::

1. 環境架構介紹

  • 七台 VM
    • 一台 VM 須能上網,下載離線安裝 Harbor、K3S、RKE2 和 Rancher 所需的套件
      • 此 VM 硬體規格:
        • CPU: 2 Core
        • Mem: 4 GB
        • Disk 需要 300 GB
    • 一台 VM 安裝 Harbor + DNS Server
    • 三台 VM 安裝 K3S + Rancher (HA)
    • 一台 VM 安裝 Nginx Load Balancer
    • 一台 VM 安裝 DownStream Cluster RKE2 (Role: AllInOne)
  • 軟體版本
    • Harbor: v2.10.2
    • K3S: v1.28.10+k3s1
    • RKE2: v1.28.10+rke2r1
    • Rancher Prime: v2.8.4
  • 作業系統
    • SUSE Linux Enterprise Server 15 SP5
    • ISO: SLE-15-SP5-Full-x86_64-GM-Media1.iso

2. Rancher Support Matrix

2.1. 可以安裝 Rancher Manager 的 K8s 平台

DistroLowest Certified VersionHighest Certified VersionNotes
RKE2v1.25v1.28See RKE2 Support Matrix for specific system requirements
RKE1v1.25v1.28See RKE1 Support Matrix for specific system requirements
K3sv1.25v1.28See k3s Support Matrix for specific system requirements
AKSv1.25v1.28-
EKSv1.25v1.28-
GKEv1.25v1.28-

2.2. 有支援的 Downstream Cluster

2.2.1. RKE2

RKE2 VersionRancher ProvisionedRancher Imported
1.28YesYes
1.27YesYes
1.26YesYes
1.25YesYes

2.2.2. K3S

K3s VersionRancher ProvisionedRancher ImportedNotes
1.28Yes (x86 Only)Yes-
1.27Yes (x86 Only)Yes-
1.26Yes (x86 Only)Yes-
1.25Yes (x86 Only)Yes-

更詳細的相關支援資訊請參考以下連結 : Rancher Manager v2.8.4 Support Matrix - SUSE Docs

Harbor 安裝先決條件

Hardware

ResourceMinimumRecommended
CPU2 cores4 cores
Memory4 GB8 GB
Disk300 GB300+ GB

OS

  • Recommand: SUSE Linux Enterprise Server 15 SP5
  • Recommand ISO: SLE-15-SP5-Full-x86_64-GM-Media1.iso

Software

  • Docker Engine Version: 20.10.10-ce+ or higher
  • Docker Compose Version: v1.18.0+ or docker compose v2 (docker-compose-plugin)
  • OpenSSL Version: Latest

K3S + Rancher 安裝先決條件

Hardware

  • CPU: 4 Core
  • Mem: 16 GB
  • Disk: 100 GB (recommend SSD)

OS

  • Recommand: SUSE Linux Enterprise Server 15 SP5
  • Recommand ISO: SLE-15-SP5-Full-x86_64-GM-Media1.iso

3. 準備全離線環境的各個安裝包

3.1. 環境確認

3.1.1. 安裝 docker 20.10.23-ce

$ sudo zypper mr -ea && \
sudo zypper in -y docker && \
sudo systemctl enable --now docker

docker 命令放在 Containers-Module_15.5-0 Repository

3.1.2. 檢查 Docker 儲存 Image 的目錄應為 /var/lib/docker

$ sudo docker info -f '{{ .DockerRootDir}}'

3.1.3. 硬碟空間規劃

  1. Docker 儲存 Container images 的空間 (/var/lib/docker) 至少要有 300 GB 的空間
  2. /home 目錄區,用來儲存全離線安裝所需之軟體套件、Container images 壓縮檔 和一些設定檔。
  3. 在虛擬化那層新增 1 顆 125 GB 的硬碟和 1 顆 300 GB 的硬碟
  4. 設定 LVM
# 1. 檢視硬碟
$ lsblk /dev/{sdb,sdc}
NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sdb    8:16   0  300G  0 disk
sdc    8:32   0  125G  0 disk

# 2. /dev/sdb 切 partition
$ sudo fdisk /dev/sdb

...

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1): 1
First sector (2048-629145599, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-629145599, default 629145599):

Created a new partition 1 of type 'Linux' and of size 300 GiB.

Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 3. /dev/sdc 切 partition
$ sudo fdisk /dev/sdc

...

Command (m for help): p
Disk /dev/sdc: 125 GiB, 134217728000 bytes, 262144000 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x8d8da0bd

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p):

Using default response p.
Partition number (1-4, default 1):
First sector (2048-262143999, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-262143999, default 262143999):

Created a new partition 1 of type 'Linux' and of size 125 GiB.

Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 8e
Changed type of partition 'Linux' to 'Linux LVM'.

Command (m for help): p
Disk /dev/sdc: 125 GiB, 134217728000 bytes, 262144000 sectors
Disk model: QEMU HARDDISK
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x8d8da0bd

Device     Boot Start       End   Sectors  Size Id Type
/dev/sdc1        2048 262143999 262141952  125G 8e Linux LVM

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

# 4. 建立 PV
$ sudo pvcreate /dev/{sdb1,sdc1}
  Physical volume "/dev/sdb1" successfully created.
  Physical volume "/dev/sdc1" successfully created.

# 5. 建立 VG
$ sudo vgcreate Container_images /dev/sdb1
  Volume group "Container_images" successfully created
$ sudo vgextend system /dev/sdc1
  Volume group "system" successfully extended

# 6. 建立 LV
$ sudo sudo lvcreate -l +100%FREE -n Container_images_lv Container_images
  Logical volume "Container_images_lv" created.
$ sudo lvresize -l +100%FREE /dev/system/home

# 7. 確認 LV 建立成功
$ sudo lvs
  LV                  VG               Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  Container_images_lv Container_images -wi-a----- 300.00g
  home                system           -wi-a----- 150.00g
  root                system           -wi-ao----  74.99g

# 8. 掛載 /var/lib/docker 目錄區
$ su

3.1.3. 檢查 Docker 儲存 Image 的目錄可用空間是否有 300 GB

$ df -hT `sudo docker info -f '{{ .DockerRootDir}}'`

螢幕輸出 :

Filesystem              Type   Size  Used Avail Use% Mounted on
/dev/mapper/system-root btrfs  300G  2.8G  296G   1% /var

3.1.4. 檢查 Docker 儲存 Image 的目錄目前已使用多大的儲存空間

$ sudo du -sh `sudo docker info -f '{{ .DockerRootDir}}'`

螢幕輸出 :

156K    /var/lib/docker

3.1.5. 確認 /home 目錄區可用空間有 150G

$ df -hT /home

二、 建立工作目錄

$ mkdir -p work/{harbor,k3s,rancher,compressed_files}

三、 準備 Harbor v2.10.0 全離線安裝包

  1. 切換工作目錄
$ cd ~/work/harbor
  1. 下載 Harbor 壓縮檔,版本 v2.10.0
$ wget https://github.com/goharbor/harbor/releases/download/v2.10.0/harbor-offline-installer-v2.10.0.tgz
  1. 下載 Docker Compose 套件,版本 v2.23.3
$ wget https://github.com/docker/compose/releases/download/v2.23.3/docker-compose-linux-x86_64
  1. 將以上離線安裝 Harbor 所需之套件壓縮成一個檔案
$ cd ..; tar -czf compressed_files/harbor-offline.tar.gz harbor/
  1. 檢查是否符合預期
$ ls -lh compressed_files/

螢幕輸出

total 629M
-rw-r--r-- 1 rancher users 629M Jan  8 14:52 harbor-offline.tar.gz

解壓縮命令 : tar -xzf compressed_files/harbor-offline.tar.gz

四、 準備 K3S v1.26.12 全離線安裝包

  1. 切換工作目錄,並切換為 root 使用者
$ cd ~/work/k3s
  1. 下載離線安裝 K3S 所需 Image 之壓縮檔及 K3S 二進位檔
$ curl -# -OL https://github.com/k3s-io/k3s/releases/download/v1.26.12%2Bk3s1/k3s-airgap-images-amd64.tar

$ curl -# -OL https://github.com/k3s-io/k3s/releases/download/v1.26.12%2Bk3s1/k3s
  1. 下載官網提供的離線安裝 K3S 所需之安裝腳本,並賦予它執行權限
$ curl -sfL https://get.k3s.io/ --output install.sh && chmod +x install.sh
  1. 檢查當前目錄下的檔案
$ ls -lh

螢幕輸出

total 621M
-rwxr-xr-x 1 rancher users  33K Jan  8 15:10 install.sh
-rw-r--r-- 1 rancher users  57M Jan  8 15:07 k3s
-rw-r--r-- 1 rancher users 564M Jan  8 15:07 k3s-airgap-images-amd64.tar
  1. 將以上離線安裝 RKE2 所需之檔案,壓縮成一個檔案
$ cd ..; tar -czf compressed_files/k3s-offline.tar.gz k3s/
  1. 檢查是否符合預期
$ ls -lh compressed_files/

螢幕輸出

-rw-r--r-- 1 762M harbor-offline.tar.gz
-rw-r--r-- 1 241M k3s-offline.tar.gz

五、 準備 Rancher Prime v2.7.9 全離線安裝包

  1. 切換工作目錄
$ cd ~/work/rancher
  1. 安裝 helm,版本 v3.13.1
$ curl -s https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash

螢幕輸出 :

Downloading https://get.helm.sh/helm-v3.13.1-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm
  1. 新增 Rancher Prime 的 Helm Chart Repository
$ helm repo add rancher-prime https://charts.rancher.com/server-charts/prime

螢幕輸出 :

"rancher-prime" has been added to your repositories
  1. 刷新 Rancher Helm Chart Repository
$ helm repo update

螢幕輸出 :

Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "rancher-prime" chart repository
Update Complete. ⎈Happy Helming!⎈
  1. 下載 Rancher chart v2.7.9
$ helm fetch rancher-prime/rancher --version=v2.7.9

這會拉下 chart 並將其作為 .tgz 檔案保存在當前目錄中。

  1. 新增和刷新 cert-manager repo
$ helm repo add jetstack https://charts.jetstack.io && \
helm repo update

螢幕輸出 :

"jetstack" has been added to your repositories
Hang tight while we grab the latest from your chart repositories...
...Successfully got an update from the "rancher-prime" chart repository
...Successfully got an update from the "jetstack" chart repository
Update Complete. ⎈Happy Helming!⎈
  1. 下載 cert-manager chart v1.11.0
$ helm fetch jetstack/cert-manager --version v1.11.0

這會拉下 chart 並將其作為 .tgz 檔案保存在當前目錄中。

  1. 下載 cert-manager 要求的 CRD
$ curl -L -o cert-manager-crd.yaml https://github.com/cert-manager/cert-manager/releases/download/v1.11.0/cert-manager.crds.yaml
  1. 下載 cert-manager 的 Container Image
$ sudo docker pull quay.io/jetstack/cert-manager-controller:v1.11.0

$ sudo docker pull quay.io/jetstack/cert-manager-webhook:v1.11.0

$ sudo docker pull quay.io/jetstack/cert-manager-cainjector:v1.11.0

$ sudo docker pull quay.io/jetstack/cert-manager-ctl:v1.11.0
  1. 修改 cert-manager 的所有 Container Images Tag
$ Registry_Name="harbor.example.com" && version="v1.11.0"

$ sudo docker tag quay.io/jetstack/cert-manager-controller:"$version" "$Registry_Name"/rancher/cert-manager-controller:"$version"

$ sudo docker tag quay.io/jetstack/cert-manager-webhook:"$version" "$Registry_Name"/rancher/cert-manager-webhook:"$version"

$ sudo docker tag quay.io/jetstack/cert-manager-cainjector:"$version" "$Registry_Name"/rancher/cert-manager-cainjector:"$version"

$ sudo docker tag quay.io/jetstack/cert-manager-ctl:"$version" "$Registry_Name"/rancher/cert-manager-ctl:"$version"
  1. 將 cert-manager 的所有 Container Images 打包成 .tar.gz 壓縮檔
$ sudo docker save "$Registry_Name"/rancher/cert-manager-controller:"$version" \
    "$Registry_Name"/rancher/cert-manager-webhook:"$version" \
    "$Registry_Name"/rancher/cert-manager-cainjector:"$version" \
    "$Registry_Name"/rancher/cert-manager-ctl:"$version" | gzip --stdout > ~/work/rancher/cert-manager-image_v1.11.0.tar.gz
  1. 下載 Rancher Images List 文字檔及蒐集 Image 所需的 Shell Script
$ wget https://github.com/rancher/rancher/releases/download/v2.8.2/rancher-images.txt
    
$ wget https://github.com/rancher/rancher/releases/download/v2.8.2/rancher-load-images.sh

$ wget https://github.com/rancher/rancher/releases/download/v2.8.2/rancher-save-images.sh
  • rancher-images.txt,此檔案包含安裝 Rancher、provision clusters 和 user Rancher tools 所需的 Images 清單。
  • rancher-save-images.sh,這個 Shell Script 從 Docker Hub 下載 rancher-images.txt 中的所有 Images,並將所有 Images 儲存為 rancher-images.tar.gz
  • rancher-load-images.sh,這個 Shell Script 從 rancher-images.tar.gz 檔案載入 Images,並將其推送到私有的 Image Registry。
  1. 對 Image List 進行排序和唯一化,以消除來源之間的任何重疊:
$ sort -u rancher-images.txt -o rancher-images.txt
  1. 賦予 rancher-save-images.sh 可以執行的權限:
$ chmod +x rancher-save-images.sh
  1. 使用 rancher-images.txt Image 清單執行 rancher-save-images.sh
$ sudo bash -x rancher-save-images.sh \
    --image-list ./rancher-images.txt \
    --source-registry "registry.rancher.com" \
    --images ~/work/rancher/rancher-images.tar.gz \
    &>> /tmp/rancher-save-images.log &

結果: Docker 開始下載 Images,請耐心等待。 這個過程需要幾分鐘到幾小時。完成後,會在 ~/work/rancher/ 目錄將輸出一個名為 rancher-images.tar.gz 的壓縮包,檢查輸出是否在目錄中。

  1. 下載 Helm 壓縮檔
$ wget https://get.helm.sh/helm-v3.13.1-linux-amd64.tar.gz -O ~/work/rancher/helm-v3.13.1-linux-amd64.tar.gz
  1. 檢查當前所有準備好的檔案
$ ls -lh ~/work/rancher

螢幕輸出 :

total 41G
-rw-r--r-- 1 rancher users 380K Jan  8 15:43 cert-manager-crd.yaml
-rw-r--r-- 1 rancher users  56M Jan  8 16:55 cert-manager-image_v1.11.0.tar.gz
-rw-r--r-- 1 rancher users  65K Jan  8 16:31 cert-manager-v1.11.0.tgz
-rw-r--r-- 1 rancher users  16M Oct 12 22:01 helm-v3.13.1-linux-amd64.tar.gz
-rw-r--r-- 1 rancher users  15K Jan  8 15:25 rancher-2.7.9.tgz
-rw-r--r-- 1 root    root   41G Jan  9 18:50 rancher-images.tar.gz
-rw-r--r-- 1 rancher users  32K Jan  8 16:55 rancher-images.txt
-rw-r--r-- 1 rancher users 4.1K Oct 27 05:39 rancher-load-images.sh
-rwxr-xr-x 1 rancher users 1.8K Oct 27 05:39 rancher-save-images.sh
  1. 壓縮全離線安裝 Rancher 會需要的所有檔案
$ cd ..; tar -czvf compressed_files/rancher-offline.tar.gz rancher/
  1. 將本次全離線需要的壓縮檔全部壓成同一個壓縮檔
$ tar -czvf full-offline.tar.gz compressed_files/

壓縮好的檔案大小 41 GB (full-offline.tar.gz)

  1. 將壓縮檔傳送至 Harbor 和 Rancher 主機
$ scp full-offline.tar.gz  <harbor 的機器>

開始離線安裝