All Products
Search
Document Center

Container Service for Kubernetes:Get started with application distribution

Last Updated:Sep 27, 2024

The application distribution feature of the Fleet instance in Distributed Cloud Container Platform for Kubernetes (ACK One) allows you to distribute applications across multiple clusters without relying on a Git repository, which simplifies the deployment process. This topic describes how to create an application in a Fleet instance and distribute it to multiple clusters by using distribution policies.

Prerequisites

Step 1 (Optional): Create a namespace in the Fleet instance

If the namespace for the application to be distributed does not exist in the Fleet instance, create it in the Fleet instance first. If it already exists, you can skip this step.

Connect to the Fleet instance by using the kubeconfig file. Run the following command to create a namespace named demo:

kubectl create namespace demo

Step 2: Create an application in the Fleet instance

The type of the application can be ConfigMap, Deployment, or Service. This topic uses an NGINX Deployment as an example to demonstrate how to distribute an application.

  1. Use the following YAML template to create a file named weighted-deployment.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      namespace: demo  # The namespace to which the application belongs.
      name: weighted-deployment
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: weighted-deployment
      template:
        metadata:
          labels:
            app: weighted-deployment
        spec:
          containers:
          - name: nginx
            image: anolis-registry.cn-zhangjiakou.cr.aliyuncs.com/openanolis/nginx:1.14.1-8.6
            ports:
            - containerPort: 80
  2. Run the following command to deploy the application:

    kubectl apply -f weighted-deployment.yaml

Step 3: Create a distribution policy in the Fleet instance to distribute an application to multiple clusters

Define a distribution policy (PropagationPolicy) to select the application and target clusters for distribution. Once established, the policy will distribute the selected application to the designated clusters.

In this example, the Deployment weighted-deployment is distributed to cluster1 and cluster2, with each cluster receiving three replicas.

  1. Create a file named propagationpolicy.yaml with the content outlined below.

    apiVersion: one.alibabacloud.com/v1alpha1
    kind: PropagationPolicy
    metadata:
      name: weighted-pp
      namespace: demo
    spec:
      resourceSelectors:
      - apiVersion: apps/v1
        kind: Deployment
        name: weighted-deployment
      placement:
        clusterAffinity:
          clusterIds:
          - ${cluster1-id} # Your cluster ID.
          - ${cluster2-id} # Your cluster ID.
        replicaScheduling:
          replicaSchedulingType: Duplicated

    The following table describes the key parameters.

    Parameter

    Description

    Field

    resourceSelectors

    Specifies one or more Kubernetes resources for distribution.

    • apiVersion: Specifies the API version of the resource to be distributed. This field is required.

    • kind: Specifies the type of the resource to be distributed. This field is required.

    • name: Specifies the name of the resource to be distributed.

    • namespace: Specifies the namespace of the resource to be distributed.

      Note

      PropagationPolicy can only apply to resources within the same namespace.

    • labelSelector: Specifies the labels used to select the resource to be distributed.

    placement

    clusterAffinity: Specifies the destination clusters for resource distribution.

    Specifies cluster IDs directly by using the clusterIds array.

    Note

    Make sure to use the cluster ID instead of the cluster name.

    replicaScheduling: Specifies the scheduling policy for replicable resources, such as Deployments, StatefulSets, and Jobs.

    If replicaSchedulingType is set to Duplicated, each cluster receives a replica of the resource with the specified number of replicas in spec.Replicas.

  2. Run the following command to create an distribution policy:

    kubectl apply -f propagationpolicy.yaml

Step 4 (Optional): Create an override policy in the Fleet instance

An override policy allows you to specify clusters and resources that require differentiated deployments. Once the policy is implemented, the selected resources will be customized and deployed to the specified clusters.

This example applies specific changes to the application deployed on cluster2 by setting the number of replicas to 1 and updating the image registry to that of Alibaba Cloud.

  1. Use the following YAML template to create a file named overridepolicy.yaml:

    apiVersion: one.alibabacloud.com/v1alpha1
    kind: OverridePolicy
    metadata:
      name: example
      namespace: demo
    spec:
      resourceSelectors:
        - apiVersion: apps/v1
          kind: Deployment
          name: weighted-pp
      overrideRules:
        - targetCluster:
            clusterNames:
              - ${cluster2-id}   # The cluster that requires different deployment.
          overriders:
            plaintext:
              - operator: replace
                path: /spec/replicas
                value: 1
            imageOverrider:
              - component: Registry
                operator: replace
                value: registry.cn-hangzhou.aliyuncs.com

    The following table describes the key parameters.

    Parameter

    Description

    Field

    resourceSelector

    Specifies the resource that requires override.

    • apiVersion: Required. Specifies the API version of the resource that requires override.

    • kind: Required. Specifies the type of the resource that requires override.

    • name: Specifies the name of the resource that requires override.

    • namespace: Specifies the namespace of the resource that requires override.

      Note

      PropagationPolicy can only apply to resources within the same namespace.

    • labelSelector: Specifies the labels used to select the resource that requires override.

    overrideRules

    Modifies the application template by using a set of override rules.

    • plaintext: Modifies the template by using JSON Patch.

    • imageOverrider: Modifies registry, repository, and tag of the image.

  2. Run the following command to create the override policy:

    kubectl apply -f overridepolicy.yaml

Step 5: Query the status of the application in multiple clusters

Run the following AMC command to check the status of application distribution. Alternatively, you can log on to the associated clusters to verify the status of the application.

Run the following command to query the status of the application:

kubectl amc get deploy -n demo

Expected output:

NAME                  CLUSTER          READY   UP-TO-DATE   AVAILABLE   AGE    ADOPTION
weighted-deployment   cxxxxxxxxxxxxx   3/3     3            3           3d4h   Y
weighted-deployment   cxxxxxxxxxxxxx   3/3     3            3           3d4h   Y

The output indicates that the application is distributed to the specified clusters.

References