管理服务发布

本页面介绍如何使用 Service Infrastructure 执行逐步的服务配置发布

更新生产服务的服务配置存在风险,可能会导致中断。通过 Service Management API,您可以逐步发布配置更改,从而缓和错误的服务配置所产生的影响。

您可以部署多个服务配置版本,并定义如何在运行时使用这些版本,所有这些都可以通过调用 services.rollouts.create 方法来启动 Rollout。一次最多可以发布 5 个服务配置。

准备工作

如要运行本指南中的示例,请确保首先按照 Service Management API 使用入门中的说明完成初始设置。

执行发布

假设您有一项基于 Service Management API 构建的托管式服务 endpointsapis.appspot.com,则可以按照下列步骤以有序的方式分阶段发布服务配置更改。

例如,endpointsapis.appspot.com 当前正在使用服务配置 old,并且您想要将其更改为使用服务配置 new。您可以创建一个发布,以利用 10% 的总流量测试新服务配置,而不是让所有生产流量都立即使用新服务配置:

# Create rollout to test the new configuration with 10% traffic.
$ gcurl -d '{
  "rolloutId": "canary-rollout",
  "serviceName": "endpointsapis.appspot.com",
  "trafficPercentStrategy": {
    "percentages": {
      "new": 10,
      "old": 90
    }
  }
}' https://servicemanagement.googleapis.com/v1/services/endpointsapis.appspot.com/rollouts
{
  "name": "operations/rollouts.endpointsapis.appspot.com:canary-rollout"
  "metadata": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
    "resourceNames": [
      "services/endpointsapis.appspot.com/rollouts/canary-rollout"
    ],
    "startTime": ...
  },
  "response": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
    "rolloutId": "canary-rollout",
    "createTime": ...
    "trafficPercentStrategy": {
      "percentages": {
        "old": 90,
        "new": 10,
      }
    },
    "serviceName": "endpointsapis.appspot.com"
  }
}

创建首次发布后,您可以通过运行以下命令,替换您自己的首次展示 ID 来获得首次发布的状态:

# Get rollout status of `operations/rollouts.endpointsapis.appspot.com:canary-rollout`.
$ gcurl https://servicemanagement.googleapis.com/v1/operations/rollouts.endpointsapis.appspot.com:canary-rollout
{
  "name": "operations/rollouts.endpointsapis.appspot.com:canary-rollout",
  "metadata": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
    "resourceNames": [
      "services/endpointsapis.appspot.com/rollouts/canary-rollout"
    ],
    "steps": [
      {
        "description": "update Service Controller",
        "status": "DONE"
      }
    ],
    "progressPercentage": 100,
    "startTime": ...
  },
  "done": true,
  "response": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
    "rolloutId": "canary-rollout",
    "createTime": ...
    "status": "SUCCESS",
    "trafficPercentStrategy": {
      "percentages": {
        "old": 90,
        "new": 10,
      }
    },
    "serviceName": "endpointsapis.appspot.com"
  }
}

确保 Canary 发布已完成并且新服务配置良好之后,您可以创建一个发布以使其提供 100%的流量:

# Create rollout to let new configuration serve 100% traffic.
$ gcurl -d '{
  "rolloutId": "full-rollout",
  "serviceName": "endpointsapis.appspot.com",
  "trafficPercentStrategy": {
    "percentages": {
      "new": 100,
    }
  }
}' https://servicemanagement.googleapis.com/v1/services/endpointsapis.appspot.com/rollouts
{
  "name": "operations/rollouts.endpointsapis.appspot.com:full-rollout",
  "metadata": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
    "resourceNames": [
      "services/endpointsapis.appspot.com/rollouts/full-rollout"
    ],
    "startTime": ...
  },
  "response": {
    "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
    "rolloutId": "full-rollout",
    "createTime": ...
    "trafficPercentStrategy": {
      "percentages": {
        "new": 100,
      }
    },
    "serviceName": "endpointsapis.appspot.com"
  }
}

如果在测试阶段发现问题,您可以通过执行以下操作回滚到旧配置:

# Rollback to the old configuration.
$ gcurl -d '{
  "rolloutId": "rollout-to-old",
  "serviceName": "endpointsapis.appspot.com",
  "trafficPercentStrategy": {
    "percentages": {
      "old": 100,
    }
  }
}' https://servicemanagement.googleapis.com/v1/services/endpointsapis.appspot.com/rollouts
{
 "name": "operations/rollouts.endpointsapis.appspot.com:rollout-to-old",
 "metadata": {
   "@type": "type.googleapis.com/google.api.servicemanagement.v1.OperationMetadata",
   "resourceNames": [
     "services/endpointsapis.appspot.com/rollouts/rollout-to-old"
   ],
   "startTime": ...
 },
 "response": {
   "@type": "type.googleapis.com/google.api.servicemanagement.v1.Rollout",
   "rolloutId": "rollout-to-old",
   "createTime": ...
   "trafficPercentStrategy": {
     "percentages": {
       "old": 100,
     }
   },
   "serviceName": "endpointsapis.appspot.com"
 }

}

查看发布历史记录

Service Management API 保留发布的历史记录。如要查看 endpointsapis.appspot.com 的发布历史记录,请执行以下操作:

# List rollout history for `endpointsapis.appspot.com`.
$ gcurl https://servicemanagement.googleapis.com/v1/services/endpointsapis.appspot.com/rollouts
{
  "rollouts": [
    {
      "rolloutId": "canary-rollout",
      "createTime": ...
      "status": "IN_PROGRESS",
      "trafficPercentStrategy": {
        "percentages": {
          "old": 90,
          "new": 10
         }
      },
      "serviceName": "endpointsapis.appspot.com"
    },
    {
      "rolloutId": "old-rollout",
      "createTime": ...
      "status": "SUCCESS",
      "trafficPercentStrategy": {
        "percentages": {
          "old": 100
         }
      },
      "serviceName": "endpointsapis.appspot.com"
    },
    ...
  ]
}