创建负载均衡器
功能介绍
创建负载均衡器。
若要创建内网IPv4负载均衡器,则需要设置vip_subnet_cidr_id。
若要创建公网IPv4负载均衡器,则需要设置publicip,以及设置vpc_id和vip_subnet_cidr_id这两个参数中的一个。
若要绑定有已有公网IPv4地址, 则需要设置publicip_ids,以及设置vpc_id和vip_subnet_cidr_id这两个参数中的一个。
若要创建内网双栈负载均衡器,则需要设置ipv6_vip_virsubnet_id。
若要创建公网双栈负载均衡器,则需要设置ipv6_vip_virsubnet_id和ipv6_bandwidth。
不支持绑定已有未使用的内网IPv4、内网IPv6或公网IPv6地址。
l4_flavor_id需要传入网络型规格id,l7_flavor_id需要传入应用型规格id。
![](https://support.huaweicloud.com/intl/zh-cn/api-elb/public_sys-resources/note_3.0-zh-cn.png)
关于计费:
若billing_info非空时,包周期。
若billing_info为空,autoscaling.enable=true时,弹性计费。
若billing_info为空,autoscaling.enable=false或未设置,charge_mode=lcu,按量计费。
若billing_info为空,autoscaling.enable=false或未设置,charge_mode=flavor,固定规格按需计费。
接口约束
vpc_id 或 vip_subnet_cidr_id 或 ipv6_vip_virsubnet_id 不能同时为空。
ip_target_enable参数表示是否启用跨VPC后端转发能力, 启用跨VPC后端转发的负载均衡器关联的member和VIP可以在不同VPC下, 未启用跨VPC后端转发的负载均衡器关联的member和VIP必须在同一个VPC下。
admin_state_up参数的值必须是true。
provider参数的值必须是vlb。
elb_virsubnet_ids参数对应的网络类型支持双栈或者只支持ipv4。 网络类型为只支持ipv4时,ipv6_vip_virsubnet_id参数需要为空。
只能通过ELB Console控制台来解绑EIP, 在ELB列表查询页面选择操作列中的更多按钮->解绑EIP。
创建LB时不能同时设置publicip_ids和publicip, 只能选择绑定已有公网IP或者选择新建公网IP,或者都不设置这两个字段。
若要绑定已有带宽,则需要设置共享带宽ID,不支持绑定独享带宽。 若需要新建独享带宽,则带宽信息中charge_mode、share_type和size字段是必须的。
调用方法
请参见如何调用API。
URI
POST /v3/{project_id}/elb/loadbalancers
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
project_id |
是 |
String |
负载均衡器的项目ID。 最小长度:0 最大长度:36 |
请求参数
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
X-Auth-Token |
否 |
String |
IAM鉴权Token。 最小长度:0 最大长度:4096 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
loadbalancer |
是 |
CreateLoadBalancerOption object |
负载均衡器对象。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
id |
否 |
String |
负载均衡器ID(UUID)。不支持该字段,请勿使用。 最小长度:0 最大长度:36 |
project_id |
否 |
String |
负载均衡器所在的项目ID。 最小长度:1 最大长度:32 |
name |
否 |
String |
负载均衡器的名称。 最小长度:0 最大长度:255 |
description |
否 |
String |
负载均衡器的描述。 最小长度:0 最大长度:255 |
vip_address |
否 |
String |
负载均衡器的IPv4虚拟IP。该地址必须包含在所在子网的IPv4网段内,且未被占用。 使用说明:
|
vip_subnet_cidr_id |
否 |
String |
负载均衡器所在子网的IPv4子网ID。若需要创建带IPv4虚拟IP的LB,该字段必须传入。 可以通过GET https://{VPC_Endpoint}/v1/{project_id}/subnets 响应参数中的neutron_subnet_id得到。 使用说明:
最小长度:1 最大长度:36 |
ipv6_vip_virsubnet_id |
否 |
String |
双栈类型负载均衡器所在子网的IPv6网络ID。可以通过GET https://{VPC_Endpoint}/v1/{project_id}/subnets 响应参数中的neutron_network_id得到。 使用说明:
|
provider |
否 |
String |
负载均衡器的生产者名称。固定为vlb。 最小长度:1 最大长度:255 |
l4_flavor_id |
否 |
String |
网络型规格ID。 使用说明:
最小长度:1 最大长度:36 |
l7_flavor_id |
否 |
String |
应用型规格ID。 使用说明:
最小长度:1 最大长度:36 |
guaranteed |
否 |
Boolean |
是否独享型负载均衡器。 取值:
当前只支持设置为true,设置为false会返回400 Bad Request 。默认:true。 |
vpc_id |
否 |
String |
负载均衡器所在的VPC ID。可以通过GET https://{VPC_Endpoint}/v1/{project_id}/vpcs 响应参数中的id得到。 使用说明:
|
availability_zone_list |
是 |
Array of strings |
可用区列表。可通过GET https://{ELB_Endponit}/v3/{project_id}/elb/availability-zones 接口来查询可用区集合列表。创建负载均衡器时,从查询结果选择某一个可用区集合,并从中选择一个或多个可用区。 |
enterprise_project_id |
否 |
String |
负载均衡器所属的企业项目ID。不能传入""、"0"或不存在的企业项目ID,创建时不传则资源属于default企业项目,默认返回"0"。 |
tags |
否 |
Array of Tag objects |
负载均衡的标签列表。示例:"tags":[{"key":"my_tag","value":"my_tag_value"}] |
admin_state_up |
否 |
Boolean |
负载均衡器的启用状态,true表示启用,false表示停用。只能设置为true。默认:true。 |
ipv6_bandwidth |
否 |
BandwidthRef object |
带宽对象ID,仅在创建或更新公网IPv6负载均衡器时有效。若选择创建新EIP并指定共享带宽时,此EIP会被分进共享带宽里面。 |
publicip_ids |
否 |
Array of strings |
负载均衡器绑定的公网IP ID。只支持绑定数组中的第一个EIP,其他将被忽略。 |
publicip |
否 |
创建ELB时,新建公网IP请求参数 |
|
elb_virsubnet_ids |
否 |
Array of strings |
下联面子网的网络ID列表。可以通过GET https://{VPC_Endpoint}/v1/{project_id}/subnets 响应参数中的neutron_network_id得到。 若不指定该字段,则按如下规则选择下联面网络:
若指定多个下联面子网,则按顺序优先使用第一个子网来为负载均衡器下联面端口分配ip地址。 下联面子网必须属于该LB所在的VPC。 |
ip_target_enable |
否 |
Boolean |
是否启用跨VPC后端转发。 开启跨VPC后端转发后,后端服务器组不仅支持添加云上VPC内的服务器,还支持添加其他VPC、其他公有云、云下数据中心的服务器。 使用共享VPC的实例使用此特性时,需确保共享资源所有者已开通VPC对等连接,否则通信异常。 仅独享型负载均衡器支持该特性。 取值:
使用说明:
|
deletion_protection_enable |
否 |
Boolean |
是否开启删除保护。 取值:false不开启,true开启。默认false不开启。 说明:
退场时需要先关闭所有资源的删除保护开关。 |
autoscaling |
否 |
弹性扩缩容配置信息。负载均衡器配置并开启弹性扩缩容后,可根据负载情况自动调整负载均衡器的规格。 使用说明:
该字段已经废弃,但仍然保留兼容性支持,建议不要使用该字段。如果传入该字段,创建的弹性实例将会有保底规格并产生对应保底规格的费用。 |
|
protection_status |
否 |
String |
修改保护状态, 取值:
|
protection_reason |
否 |
String |
设置保护的原因 说明:
仅当protection_status为consoleProtection时有效。 最小长度:0 最大长度:255 |
ipv6_vip_address |
否 |
String |
双栈类型负载均衡器的IPv6地址。 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
ip_version |
否 |
Integer |
IP版本。 取值:4表示IPv4,6表示IPv6。 缺省值:4 |
network_type |
是 |
String |
弹性公网IP的网络类型,默认5_bgp,更多请参考弹性公网ip创建。 华南-深圳局点该参数取值只能为5_gray 最小长度:1 最大长度:36 |
billing_info |
否 |
String |
资源账单信息。 取值:
最小长度:1 最大长度:1024 |
description |
否 |
String |
弹性公网IP的描述信息,不支持特殊字符 最小长度:1 最大长度:255 |
bandwidth |
是 |
bandwidth |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
name |
否 |
String |
带宽名称。 取值:1-64个字符,支持数字、字母、中文、_(下划线)、-(中划线)、.(点) 使用说明:
最小长度:1 最大长度:64 |
size |
否 |
Integer |
带宽大小 取值范围:默认1Mbit/s~2000Mbit/s(具体范围以各区域配置为准,请参见控制台对应页面显示)。 注意:调整带宽时的最小单位会根据带宽范围不同存在差异。 小于等于300Mbit/s:默认最小单位为1Mbit/s。 300Mbit/s~1000Mbit/s:默认最小单位为50Mbit/s。 大于1000Mbit/s:默认最小单位为500Mbit/s。 使用说明:当id字段为null时,size是必须的。 最小值:0 最大值:99999 |
charge_mode |
否 |
String |
计费模式。 取值:bandwidth表示按带宽计费,traffic表示按流量计费。 使用说明:当id字段为null时,charge_mode是必须的。 最小长度:1 最大长度:36 |
share_type |
否 |
String |
带宽类型。 取值:
使用说明:
|
billing_info |
否 |
String |
资源账单信息。 如果billing_info不为空,说明是包周期计费的带宽,否则为按需计费的带宽 最小长度:1 最大长度:1024 |
id |
否 |
String |
功能说明:使用已有的共享带宽创建IP 取值范围:共享带宽ID 使用说明: WHOLE类型的带宽ID; 在预付费的情况下,不填该值。该字段取空字符串时,会被忽略。 最小长度:1 最大长度:36 |
参数 |
是否必选 |
参数类型 |
描述 |
---|---|---|---|
period_type |
是 |
String |
订购周期类型,当前支持包月和包年: month:月; year:年; |
period_num |
否 |
Integer |
订购周期数,取值会随运营策略变化。 period_type为month时,为[1,9], period_type为year时,为[1,3] 最小值:1 最大值:9 缺省值:1 |
auto_renew |
否 |
Boolean |
是否自动续订; true:自动续订 false:不自动续订 缺省值:false |
auto_pay |
否 |
Boolean |
下单订购后,是否自动从客户的账户中支付; true:自动支付; false:不自动支付。 自动支付时,只能使用账户的现金支付;如果要使用代金券,请选择不自动支付,然后在用户费用中心,选择代金券支付。 缺省值:false |
响应参数
状态码: 201
参数 |
参数类型 |
描述 |
---|---|---|
loadbalancer |
LoadBalancer object |
负载均衡器对象。(包周期场景不返回该字段) |
loadbalancer_id |
String |
负载均衡器的ID 最小长度:0 最大长度:36 |
order_id |
String |
订单号 最小长度:0 最大长度:36 |
request_id |
String |
请求ID。 注:自动生成 。 最小长度:0 最大长度:36 |
参数 |
参数类型 |
描述 |
---|---|---|
id |
String |
负载均衡器ID。 |
description |
String |
负载均衡器描述信息。 最小长度:1 最大长度:255 |
provisioning_status |
String |
负载均衡器的配置状态。 取值:
|
admin_state_up |
Boolean |
负载均衡器的启用状态。取值:false停用,true启用。 |
provider |
String |
负载均衡器的生产者名称。固定为vlb。 |
pools |
Array of PoolRef objects |
负载均衡器直接关联的后端云服务器组的ID列表。 |
listeners |
Array of ListenerRef objects |
负载均衡器关联的监听器的ID列表。 |
operating_status |
String |
负载均衡器的操作状态。 取值:
|
name |
String |
负载均衡器的名称。 |
project_id |
String |
负载均衡器所属的项目ID。 |
vip_subnet_cidr_id |
String |
负载均衡器所在子网的IPv4子网ID。 |
vip_address |
String |
负载均衡器的IPv4虚拟IP地址。 |
vip_port_id |
String |
负载均衡器的IPv4对应的port ID。 |
tags |
Array of Tag objects |
负载均衡的标签列表。 |
created_at |
String |
负载均衡器的创建时间。格式:yyyy-MM-dd'T'HH:mm:ss'Z' |
updated_at |
String |
负载均衡器的更新时间。格式:yyyy-MM-dd'T'HH:mm:ss'Z' |
guaranteed |
Boolean |
是否独享型LB。 取值:
|
vpc_id |
String |
负载均衡器所在VPC ID。 |
eips |
Array of EipInfo objects |
负载均衡器绑定的EIP。只支持绑定一个EIP。 注:该字段与publicips一致。 |
ipv6_vip_address |
String |
双栈类型负载均衡器的IPv6地址。 |
ipv6_vip_virsubnet_id |
String |
双栈类型负载均衡器所在子网的IPv6网络ID。 |
ipv6_vip_port_id |
String |
双栈类型负载均衡器的IPv6对应的port ID。 |
availability_zone_list |
Array of strings |
负载均衡器所在的可用区列表。 |
enterprise_project_id |
String |
企业项目ID。创建时不传则返回"0",表示资源属于default企业项目。 注:"0"并不是真实存在的企业项目ID,在创建、更新和查询时不能作为请求参数传入。 |
billing_info |
String |
资源账单信息。 取值:
最小长度:1 最大长度:1024 |
l4_flavor_id |
String |
网络型规格ID。 对于弹性扩缩容实例,表示上限规格。 当传入的规格类型为L4,表示该实例为固定规格实例,按规格计费; 当传入的规格类型为L4_elastic_max,表示该实例为弹性实例,按LCU计费 最小长度:1 最大长度:255 |
l4_scale_flavor_id |
String |
四层弹性Flavor ID。 不支持该字段,请勿使用。 最小长度:1 最大长度:255 |
l7_flavor_id |
String |
应用型ID。 对于弹性扩缩容实例,表示上限规格ID。 当传入的规格类型为L7,表示该实例为固定规格实例,按规格计费; 当传入的规格类型为L7_elastic_max,表示该实例为弹性实例,按LCU计费 最小长度:1 最大长度:255 |
l7_scale_flavor_id |
String |
七层弹性Flavor ID。 不支持该字段,请勿使用。 最小长度:1 最大长度:255 |
publicips |
Array of PublicIpInfo objects |
负载均衡器绑定的公网IP。只支持绑定一个公网IP。 注:该字段与eips一致。 |
global_eips |
Array of GlobalEipInfo objects |
负载均衡器绑定的global eip。只支持绑定一个globaleip。 |
elb_virsubnet_ids |
Array of strings |
下联面子网的网络ID列表。 |
elb_virsubnet_type |
String |
下联面子网类型
|
ip_target_enable |
Boolean |
是否启用跨VPC后端转发。 开启跨VPC后端转发后,后端服务器组不仅支持添加云上VPC内的服务器,还支持添加其他VPC、其他公有云、云下数据中心的服务器。 使用共享VPC的实例使用此特性时,需确保共享资源所有者已开通VPC对等连接,否则通信异常。 仅独享型负载均衡器支持该特性。 取值:
使用说明:
|
frozen_scene |
String |
负载均衡器的冻结场景。 若负载均衡器有多个冻结场景,用逗号分隔。 取值:
|
ipv6_bandwidth |
BandwidthRef object |
带宽对象ID,仅在创建或更新公网IPv6负载均衡器时有效。若选择创建新EIP并指定共享带宽时,此EIP会被分进共享带宽里面。 |
deletion_protection_enable |
Boolean |
是否开启删除保护。 取值:
说明:
退场时需要先关闭所有资源的删除保护开关。 仅当前局点启用删除保护特性后才会返回该字段。 |
autoscaling |
AutoscalingRef object |
弹性扩缩容配置信息。负载均衡器配置并开启弹性扩缩容后,可根据负载情况自动调整负载均衡器的规格。 使用说明:
该字段已经废弃,但仍然保留兼容性支持,建议不要使用该字段。如果传入该字段,创建的弹性实例将会有保底规格并产生对应保底规格的费用。 |
public_border_group |
String |
LB所属AZ组 |
charge_mode |
String |
收费模式。取值:
|
waf_failure_action |
String |
WAF故障时的流量处理策略。discard:丢弃,forward: 转发到后端(默认) 使用说明:只有绑定了waf的LB实例,该字段才会生效。 |
protection_status |
String |
修改保护状态, 取值:
|
protection_reason |
String |
设置保护的原因 说明:
仅当protection_status为consoleProtection时有效。 最小长度:0 最大长度:255 |
log_group_id |
String |
LB所绑定的logtank对应的group id |
log_topic_id |
String |
LB所绑定的logtank对应的topic id |
参数 |
参数类型 |
描述 |
---|---|---|
eip_id |
String |
eip_id |
eip_address |
String |
eip_address |
ip_version |
Integer |
IP版本号。 取值:4表示IPv4,6表示IPv6。 |
参数 |
参数类型 |
描述 |
---|---|---|
publicip_id |
String |
弹性公网ip配置id |
publicip_address |
String |
IP地址 |
ip_version |
Integer |
IP版本信息。 取值范围:4和6 4:IPv4 6:IPv6 |
请求示例
示例1 : 创建一个私网IPv4负载均衡器
POST https://{ELB_Endponit}/v3/060576798a80d5762fafc01a9b5eedc7/elb/loadbalancers { "loadbalancer" : { "name" : "loadbalancer", "description" : "simple lb", "vip_subnet_cidr_id" : "1992ec06-f364-4ae3-b936-6a8cc24633b7", "admin_state_up" : true, "availability_zone_list" : [ "AZ1" ] } }
示例2 : 创建公网IPv4负载均衡器
POST https://{ELB_Endponit}/v3/060576782980d5762f9ec014dd2f1148/elb/loadbalancers { "loadbalancer" : { "vip_subnet_cidr_id" : "e6e9271d-aef4-48f0-a93a-ccc7b09032c1", "availability_zone_list" : [ "AZ1" ], "admin_state_up" : true, "publicip" : { "network_type" : "5_bgp", "bandwidth" : { "size" : 2, "share_type" : "PER", "charge_mode" : "bandwidth", "name" : "bandwidth_test" } }, "name" : "elb_eip-test" } }
响应示例
状态码: 201
POST操作正常返回。
{ "loadbalancer" : { "name" : "my_loadbalancer", "id" : "29cc669b-3ac8-4498-9094-bdf6193425c2", "project_id" : "060576798a80d5762fafc01a9b5eedc7", "description" : "", "vip_port_id" : "98697944-0cc7-4d3b-a829-001c2fb82232", "vip_address" : "192.168.0.214", "admin_state_up" : true, "provisioning_status" : "ACTIVE", "operating_status" : "ONLINE", "listeners" : [ ], "pools" : [ ], "tags" : [ { "key" : "tab_key", "value" : "tag1" } ], "provider" : "vlb", "created_at" : "2023-03-22T07:59:57Z", "updated_at" : "2023-03-22T07:59:59Z", "vpc_id" : "a1f33a4c-95b9-48a7-9350-684e2ed844b3", "enterprise_project_id" : "134f2181-5720-47e7-bd78-1356ed3737d6", "availability_zone_list" : [ ], "ipv6_vip_address" : null, "ipv6_vip_virsubnet_id" : null, "ipv6_vip_port_id" : null, "publicips" : [ { "publicip_id" : "3388574a-4f6f-4471-869e-97d74d21eee9", "publicip_address" : "88.88.87.205", "ip_version" : 4 } ], "global_eips" : [ ], "elb_virsubnet_ids" : [ ], "elb_virsubnet_type" : null, "ip_target_enable" : false, "autoscaling" : { "enable" : false, "min_l7_flavor_id" : "" }, "frozen_scene" : null, "public_border_group" : "center", "eips" : [ { "eip_id" : "3388574a-4f6f-4471-869e-97d74d21eee9", "eip_address" : "88.88.87.205", "ip_version" : 4 } ], "guaranteed" : false, "billing_info" : null, "l4_flavor_id" : null, "l4_scale_flavor_id" : null, "l7_flavor_id" : null, "l7_scale_flavor_id" : null, "waf_failure_action" : "", "vip_subnet_cidr_id" : "abf31f3b-706e-4e55-a6dc-f2fcc707fd3a" }, "request_id" : "bf29597181cb81b30d19f1a0115a157d" }
SDK代码示例
SDK代码示例如下。
示例1 : 创建一个私网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.elb.v3.region.ElbRegion; import com.huaweicloud.sdk.elb.v3.*; import com.huaweicloud.sdk.elb.v3.model.*; import java.util.List; import java.util.ArrayList; public class CreateLoadBalancerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); ElbClient client = ElbClient.newBuilder() .withCredential(auth) .withRegion(ElbRegion.valueOf("<YOUR REGION>")) .build(); CreateLoadBalancerRequest request = new CreateLoadBalancerRequest(); CreateLoadBalancerRequestBody body = new CreateLoadBalancerRequestBody(); List<String> listLoadbalancerAvailabilityZoneList = new ArrayList<>(); listLoadbalancerAvailabilityZoneList.add("AZ1"); CreateLoadBalancerOption loadbalancerbody = new CreateLoadBalancerOption(); loadbalancerbody.withName("loadbalancer") .withDescription("simple lb") .withVipSubnetCidrId("1992ec06-f364-4ae3-b936-6a8cc24633b7") .withAvailabilityZoneList(listLoadbalancerAvailabilityZoneList) .withAdminStateUp(true); body.withLoadbalancer(loadbalancerbody); request.withBody(body); try { CreateLoadBalancerResponse response = client.createLoadBalancer(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
示例2 : 创建公网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
package com.huaweicloud.sdk.test; import com.huaweicloud.sdk.core.auth.ICredential; import com.huaweicloud.sdk.core.auth.BasicCredentials; import com.huaweicloud.sdk.core.exception.ConnectionException; import com.huaweicloud.sdk.core.exception.RequestTimeoutException; import com.huaweicloud.sdk.core.exception.ServiceResponseException; import com.huaweicloud.sdk.elb.v3.region.ElbRegion; import com.huaweicloud.sdk.elb.v3.*; import com.huaweicloud.sdk.elb.v3.model.*; import java.util.List; import java.util.ArrayList; public class CreateLoadBalancerSolution { public static void main(String[] args) { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment String ak = System.getenv("CLOUD_SDK_AK"); String sk = System.getenv("CLOUD_SDK_SK"); ICredential auth = new BasicCredentials() .withAk(ak) .withSk(sk); ElbClient client = ElbClient.newBuilder() .withCredential(auth) .withRegion(ElbRegion.valueOf("<YOUR REGION>")) .build(); CreateLoadBalancerRequest request = new CreateLoadBalancerRequest(); CreateLoadBalancerRequestBody body = new CreateLoadBalancerRequestBody(); CreateLoadBalancerBandwidthOption bandwidthPublicip = new CreateLoadBalancerBandwidthOption(); bandwidthPublicip.withName("bandwidth_test") .withSize(2) .withChargeMode(CreateLoadBalancerBandwidthOption.ChargeModeEnum.fromValue("bandwidth")) .withShareType(CreateLoadBalancerBandwidthOption.ShareTypeEnum.fromValue("PER")); CreateLoadBalancerPublicIpOption publicipLoadbalancer = new CreateLoadBalancerPublicIpOption(); publicipLoadbalancer.withNetworkType("5_bgp") .withBandwidth(bandwidthPublicip); List<String> listLoadbalancerAvailabilityZoneList = new ArrayList<>(); listLoadbalancerAvailabilityZoneList.add("AZ1"); CreateLoadBalancerOption loadbalancerbody = new CreateLoadBalancerOption(); loadbalancerbody.withName("elb_eip-test") .withVipSubnetCidrId("e6e9271d-aef4-48f0-a93a-ccc7b09032c1") .withAvailabilityZoneList(listLoadbalancerAvailabilityZoneList) .withAdminStateUp(true) .withPublicip(publicipLoadbalancer); body.withLoadbalancer(loadbalancerbody); request.withBody(body); try { CreateLoadBalancerResponse response = client.createLoadBalancer(request); System.out.println(response.toString()); } catch (ConnectionException e) { e.printStackTrace(); } catch (RequestTimeoutException e) { e.printStackTrace(); } catch (ServiceResponseException e) { e.printStackTrace(); System.out.println(e.getHttpStatusCode()); System.out.println(e.getRequestId()); System.out.println(e.getErrorCode()); System.out.println(e.getErrorMsg()); } } }
示例1 : 创建一个私网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkelb.v3.region.elb_region import ElbRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkelb.v3 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = ElbClient.new_builder() \ .with_credentials(credentials) \ .with_region(ElbRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateLoadBalancerRequest() listAvailabilityZoneListLoadbalancer = [ "AZ1" ] loadbalancerbody = CreateLoadBalancerOption( name="loadbalancer", description="simple lb", vip_subnet_cidr_id="1992ec06-f364-4ae3-b936-6a8cc24633b7", availability_zone_list=listAvailabilityZoneListLoadbalancer, admin_state_up=True ) request.body = CreateLoadBalancerRequestBody( loadbalancer=loadbalancerbody ) response = client.create_load_balancer(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
示例2 : 创建公网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
# coding: utf-8 from huaweicloudsdkcore.auth.credentials import BasicCredentials from huaweicloudsdkelb.v3.region.elb_region import ElbRegion from huaweicloudsdkcore.exceptions import exceptions from huaweicloudsdkelb.v3 import * if __name__ == "__main__": # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak = __import__('os').getenv("CLOUD_SDK_AK") sk = __import__('os').getenv("CLOUD_SDK_SK") credentials = BasicCredentials(ak, sk) \ client = ElbClient.new_builder() \ .with_credentials(credentials) \ .with_region(ElbRegion.value_of("<YOUR REGION>")) \ .build() try: request = CreateLoadBalancerRequest() bandwidthPublicip = CreateLoadBalancerBandwidthOption( name="bandwidth_test", size=2, charge_mode="bandwidth", share_type="PER" ) publicipLoadbalancer = CreateLoadBalancerPublicIpOption( network_type="5_bgp", bandwidth=bandwidthPublicip ) listAvailabilityZoneListLoadbalancer = [ "AZ1" ] loadbalancerbody = CreateLoadBalancerOption( name="elb_eip-test", vip_subnet_cidr_id="e6e9271d-aef4-48f0-a93a-ccc7b09032c1", availability_zone_list=listAvailabilityZoneListLoadbalancer, admin_state_up=True, publicip=publicipLoadbalancer ) request.body = CreateLoadBalancerRequestBody( loadbalancer=loadbalancerbody ) response = client.create_load_balancer(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)
示例1 : 创建一个私网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" elb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := elb.NewElbClient( elb.ElbClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateLoadBalancerRequest{} var listAvailabilityZoneListLoadbalancer = []string{ "AZ1", } nameLoadbalancer:= "loadbalancer" descriptionLoadbalancer:= "simple lb" vipSubnetCidrIdLoadbalancer:= "1992ec06-f364-4ae3-b936-6a8cc24633b7" adminStateUpLoadbalancer:= true loadbalancerbody := &model.CreateLoadBalancerOption{ Name: &nameLoadbalancer, Description: &descriptionLoadbalancer, VipSubnetCidrId: &vipSubnetCidrIdLoadbalancer, AvailabilityZoneList: listAvailabilityZoneListLoadbalancer, AdminStateUp: &adminStateUpLoadbalancer, } request.Body = &model.CreateLoadBalancerRequestBody{ Loadbalancer: loadbalancerbody, } response, err := client.CreateLoadBalancer(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
示例2 : 创建公网IPv4负载均衡器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
package main import ( "fmt" "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic" elb "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3" "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/model" region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/elb/v3/region" ) func main() { // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security. // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment ak := os.Getenv("CLOUD_SDK_AK") sk := os.Getenv("CLOUD_SDK_SK") auth := basic.NewCredentialsBuilder(). WithAk(ak). WithSk(sk). Build() client := elb.NewElbClient( elb.ElbClientBuilder(). WithRegion(region.ValueOf("<YOUR REGION>")). WithCredential(auth). Build()) request := &model.CreateLoadBalancerRequest{} nameBandwidth:= "bandwidth_test" sizeBandwidth:= int32(2) chargeModeBandwidth:= model.GetCreateLoadBalancerBandwidthOptionChargeModeEnum().BANDWIDTH shareTypeBandwidth:= model.GetCreateLoadBalancerBandwidthOptionShareTypeEnum().PER bandwidthPublicip := &model.CreateLoadBalancerBandwidthOption{ Name: &nameBandwidth, Size: &sizeBandwidth, ChargeMode: &chargeModeBandwidth, ShareType: &shareTypeBandwidth, } publicipLoadbalancer := &model.CreateLoadBalancerPublicIpOption{ NetworkType: "5_bgp", Bandwidth: bandwidthPublicip, } var listAvailabilityZoneListLoadbalancer = []string{ "AZ1", } nameLoadbalancer:= "elb_eip-test" vipSubnetCidrIdLoadbalancer:= "e6e9271d-aef4-48f0-a93a-ccc7b09032c1" adminStateUpLoadbalancer:= true loadbalancerbody := &model.CreateLoadBalancerOption{ Name: &nameLoadbalancer, VipSubnetCidrId: &vipSubnetCidrIdLoadbalancer, AvailabilityZoneList: listAvailabilityZoneListLoadbalancer, AdminStateUp: &adminStateUpLoadbalancer, Publicip: publicipLoadbalancer, } request.Body = &model.CreateLoadBalancerRequestBody{ Loadbalancer: loadbalancerbody, } response, err := client.CreateLoadBalancer(request) if err == nil { fmt.Printf("%+v\n", response) } else { fmt.Println(err) } }
更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。
状态码
状态码 |
描述 |
---|---|
201 |
POST操作正常返回。 |
错误码
请参见错误码。