Propagate a CRD application with Karmada
This section guides you to:
- 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.
git clone https://github.com/karmada-io/karmadacd karmadahack/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.
cd samples/guestbook
Set the KUBECONFIG environment with Karmada configuration.
export KUBECONFIG=${HOME}/.kube/karmada.config
- Create Guestbook CRD in Karmada control plane
kubectl apply -f guestbooks-crd.yaml
The CRD should be applied to karmada-apiserver.
- Create ClusterPropagationPolicy that will propagate Guestbook CRD to member1
kubectl apply -f guestbooks-clusterpropagationpolicy.yaml
# guestbooks-clusterpropagationpolicy.yamlapiVersion: policy.karmada.io/v1alpha1kind: ClusterPropagationPolicymetadata:name: example-policyspec:resourceSelectors:- apiVersion: apiextensions.k8s.io/v1kind: CustomResourceDefinitionname: guestbooks.webapp.my.domainplacement:clusterAffinity:clusterNames:- 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.
- Create a Guestbook CR named
guestbook-samplein Karmada control plane
kubectl apply -f guestbook.yaml
- Create PropagationPolicy that will propagate
guestbook-sampleto member1
kubectl apply -f guestbooks-propagationpolicy.yaml
# guestbooks-propagationpolicy.yamlapiVersion: policy.karmada.io/v1alpha1kind: PropagationPolicymetadata:name: example-policyspec:resourceSelectors:- apiVersion: webapp.my.domain/v1kind: Guestbookplacement:clusterAffinity:clusterNames:- member1
- Check the
guestbook-samplestatus from Karmada
kubectl get guestbook -oyaml
The output is similar to:
apiVersion: webapp.my.domain/v1kind: Guestbookmetadata:annotations:kubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"webapp.my.domain/v1","kind":"Guestbook","metadata":{"annotations":{},"name":"guestbook-sample","namespace":"default"},"spec":{"alias":"Name","configMapName":"test","size":2}}creationTimestamp: "2022-11-18T06:56:24Z"generation: 1labels:propagationpolicy.karmada.io/name: example-policypropagationpolicy.karmada.io/namespace: defaultname: guestbook-samplenamespace: defaultresourceVersion: "682895"uid: 2f8eda5f-35ab-4ac3-bcd4-affcf36a9341spec:alias: NameconfigMapName: testsize: 2
Customize CRD application
- Create OverridePolicy that will override the size field of guestbook-sample in member1
kubectl apply -f guestbooks-overridepolicy.yaml
# guestbooks-overridepolicy.yamlapiVersion: policy.karmada.io/v1alpha1kind: OverridePolicymetadata:name: guestbook-samplespec:resourceSelectors:- apiVersion: webapp.my.domain/v1kind: GuestbookoverrideRules:- targetCluster:clusterNames:- member1overriders:plaintext:- path: /spec/sizeoperator: replacevalue: 4- path: /metadata/annotationsoperator: addvalue: {"OverridePolicy":"test"}
- Check the size field of
guestbook-samplefrom member cluster
kubectl --kubeconfig=${HOME}/.kube/members.config config use-context member1kubectl --kubeconfig=${HOME}/.kube/members.config get guestbooks -o yaml
If it works as expected, the .spec.size will be overwritten to 4:
apiVersion: webapp.my.domain/v1kind: Guestbookmetadata:annotations:OverridePolicy: testkubectl.kubernetes.io/last-applied-configuration: |{"apiVersion":"webapp.my.domain/v1","kind":"Guestbook","metadata":{"annotations":{},"name":"guestbook-sample","namespace":"default"},"spec":{"alias":"Name","configMapName":"test","size":2}}resourcebinding.karmada.io/name: guestbook-sample-guestbookresourcebinding.karmada.io/namespace: defaultresourcetemplate.karmada.io/uid: 2f8eda5f-35ab-4ac3-bcd4-affcf36a9341creationTimestamp: "2022-11-18T06:56:37Z"generation: 2labels:propagationpolicy.karmada.io/name: example-policypropagationpolicy.karmada.io/namespace: defaultresourcebinding.karmada.io/key: 6849fdbd59work.karmada.io/name: guestbook-sample-6849fdbd59work.karmada.io/namespace: karmada-es-member1name: guestbook-samplenamespace: defaultresourceVersion: "430024"uid: 8818e33d-10bf-4270-b3b9-585977425bc9spec:alias: NameconfigMapName: testsize: 4