创建服务时,如果你新建的服务需要依赖某个功能或服务实例才能运行时,您可以使用计算巢服务间的依赖功能,将已部署的服务和需要创建的服务实例导入到新建的服务中以达到简化配置的目的。本文介绍如何使用服务间的依赖功能创建计算巢服务。
操作步骤
此处以新建一个服务,并且在服务的配置文件中引入zookeeper服务的集群地址为例,介绍如何使用计算巢服务间的依赖功能,同步创建zookeeper的服务实例并引入到新建的服务中。
获取已存在的zookeeper服务ID和需要创建的服务实例版本。
此处获取到的服务ID为:service-c679a8e5048xxxxxxx。需要创建的服务实例版本为:版本4。
版本4中zookeeper服务模板参数如下:
Parameters: PayType: Type: String Label: en: ECS Instance Charge Type zh-cn: 付费类型 Default: PostPaid AllowedValues: - PostPaid - PrePaid AssociationProperty: ChargeType AssociationPropertyMetadata: LocaleKey: InstanceChargeType PayPeriodUnit: Type: String Label: en: Pay Period Unit zh-cn: 购买资源时长周期 Default: Month AllowedValues: - Month - Year AssociationProperty: PayPeriodUnit AssociationPropertyMetadata: Visible: Condition: Fn::Not: Fn::Equals: - ${PayType} - PostPaid PayPeriod: Type: Number Description: en: When the resource purchase duration is Month, the value of Period ranges from 1 to 9, 12, 24, 36, 48, or 60. <br><b><font color='red'> When ECS instance types are PrePaid valid </b></font> zh-cn: 当购买资源时长为Month时,Period取值:1~9 <br><b><font color='red'>当ECS实例类型为PrePaid有效</b></font> Label: en: Period zh-cn: 购买资源时长 Default: 1 AllowedValues: - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 AssociationProperty: PayPeriod AssociationPropertyMetadata: Visible: Condition: Fn::Not: Fn::Equals: - ${PayType} - PostPaid ZoneId: Type: String AssociationProperty: ALIYUN::ECS::Instance::ZoneId Label: en: VSwitch Availability Zone zh-cn: 交换机可用区 VpcId: AssociationProperty: ALIYUN::ECS::VPC::VPCId Type: String Description: en: Please search the ID starting with (vpc-xxx) from console-Virtual Private Cloud zh-cn: 现有虚拟专有网络的实例ID Label: en: VPC ID zh-cn: 专有网络VPC实例ID VSwitchId: AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId AssociationPropertyMetadata: VpcId: ${VpcId} ZoneId: ${ZoneId} Type: String Description: en: Instance ID of existing business network switches, console-Virtual Private Cloud-VSwitches under query zh-cn: 现有业务网络交换机的实例ID Label: en: VSwitch ID zh-cn: 交换机实例ID AutoCreateSecurityGroup: Type: Boolean Default: true Label: zh-cn: 自动创建安全组 en: auto create security group SecurityGroupId: Type: String AssociationProperty: ALIYUN::ECS::SecurityGroup::SecurityGroupId AssociationPropertyMetadata: VpcId: ${VpcId} Visible: Condition: Fn::Equals: - ${AutoCreateSecurityGroup} - false Default: '' Description: en: Please search the business security group ID starting with(sg-xxx)from console-ECS-Network & Security zh-cn: 现有业务安全组的实例ID Label: en: Business Security Group ID zh-cn: 业务安全组ID EcsInstanceType: Type: String Label: en: Instance Type zh-cn: 实例类型 AssociationProperty: ALIYUN::ECS::Instance::InstanceType AssociationPropertyMetadata: ZoneId: ${ZoneId} InstanceChargeType: ${InstanceChargeType} SystemDiskSize: Default: 40 Type: Number Description: zh-cn: 系统盘大小, 取值范围:[40, 500], 单位:GB。 en: 'System disk size of each node, range of values: 40-500, units: GB.' MinValue: 40 MaxValue: 500 Label: zh-cn: 系统盘空间 en: System Disk Space DataDiskSize: Default: 40 Type: Number Description: zh-cn: 数据盘大小, 取值范围:[40, 500], 单位:GB。 en: 'System disk size of each node, range of values: 40-500, units: GB.' MinValue: 40 MaxValue: 500 Label: zh-cn: 数据盘空间 en: Data Disk Space AllocatePublicIp: Default: false Type: Boolean Label: zh-cn: 开启公网IP en: allocate public ip InternetMaxBandwidthOut: Default: 0 Type: Number Label: zh-cn: 流量公网带宽 en: Internet Max Bandwidth Out MinValue: 0 MaxValue: 100 Description: zh-cn: 取值范围0-100, 0为不开公网ip en: no public ip if zero AssociationPropertyMetadata: Visible: Condition: Fn::Equals: - ${AllocatePublicIp} - true InternetChargeType: Default: PayByTraffic Type: String Label: zh-cn: 流量付费类型 en: Internet Charge Type AllowedValues: - PayByBandwidth - PayByTraffic AssociationPropertyMetadata: LocaleKey: InternetChargeType Visible: Condition: Fn::Equals: - ${AllocatePublicIp} - true InstancePassword: NoEcho: true Type: String Description: en: Server login password, Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in) zh-cn: 服务器登录密码,长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号) AllowedPattern: '[0-9A-Za-z\_\-\&:;''<>,=%`~!@#\(\)\$\^\*\+\|\{\}\[\]\.\?\/]+$' Label: en: Instance Password zh-cn: 实例密码 ConstraintDescription: en: Length 8-30, must contain three(Capital letters, lowercase letters, numbers, ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ Special symbol in) zh-cn: 长度8-30,必须包含三项(大写字母、小写字母、数字、 ()`~!@#$%^&*_-+=|{}[]:;'<>,.?/ 中的特殊符号) MinLength: 8 MaxLength: 30 AssociationProperty: ALIYUN::ECS::Instance::Password
模板参数对应的输出结果如下:
Outputs: # 将内网ip做为http返回的地址显示在控制台 PrivateEndpoint: Description: zh-cn: 内网IP地址 en: Private IP Addresses Value: Fn::Sub: - http://${ServerAddress}:2181 - ServerAddress: Fn::Select: - 0 - Fn::GetAtt: - EcsInstanceGroup - PrivateIps # 将公网ip做为http返回的地址显示在控制台 PublicEndpoint: Condition: IfAllocatePublicIp Description: zh-cn: 对外暴露的公网IP地址 en: Public IP Addresses Value: Fn::Sub: - http://${ServerAddress}:2181 - ServerAddress: Fn::Select: - 0 - Fn::GetAtt: - EcsInstanceGroup - PublicIps
获取到关联服务的相关信息后,利用
ALIYUN::ComputeNest::ServiceInstance
类型,将zookeeper服务模板中的参数引入到resource
中,模板内容如下:ROSTemplateFormatVersion: "2015-09-01" Parameters: PayType: Type: String Label: en: ECS Instance Charge Type zh-cn: 付费类型 Default: PostPaid AllowedValues: - PostPaid - PrePaid AssociationProperty: ChargeType AssociationPropertyMetadata: LocaleKey: InstanceChargeType PayPeriodUnit: Type: String Label: en: Pay Period Unit zh-cn: 购买资源时长周期 Default: Month AllowedValues: - Month - Year AssociationProperty: PayPeriodUnit AssociationPropertyMetadata: Visible: Condition: Fn::Not: Fn::Equals: - ${PayType} - PostPaid PayPeriod: Type: Number Label: en: Period zh-cn: 购买资源时长 Default: 1 AllowedValues: - 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9 AssociationProperty: PayPeriod AssociationPropertyMetadata: Visible: Condition: Fn::Not: Fn::Equals: - ${PayType} - PostPaid ZoneId: Type: String AssociationProperty: ALIYUN::ECS::Instance:ZoneId Label: en: Zone ID zh-cn: 可用区 VpcId: Type: String Label: en: VPC Id zh-cn: 专有网络VPC id AssociationProperty: ALIYUN::ECS::VPC::VPCId VSwitchId: Type: String Label: en: VSwitch Id zh-cn: 交换机ID AssociationProperty: ALIYUN::ECS::VSwitch::VSwitchId AssociationPropertyMetadata: VpcId: ${VpcId} ZoneId: ${ZoneId} LoginPassword: NoEcho: true Type: String Description: en: 'Length 8-32 characters, can contain size letters, Numbers and special symbols, including:! @ # $ % ^ & * ( ) _ + - =' zh-cn: 长度8-32个字符,可包含大小字母、数字及特殊符号(包含:!@#$%^&*()_+-=) Label: en: Instance Password zh-cn: 实例密码 AssociationProperty: ALIYUN::ECS::Instance::Password ConstraintDescription: en: '8-32 characters, can contain size letters, Numbers and special symbols, including:! @ # $ % ^ & * ( ) _ + - =' zh-cn: 8-32个字符,可包含大小字母、数字及特殊符号(包含:!@#$%^&*()_+-=) MinLength: 8 MaxLength: 32 SecurityGroupId: Type: String AssociationProperty: ALIYUN::ECS::SecurityGroup::SecurityGroupId AssociationPropertyMetadata: VpcId: ${VpcId} Default: '' Description: en: Please search the business security group ID starting with(sg-xxx)from console-ECS-Network & Security zh-cn: 现有业务安全组的实例ID Label: en: Business Security Group ID zh-cn: 业务安全组ID EcsInstanceType: Type: String Label: en: Instance Type zh-cn: 跳板机实例类型 AssociationProperty: ALIYUN::ECS::Instance::InstanceType AssociationPropertyMetadata: ZoneId: ${ZoneId} InstanceChargeType: ${PayType} Default: ecs.g6.large SystemDiskCategory: Type: String AllowedValues: - cloud_efficiency - cloud_ssd - cloud_essd Label: en: System Disk Category zh-cn: 系统盘类型 AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory AssociationPropertyMetadata: LocaleKey: DiskCategory InstanceType: ${EcsInstanceType} Default: cloud_essd SystemDiskSize: Default: 40 Type: Number Label: zh-cn: 系统盘空间 (GB) en: System Disk Space (GB) Resources: Zookeeper: Type: ALIYUN::ComputeNest::ServiceInstance Properties: Service: ServiceId: service-c679a8e5048xxxxxxx Version: 4 TemplateName: 单机版 Parameters: RegionId: cn-hangzhou PayType: Ref: PayType PayPeriodUnit: Ref: PayPeriodUnit PayPeriod: Ref: PayPeriod ZoneId: Ref: ZoneId VpcId: Ref: VpcId VSwitchId: Ref: VSwitchId AutoCreateSecurityGroup: true SecurityGroupId: Ref: SecurityGroupId EcsInstanceType: Ref: EcsInstanceType SystemDiskSize: 40 DataDiskSize: 40 AllocatePublicIp: false InstancePassword: Ref: LoginPassword EcsInstanceGroup: Type: ALIYUN::ECS::InstanceGroup Properties: # 实例名 InstanceName: Fn::Join: - '-' - - Ref: ALIYUN::StackName - '[1,4]' IoOptimized: optimized # 付费类型 InstanceChargeType: Ref: PayType PeriodUnit: Ref: PayPeriodUnit Period: Ref: PayPeriod # 网络配置 VpcId: Ref: VpcId ZoneId: Ref: ZoneId VSwitchId: Ref: VSwitchId # 磁盘类型和大小 SystemDiskCategory: cloud_essd SystemDiskSize: 200 DiskMappings: - Category: cloud_essd Size: 200 MaxAmount: 1 # 镜像 ImageId: centos_7 # 实例类型 InstanceType: Ref: EcsInstanceType Password: Ref: LoginPassword # 公网开启 AllocatePublicIP: true # 公网带宽 InternetMaxBandwidthOut: 100 SecurityGroupId: Ref: SecurityGroupId UserData: Fn::Sub: - | # 将zookeeper集群地址写入服务的配置文件 cat >> configuration.conf << EOF ${ZookeeperInstance} "EOF" # 获取到waitConditionHandle的地址放到 ${CurlCli}变量里 - ZookeeperInstance: Fn::Jq: - 'All' - '.PublicEndpoint' - Fn::GetAtt: - Zookeeper - Outputs Outputs: ServiceInstanceOutputs: Value: Fn::GetAtt: - EcsInstanceGroup - InstanceIds ServiceInstanceComponents: Value: Fn::GetAtt: - EcsInstanceGroup - PrivateIps
在该模板中可以看到,模板中Zookeeper资源中的
Properties
下Parameters
的定义与zookeeper服务中parameters
的定义是一一对应的,目的是为了指定创建zookeeper服务中实例的参数;Service
则指定了zookeeper的服务ID和服务版本。若您引用的服务有多个服务模板,那么您需要在引用服务的模板参数
TemplateName
中定义具体的模板名称。TemplateName
在模板中与Service
和Parameters
同一级别。更多参数,请参见ALIYUN::ComputeNest::ServiceInstance。您可以使用如下命令获取zookeeper服务中服务实例的输出内容。
Fn::GetAtt: - Zookeeper - Outputs
一般情况下,
Outputs
是一个JSON格式的数据,利用Fn::Jq
容易获取。本示例中zookeeper服务的Outputs
格式是:{"PrivateEndpoint":"http://10.x.x.192:2181", "PublicEndpoint":"http://10.x.x.196:2181"}
重要为了防止您误操作删除子服务实例导致整个服务不可用,计算巢做了限制,您无法单独删除子服务实例。当您删除父服务实例的时候,计算巢会将其所依赖的所有服务实例删除后再删除。
如果子服务实例已经与云市场商品关联,则云市场商品将自动扣费。