Propagate a CRD application with Karmada

This section guides you to cover:

  • Install Karmada control plane.
  • Propagate a CRD application to multiple clusters.
  • Customize the application within a specific cluster.

Start up Karmada clusters

To start up Karmada, you can refer to here. If you just want to try Karmada, we recommend building a development environment by hack/local-up-karmada.sh.

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

Propagate CRD application

The following steps walk you through how to propagate a Guestbook defined by a CRD.

Assume you are under the guestbook directory of the Karmada repo.

  1. cd samples/guestbook

Set the KUBECONFIG environment with Karmada configuration.

  1. export KUBECONFIG=${HOME}/.kube/karmada.config
  1. Create Guestbook CRD in Karmada control plane
  1. kubectl apply -f guestbooks-crd.yaml

The CRD should be applied to karmada-apiserver.

  1. Create ClusterPropagationPolicy that will propagate Guestbook CRD to 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

The CRD will be propagated to member clusters according to the rules defined in ClusterPropagationPolicy.

Note: We can only use ClusterPropagationPolicy not PropagationPolicy here. Please refer to FAQ Difference between PropagationPolicy and ClusterPropagationPolicy for more details.

  1. Create a Guestbook CR named guestbook-sample in Karmada control plane
  1. kubectl apply -f guestbook.yaml
  1. Create PropagationPolicy that will propagate guestbook-sample to 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. Check the guestbook-sample status from Karmada
  1. kubectl get guestbook -oyaml

The output is similar to:

  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

Customize CRD application

  1. Create OverridePolicy that will override the size field of guestbook-sample in member1
  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. Check the size field of guestbook-sample from member cluster
  1. kubectl --kubeconfig=${HOME}/.kube/members.config config use-context member1
  2. kubectl --kubeconfig=${HOME}/.kube/members.config get guestbooks -o yaml

If it works as expected, the .spec.size will be overwritten to 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