Automatically build and update images for ECI scaling groups by using an Alibaba Cloud DevOps pipeline

Updated at:
Copy as MD

Integrate Alibaba Cloud DevOps pipelines with Auto Scaling to automatically build and deploy container images to your ECI scaling group with a single click.

How it works

This solution uses Alibaba Cloud DevOps pipelines to automate image building and deployment after you update your code:

  1. The pipeline builds your code repository into a container image.

  2. The pipeline publishes the image to Container Registry (ACR).

  3. The pipeline uses Operation Orchestration Service (OOS) to update the scaling group instances and scaling configuration to use the new image.

What is Alibaba Cloud DevOps?

Prerequisites

If you log on with a RAM user, ensure you have the required permissions. Manage RAM user permissions.

Sample custom policy

{
    "Version": "1",
    "Statement": [
        {
            "Action": [
                "ess:*",
                "cr:*",
                "rdc:*",
                "vpc:Describe*",
                "ecs:DescribeSecurityGroups",
                "oos:ListExecutions",
                "oos:ListTaskExecutions",
                "oos:GetExecutionTemplate",
                "eci:Describe*",
                "eci:ListUsage",
                "oss:GetExecutionTemplate",
                "ram:GetRole"
            ],
            "Resource": "*",
            "Effect": "Allow"
        }
    ]
}

Configure the environment

Complete the following steps to set up one-click image deployment.

Step 1: Create a Container Registry (ACR) repository

Important

If you already have a Container Registry (ACR) repository, you can skip this step.

The repository stores your built images.

  1. Log on to the Container Registry console.

  2. Create a Container Registry instance.

    1. In the top navigation bar, select a region.

      Note

      Keep the repository and scaling group in the same region to enable VPC communication via internal domain names. Cross-region setups require public network access for both the repository and scaling group instances.

    2. On the Instances page, click Personal Instance and wait for the instance to be created.

    3. After the instance is created, click Reset Docker Login Password on the instance details page and follow the on-screen instructions to set the password.

  3. After setting the registry login password, in the left-side navigation pane, click Namespaces. Then, click Create Namespace and follow the on-screen instructions to create a namespace.

  4. In the left-side navigation pane, choose Repositories and then click Create Repository. Configure the parameters as prompted. The following table describes the main parameters.

    • Repository information

      Parameter

      Description

      Example

      Namespaces

      Select the namespace that you created in the previous step.

      ess_eci_test_namespace

      Repository Name

      Enter a custom name for the repository.

      ess_eci_test_repo

      Repository Type

      Select Private.

      Private

      Summary

      Enter a brief description of the repository's purpose.

      test

    • Code source

      Select Local Repository and then click Create Repository.

  5. After the repository is created, you are redirected to its Details page. Find and copy the Public endpoint for later use.

Step 2: Create an ECI scaling group

Important

If you already have a scaling group, you can skip this step.

You can create a scaling group in one of the following ways:

  • By using a YAML file

  • By using the web console form

Use YAML file

  1. Log on to the Auto Scaling console.

  2. In the top navigation bar, select a region. In the left-side navigation pane, click Scaling Groups, and then click Create Scaling Group.

  3. On the Create by YAML File tab, configure the scaling group and click Create. This topic uses the following configuration. Configure an ECI scaling group by using a Kubernetes YAML file.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-demo
      annotations:
        k8s.aliyun.com/ess-scaling-group-name: ess-eci-test
        k8s.aliyun.com/ess-scaling-group-min-size: 0
        k8s.aliyun.com/ess-scaling-group-max-size: 5
    spec:
      selector:
        matchLabels:
          app: nginx-demo
      replicas: 0
      template:
        metadata:
          labels:
            app: nginx-demo
          annotations:
            k8s.aliyun.com/eci-with-eip: false
        spec:
          containers:
            - name: nginx
              image: registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx:latest
              ports:
                - containerPort: 80
                  name: http
                - containerPort: 443
                  name: https
              resources:
                requests:
                  memory: 0.05Gi
                  cpu: 50m
                limits:
                  memory: 1Gi
                  cpu: '1'
    Warning

    Use your own application image if available; otherwise, use the sample image. The pipeline replaces it later. An image is required to create the scaling group.

    The following table describes the parameters.

    Parameter

    Description

    Example

    k8s.aliyun.com/ess-scaling-group-name

    The name of the scaling group.

    ess-eci-test

    k8s.aliyun.com/ess-scaling-group-min-size

    Minimum instances in the scaling group. Auto Scaling adds instances when the count drops below this value.

    0

    k8s.aliyun.com/ess-scaling-group-max-size

    Maximum instances in the scaling group. Auto Scaling removes instances when the count exceeds this value.

    5

    k8s.aliyun.com/eci-with-eip

    Whether to automatically assign an Elastic IP (EIP) address.

    false

Use console form

  1. Log on to the Auto Scaling console.

  2. In the top navigation bar, select a region.

  3. Create a scaling group.

    1. In the left-side navigation pane, click Scaling Groups, and then click Create Scaling Group.

    2. On the Create by Form tab, configure the scaling group and click Create. This topic uses the following configuration. Create a scaling group.

      Parameter

      Description

      Example

      Scaling Group Name

      Enter a name for the scaling group.

      ess-eci-test

      Type

      Select ECI. This specifies that the instances in the scaling group are ECI instances.

      ECI

      Instance Configuration Source

      Creates a scaling configuration after the scaling group is created.

      Create from Scratch

      Minimum Number of Instances

      The minimum number of ECI instances in the scaling group.

      0

      Maximum Number of Instances

      The maximum number of ECI instances in the scaling group.

      5

      VPC

      Instances in this scaling group are deployed in this VPC.

      vpc-test****-001

      vSwitch

      Specify vSwitches in multiple zones to improve scale-out success rates.

      vsw-test****

  4. Create a scaling configuration for the scaling group.

    1. Find the scaling group that you just created, and click More to go to the scaling group details page.

    2. On the Instance Configuration Sources tab, click Scaling Configuration, and then click Create Scaling Configuration to go to the Create Scaling Configuration page.

    3. This topic uses the following configuration. Keep all other parameters at their default values. After completing the configuration, click Confirm Configuration, and then click Create Order. Create a scaling configuration for ECI instances.

      Parameter

      Description

      Example

      Billing Method

      Select a billing method based on your requirements.

      Pay-as-you-go

      Security Group

      Select a security group based on your requirements.

      sg-****oer

      Container Configuration > Container Name

      Enter a name for your container.

      nginx

      Container Configuration > Image

      Select the image and version for the container.

      Warning

      Use your own application image if available; otherwise, use the sample image. An image is required to create the scaling group.

      • Image Name: registry-vpc.cn-hangzhou.aliyuncs.com/eci_open/nginx

      • Image Tag: latest

    4. On the Scaling Configuration tab, find the scaling configuration that you just created and click Enable.

Step 3: Configure a DevOps pipeline

Set up an Alibaba Cloud DevOps pipeline to automate image building and deployment.

  1. On the Scaling Groups page, click the Publish Management tab, and then click Release with Alibaba Cloud DevOps.

  2. Select an Alibaba Cloud DevOps organization and click Next.

    Important

    If you have not created an Alibaba Cloud DevOps organization, click Create Organization and follow the prompts. After selecting an organization, complete the authorization as prompted.

  3. Configure the pipeline.

    1. Configure the pipeline source. In the Pipeline Source section, click Add, select Sample code source, and then click Add.

      Note

      If you have your own code source, select your code repository URL as prompted.

    2. Add an image build task.

      1. In the Image Building Task section, click Add.

      2. In the Image Building Task > Select Service Connection, click Create Service Connection. You are redirected to the service connection management page of Alibaba Cloud DevOps. Click Create Service Connection.

      3. Select Container Registry (ACR), click Next, select a service for Service Authorization, and then click Create. If no service is available, click Create to go to the authorization page.

      4. Return to the Image Building Task page. For the Select Service Connection parameter, select the service connection that you created. For the Repositories parameter, select the repository that you created. Then, click Add.

        Set Image type to Personal Edition, select Alibaba Cloud DevOps Beijing Build Cluster for Build cluster, and select the target region such as China (Hangzhou) for Region.

    3. Add a deployment task.

      1. In the Deployment Task section, click Add. In the Select Service Connection section, click Create Service Connection.

        In the deployment task configuration panel, set Task name to ESS ECI Deployment, select Alibaba Cloud DevOps Beijing Build Cluster for Build cluster, select nginx for Container to update, set Release batches to 2 batches, and set Batch method to Auto-confirm.

      2. For Service Connection Type, select Auto Scaling (ESS), click Next, and then click Create.

      3. In the Image Deployment section, select Image built and pushed to Alibaba Cloud Container Registry Personal Edition.Image VPC Address, and then click Add.

        Note

        If your scaling group and Container Registry are in the same region, select Mirror VPC address. Cross-region setups require public network access to pull the image.

    4. Click Create.

Run the pipeline

Important

To allow another RAM user to use this feature, ensure the RAM user belongs to your Alibaba Cloud DevOps organization and has pipeline permissions. The RAM user needs at least:

  • Manage permissions on Alibaba Cloud DevOps (RDC).

  • Read-only access to Resource Access Management (RAM).

  • Manage permissions on Operation Orchestration Service (OOS).

After updating your code, on the Publish by Using Apsara DevOps tab, click Run to build and deploy the new image to your scaling group.

  1. Prerequisites

  2. Build and deploy the image with one click.

    1. Go to the scaling group, click the Publish Management tab, click the Release with Alibaba Cloud DevOps sub-tab, and then click Run. A new entry appears in the run history list.

    2. In the run history list, click Details to monitor the pipeline. After completion, the scaling group instances and scaling configuration update automatically, ensuring new scale-out instances use the new image.

      • On the Instance Configuration Sources tab, find the active scaling configuration, click Modify, and verify that the Container Configuration > Image section is updated.

      • Manually trigger a scale-out event. On the Instances tab, click the newly added ECI instance to confirm that it uses the new image.

      • For existing ECI instances, go to the Instances tab, click an instance name to open its details page, and confirm that the image has been updated.