更新时间:2024-10-31 GMT+08:00
示例三:配置云服务器高可用的虚拟IP功能
操作场景
虚拟IP主要用在弹性云服务器的主备切换,达到高可用性的目的。当主服务器发生故障无法对外提供服务时,动态将虚拟IP切换到备服务器,继续对外提供服务。
本章节指导用户通过调用一系列虚拟IP相关API,使应用具备高可用性。
前提条件
通过IAM服务获取到的Token有效期为24小时,需要使用同一个Token鉴权时,可以先将Token缓存,避免频繁调用。
操作步骤
- 创建虚拟IP。
- 发送“POST https://VPC的Endpoint/v2.0/ports”。
- 在Request Header中增加“X-Auth-Token”。
- 在Request Body中传入参数如下,其中vip所在子网需与ECS同子网。
{ "port": { "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "device_owner": "neutron:VIP_PORT", "name": "vip_port_test" } }
或者如下,可以指定IP创建:
{ "port": { "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "device_owner": "neutron:VIP_PORT", "name": "vip_port_test", "fixed_ips": [ { "ip_address": "192.168.0.220" } ] } }
- 查看请求响应结果。
- 请求成功时,响应参数如下:
{ "port": { "id": "a7d98f3c-b42f-460b-96a1-07601e145961", "name": "port-test", "status": "DOWN", "admin_state_up": true, "fixed_ips": [{ "subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "ip_address": "192.168.0.220" }], "mac_address": "fa:16:3e:01:f7:90", "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "tenant_id": "db82c9e1415a464ea68048baa8acc6b8", "project_id": "db82c9e1415a464ea68048baa8acc6b8", "device_id": "", "device_owner": "neutron:VIP_PORT", "security_groups": ["d0d58aa9-cda9-414c-9c52-6c3daf8534e6"], "extra_dhcp_opts": [], "allowed_address_pairs": [], "binding:vnic_type": "normal", "binding:vif_details": {}, "binding:profile": {}, "port_security_enabled": true, "created_at": "2018-09-20T01:45:26", "updated_at": "2018-09-20T01:45:26" } }
- 请求异常时,错误码请参见错误码。
- 请求成功时,响应参数如下:
- 通过云服务器的ID查询网卡信息。
- 发送“GET https://VPC的Endpoint/v2.0/ports?device_id={ecs_id}&network_id={network_id}”
- 在Request Header中增加“X-Auth-Token”。
- 查看请求响应结果。
- 请求成功时,响应参数如下:
{ "ports": [{ "id": "02c72193-efec-42fb-853b-c33f2b802467", "name": "", "status": "ACTIVE", "admin_state_up": true, "fixed_ips": [{ "subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "ip_address": "192.168.0.75" }], "mac_address": "fa:16:3e:47:5f:c1", "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "tenant_id": "db82c9e1415a464ea68048baa8acc6b8", "project_id": "db82c9e1415a464ea68048baa8acc6b8", "device_id": "ea61f836-b52f-41bf-9d06-685644001d6f", "device_owner": "compute:br-iaas-odin1a", "security_groups": [ "e0598d96-9451-4f8a-8de0-b8b4d451d9e7" ], "extra_dhcp_opts": [], "allowed_address_pairs": [], "binding:vnic_type": "normal", "binding:vif_details": { "primary_interface": true }, "binding:profile": {}, "port_security_enabled": true, "created_at": "2020-06-20T08:07:29", "updated_at": "2020-06-20T08:07:29" }] }
- 请求异常时,错误码请参见错误码。
- 请求成功时,响应参数如下:
- 将云服务器与虚拟IP绑定。
- 发送“PUT https://VPC的Endpoint/v2.0/ports/{port_id}”。port_id为上面创建的虚拟IP对应的port_id。
- 在Request Header中增加“X-Auth-Token”。
- 在Request Body中传入参数如下,ip_address为2查询出的云服务器的网卡ip。
{ "port": { "allowed_address_pairs": [{ "ip_address": "192.168.0.75" }] } }
- 查看请求响应结果。
- 请求成功时,响应参数如下:
{ "port": { "id": "a7d98f3c-b42f-460b-96a1-07601e145961", "name": "port-test", "status": "DOWN", "admin_state_up": true, "fixed_ips": [{ "subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "ip_address": "192.168.0.220" }], "mac_address": "fa:16:3e:01:f7:90", "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "tenant_id": "db82c9e1415a464ea68048baa8acc6b8", "project_id": "db82c9e1415a464ea68048baa8acc6b8", "device_id": "", "device_owner": "neutron:VIP_PORT", "security_groups": ["d0d58aa9-cda9-414c-9c52-6c3daf8534e6"], "extra_dhcp_opts": [], "allowed_address_pairs": [{ "ip_address": "192.168.0.75" }] "binding:vnic_type": "normal", "binding:vif_details": {}, "binding:profile": {}, "port_security_enabled": true, "created_at": "2018-09-20T01:45:26", "updated_at": "2018-09-20T01:45:26" } }
- 请求异常时,错误码请参见错误码。
- 请求成功时,响应参数如下:
- 关闭云服务器网卡的源/目的检查。
- 发送“PUT https://VPC的Endpoint/v2.0/ports/{port_id}”。port_id为2查询到的网卡ID。
- 在Request Header中增加“X-Auth-Token”。
- 在Request Body中传入参数如下:ip_address为1.1.1.1/0查询出的云服务器的网卡ip。
{ "port": { "allowed_address_pairs": [{ "ip_address": "1.1.1.1/0" }] } }
- 查看请求响应结果。
- 请求成功时,响应参数如下:
{ "port": { "id": "02c72193-efec-42fb-853b-c33f2b802467", "name": "", "status": "ACTIVE", "admin_state_up": true, "fixed_ips": [{ "subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12", "ip_address": "192.168.0.75" }], "mac_address": "fa:16:3e:47:5f:c1", "network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d", "tenant_id": "db82c9e1415a464ea68048baa8acc6b8", "project_id": "db82c9e1415a464ea68048baa8acc6b8", "device_id": "ea61f836-b52f-41bf-9d06-685644001d6f", "device_owner": "compute:br-iaas-odin1a", "security_groups": ["e0598d96-9451-4f8a-8de0-b8b4d451d9e7"], "extra_dhcp_opts": [], "allowed_address_pairs": [{ "ip_address": "1.1.1.1/0" }], "binding:vnic_type": "normal", "binding:vif_details": { "primary_interface": true }, "binding:profile": {}, "port_security_enabled": true, "created_at": "2020-06-20T08:07:29", "updated_at": "2020-06-20T08:07:29" } }
- 请求异常时,错误码请参见错误码。
- 请求成功时,响应参数如下:
父主题: 应用示例