網(wǎng)站建設 無錫登錄注冊入口
《OpenShift 4.x HOL教程匯總》
說明:本文已經(jīng)在OpenShift 4.14環(huán)境中驗證
文章目錄
- 什么是 MTC ?
- MTC 中有關應用遷移的概念
- MTC 中的定制資源
- 準備對象存儲
- 配置遷移源集群的環(huán)境
- 安裝 MTC 運行環(huán)境
- 部署 file-uploader 測試應用
- 查看集群配置
- 配置遷移目標集群的環(huán)境
- 安裝 MTC 運行環(huán)境
- 添加 Replication Repository (MigStorage)
- 添加遷移的 OpenShift 集群
- 添加遷移計劃
- 應用遷移和回退
- 執(zhí)行遷移計劃
- 回退遷移
- 其他功能
- 查看遷移過程涉及到哪些對象
- 不遷移某些特定資源
- 演示視頻
- 參考
什么是 MTC ?
紅帽的容器遷移工具包(Migration Toolkit for Containers - MTC)可以在不同的 OpenShift Container Platform 集群之間以命名空間的粒度復制有狀態(tài)應用工作負載。復制的應用資源包括這些應用運行相關的 Kubernetes 對象、鏡像和應用使用的 PV 數(shù)據(jù)。
MTC 使用了開源項目 Velero 來實現(xiàn)在源和目標集群之間復制應用資源。在復制應用資源過程中,被復制的應用資源是存放在基于對象存儲的副本庫中。對象存儲將在間接遷移過程中用來復制 Image、PV 和 Kubernetes 對象,或在直接 PV 遷移或直接 Image 遷移過程中用來復制 Kubernetes 對象。
為了在源集群和目標集群之間遷移應用,需要將 MTC 運行在每個 OpenShift 集群上??蓞⒁?#xff1a;《MTC 文檔》已確認需要使用那個版本的 MTC Operator。
MTC 中有關應用遷移的概念
- 間接遷移:使用 Restic 將 Image、PV 和 Kubernetes 對象先從源群集復制到副本庫,再從副本復制到目標群集。
- Direct volume migration - DVM:使用 Rsync 將 PV 直接從源群集復制到目標群集。
- Direct image migration - DIM:將 Image 直接從源群集復制到目標群集。
- Cutover migration:先停止源群集上的應用程序,再將其資源遷移到目標群集。
- Stage migration:無需停止應用程序,只將數(shù)據(jù)復制到目標群集,不遷移應用的 Kubernetes 對象。
- Host cluster:通常是目標集群,用來運行 migration-controller 和 web console。
- Remote cluster:通常是源集群。遠程群集需要一個公開的 Registry Route,用來接遷移映像。另外還需要有用來訪問 migration-controller 服務賬戶的 Secret。
- MTC web console 和 Migration Controller:運行在 Host cluster 之上用來運行 MTC 的控制臺和控制器。缺省使用目標集群充當 Host cluster,但也可運行在獨立集群或源集群之上。
MTC 中的定制資源
MTC 使用以下幾種 CR 定制資源完成應用資源的遷移:
- MigCluster:定義集群的配置。
- MigStorage:定義存儲的配置。
- MigPlan:定義遷移涉及的源和目標集群、副本庫和命名空間等配置。
- MigMigration:每次在源和目標集群之間執(zhí)行遷移計劃的動作。
- Backup:當執(zhí)行遷移計劃時,在源集群中創(chuàng)建 Velero 的 Backup CR。
- Restore:當執(zhí)行遷移計劃時,在目標集群中創(chuàng)建 Velero 的 Restore CR。
準備對象存儲
MTC 的 Replication Repository 可以使用以下對象存儲:
- Amazon Web Services (AWS) S3
- Google Cloud Provider (GCP)
- Microsoft Azure Generic
- S3 object storage, MinIO 或 Ceph
請參考《OpenShift 4 - 部署運行 MinIO 對象存儲》在 OpenShift 4 上部署 MinIO 環(huán)境。然后根據(jù)文檔創(chuàng)建一個名為 ocp-migration-bucket 的 Bucket。
配置遷移源集群的環(huán)境
注意:本文以 OpenShift 3 為遷移的源集群。如果源集群是 OpenShift 4 的環(huán)境,可以參考下一章節(jié)先在 OpenShift 4 上安裝 MTC Operator,然后使用缺省配置創(chuàng)建一個 MigrationController 對象即可。隨后可繼續(xù)完成本文的“部署 file-uploader 測試應用”等后續(xù)操作。
安裝 MTC 運行環(huán)境
- 執(zhí)行命令,根據(jù) 2 個 yaml文件創(chuàng)建 MTC 運行環(huán)境。
$ mkdir $HOME/mtc
$ sudo docker cp $(sudo docker create registry.redhat.io/rhmtc/openshift-migration-rhel7-operator:v1.3.1):/operator.yml $HOME/mtc
$ sudo docker cp $(sudo docker create registry.redhat.io/rhmtc/openshift-migration-rhel7-operator:v1.3.1):/controller-3.yml $HOME/mtc
$ oc create -f $HOME/mtc/operator.yml
$ oc create -f $HOME/mtc/controller-3.yml
- 查看部署的 Operator資源
$ oc get pods -n openshift-migration
NAME READY STATUS RESTARTS AGE
migration-operator-77d77fff48-28t9c 1/1 Running 0 1m
restic-b2k9z 1/1 Running 0 31s
restic-c8hwm 1/1 Running 0 31s
restic-gpnjz 1/1 Running 0 31s
restic-nfqwd 1/1 Running 0 31s
velero-585b8ddc7d-ftprk 1/1 Running 0 31s
部署 file-uploader 測試應用
- 在 OpenShift 3 的控制臺先進入 “file-uploader” 項目,然后在 Service Catalog 中找到 “PHP”。然后根據(jù)下圖創(chuàng)建 “file-uploader” 應用。
其中 Git Repository - https://github.com/christianh814/openshift-php-upload-demo
- 進入名為 “file-uploader” 的 Deployment ,然后查看 Configuration 欄目。在下圖中修改 Replicas 數(shù)量,將其增加到 3 個。
- 然后點擊上圖下方的 “Add Storage” 鏈接,在跳轉的 “Add Storage” 頁面中點擊“Create Storage”。
- 在“Create Storage”頁面先創(chuàng)建一個名為 “file-uploader-vol-claim” 的 PVC,然后在 Deployment 中按照下圖使用這個新建的 PVC。
- 最后通過 Route 訪問應用,然后上傳一個圖像。
查看集群配置
- 查到 ServiceAccount 中的 migration-controller 令牌。后面將使用 OCP3-TOKEN 引用這個令牌,并用來訪問這個 OpenShift 3 集群。
$ oc sa get-token migration-controller -n openshift-migration
eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJvcGVuc2hpZnQtbWlncmF0aW9uIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6Im1pZ3JhdGlvbi1jb250cm9sbGVyLXRva2VuLTJmNmhrIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQubmFtZSI6Im1pZ3JhdGlvbi1jb250cm9sbGVyIiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZXJ2aWNlLWFjY291bnQudWlkIjoiNmQyNmVjN2UtMWQzYS0xMWViLWI2ZjQtMTYzZjM1MzNlNTFmIiwic3ViIjoic3lzdGVtOnNlcnZpY2VhY2NvdW50Om9wZW5zaGlmdC1taWdyYXRpb246bWlncmF0aW9uLWNvbnRyb2xsZXIifQ.Nf134JLn9cMmNORxZC8ilJN9W43sj0_tj_B5c3Bf6l-Z4mcdXnjgR1NLH11qqJc7-9H9bgTfm8lza0kQZgEcsNIY47nxPoezkKGoOXVKEpTsaP_inykHRr6iqUuZgKiCu9I0n8st27dG0VYE8bpboPOlO8iITTH7VHbLVnWEKgk-_5CKnSmycsgFoqrfWYSEi9qbnxi2PkrQ3wLhic8RmItvCgpbvr62eV2z5Q82Gxtf9x4-RPLK5_vFtKHvK8vXjs2mw-jihM_LBuxRLbQCI64_F5MRUowFp8zUIaAuqCDtUVAPukJq6dt91tPTXg_YuSZGHV5s_NVeSIb1PDqUWg
- 獲得 OpenShift 3 集群的 API 地址,后面將使用 OCP3-API 引用這個 API 地址。
$ oc whoami --show-server
https://master.bdvwt.sandbox725.opentlc.com:443
配置遷移目標集群的環(huán)境
安裝 MTC 運行環(huán)境
- 使用缺省配置安裝 Migration Toolkit for Containers Operator。注意:缺省會安裝到 openshift-migration 項目中。
- 使用缺省配置創(chuàng)建一個 MigrationController 對象,創(chuàng)建過程還會自動創(chuàng)建一個 MigCluster 對象,并且安裝 OADP Operator。
- 查看運行在 “openshift-migration” 項目中的 Pod,還可在 “開發(fā)者” 視圖的 “拓撲” 中可以查看部署資源的狀態(tài),或查看 “migration-controller” 的 Resources 欄目中的資源狀態(tài)。
$ oc get pods -n openshift-migration
NAME READY STATUS RESTARTS AGE
migration-controller-5bc4bc78b9-pbdwn 2/2 Running 0 7m20s
migration-log-reader-6f8bfbdb99-2tjwb 2/2 Running 0 7m19s
migration-operator-5bcf744cc7-mz2gm 1/1 Running 0 9m25s
migration-ui-7d687d9476-xqc6t 1/1 Running 0 7m13s
node-agent-mzkfq 1/1 Running 0 7m40s
openshift-adp-controller-manager-7c67d4554c-5h68m 1/1 Running 0 9m28s
velero-6dc746b548-4whf2 1/1 Running 0 7m40s
- 查看名為 “migration ” 的路由地址,然后登錄該地址后可以看到 Migration Toolkit for Containers 的控制臺。
$ oc get route migration -n openshift-migration -o jsonpath=https://{.spec.host}
https://migration-openshift-migration.apps.cluster-n8rwq.n8rwq.sandbox1048.opentlc.com
添加 Replication Repository (MigStorage)
- 進入 MTC 控制臺的 Replication repositories 菜單,然后點擊 “Add replication repository” 。
- 在 “Add replication repository” 窗口中按照以下填寫配置,最后點擊 “Add Repository” 會顯示 “Connection successful”。
Storage provider type : S3
Replication repository name:ocp-migration-repository
S3 bucket name:ocp-migration-bucket
S3 endpoint:Minio 服務的路由地址
S3 provider access key:minio
S3 provider secret access key:minio123
- 完成后可以在 OpenShift 控制臺的 MTC Operator 中查找創(chuàng)建的 MigStore 對象。
添加遷移的 OpenShift 集群
- 進入 MTC 控制臺中的 “Clusters” 菜單,然后點擊 “Add cluster”。在 “Add cluster” 窗口提供以下配置:
Cluster name:ocp3
URL:https://OCP3-API
Service account token:OCP3-TOKEN
然后點擊 “Add cluster” 按鈕,成功后會顯示“Connection successful”。
- 完成后 Clusters 中的 “ocp3” 是遷移的源集群,“host” 是遷移的目標集群。
添加遷移計劃
- 進入 MTC 控制臺的 Migration plan,然后點擊 “Add migration plan”。
- 在 “Create a migration plan” 窗口的 General 步驟按照下圖設置:
Plan name:file-uploader-mig-plan
Source cluster:ocp3
Target cluster:host
Repository:ocp-migration-repository
- 在 Namespaces 步驟選中 “file-uploader”。
- 在 Persistent volumes 步驟的 “Migration type” 選擇 “Copy”。
- 在Copy options 步驟的 “Copy method” 選擇 “Filesystem copy”,然后在 Target storage class 中選擇一個目標集群的 StoreClass。
- 在 5、6 步驟接受缺省即可完成。
應用遷移和回退
執(zhí)行遷移計劃
- 在 file-uploader-migration-plan 遷移計劃的下列菜單中選擇 “Cutover”。注意:如果是 Stage,則只遷移 PV 數(shù)據(jù),而不遷移 Kubernetes 對象。
- 在彈出的窗口中點擊 “Migrate”。
- 進入上圖 “Migrations” 列的鏈接,查看遷移的執(zhí)行進度,知道全部執(zhí)行完。
- 在目標 OpenShift 4 集群中確認已經(jīng)有 file-uploader 項目,并且file-uploader應用已經(jīng)可以訪問,并且已經(jīng)有前面上傳的文件。
- 確認此時在遷移源 OpenShift 3 集群的 file-uploader 應用中的 Pod 數(shù)量已經(jīng)降為 “0”,
- 確認遷移目標 OpenShift 4 集群的 file-uploader 應用中的 Pod 數(shù)量為 “3,且應用中已包含測試圖片,這說明 PV 也遷移成功。
回退遷移
- 在 file-uploader-migration-plan 遷移計劃的下列菜單中選擇 “Rollback”。
- 查看執(zhí)行進入直到完成。
- 確認遷移目標 OpenShift 4 的 file-uploader 應用已經(jīng)不能訪問,而遷移源 OpenShift 3 的 file-uploader 應用已經(jīng)恢復訪問了。
其他功能
查看遷移過程涉及到哪些對象
可以查看 MigAnalytic 對象,它記錄了遷移過程涉及到哪些類型的對象被遷移了。
不遷移某些特定資源
可以定制 MigrationController 對象,以定義默認對那些資源進行遷移。
apiVersion: migration.openshift.io/v1alpha1
kind: MigrationController
metadata:name: migration-controllernamespace: openshift-migration
spec:disable_image_migration: truedisable_pv_migration: true...excluded_resources:- imagetags- templateinstances- clusterserviceversions- packagemanifests- subscriptions- servicebrokers- servicebindings- serviceclasses- serviceinstances- serviceplans- operatorgroups- events
演示視頻
視頻
參考
https://docs.openshift.com/container-platform/4.15/migration_toolkit_for_containers/about-mtc.html
https://docs.openshift.com/container-platform/4.15/migration_toolkit_for_containers/advanced-migration-options-mtc.html
https://docs.openshift.com/container-platform/4.15/migrating_from_ocp_3_to_4/installing-3-4.html
https://github.com/migtools/labs/blob/master/mtc/bookbag/workshop/content/Intro.adoc
https://developer.ibm.com/tutorials/migrate-kubernetes-cluster-openshift-konveyor-crane/
https://redhat-cop.github.io/openshift-migration-best-practices/
https://github.com/konveyor/labs/tree/master/mtc
https://github.com/konveyor/mig-demo-apps
https://www.konveyor.io/blog/direct-migration/
https://access.redhat.com/articles/5064151
https://www.redhat.com/it/blog/a-guide-to-migrating-workloads-from-ocp3-to-red-hat-openshift-service-on-aws-rosa-using-the-migration-toolkit-for-containers-mtc