摘要:9個步驟經過API建立雲服務器。
事情是這樣的,公司成立了一個新項目,要給項目建官網,並搭建一個web服務器。按照個人習慣,搭建一個本地虛擬機就行了,部署簡單方便還好配置,雲服務器嘛,寫了10年代碼,仍是不習慣那玩意兒。html
我這剛開始動手,就看見實習生屁顛屁顛地跑過來,我心想這小子確定是想來學虛擬機搭建部署的,這麼簡單的事都不會,真是白瞎了他那985的學校了。web
但是...萬萬沒想到啊,小實習生跑到我老大那裏邀功去了!!!遠遠地聽見他說:「公司新項目跑在雲服務器上了,之後能夠根據訪問量隨時進行彈性擴展,安全穩定還快。」api
啥?雲服務器?這小子竟然會配置雲服務器?聽完彙報,老大看向了我,帶着小實習生往我這邊走過來……安全
「哼,叫你邀功,撞槍口上了吧,我就說雲服務器配置沒那麼容易,還不是得來找我~」這麼想着有點得意,老大走過來拍拍個人肩膀,說:「你跟他學一下怎麼配置雲服務器,別再用虛擬機了,之後都是麻煩事。」bash
我...他...讓我跟一個實習生學???服務器
實習生衝我微微一笑,就走了,沒過一會扔給我一份文檔,上面寫着:「建立彈性雲服務器流程詳解」。打開粗略掃了一眼,代碼都直接貼上來了?瞧不起誰?感受有被冒犯到。網絡
技術人嘛,看到代碼仍是沒忍住跑一遍,等我發現bug或跑不通,再給他上上課!app
結果……真香。ui
內容是這樣的:
一、查詢可用區。
(1)接口相關信息
URI格式:GET /v2.1/{project_id}/os-availability-zone
(2)請求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-availability-zone
(3)響應示例
{
"availabilityZoneInfo": [
{
"hosts": null,
"zoneState": {
"available": true
},
"zoneName":"zone_01"
},
{
"hosts": null,
"zoneState": {
"available": true
},
"zoneName": "zone_01"
}
]
}複製代碼
二、根據實際須要選擇可用區,並記錄可用區的zoneName。
一、查詢雲服務器規格信息。
(1)接口相關信息
URI格式:GET/v2.1/{project_id}/flavors/detail{?minDisk,minRam,is_public,sort_key,sort_dir}
其中,「?」後的字段爲查詢規格時可選的查詢檢索參數。
(2)請求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/flavors/detail
(3)響應示例
{
"flavors": [
{
"name": "c1.2xlarge",
"links": [
{
"href": "https://xxx/v2.1/74610f3a5ad941998e91f076297ecf27/flavors/c1.2xlarge",
"rel": "self"
},
{
"href": "https://xxx/74610f3a5ad941998e91f076297ecf27/flavors/c1.2xlarge",
"rel": "bookmark"
}
],
"ram": 8192,
"OS-FLV-DISABLED:disabled": false,
"vcpus": 8,
"swap": "",
"os-flavor-access:is_public": true,
"rxtx_factor": 1,
"OS-FLV-EXT-DATA:ephemeral": 0,
"disk": 0,
"id": "c1.2xlarge"
}
]
}複製代碼
二、 根據實際須要選擇規格,並記錄規格的ID。
一、查詢鏡像。
(1)接口相關信息
URI格式:GET /v2.1/{project_id}/images/detail
(2)請求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/images/detail
(3)響應示例
{
"images": [
{
"OS-EXT-IMG-SIZE:size": 0,
"metadata": {
"__os_type": "Linux",
"hw_vif_multiqueue_enabled": "true",
"__imagetype": "gold",
"__quick_start": "true",
"virtual_env_type": "FusionCompute",
"__support_xen": "true",
"__support_kvm": "true",
"__image_source_type": "uds",
"__platform": "EulerOS",
"__os_version": "EulerOS 2.2 64bit",
"__os_bit": "64",
"__isregistered": "false"
},
"created": "2018-05-14T06:13:50Z",
"minRam": 0,
"name": "DBS-MySQL-Image_2.1.3.3",
"progress": 100,
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
{
"rel": "alternate",
"href": "https://None/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"type": "application/vnd.openstack.image"
}
],
"id": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"updated": "2018-05-14T06:13:52Z",
"minDisk": 40,
"status": "ACTIVE"
}
]
}複製代碼
二、根據須要選擇鏡像,並記錄鏡像ID。
一、查詢網絡。
(1)接口相關信息
URI格式:GET /v2.1/{project_id}/os-networks
(2)請求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-networks
(3)響應示例
{
"networks": [
{
"id": "07a9557d-4256-48ae-847c-415a9c8f7ff6",
"label": "b_tt3_td1b",
"broadcast": null,
"cidr": null,
"dns1": null,
"dns2": null,
"gateway": null,
"netmask": null,
"cidr_v6": null,
"gateway_v6": null,
"netmask_v6": null
}
]
}複製代碼
二、根據須要選擇網絡,並記錄網絡ID。
如需設置爲密碼登陸方式,請直接跳轉至步驟七。
一、建立密鑰對。
(1)接口相關信息
URI格式:POST /v2.1/{project_id}/os-keypairs
(2)請求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs
Body:
{
"keypair": {
"type": "ssh",
"name": "demo1",
"user_id": "fake"
}
}複製代碼
(3)響應示例
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrR5Gcwlh5ih7JOvzIUuQxS5qzWWPMYHeDXkDKSQ9W5pumOV05SiO3WCswnaQ5xMdOl31mNiHtwlwq9dJi7X6jJBB2shTD+00G5WuwkBbFU4CLvt1B44u0NUiaTJ35NAvW2/4XvpXm9OwiQ3B5ge6ZY7Esi38Unh+pkbhPkYxNBCK8yoOlojQhWs75abdxZBi811/8RwLcNiFiocA2RGxtRjBdpEScj+1TU+OcfZdQnr0AFbO11z7yxfIygwwzVTgUuJNbMbKHStQqRbklfMlHY4RBPQgb7RN/YaXKTQSXT84k+D9xlDNo7Wj4fwOJTOz/s/PvbIOqjRHt9D6Y4IKd Generated-by-Nova\n",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAq0eRnMJYeYoeyTr8yFLkMUuas1ljzGB3g15AykkPVuabpjld\nOUojt1grMJ2kOcTHTpd9ZjY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TluKp0wyJjbSd34oeqpH/2r2ivddrOysxoqa8jg\n4IQDZyLvj7MaKjQxrieqP89+y9Or9TMFo1xB46x2G8EN8/xHuA9YGnZSPFtWv72m\nhRqV0hv82amWsA0vHnRUSwKBgDsKHXvrTMbNkNhkykMXCH5iyWiBFSyZa1ZJMlgf\nknsqfdzeVPwF6E55QKAN2uuTlwzG/3ljPxahR1hvmUJjQN9JSBiUKbtW6GPCRVbr\nf/jLi1Iu99COZdluVKeybqn8Z/aSNP24DR9FM8kxzZ1IMPaTBmhFypp6BclhcLBt\nxTG1AoGAfcrkVbV1SOy7fECUtMpUECcw0yU4GWj3sR2RbII63C500RVYQlUpUaRR\naANbASHTVR4myOKtGSxEUhAQHlxFDwsDL7W3gzAqTFbEDp1xAAUyT/nkOAhQjEm4\nORFdDETeXLQG1KMUj+8AdnhfYp3JTdft6rmPpZEBUFiCAUMAvb0=\n-----END RSA PRIVATE KEY-----\n",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "demo1",
"fingerprint": "57:a7:a2:ed:5f:aa:e7:54:62:2e:bb:e7:92:22:cb:40"
}
}複製代碼
二、導入密鑰。
(1)接口相關信息
URI格式:POST /v2.1/{project_id}/os-keypairs
(2)請求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs
Body:
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CIYohLuMIpbln6LDtfRPpdhEh3lxL8MM61gyfpKzeKkwkEpSFj27Rgh6zCyJgBpkA2A0HTP737UlitahL4faCWDIS+Vj6mbcfkWiMhuMCzTZgSKAZ4PfoG4B5HJhR52C6A4XLiQFT9heh9gnIsIG+uTogTKUbcJKuN7M6AraJpul6eHhV9YI4433sDmuiBF/njvreVPWwAHlAkgT9I8q1T/cfEFiwzXpdGbkK5O8NC7K+qNbbdKihlahONt Generated-by-Nova\n",
"type": "ssh",
"name": "demo2",
"user_id": "fake"
}
}複製代碼
(3)響應示例
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CIYohLuMIpbln6LDtfRPpdhEh3lxL8MM61gyfpKzeKkwkEpSFj27Rgh6zCyJgBpkA2A0HTP737UlitahL4faCWDIS+Vj6mbcfkWiMhuMCzTZgSKAZ4PfoG4B5HJhR52C6A4XLiQFT9heh9gnIsIG+uTogTKUbcJKuN7M6AraJpul6eHhV9YI4433sDmuiBF/njvreVPWwAHlAkgT9I8q1T/cfEFiwzXpdGbkK5O8NC7K+qNbbdKihlahONt Generated-by-Nova\n",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "demo2",
"fingerprint": "dd:44:45:49:d9:f6:4f:c0:24:2d:81:aa:c4:4b:83:c2"
}
}複製代碼
二、 記錄響應消息體中的name信息。如本例中爲「demo2」。
(1)接口相關信息
URI格式:POST /v2.1/{project_id}/servers
說明:
本示例是指定image建立雲服務器,所以:
● block_device_mapping_v2.1中,「source_type」需設置爲「image」,「uuid」設置爲選擇的鏡像ID,「destination_type」設置爲「volume」,「boot_index」設置爲「0」。
● 「volume_size」應大於等於鏡像元數據中指定的最小值。
(2)請求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm1",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"key_name": "demo2",
"availability_zone": "az_test_01"
}
}複製代碼
(3)響應示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/6d311127-bce1-48db-bf0f-cac9f8f7f077"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/6d311127-bce1-48db-bf0f-cac9f8f7f077"
}
],
"id": "6d311127-bce1-48db-bf0f-cac9f8f7f077",
"adminPass": "WcC4QoVZPXpV"
}
}複製代碼
(1)使用非cloud-init鏡像建立雲服務器時,Windows和Linux系統都使用「adminPass」參數實現密碼注入。
(2)使用cloud-init鏡像建立雲服務器時:
l 對於Linux系統,只能使用「user_data」進行密碼注入,「user_data」字符串長度小於65535,且必須是base64加密的;
l 對於Windows系統,只能經過元數據「admin_pass」進行密碼注入。
說明:
本示例是指定image建立彈性雲服務器,所以:
(1)block_device_mapping_v2.1中,「source_type」需設置爲「image」,「uuid」設置爲選擇的鏡像ID,「destination_type」設置爲「volume」,「boot_index」設置爲「0」。
(2)「volume_size」應大於等於鏡像元數據中指定的最小值。
一、建立非cloud-init鏡像、密碼方式登陸鑑權的彈性雲服務器。
(1)接口相關信息
URI格式:POST /v2.1/{project_id}/servers
(2)請求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm1",
"adminPass":"NOVAGLANCEI@123",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"availability_zone": "az_test_01"
}
}複製代碼
(3)響應示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/73991cff-37c6-491a-9a8a-377397729fce"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/73991cff-37c6-491a-9a8a-377397729fce"
}
],
"id": "73991cff-37c6-491a-9a8a-377397729fce",
"adminPass": "NOVAGLANCEI@123"
}
}複製代碼
二、建立cloud-init鏡像(以Linux爲例)、密碼方式登陸鑑權的彈性雲服務器。
(1)接口相關信息
URI格式:POST /v2.1/{project_id}/servers
(2)請求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm2",
"user_data":"IyEvYmluL2Jhc2gKZWNobyAncm9vdDokNiRuaERlcUIkajBkelVMbkFrbWo4bWlFREFzT0VONkVPdGg1RmdqWnQybUVWZVRocC5OVDJXelZVdzFuNzhnL3dNdDE0dVpSQ1V3LzhHS3VGdEViQ0lPQS5vN1BZQzEnIHwgY2hwYXNzd2QgLWU7",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"availability_zone": "az_test_01"
}
}複製代碼
(3)響應示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/7c11f362-cc96-4306-85b6-b432a639ca5e"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/7c11f362-cc96-4306-85b6-b432a639ca5e"
}
],
"id": "7c11f362-cc96-4306-85b6-b432a639ca5e",
"admin_pass": "J3xMSivHCuq5"
}
}複製代碼
(1)接口相關信息
URI格式:GET /v2.1/{project_id}/servers/{server_id}
(2)請求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6
其中:
0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6爲建立的雲服務器UUID。
(3)響應示例
{
"server": {
"tenant_id": "74610f3a5ad941998e91f076297ecf27",
"addresses": {
"2a6f4aa6-d93e-45f5-a8cb-b030dbf8cd68": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:88:01:1b",
"OS-EXT-IPS:type": "fixed",
"addr": "192.168.2.192",
"version": 4
}
]
},
"metadata": {},
"OS-EXT-STS:task_state": null,
"OS-DCF:diskConfig": "MANUAL",
"OS-EXT-AZ:availability_zone": "az_test_01",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6"
}
],
"OS-EXT-STS:power_state": 1,
"id": "0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6",
"os-extended-volumes:volumes_attached": [
{
"id": "b551445a-e749-4d53-932a-638a455cb6c3"
}
],
"OS-EXT-SRV-ATTR:host": "pod1_test_01",
"image": {
"links": [
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
}
],
"id": "11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "",
"accessIPv6": "",
"created": "2018-05-25T01:47:11Z",
"hostId": "b2792bef989888d2df1f51bff81de5ac58a4117f4e9ec3059c1a0410",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "nova001@36",
"key_name": null,
"flavor": {
"links": [
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/flavors/c1.large"
}
],
"id": "c1.large"
},
"security_groups": [
{
"name": "default"
}
],
"config_drive": "",
"OS-EXT-STS:vm_state": "active",
"OS-EXT-SRV-ATTR:instance_name": "instance-001883cd",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "zttestvm1",
"progress": 0,
"OS-SRV-USG:launched_at": "2018-05-25T01:47:55.755922",
"updated": "2018-05-25T01:47:55Z",
"status": "ACTIVE"
}
}複製代碼
按照步驟指示,我很快完成了雲服務器的搭建,不由有些驚訝,這小子怎麼懂這麼多?等等...華爲雲API Explorer?打開華爲雲API Explorer的官網,我好像打開了新世界的大門,有不少已配置好的環境和接口,直接調用調試就能夠了,之後怕是沒有什麼可以難住我了!
目前API Explorer平臺已開放EI企業智能、計算、應用服務、網絡、軟件開發平臺、視頻等70+雲服務,共上線2000+個API、6000+個錯誤碼。在前期試運行期間,華爲雲API Explorer平臺上的API接口也已被多家企業成功接入。
點擊查看詳情:《華爲雲一站式API解決方案平臺API Explorer上線》
據悉,華爲雲API Explorer平臺在將來幾個月會實現更多功能,好比支持SDK示例代碼、CLI等特性,同時也會開放更多的雲服務API接口,鏈接更多開發者實現創新、拓寬創新邊界。