亚马逊AWS官方博客

Service Broker 在 AWS 中国区的落地

红帽OpenShift是全球领先的企业级Kubernetes容器平台。目前OpenShift拥有大量客户(全球超过1000个),跨越行业垂直市场,并部署大量部署在私有云和公有云上。OpenShift与AWS的集成是所有云平台集成最好的。

图1 AWS和OpenShift的集成

 

OpenShift运行在AWS上,除了支持动态创建EBS卷、调用ELB等基础功能外,还可以通过Service Broker直接创建AWS原生服务,并提供给OpenShift中的容器化应用使用。

本文就针对OpenShift 3.11在AWS中国区如何使用Service Broker进行详细说明。

1.  Service Broker简介

1.1 AWS Service Broker介绍

AWS ServiceBroker是一个开源项目,它可以将AWS上的原生服务公开给应用平台,并提供与运行中的应用程序无缝集成,OpenShift支持通过这种方式为应用提供集成AWS服务的能力。

AWS ServiceBroker是基于Open Service Broker API实现的。在OpenShift平台上,使用Kubernetes Service Catalog作为中间层,允许用户使用资源文件和OpenShift界面部署服务。如下图所示:

图2 OpenShift中的AWS Service

 

目前AWS ServiceBroker支持一部分AWS服务,包括 Amazon关系数据库服务 (Amazon RDS)、Amazon EMR、Amazon DynamoDB、Amazon S3和 Amazon简单队列服务 (Amazon SQS)等,有关完整列表,请参阅AWS ServiceBroker社区https://github.com/awslabs/aws-servicebroker/tree/master/templates。这些ServiceBroker包括管理基础架构、资源和构建逻辑的CloudFormation模板。这些模板包含一些规范和可自定义的参数,这些参数可以为生产、测试和开发环境提供了最佳实践。

应用程序可以通过一组值 (如访问地址和认证信息) 来使用创建好的AWS服务,这些值被保存在Secret对象中,通过执行绑定的过程挂载到应用程序中。这使得开发人员在不了解或深入了解基础资源的情况下创建使用AWS服务,并与OpenShift上应用交互。大致的流程图如下图所示:

图3 AWS Service Broker 的工作流程

 

从图中可以看出大致的流程:

  • 在OpenShift中,开发人员通过Service Catalog选择想要的AWS服务,填写必要的参数后点击创建。
  • OpenShift中的aws-servicebroker接受请求,向AWS发起创建CloudFormation Stack,CloudFormation把需要的AWS服务创建完成后,将一些必要的信息返回,如连接地址、认证信息等。
  • 在OpenShift中,创建Secret对象保存传回的AWS服务信息。
  • 开发人员在OpenShift的Provisioned Services界面,完成AWS服务和OpenShift应用的绑定,应用中完成AWS服务消费。

1.2 AWS Service Broker运行规划

AWS ServiceBroker运行在OpenShift上,运行方式规划如下:

  • AWS ServiceBroker以独立的容器运行在单独的OpenShift namespace中,例如aws-sb。
  • AWS ServiceBroker需要使用一个IAM Role或者AK/SK来操作AWS资源,优先推荐使用IAM Role。IAM Role需要具备创建Service Broker支持的AWS服务的权限。
  • AWS ServiceBroker创建的服务运行在独立账户下的VPC中,VPC需要提前创建。
  • AWS ServiceBroker所需要的模版文件存放在宁夏区的S3桶中。

2.AWS Service Broker的安装

AWS ServiceBroker的安装也比较简单,使用官方提供的模版在aws-sb项目中启动AWS ServiceBroker容器即可。在中国区安装需要同步模版到自定义的S3桶中。

安装所需要的文件存放在Github中,在安装之前同步仓库。

 

# git clone https://github.com/awslabs/aws-servicebroker.git

 

2.1 创建DynamoDB

AWS ServiceBroker使用DynamoDB记录从S3桶中发现模版的元数据。可以通过多种方式创建所需要的DynamoDB的表。这里我们使用社区提供的CloudFormation模版完成创建,因为模版无法直接在中国区使用,需要进行少量的修改。修改后的模版文件内容如下。

 

# cd setup

# vi prerequisites.yaml
AWSTemplateFormatVersion: "2010-09-09"
Description: >
  Creates the prerequisites for the AWS Service Broker -
  https://github.com/awslabs/aws-servicebroker/blob/master/docs/install_prereqs.md
Resources:
  BrokerTable:
    Type: "AWS::DynamoDB::Table"
    Properties:
      AttributeDefinitions:
      - AttributeName: id
        AttributeType: S
      - AttributeName: userid
        AttributeType: S
      - AttributeName: type
        AttributeType: S
      KeySchema:
      - AttributeName: id
        KeyType: HASH
      - AttributeName: userid
        KeyType: RANGE
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 5
      TableName: "awssb"
      GlobalSecondaryIndexes:
      - IndexName: "type-userid-index"
        KeySchema:
        - AttributeName: type
          KeyType: HASH
        - AttributeName: userid
          KeyType: RANGE
        Projection:
          ProjectionType: INCLUDE
          NonKeyAttributes: [ id, userid, type, locked ]
        ProvisionedThroughput:
          ReadCapacityUnits: 5
          WriteCapacityUnits: 5
  BrokerUser:
    Type: "AWS::IAM::User"
    Properties:
      Policies:
      - PolicyName: AwsServiceBrokerPolicy
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
          - Action: [ "s3:GetObject", "s3:ListBucket" ]
            Resource: [ "arn:aws-cn:s3:::myawsservicebroker/templates/*", "arn:aws-cn:s3:::myawsservicebroker" ]
            Effect: "Allow"
          - Action: [ "dynamodb:PutItem", "dynamodb:GetItem", "dynamodb:DeleteItem" ]
            Resource: !Sub "arn:aws-cn:dynamodb:${AWS::Region}:${AWS::AccountId}:table/${BrokerTable}"
            Effect: "Allow"
          - Action: [ "ssm:GetParameter", "ssm:GetParameters" ]
            Resource:
            - !Sub "arn:aws-cn:ssm:${AWS::Region}:${AWS::AccountId}:parameter/asb-*"
            - !Sub "arn:aws-cn:ssm:${AWS::Region}:${AWS::AccountId}:parameter/Asb*"
            Effect: "Allow"
      - PolicyName: AwsServiceBrokerProvisioningPolicy
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
          - Action: [ "ssm:PutParameter", "ssm:GetParameter", "ssm:GetParameters" ]
            Resource:
            - !Sub "arn:aws-cn:ssm:${AWS::Region}:${AWS::AccountId}:parameter/asb-*"
            - !Sub "arn:aws-cn:ssm:${AWS::Region}:${AWS::AccountId}:parameter/Asb*"
            Effect: "Allow"
          - Action: "s3:GetObject"
            Resource: "arn:aws-cn:s3:::myawsservicebroker/templates/*"
            Effect: "Allow"
          - Action:
            - "cloudformation:CreateStack"
            - "cloudformation:DeleteStack"
            - "cloudformation:DescribeStacks"
            - "cloudformation:DescribeStackEvents"
            - "cloudformation:UpdateStack"
            - "cloudformation:CancelUpdateStack"
            Resource: !Sub "arn:aws-cn:cloudformation:${AWS::Region}:${AWS::AccountId}:stack/aws-service-broker-*/*"
            Effect: "Allow"
          - Action: [ "athena:*", "dynamodb:*", "kms:*", "elasticache:*", "elasticmapreduce:*", "kinesis:*", "rds:*",
                      "redshift:*", "route53:*", "s3:*", "sns:*", "sns:*", "sqs:*", "ec2:*", "iam:*", "lambda:*" ]
            Resource: "*"
            Effect: "Allow"
Outputs:
  IAMUser:
    Value: !Ref BrokerUser

上述CloudFormation文件中主要会创建两个资源对象DynamoDB和IAM user,以及赋予IAM一些Policy。

重要参数及修改参数说明:

  • TableName:创建DynamoDB的名称,默认为awssb。
  • BrokerUser:用于创建名为BrokerUser的User,可以用于安装servicebroker时使用。强烈建议使用IAM Role,并赋予BrokerUser所拥有的权限,而不是使用AK/SK。
  • Resource:Policy中的所有Resource字段必须修改arn:aws:xxx为arn:aws-cn:xxx。
  • 对于S3的权限需要修改为自定义存放模版的Bucket,该Bucket为中国区创建的S3桶,如示例中的myawsservicebroker。

如:

Resource: [ “arn:aws-cn:s3:::myawsservicebroker/templates/*”, “arn:aws-cn:s3:::myawsservicebroker” ]

Resource: “arn:aws-cn:s3:::myawsservicebroker/templates/*”

 

将prerequisites.yaml中相应的参数修改完成后,在AWS控制台创建CloudFormation Stack。Stack创建完成后,会自动创建Dynamodb的Table,默认名称为awssb。同时会创建一个用于AWS ServiceBroker的IAM User,并且设置合适的权限。但是默认不会为创建IAM用户生成AK和SK,我们强烈建议创建独立的IAM Role实现认证。

 

如果使用命令行创建Dynamodb和IAM Role,请参见GitHub说明。

2.2 创建保存模版的S3 bucket

社区提供的模版在存放在AWS Global区的S3桶awsservicebroker中,在中国区安装需要创建一个自定义的S3 bucket来保存模版,本文档Bucket示例为myawsservicebroker,需要在中国区AWS Console或者命令行预先创建。S3桶不要开启Public access。

然后同步us-west-2区域的S3 bucket awsservicebroker。

# export AWS_ACCESS_KEY_ID=xxxxx

# export AWS_SECRET_ACCESS_KEY=xxxxxxx

# export AWS_DEFAULT_REGION=us-east-2

# mkdir awsservicebroker

# aws s3 sync s3://awsservicebroker ./awsservicebroker

 

同步awsservicebroker内容到中国区创建的s3桶myawsservicebroker中

# export AWS_DEFAULT_REGION=cn-northwest-1

#  export AWS_ACCESS_KEY_ID=xxxxxx

#  export AWS_SECRET_ACCESS_KEY=xxxxx

# cd awsservicebroker

# aws s3 sync ./ s3://myawsservicebroker/

 

2.3 OpenShift部署ServiceCatalog

AWS ServiceBroker的使用需要依赖于OpenShift的ServiceCatalog和template-service-broker,需要执行如下步骤完成部署。

在安装OpenShift集群的Ansible Server上执行。修改安装OpenShift的inventory文件,添加如下参数:

 

openshift_enable_service_catalog=true

template_service_broker_install=true

ansible_service_broker_install=false

 

service_catalog和template_service_broker必须安装,ansible_service_broker_install可以选择不安装。

修改完成之后,执行如下命令安装:

# cd /usr/share/ansible/openshift-ansible/

# ansible-playbook -vv playbooks/openshift-service-catalog/config.yml

 

如果使用离线安装,则保证servicecatalog和template-service-broker需要的镜像registry.access.redhat.com/openshift3/ose-service-catalog:v<ocp_version>和registry.access.redhat.com/openshift3/ose-template-service-broker: v<ocp_version>在私有镜像仓库中存在。

2.4 部署AWS ServiceBroker

在OpenShift任意一个Master节点操作。

# mkdir awssb

# cd awssb

 

下载安装所需要的文件,目前发布的最新版本为v1.0.1。

# wget https://raw.githubusercontent.com/awslabs/aws-servicebroker/release-v1.0.1/packaging/openshift/deploy.sh

# wget https://raw.githubusercontent.com/awslabs/aws-servicebroker/release-v1.0.1/packaging/openshift/aws-servicebroker.yaml

# wget https://raw.githubusercontent.com/awslabs/aws-servicebroker/release-v1.0.1/packaging/openshift/parameters.env

# chmod +x deploy.sh

 

修改参数文件:

# vi parameters.env

TARGETACCOUNTID=xxx

TARGETROLENAME=xxx

VPCID=vpc-xxx

REGION=cn-northwest-1

IMAGE=awsservicebroker/aws-servicebroker:1.0.1

IMAGEPULLPOLICY=IfNotPresent

S3BUCKET=myawsservicebroker

S3KEY=templates/latest

S3REGION=cn-northwest-1

TABLENAME=awssb

VERBOSITY=10

BROKERID=awsservicebroker

PRESCRIBE_OVERRIDES=true

 

重要参数说明:

  • TARGETACCOUNTID:必填项,部署awssb的用户的accountID,为12位数字。
  • TARGETROLENAME:可选项,强烈建议使用IAMRole实现认证,不要使用AK/SK。
  • VPCID:必填项,选择部署AWS服务所在的VPC。
  • REGION:必填项,选择部署AWS服务所在的区域。
  • IMAGE:必填项,选择aws-service-broker使用的镜像,如果是私有仓库,则需要修改镜像地址,默认使用docker.io。
  • IMAGEPULLPOLICY:必填项,选择拉取aws-servicebroker镜像的策略。
  • S3BUCKET:中国区必填项,如果在国外,则可以直接使用默认值,在中国需要填写自定义的S3 Bucket。
  • S3KEY:默认值即可,用于过滤检测已经发布的的ServiceBroker模版。
  • S3REGION:如果使用自定义的S3 Bucket,则必须匹配自定义Bucket所在的REGION。
  • TABLENAME:必填项,Dynamodb的table名称,必须与前面创建的table名一致。
  • VERBOSITY:可选项,ServiceBroker容器的日志级别
  • BROKERID:必填项,设定创建的Broker的名称。也就是在同一个集群中可以创建多个ServiceBroker实例。
  • PRESCRIBE_OVERRIDES:可选项,是否支持参数覆盖,默认为true。

 

参数文件修改完成后,以IAM Role实现认证,则执行如下部署脚本:

# ./deploy.sh

 

部署脚本会在OpenShift创建项目aws-sb,并实例化OpenShift模版aws-servicebroker.yaml,并通过环境变量引用参数。

2.5 部署后检测

在任意Master节点执行操作。

# oc project aws-sb

# oc get pods | grep aws-servicebroker

# oc logs <aws-servicebroker-pod-name>

 

检查log中没有任何报错,并且有发现service broker的日志,如下所示:

……

I0718 04:42:01.033560       1 util.go:91] “awsservicebroker_all_all_all_VpcId”=”vpc-0859afa78994bff49”

I0718 04:42:01.033566       1 util.go:91] “awsservicebroker_all_all_all_target_account_id”=”180996712413”

I0718 04:42:01.033595       1 awsbroker.go:174] Listing objects bucket: myawsservicebroker region: cn-northwest-1 prefix: templates/latest

I0718 04:42:01.118989       1 awsbroker.go:193] Found 18 objects

I0718 04:42:01.119134       1 awsbroker.go:168] Updating listings cache with

 

登录OpenShift WebConsole查看是否已经有部署AWS服务的模版,如下图所示。

图4 OpenShift Console中的AWS服务模版

 

在AWS的DynamoDB中可以看到发现的ServiceBroker元数据信息,如下图所示:

图5 DynamoDB中的ServiceBroker列表

 

这些模版虽然已经在DynamoDB和OpenShift中展现出来,但是在OpenShift中依然无法完成创建AWS服务。主要原因在与默认官方提供的Broker模版无法在国内创建。

3.  AWS ServiceBroker自定义模版

默认社区提供的CloudFromation模版无法直接在中国区使用,所有的模版需要进行修改才能使用。本章节我们就说明修改模版的方法。

OpenShift界面显示的AWS服务的模版是通过aws-servicebroker在S3中发现,并更新信息到DynamoDB中。如果要删除某个模版,需要同时在S3和dynamodb中删除对应的条目,识别的ServiceBroker模版在定义的S3桶myawsservicebroker中的template/latest中,修改模版就是修改这个目录下的yaml文件。

S3桶template/latest下存放的是所有的ServiceBroker模版,只是在原本的CloudFormation模版中添加了ServiceBroker相关的元数据,以S3为例的Metadata字段。

# cat s3-main.yaml
AWSTemplateFormatVersion: 2010-09-09
Description: 'AWS Service Broker - Amazon S3 (qs-1nt0fs937)'
Metadata:
  'AWS::ServiceBroker::Specification':
    Version: 1.0
    Tags:
      - AWS
      - S3
      - Object Storage
    Name: s3
    DisplayName: Amazon S3
    LongDescription: Amazon Simple Storage Service (Amazon S3) is storage for the
      Internet. You can use Amazon S3 to store and retrieve any amount of data at
      any time, from anywhere on the web. You can accomplish these tasks using the
      simple and intuitive web interface of the AWS Management Console.
    ImageUrl: https://s3.amazonaws.com/thp-aws-icons-dev/Storage_AmazonS3_LARGE.png
    DocumentationUrl: https://aws.amazon.com/documentation/s3/'
    ProviderDisplayName: "Amazon Web Services"
    Bindings:
      IAM:
        AddKeypair: True
        Policies:
          - PolicyDocument: {
            "Version": "2012-10-17",
            "Statement": [
            {
              "Action": [
                "s3:AbortMultipartUpload",
                "s3:DeleteObject",
                "s3:GetObject",
                "s3:GetObjectAcl",
                "s3:GetObjectTagging",
                "s3:GetObjectTorrent",
                "s3:GetObjectVersion",
                "s3:GetObjectVersionAcl",
                "s3:GetObjectVersionTagging",
                "s3:GetObjectVersionTorrent",
                "s3:ListBucketMultipartUploads",
                "s3:ListMultipartUploadParts",
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:PutObjectTagging"
              ],
              "Effect": "Allow",
              "Resource": !If [ RetainBucket, !Sub "${S3BucketRetain.Arn}/*", !Sub "${S3BucketDelete.Arn}/*" ]
            },
            {
              "Action": [
                "s3:ListBucket"
              ],
              "Resource": !If [ RetainBucket, !GetAtt S3BucketRetain.Arn, !GetAtt S3BucketDelete.Arn ],
              "Effect": "Allow"
            }
            ]
          }
    ServicePlans:
      production:
        DisplayName: "Production"
        Description: 'S3 Bucket pre-configured with production best practices'
        LongDescription: "Amazon Simple Storage Service (Amazon S3) is storage for the Internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web. You can accomplish these tasks using the simple and intuitive web interface of the AWS Management Console."
        Cost: "https://aws.amazon.com/s3/pricing/"
        ParameterValues:
          BucketName: "Auto"
          LoggingPrefix: S3AccessLogs
          # TODO: add glacier lifecycle for previous versions
          EnableGlacierLifeCycle: "False"
          GlacierLifeCycleTransitionInDays: "30"
          LifeCyclePrefix: Archive
          EnableVersioning: "True"
          BucketAccessControl: Private
          EnableLogging: "True"
          PreventDeletion: "True"
      custom:
        DisplayName: "Custom"
        Description: 'S3 Bucket pre-configured with custom configuration'
        LongDescription: "Amazon Simple Storage Service (Amazon S3) is storage for the Internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web. You can accomplish these tasks using the simple and intuitive web interface of the AWS Management Console."
        Cost: "https://aws.amazon.com/s3/pricing/"
  'AWS::CloudFormation::Interface':
    ParameterGroups:
  ……CloudFormation模版的内容 ……

 

 

如果是修改现有的社区模版以适配中国区使用的话,则直接更新现有的模版内容,实现在中国区创建。

如果是新建ServiceBroker模版,则需要在测试通过的Cloudformation模版添加ServcieBroker相关的元数据,然后上传到S3桶中。元数据生成可以使用社区提供的工具https://github.com/ndrest-amzn/ServiceBrokerMetaGen生成。

更新的模版需要稍等几分钟,会更新DynamoDB中元数据,然后在OpenShift界面创建服务测试更新的模版。

4.  结语

本文介绍了在中国区的OpenShift中部署AWS Service Broker,赋予容器平台新的魔法,真正意义上解决了开发人员自服务方式创建AWS原生服务,大大加快了业务开发需要基础环境的创建周期。

如果您需要关于容器的专业的服务和指导,欢迎联系AWS ProServe专业服务团队。

 

本篇作者

郭跃军

亚马逊 AWS 专业服务团队云架构咨询顾问。擅长PaaS 建设运维、 DevOps 以及微服务改造迁移等有丰富的经验。负责企业级客户的云架构设计和优化、云上自动化运维、容器化平台设计咨询等,对云原生技术有深入的研究和热情。