You can create triggers for each repository in Container Registry. After you create a trigger for a repository, a notification is sent to you when a container image in the repository is updated. When the base image of an application is updated and the update matches the trigger conditions, the updated base image is pulled to redeploy the application. This topic describes how to automate Application Center to pull an updated base image and use the image to redeploy the application by creating a trigger.

Prerequisites

A container image is pushed to a repository in Container Registry. For more information, see Use a Container Registry Enterprise Edition instance to push and pull images.

Step 1: Create an application

  1. Log on to the ACK console.
  2. In the left-side navigation pane of the ACK console, choose Multi-cluster > Application Center.
  3. On the Application Configurations wizard page, set the parameters.
    1. Set Name and Description.
    2. Click the Create Template tab and then click Create with YAML in the upper-right corner of the tab.
    3. Copy the following code into the Create panel and click OK.
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        creationTimestamp: null
        labels:
          app: demo
        name: demo
      spec:
        minReadySeconds: 5
        progressDeadlineSeconds: 60
        revisionHistoryLimit: 5
        selector:
          matchLabels:
            app: demo
        strategy:
          rollingUpdate:
            maxUnavailable: 1
          type: RollingUpdate
        template:
          metadata:
            annotations:
              prometheus.io/port: "9797"
              prometheus.io/scrape: "true"
            creationTimestamp: null
            labels:
              app: demo
          spec:
            containers:
            - image: liusheng-registry.cn-beijing.cr.aliyuncs.com/liusheng/rollouts-demo:blue
              imagePullPolicy: IfNotPresent
              name: demo
              ports:
              - containerPort: 8080
                name: http
                protocol: TCP
              readinessProbe:
                initialDelaySeconds: 5
                tcpSocket:
                  port: 8080
                timeoutSeconds: 5
              resources:
                limits:
                  cpu: "2"
                  memory: 512Mi
                requests:
                  cpu: 100m
                  memory: 64Mi
      status: {}
      ---
      apiVersion: v1
      kind: Service
      metadata:
        name: demo-svc
      spec:
        selector:
          app: demo
        ports:
          - protocol: TCP
            port: 80
            targetPort: 8080
      ---
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: demo
        labels:
          app: demo
      spec:
        rules:
          - host: app.demo.example.com
            http:
              paths:
                - backend:
                    serviceName: demo-svc
                    servicePort: 80
      ---

      The following table describes some parameters.

      Parameter Description
      minReadySeconds The minimum number of seconds to wait before a rolling update starts. The cluster performs a rolling update after the specified time period ends.
      progressDeadlineSeconds The maximum number of seconds to wait before a rolling update is considered stalled. If the rolling update is considered stalled, the system stops counting the seconds.
      revisionHistoryLimit The maximum number of ReplicaSets that you want to retain.
      rollingUpdate.maxUnavailable The maximum number of pods that can be in the Unavailable state during the rolling update process.
      image The address of the image that is stored in the repository of Container Registry. In this example, the image of the demo application is used.
    4. Click Next.

Step 2: Generate the webhook URL of a trigger in Application Center

  1. Log on to the ACK console.
  2. In the left-side navigation pane of the ACK console, choose Multi-cluster > Application Center.
  3. On the Application Center page, click the name of the application that you created in Step 1.
  4. On the application details page, click Trigger in the upper-right corner.
  5. In the Trigger panel, click Create. After the webhook URL of the trigger appears, click Copy.
    Note We recommend that you keep the webhook URL confidential.

Step 3: Create a trigger in Container Registry

  1. Log on to the Container Registry console.
  2. In the top navigation bar, select a region.
  3. In the left-side navigation pane, click Instances.
  4. On the Instances page, click the required Container Registry Enterprise Edition instance.
  5. On the Repositories page, click the name of the repository that is used when you created the application in Step 1.
  6. On the repository details page, click Trigger. Then, click Create.
  7. In the Create Trigger dialog box, specify the name of the trigger, enter the webhook URL of the trigger generated in Step 2, and set Trigger to All. Then, click Confirm.
    The following trigger modes are supported: All, By RegExp, and By Tags. The following section describes the trigger modes:
    • All: Each time an image is updated, Application Center pulls the updated image.
    • By RegExp: A regular expression is used to filter image updates. Application Center pulls an image only if the image is updated and the new image version matches the regular expression.
    • By Tags: Tags are used to filter image updates. Application Center pulls an image only if the image is updated and the new image version matches one of the specified tags.
    Note If Application Center fails to pull an image, you can go to the Trigger page to find the trigger and click Access Log in the Actions column. In the Access Log message, check the cause of exceptions from the response bodies.

Step 4: Redeploy the application by using the updated container image

When a container image in the repository is updated, the updated image is automatically pulled and used to redeploy the application.

  1. Run the following command to log on to the repository that is used to create the application in Step 1.
    Note If the instance of Container Registry Enterprise Edition is created in the China (Hangzhou) region, you must set <the region where the instance of Enterprise Edition is created> to hangzhou in the command.
    docker login --username=<username used to log on to Container Registry> <name of the instance of Container Registry Enterprise Edition>-registry.cn-<the region where the instance of Container Registry Enterprise Edition is created>.cr.aliyuncs.com
  2. Run the following command to add a tag to the image:
    docker tag registry.cn-hangzhou.aliyuncs.com/acs/rollouts-demo:green liusheng-registry.cn-beijing.cr.aliyuncs.com/liusheng/rollouts-demo:green
  3. Run the following command to push the new version of the container image to the repository that is used when you created the application in Step 1.
    docker push liusheng-registry.cn-beijing.cr.aliyuncs.com/liusheng/rollouts-demo:green

Verify the result

  1. View the orchestration template.
    After the container image is updated, Application Center pulls the updated image and updates the orchestration template of the application.
    1. Log on to the ACK console.
    2. In the left-side navigation pane of the ACK console, choose Marketplace > Orchestration Templates.
    3. On the Templates page, find the template that you created in Step 1 and click Details. On the Details page, the updated image is used in the template.
  2. Enter app.demo.example.com in the address bar of your browser and press Enter to access the application.

    If the page shown in the following figure appears in your browser, the application is updated.

    Verify the result