通过 Karmada 分发 CRD

在本节中,我们将引导您完成以下内容:

  • 安装 Karmada 控制平面。
  • 将 CRD 分发到多个集群。
  • 在特定集群中自定义 CRD。

启动 Karmada 集群

想要启动 Karmada,您可以参考 here.

如果您只想尝试 Karmada,请使用 hack/local-up-karmada.sh 构建开发环境.

  1. git clone https://github.com/karmada-io/karmada
  2. cd karmada
  3. hack/local-up-karmada.sh

分发 CRD

下面的步骤指导您如何分发 CRD Guestbook

假设您在 Karmada 仓库的 guestbook 目录下。

  1. cd samples/guestbook

使用 Karmada 配置设置 KUBECONFIG 环境变量。

  1. export KUBECONFIG=${HOME}/.kube/karmada.config
  1. 在 Karmada 的控制平面上创建 Guestbook CRD
  1. kubectl apply -f guestbooks-crd.yaml

此 CRD 应该被应用到 karmada-apiserver

  1. 创建 ClusterPropagationPolicy,将 Guestbook CRD 分发到 member1
  1. kubectl apply -f guestbooks-clusterpropagationpolicy.yaml
  1. # guestbooks-clusterpropagationpolicy.yaml
  2. apiVersion: policy.karmada.io/v1alpha1
  3. kind: ClusterPropagationPolicy
  4. metadata:
  5. name: example-policy
  6. spec:
  7. resourceSelectors:
  8. - apiVersion: apiextensions.k8s.io/v1
  9. kind: CustomResourceDefinition
  10. name: guestbooks.webapp.my.domain
  11. placement:
  12. clusterAffinity:
  13. clusterNames:
  14. - member1

根据 ClusterPropagationPolicy 中定义的规则,此 CRD 将分发到成员集群。

注意:在这里我们只能使用 ClusterPropagationPolicy 而不是 PropagationPolicy。 更多详细信息,请参考 FAQ PropagationPolicy and ClusterPropagationPolicy

  1. 在 Karmada 控制平面上创建名为 guestbook-sample 的 Guestbook CR
  1. kubectl apply -f guestbook.yaml
  1. 创建 PropagationPolicy,将 guestbook-sample 分发到 member1
  1. kubectl apply -f guestbooks-propagationpolicy.yaml
  1. # guestbooks-propagationpolicy.yaml
  2. apiVersion: policy.karmada.io/v1alpha1
  3. kind: PropagationPolicy
  4. metadata:
  5. name: example-policy
  6. spec:
  7. resourceSelectors:
  8. - apiVersion: webapp.my.domain/v1
  9. kind: Guestbook
  10. placement:
  11. clusterAffinity:
  12. clusterNames:
  13. - member1
  1. 检查 Karmada 中 guestbook-sample 的状态
  1. kubectl get guestbook -oyaml

输出类似于以下内容:

  1. apiVersion: webapp.my.domain/v1
  2. kind: Guestbook
  3. metadata:
  4. annotations:
  5. kubectl.kubernetes.io/last-applied-configuration: |
  6. {"apiVersion":"webapp.my.domain/v1","kind":"Guestbook","metadata":{"annotations":{},"name":"guestbook-sample","namespace":"default"},"spec":{"alias":"Name","configMapName":"test","size":2}}
  7. creationTimestamp: "2022-11-18T06:56:24Z"
  8. generation: 1
  9. labels:
  10. propagationpolicy.karmada.io/name: example-policy
  11. propagationpolicy.karmada.io/namespace: default
  12. name: guestbook-sample
  13. namespace: default
  14. resourceVersion: "682895"
  15. uid: 2f8eda5f-35ab-4ac3-bcd4-affcf36a9341
  16. spec:
  17. alias: Name
  18. configMapName: test
  19. size: 2

自定义 CRD

  1. 创建 OverridePolicy,将覆盖 member1 中 guestbook-sample 的 size 字段。
  1. kubectl apply -f guestbooks-overridepolicy.yaml
  1. # guestbooks-overridepolicy.yaml
  2. apiVersion: policy.karmada.io/v1alpha1
  3. kind: OverridePolicy
  4. metadata:
  5. name: guestbook-sample
  6. spec:
  7. resourceSelectors:
  8. - apiVersion: webapp.my.domain/v1
  9. kind: Guestbook
  10. overrideRules:
  11. - targetCluster:
  12. clusterNames:
  13. - member1
  14. overriders:
  15. plaintext:
  16. - path: /spec/size
  17. operator: replace
  18. value: 4
  19. - path: /metadata/annotations
  20. operator: add
  21. value: {"OverridePolicy":"test"}
  1. 检查来自成员集群的 guestbook-sample 的 size 字段
  1. kubectl --kubeconfig=${HOME}/.kube/members.config config use-context member1
  2. kubectl --kubeconfig=${HOME}/.kube/members.config get guestbooks -o yaml

如果按预期工作,则 .spec.size 将被覆盖为 4:

  1. apiVersion: webapp.my.domain/v1
  2. kind: Guestbook
  3. metadata:
  4. annotations:
  5. OverridePolicy: test
  6. kubectl.kubernetes.io/last-applied-configuration: |
  7. {"apiVersion":"webapp.my.domain/v1","kind":"Guestbook","metadata":{"annotations":{},"name":"guestbook-sample","namespace":"default"},"spec":{"alias":"Name","configMapName":"test","size":2}}
  8. resourcebinding.karmada.io/name: guestbook-sample-guestbook
  9. resourcebinding.karmada.io/namespace: default
  10. resourcetemplate.karmada.io/uid: 2f8eda5f-35ab-4ac3-bcd4-affcf36a9341
  11. creationTimestamp: "2022-11-18T06:56:37Z"
  12. generation: 2
  13. labels:
  14. propagationpolicy.karmada.io/name: example-policy
  15. propagationpolicy.karmada.io/namespace: default
  16. resourcebinding.karmada.io/key: 6849fdbd59
  17. work.karmada.io/name: guestbook-sample-6849fdbd59
  18. work.karmada.io/namespace: karmada-es-member1
  19. name: guestbook-sample
  20. namespace: default
  21. resourceVersion: "430024"
  22. uid: 8818e33d-10bf-4270-b3b9-585977425bc9
  23. spec:
  24. alias: Name
  25. configMapName: test
  26. size: 4