github.com/oam-dev/kubevela@v1.9.11/references/docgen/def-doc/policy/replication.eg.md (about)

     1  ### Background
     2  
     3  In KubeVela, we can dispatch resources across the clusters. But projects like [OpenYurt](https://openyurt.io) have finer-grained division like node pool.
     4  This requires to dispatch some similar resources to the same cluster. These resources are called replication. Back to the example of OpenYurt, it can
     5  integrate KubeVela and replicate the resources then dispatch them to the different node pool.
     6  
     7  ### Usage
     8  
     9  Replication is an internal policy. It can be only used with `deploy` workflow step. When using replication policy. A new field `replicaKey` will be added to context.
    10  User can use definitions that make use of `context.replicaKey`. For example, apply a replica-webservice ComponentDefinition.
    11  
    12  In this ComponentDefinition, we can use `context.replicaKey` to distinguish the name of Deployment and Service.
    13  
    14  > **NOTE**: ComponentDefinition below is trimmed for brevity. See complete YAML in [replication.yaml](https://github.com/kubevela/kubevela/tree/master/test/e2e-test/testdata/definition/replication.yaml)
    15  
    16  ```yaml
    17  apiVersion: core.oam.dev/v1beta1
    18  kind: ComponentDefinition
    19  metadata:
    20    annotations:
    21      definition.oam.dev/description: Webservice, but can be replicated
    22    name: replica-webservice
    23    namespace: vela-system
    24  spec:
    25    workload:
    26      type: autodetects.core.oam.dev
    27    schematic:
    28      cue:
    29        template: |
    30          output: {
    31          	apiVersion: "apps/v1"
    32          	kind:       "Deployment"
    33          	metadata: {
    34          		if context.replicaKey != _|_ {
    35          			name: context.name + "-" + context.replicaKey
    36          		}
    37          		if context.replicaKey == _|_ {
    38          			name: context.name
    39          		}
    40          	}
    41          	spec: {
    42          		selector: matchLabels: {
    43          			"app.oam.dev/component": context.name
    44          			if context.replicaKey != _|_ {
    45          				"app.oam.dev/replicaKey": context.replicaKey
    46          			}
    47          		}
    48  
    49          		template: {
    50          			metadata: {
    51          				labels: {
    52          					if parameter.labels != _|_ {
    53          						parameter.labels
    54          					}
    55          					if parameter.addRevisionLabel {
    56          						"app.oam.dev/revision": context.revision
    57          					}
    58          					"app.oam.dev/name":      context.appName
    59          					"app.oam.dev/component": context.name
    60          					if context.replicaKey != _|_ {
    61          						"app.oam.dev/replicaKey": context.replicaKey
    62          					}
    63  
    64          				}
    65          				if parameter.annotations != _|_ {
    66          					annotations: parameter.annotations
    67          				}
    68          			}
    69          		}
    70          	}
    71          }
    72          outputs: {
    73          	if len(exposePorts) != 0 {
    74          		webserviceExpose: {
    75          			apiVersion: "v1"
    76          			kind:       "Service"
    77          			metadata: {
    78          				if context.replicaKey != _|_ {
    79          					name: context.name + "-" + context.replicaKey
    80          				}
    81          				if context.replicaKey == _|_ {
    82          					name: context.name
    83          				}
    84          			}
    85          			spec: {
    86          				selector: {
    87          					"app.oam.dev/component": context.name
    88          					if context.replicaKey != _|_ {
    89          						"app.oam.dev/replicaKey": context.replicaKey
    90          					}
    91          				}
    92          				ports: exposePorts
    93          				type:  parameter.exposeType
    94          			}
    95          		}
    96          	}
    97          }
    98  ```
    99  
   100  Then user can apply application below. Replication policy is declared in `application.spec.policies`. These policies are used in `deploy-with-rep` workflow step.
   101  They work together to influence the `deploy` step.
   102  
   103  - override: select `hello-rep` component to deploy.
   104  - topology: select cluster `local` to deploy.
   105  - replication: select `hello-rep` component to replicate.
   106  
   107  As a result, there will be two Deployments and two Services:
   108  
   109  ```yaml
   110  apiVersion: core.oam.dev/v1beta1
   111  kind: Application
   112  metadata:
   113    name: app-replication-policy
   114  spec:
   115    components:
   116      - name: hello-rep
   117        type: replica-webservice
   118        properties:
   119          image: crccheck/hello-world
   120          ports:
   121            - port: 80
   122              expose: true
   123    policies:
   124      - name: comp-to-replicate
   125        type: override
   126        properties:
   127          selector: [ "hello-rep" ]
   128      - name: target-default
   129        type: topology
   130        properties:
   131          clusters: [ "local" ]
   132      - name: replication-default
   133        type: replication
   134        properties:
   135          keys: ["beijing","hangzhou"]
   136          selector: ["hello-rep"]
   137  
   138    workflow:
   139      steps:
   140        - name: deploy-with-rep
   141          type: deploy
   142          properties:
   143            policies: ["comp-to-replicate","target-default","replication-default"]
   144  ```
   145  
   146  ```shell
   147  kubectl get deploy -n default
   148  NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
   149  hello-rep-beijing    1/1     1            1           5s
   150  hello-rep-hangzhou   1/1     1            1           5s
   151  
   152  kubectl get service -n default
   153  NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
   154  hello-rep-hangzhou   ClusterIP   10.43.23.200   <none>        80/TCP    41s
   155  hello-rep-beijing    ClusterIP   10.43.24.116   <none>        80/TCP    12s
   156  ```