Help Center/ Distributed Cache Service/ API Reference/ Examples/ Example 1: Creating a DCS Redis Instance
Updated on 2024-03-05 GMT+08:00

Example 1: Creating a DCS Redis Instance

Scenario

This section describes how to create a DCS Redis instance by calling APIs. For details about how to call APIs, see Calling APIs.

A 2 GB single-node DCS Redis 5.0 instance is used as an example. The instance uses the Arm CPU architecture and can be accessed without a password.

Involved APIs

When creating a DCS Redis instance, you need to query the product specifications, VPC, and subnet. Call APIs to perform the following steps:

Step 1: Determine the VPC

  1. Query VPCs.

    • URI

      URI format: GET /v1/{project_id}/vpcs

    • Example request

      GET https://{vpc_endpoint}/v1/7d80ae32f57b499eb8781f9a9f57c538/vpcs

      To obtain the value of {vpc_endpoint}, see Regions and Endpoints.

    • Example response
      {
      	"vpcs": [{
      		"id": "743bf021-2c2d-4511-aeac-85bd48c06af7",
      		"name": "vpc-d2d4",
      		"description": "",
      		"cidr": "192.168.0.0/16",
      		"status": "OK",
      		"routes": [],
      		"enterprise_project_id": "0"
      	}]
      }

  2. Record the required VPC ID.

Step 2: Determine the Subnet

  1. Query subnets.

    • URI

      URI format: GET /v1/{project_id}/subnets?{vpc_id}

    • Example request

      GET https://{vpc_endpoint}/v1/7d80ae32f57b499eb8781f9a9f57c538/subnets?vpc_id=743bf021-2c2d-4511-aeac-85bd48c06af7

      To obtain the value of {vpc_endpoint}, see Regions and Endpoints.

    • Example response
      {
      	"subnets": [{
      		"id": "b0d6e0ac-fcce-4e11-a4a6-707e851ae1c3",
      		"name": "subnet-d2e4",
      		"description": "",
      		"cidr": "192.168.0.0/24",
      		"dnsList": ["100.125.1.250",
      		"100.125.129.250"],
      		"status": "ACTIVE",
      		"vpc_id": "743bf021-2c2d-4511-aeac-85bd48c06af7",
      		"ipv6_enable": false,
      		"gateway_ip": "192.168.0.1",
      		"dhcp_enable": true,
      		"primary_dns": "100.125.1.250",
      		"secondary_dns": "100.125.129.250",
      		"availability_zone": "cn-southwest-2a",
      		"neutron_network_id": "b0d6e0ac-fcce-4e11-a4a6-707e851ae1c3",
      		"neutron_subnet_id": "3d4ccce0-cadc-4af4-8c21-14d2857ffe5e",
      		"extra_dhcp_opts": []
      	}]
      }

  2. Record the required subnet ID.

Step 3: Determine the Product Specifications

  1. Query product specifications.

    • URI

      URI format: GET /v2/{project_id}/flavors?spec_code={spec_code}&cache_mode={cache_mode}&engine={engine}&engine_version={engine_version}&cpu_type={cpu_type}&capacity={capacity}

    • Example request

      GET https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/flavors?cache_mode=single&engine=Redis&engine_version=5.0&cpu_type=aarch64&capacity=2

      To obtain the value of {dcs_endpoint}, see Regions and Endpoints.

    • Example response
      [{
      	"dec": false,
      	"spec_code": "redis.single.au1.large.2",
      	"cloud_service_type_code": "hws.service.type.dcs",
      	"cloud_resource_type_code": "hws.resource.type.dcs3",
      	"cache_mode": "single",
      	"engine": "redis",
      	"engine_version": "4.0;5.0",
      	"product_type": "generic",
      	"cpu_type": "aarch64",
      	"storage_type": "DRAM",
      	"capacity": ["2"],
      	"billing_mode": ["Hourly",
      	"RI",
      	"Monthly",
      	"Yearly"],
      	"tenant_ip_count": 1,
      	"pricing_type": "normal",
      	"is_dec": false,
      	"attrs": [{
      		"capacity": "2",
      		"name": "max_memory",
      		"value": "2"
      	},
      	{
      		"capacity": "2",
      		"name": "max_connections",
      		"value": "10000"
      	},
      	{
      		"capacity": "2",
      		"name": "sharding_num",
      		"value": "1"
      	},
      	{
      		"capacity": "2",
      		"name": "proxy_num",
      		"value": "0"
      	},
      	{
      		"capacity": "2",
      		"name": "db_number",
      		"value": "256"
      	},
      	{
      		"capacity": "2",
      		"name": "max_clients",
      		"value": "10000"
      	},
      	{
      		"capacity": "2",
      		"name": "max_bandwidth",
      		"value": "128"
      	}],
      	"flavors_available_zones": [{
      		"capacity": "2",
      		"unit": "GB",
      		"available_zones": ["a0865121f83b41cbafce65930a22a6e8",
      		"effdcbc7d4d64a02aa1fa26b42f56533"],
      		"az_codes": ["cn-north-4b",
      		"cn-north-4a"]
      	}]
      }]

  2. Select a product as required and record the product specification code and the code of the AZ where resources are available.

Step 4: Create an Instance

  • URI

    URI format: POST /v2/{project_id}/instances

  • Example request

    POST https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/instances

    To obtain the value of {dcs_endpoint}, see Regions and Endpoints.

    Body:

    {
    	"az_codes": ["cn-north-4a"],
    	"capacity": 2,
    	"engine": "Redis",
    	"engine_version": "5.0",
    	"name": "dcs-api-test",
    	"no_password_access": true,
    	"security_group_id": "1982d3a8-67a0-4fc9-a850-bc42a26ba2c0",
    	"spec_code": "redis.single.xu1.large.2",
    	"subnet_id": "b0d6e0ac-fcce-4e11-a4a6-707e851ae1c3",
    	"vpc_id": "743bf021-2c2d-4511-aeac-85bd48c06af7"
    }
  • Example response
    {
    	"instances": [{
    		"instance_id": "21bc7b53-2494-4f10-bb0b-c0b913d9e329",
    		"instance_name": "dcs-api-test"
    	}]
    }

    For details about the parameters for creating a DCS Redis instance, see Creating a DCS Instance.

Step 5: Query the Creation Result

  • URI

    URI format: GET /v2/{project_id}/instances/{instance_id}

  • Example request

    GET https://{dcs_endpoint}/v2/666486c2d9b948c1bbea57e714d744fa/instances/21bc7b53-2494-4f10-bb0b-c0b913d9e329

    To obtain the value of {dcs_endpoint}, see Regions and Endpoints.

  • Example response
    {
    	"free": null,
    	"max_memory": 2048,
    	"used_memory": 1,
    	"instance_id": "21bc7b53-2494-4f10-bb0b-c0b913d9e329",
    	"name": "dcs-api-test",
    	"resource_spec_code": "redis.single.au1.large.2",
    	"engine": "Redis",
    	"engine_version": "5.0",
    	"internal_version": null,
    	"charging_mode": 0,
    	"capacity": 2,
    	"capacity_minor": null,
    	"vpc_id": "743bf021-2c2d-4511-aeac-85bd48c06af7",
    	"vpc_name": "vpc-d2d4",
    	"ip": "192.168.0.100",
    	"domain_name": "redis-88a7bbb-dcs-api-test.dcs.huaweicloud.com",
    	"readonly_domain_name": null,
    	"port": 6379,
    	"status": "RUNNING",
    	"freeze_scene": null,
    	"created_at": "2020-06-05T03:30:36.273Z",
    	"update_at": "2020-06-05T03:30:47.231Z",
    	"error_code": null,
    	"user_id": "d53977d1adfb49c5b025ba7d33a13fd7",
    	"user_name": "paas_dcs_a00421997_02",
    	"maintain_begin": "02:00:00",
    	"maintain_end": "06:00:00",
    	"no_password_access": "true",
    	"access_user": null,
    	"enable_publicip": false,
    	"publicip_id": null,
    	"publicip_address": null,
    	"enable_ssl": false,
    	"service_upgrade": false,
    	"service_task_id": "",
    	"enterprise_project_id": "0",
    	"security_group_id": null,
    	"tags": [],
    	"product_type": "generic",
    	"cpu_type": "aarch64",
    	"storage_type": "DRAM",
    	"launched_at": "2020-06-05T03:30:47.238Z",
    	"is_free": null,
    	"libos": false,
    	"cache_mode": "single",
    	"available_zones": ["d539378ec1314c85b76fefa3f7071458"],
    	"subnet_id": "a4112635-3ec0-471c-95c3-5cf49b9533af",
    	"backend_addrs": null,
    	"crr_role": null,
    	"cloud_service_type_code": "hws.service.type.dcs",
    	"cloud_resource_type_code": "hws.resource.type.dcs3",
    	"support_slow_log_flag": null,
    	"ipv6": null,
    	"enable_ipv6": false,
    	"description": "",
    	"product_id": "redis.single.au1.large.2-h",
    	"security_group_name": null,
    	"subnet_name": "dcs-beta",
    	"order_id": null,
    	"subnet_cidr": "192.168.0.0/24",
    	"task": null,
    	"instance_backup_policy": null,
    	"enterprise_project_name": null
    }

    If the instance status is RUNNING, the instance has been created successfully.