Skip to content

Commit

Permalink
add tutorial doc in website repo about how to do migration rollback
Browse files Browse the repository at this point in the history
Signed-off-by: wulemao <1194736083@qq.com>
  • Loading branch information
wulemao committed Oct 26, 2024
1 parent a612b36 commit f634329
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 100 deletions.
5 changes: 5 additions & 0 deletions docs/tutorials/resource-migration-rollback.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: Resource Migration Rollback
---

## Objectives
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
title: 迁移回滚
---

## 目标
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ title: 平滑迁移

在此背景下,本节将引导您完成:

- 以资源为粒度将所有已部署资源从原集群迁移到 Karmada。
- 应用更高优先级的 `PropagationPolicy`,以满足以应用为粒度的更多分发需求。
- 将已部署在原单集群的资源迁移到 Karmada。
- 迁移回滚,从 Karmada 删除迁移的资源并保留成员集群资源

## 前提条件

Expand All @@ -31,31 +31,13 @@ export KUBECONFIG=~/.kube/karmada.config:~/.kube/members.config
>
> 执行上述命令后,您将看到Karmada控制面和多个成员集群已安装完成。
### 在 karmada-controller-manager 开启 PropagationPolicyPreemption 特性开关

#### 步骤二: 运行命令

```shell
kubectl --context karmada-host get deploy karmada-controller-manager -n karmada-system -o yaml | sed '/- --failover-eviction-timeout=30s/{n;s/- --v=4/- --feature-gates=PropagationPolicyPreemption=true\n &/g}' | kubectl --context karmada-host replace -f -
```

> **说明:**
>
> `PropagationPolicy Priority and Preemption` 特性是在 v1.7 版本中引入的,它由特性开关 `PropagationPolicyPreemption` 控制,默认是关闭的。
>
> 您只需执行上面的一条命令即可启用此特性开关。或者,如果您想使用更谨慎的方法,您可以这样做:
>
> 1. 执行 `kubectl --context karmada-host edit deploy karmada-controller-manager -n karmada-system`
> 2. 检查 `spec.template.spec.containers[0].command` 字段是否有 `--feature-gates=PropagationPolicyPreemption=true` 这一行。
> 3. 如果没有,您需要添加 `--feature-gates=PropagationPolicyPreemption=true` 到上述字段中。
### 在成员集群中预置资源

为了模拟成员集群中已经存在现有资源,我们将一些简单的 Deployment 和 Service 部署到 `member1` 集群。
为了模拟成员集群中已经存在现有资源,我们将简单的 Deployment 部署到 `member1` 集群。

#### 步骤三: 编写代码
#### 步骤二: 编写代码

创建新文件 `/tmp/deployments-and-services.yaml` 并写入以下文本:
创建新文件 `/tmp/deployments.yaml` 并写入以下文本:

```yaml
apiVersion: apps/v1
Expand All @@ -77,54 +59,43 @@ spec:
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx
type: NodePort
ports:
- port: 80
nodePort: 30000
targetPort: 80
```
#### 步骤四: 运行命令
#### 步骤三: 运行命令
```shell
$ kubectl --context member1 apply -f /tmp/deployments-and-services.yaml
$ kubectl --context member1 apply -f /tmp/deployments.yaml
deployment.apps/nginx-deploy created
service/nginx-svc created
deployment.apps/hello-deploy created
service/hello-svc created

$ karmadactl --karmada-context karmada-apiserver get deploy --operation-scope=all
NAME CLUSTER READY UP-TO-DATE AVAILABLE AGE ADOPTION
nginx-deploy member1 2/2 2 2 15m N
```

因此,我们可以使用 `member1` 作为已部署现有资源的集群。
您将看到当前 nginx-deploy 部署在 member1 集群,`ADOPTION=N` 表示它此时不是由 Karmada 管理

## 指导

### 将所有资源迁移到 Karmada
### 将资源迁移到 Karmada

#### 步骤一: 运行命令

```shell
$ kubectl --context karmada-apiserver apply -f /tmp/deployments-and-services.yaml
$ kubectl --context karmada-apiserver apply -f /tmp/deployments.yaml
deployment.apps/nginx-deploy created
service/nginx-svc created
deployment.apps/hello-deploy created
service/hello-svc created

$ karmadactl --karmada-context karmada-apiserver get deploy --operation-scope=all
NAME CLUSTER READY UP-TO-DATE AVAILABLE AGE ADOPTION
nginx-deploy Karmada 0/2 0 0 7s -
nginx-deploy member1 2/2 2 2 16m N
```

> **说明:**
>
> 相同的 Deployments 和 Services 应被部署到 Karmada 控制面,作为 [ResourceTemplate](https://karmada.io/docs/core-concepts/concepts#resource-template)
您将看到该 Deployment 被部署到 Karmada 控制面,作为 [ResourceTemplate](https://karmada.io/docs/core-concepts/concepts#resource-template)
此时尚无匹配的 PropagationPolicy 对其分发。

#### 步骤二: 编写代码

创建新文件 `/tmp/pp-for-migrating-deployments-and-services.yaml` 并写入以下文本:
创建新文件 `/tmp/pp-for-migrating-deployments.yaml` 并写入以下文本:

```yaml
apiVersion: policy.karmada.io/v1alpha1
Expand All @@ -137,12 +108,11 @@ spec:
clusterAffinity:
clusterNames:
- member1
priority: 0
- member2
resourceSelectors:
- apiVersion: apps/v1
kind: Deployment
- apiVersion: v1
kind: Service
name: nginx-deploy
schedulerName: default-scheduler
```
Expand All @@ -158,75 +128,58 @@ spec:
应用上述 `PropagationPolicy` 到 Karmada 控制面:

```shell
$ kubectl --context karmada-apiserver apply -f /tmp/pp-for-migrating-deployments-and-services.yaml
$ kubectl --context karmada-apiserver apply -f /tmp/pp-for-migrating-deployments.yaml
propagationpolicy.policy.karmada.io/migrate-pp created
```

#### 步骤四: 验证

```shell
$ kubectl --context karmada-apiserver get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deploy 2/2 2 2 38s
$ kubectl --context karmada-apiserver get rb
NAME SCHEDULED FULLYAPPLIED AGE
nginx-deploy-deployment True True 13s
nginx-svc-service True True 13s
$ karmadactl --karmada-context karmada-apiserver get deploy --operation-scope=all
NAME CLUSTER READY UP-TO-DATE AVAILABLE AGE ADOPTION
nginx-deploy Karmada 4/2 4 4 34s -
nginx-deploy member1 2/2 2 2 16m Y
nginx-deploy member2 2/2 2 2 8s Y
```

您将看到 Karmada 中的 Deployment 已全部就绪,并且 `ResourceBinding` 的 `FULLYAPPLIED` 为 True,这表示 `member1` 集群中的现有资源已被 Karmada 接管。
您将看到 Karmada 中的 Deployment 已全部就绪,其中:

* member1 集群的 `nginx-deploy` 成功被 Karmada 接管:`AGE=16m` 说明该资源是接管而不是重新创建,`ADOPTION=Y` 表示该资源现在由 Karmada 管理。
* member2 集群原本没有 `nginx-deploy`,现在以复制模式分发成功,`ADOPTION=Y` 表示该资源现在由 Karmada 管理。

至此,您已经完成了迁移,是不是很简单?

### 应用更高优先级的 PropagationPolicy
### 对已迁移的资源进行回滚

#### 步骤五: 编写代码
#### 步骤五: 对 PropagationPolicy 设置 preserveResourcesOnDeletion

创建新文件 `/tmp/pp-for-nginx-app.yaml` 并写入以下文本:
执行下述命令修改 `PropagationPolicy/migrate-pp`,设置 `spec.preserveResourcesOnDeletion=true`

```yaml
apiVersion: policy.karmada.io/v1alpha1
kind: PropagationPolicy
metadata:
name: nginx-pp
spec:
conflictResolution: Overwrite
placement:
clusterAffinity:
clusterNames:
- member1
- member2 ## propagate to more clusters other than member1
priority: 10 ## priority greater than above PropagationPolicy (10 > 0)
preemption: Always ## preemption should equal to Always
resourceSelectors:
- apiVersion: apps/v1
kind: Deployment
name: nginx-deploy
- apiVersion: v1
kind: Service
name: nginx-svc
schedulerName: default-scheduler
```shell
$ kubectl --context karmada-apiserver patch pp migrate-pp --type='json' -p '[{"op": "replace", "path": "/spec/preserveResourcesOnDeletion", "value": true}]'
propagationpolicy.policy.karmada.io/nginx-pp patched
```

#### 步骤六: 运行命令
### 步骤六:从控制面删除资源模板

应用上述 `PropagationPolicy` 到 Karmada 控制面
执行下述命令

```shell
$ kubectl --context karmada-apiserver apply -f /tmp/pp-for-nginx-app.yaml
propagationpolicy.policy.karmada.io/nginx-pp created
$ kubectl --context karmada-apiserver delete -f /tmp/deployments.yaml
deployment.apps "nginx-deploy" deleted
```

#### 步骤七: 验证
### 步骤七:验证

执行下述命令:

```shell
$ kubectl --context member2 get deploy -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deploy 2/2 2 2 5m24s nginx nginx:latest app=nginx
$ kubectl --context member2 get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx-svc NodePort 10.13.161.255 <none> 80:30000/TCP 54s app=nginx
$ karmadactl --karmada-context karmada-apiserver get deploy --operation-scope=all
NAME CLUSTER READY UP-TO-DATE AVAILABLE AGE ADOPTION
nginx-deploy member1 2/2 2 2 17m N
nginx-deploy member2 2/2 2 2 49s N
...
```

您将看到 `nginx` 应用相关的资源被分发到 `member2` 集群,这表示更高优先级的 `PropagationPolicy` 生效了。
您将看到,控制面的资源模板已删除,并且成员集群中的资源依然可以保留,`ADOPTION=N` 表示该资源不是由 Karmada 管理。
至此,您已经完成了迁移回滚。
1 change: 1 addition & 0 deletions sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module.exports = {
"tutorials/autoscaling-federatedhpa-with-cronfederatedhpa",
"tutorials/autoscaling-workload-with-cronfederatedhpa",
"tutorials/resource-migration",
"tutorials/resource-migration-rollback",
"tutorials/access-service-across-clusters",
"tutorials/workload-rebalancer",
],
Expand Down

0 comments on commit f634329

Please sign in to comment.