GCE 放置策略
每个虚拟机 (VM) 实例在位于数据中心集群的机架中的物理服务器或主机上运行。您可以使用以下放置政策来控制虚拟机在可用区内相对于彼此的物理位置:
- 分散放置政策。如果您希望虚拟机之间彼此远离以减少主机系统故障的影响,请使用分散政策。分散放置政策会将虚拟机实例精确分散放置在数据中心底层基础设施上,以免虚拟机共享相同的主机或电源系统。这种方法可以减少主机或电源故障的影响。
- 紧凑放置政策。如果您希望虚拟机之间彼此靠近以缩短虚拟机之间的网络延迟时间,请使用紧凑政策。 紧凑放置政策会将虚拟机实例放置在彼此靠近的位置,以缩短虚拟机之间的网络延迟时间。
支持和限制
放置政策具有以下支持和限制:
- 分散放置政策:
- 每个政策最多支持 8 个虚拟机。
- 应用于动态数量的虚拟机。
- 仅支持 N1、N2、N2D、C2 机器类型。
- 支持实时迁移。
- 不能与虚拟机放置拓扑 API 搭配使用。
- 紧凑放置政策:
- 每个政策最多支持 150 个虚拟机。
- 仅支持 A2、C2、C2D、N2 和 N2D 机器类型。
- 不支持实时迁移。
- 应用于主机维护事件设置为
TERMINATE
的虚拟机。 - 无法应用于现有虚拟机。
- 分散放置和紧凑放置政策:
- 无法应用于预留资源或单租户节点。
一、创建策略
创建打散策略:
gcloud compute resource-policies create group-placement wanggaoli-policy \
--availability-domain-count 4 \
--region asia-northeast1 \
--project mec-test-344202
创建紧凑策略
gcloud compute resource-policies create group-placement wanggaoli-jincou \
--collocation COLLOCATED \
--region asia-northeast1 \
--project mec-test-344202
删除打散策略
gcloud compute resource-policies delete wanggaoli-policy \
--region asia-northeast1 \
--project mec-test-344202
删除紧凑策略
gcloud compute resource-policies delete wanggaoli-jincou \
--region asia-northeast1 \
--project mec-test-344202
二、应用策略
对现有VM应用打散策略
gcloud compute instances add-resource-policies wanggaoli-msp \
--zone asia-northeast1-b \
--resource-policies wanggaoli-policy \
--project mec-test-344202
对新建VM应用紧凑策略
获取需要安装的image名称
[wanggaoli@192:] ~ $ gcloud compute images list |grep debian
NAME: debian-10-buster-v20220920
PROJECT: debian-cloud
FAMILY: debian-10
NAME: debian-11-bullseye-arm64-v20220920
PROJECT: debian-cloud
FAMILY: debian-11-arm64
NAME: debian-11-bullseye-v20220920
PROJECT: debian-cloud
FAMILY: debian-11
gcloud compute instances create wanggaoli-n2 \
--network=wanggaoli-vpc \
--subnet=asia-jp-t \
--machine-type n2-standard-2 \
--zone asia-northeast1-b \
--resource-policies wanggaoli-jincou \
--image-family debian-11 \
--image-project debian-cloud \
--project mec-test-344202 \
--maintenance-policy=TERMINATE \
--no-restart-on-failure \
--async
三、日志验证(打散):
当应用打散策略时,机器会进行一次实时迁移,操作用户为system,在logging中可以看到compute.instances.migrateOnHostMaintenance
的消息。
应用policy
{
"protoPayload": {
"@type": "type.googleapis.com/google.cloud.audit.AuditLog",
"authenticationInfo": {
"principalEmail": "wanggaoli@yunion-hk.com",
"principalSubject": "user:wanggaoli@yunion-hk.com"
},
"requestMetadata": {
"callerIp": "34.168.207.222",
"callerSuppliedUserAgent": "google-cloud-sdk gcloud/405.0.0 command/gcloud.compute.instances.add-resource-policies invocation-id/5bef415c302c4d1fb9bf49fdd51ce940 environment/devshell environment-version/None interactive/True from-script/False python/3.9.2 term/screen (Linux 5.10.133+),gzip(gfe)",
"requestAttributes": {
"time": "2022-10-21T05:39:19.679242Z",
"auth": {}
},
"destinationAttributes": {}
},
"serviceName": "compute.googleapis.com",
"methodName": "v1.compute.instances.addResourcePolicies",
"authorizationInfo": [
{
"permission": "compute.instances.addResourcePolicies",
"granted": true,
"resourceAttributes": {
"service": "compute",
"name": "projects/mec-test-344202/zones/asia-northeast1-b/instances/wanggaoli-dasan",
"type": "compute.instances"
}
},
{
"permission": "compute.resourcePolicies.use",
"granted": true,
"resourceAttributes": {
"service": "compute",
"name": "projects/mec-test-344202/regions/asia-northeast1/resourcePolicies/wanggaoli-policy",
"type": "compute.resourcePolicies"
}
}
],
"resourceName": "projects/mec-test-344202/zones/asia-northeast1-b/instances/wanggaoli-dasan",
"request": {
"resourcePolicys": [
"https://compute.googleapis.com/compute/v1/projects/mec-test-344202/regions/asia-northeast1/resourcePolicies/wanggaoli-policy"
],
"@type": "type.googleapis.com/compute.instances.addResourcePolicies"
},
"response": {
"insertTime": "2022-10-20T22:39:19.512-07:00",
"zone": "https://www.googleapis.com/compute/v1/projects/mec-test-344202/zones/asia-northeast1-b",
"@type": "type.googleapis.com/operation",
"selfLink": "https://www.googleapis.com/compute/v1/projects/mec-test-344202/zones/asia-northeast1-b/operations/operation-1666330758905-5eb84dcf6801e-d6151f23-a92800a7",
"operationType": "addResourcePolicies",
"status": "RUNNING",
"name": "operation-1666330758905-5eb84dcf6801e-d6151f23-a92800a7",
"startTime": "2022-10-20T22:39:19.523-07:00",
"targetLink": "https://www.googleapis.com/compute/v1/projects/mec-test-344202/zones/asia-northeast1-b/instances/wanggaoli-dasan",
"id": "114252927899504232",
"selfLinkWithId": "https://www.googleapis.com/compute/v1/projects/mec-test-344202/zones/asia-northeast1-b/operations/114252927899504232",
"user": "wanggaoli@yunion-hk.com",
"targetId": "4609340071823249091",
"progress": "0"
},
"resourceLocation": {
"currentLocations": [
"asia-northeast1-b"
]
}
},
"insertId": "m75avxe2jwhk",
"resource": {
"type": "gce_instance",
"labels": {
"zone": "asia-northeast1-b",
"instance_id": "4609340071823249091",
"project_id": "mec-test-344202"
}
},
"timestamp": "2022-10-21T05:39:18.953745Z",
"severity": "NOTICE",
"logName": "projects/mec-test-344202/logs/cloudaudit.googleapis.com%2Factivity",
"operation": {
"id": "operation-1666330758905-5eb84dcf6801e-d6151f23-a92800a7",
"producer": "compute.googleapis.com",
"first": true
},
"receiveTimestamp": "2022-10-21T05:39:20.472146749Z"
}
应用热迁移
{
"protoPayload": {
"@type": "type.googleapis.com/google.cloud.audit.AuditLog",
"status": {
"message": "Instance migrated during Compute Engine maintenance."
},
"authenticationInfo": {
"principalEmail": "system@google.com"
},
"serviceName": "compute.googleapis.com",
"methodName": "compute.instances.migrateOnHostMaintenance",
"resourceName": "projects/mec-test-344202/zones/asia-northeast1-b/instances/wanggaoli-dasan",
"request": {
"@type": "type.googleapis.com/compute.instances.migrateOnHostMaintenance"
}
},
"insertId": "63xaaee2kwnc",
"resource": {
"type": "gce_instance",
"labels": {
"project_id": "mec-test-344202",
"instance_id": "4609340071823249091",
"zone": "asia-northeast1-b"
}
},
"timestamp": "2022-10-21T05:40:27.077225Z",
"severity": "INFO",
"logName": "projects/mec-test-344202/logs/cloudaudit.googleapis.com%2Fsystem_event",
"operation": {
"id": "systemevent-1666330806000-5eb84dfc51980-98bea281-310361bf",
"producer": "compute.instances.migrateOnHostMaintenance",
"first": true,
"last": true
},
"receiveTimestamp": "2022-10-21T05:40:27.820810827Z"
}
打散日志
{
"protoPayload": {
"@type": "type.googleapis.com/google.cloud.audit.AuditLog",
"authenticationInfo": {
"principalEmail": "wanggaoli@yunion-hk.com",
"principalSubject": "user:wanggaoli@yunion-hk.com"
},
"requestMetadata": {
"callerIp": "34.168.207.222",
"callerSuppliedUserAgent": "google-cloud-sdk gcloud/405.0.0 command/gcloud.compute.instances.add-resource-policies invocation-id/5bef415c302c4d1fb9bf49fdd51ce940 environment/devshell environment-version/None interactive/True from-script/False python/3.9.2 term/screen (Linux 5.10.133+),gzip(gfe)",
"requestAttributes": {},
"destinationAttributes": {}
},
"serviceName": "compute.googleapis.com",
"methodName": "v1.compute.instances.addResourcePolicies",
"resourceName": "projects/mec-test-344202/zones/asia-northeast1-b/instances/wanggaoli-dasan",
"request": {
"@type": "type.googleapis.com/compute.instances.addResourcePolicies"
}
},
"insertId": "-kiq7jvd741i",
"resource": {
"type": "gce_instance",
"labels": {
"project_id": "mec-test-344202",
"zone": "asia-northeast1-b",
"instance_id": "4609340071823249091"
}
},
"timestamp": "2022-10-21T05:40:28.924222Z",
"severity": "NOTICE",
"logName": "projects/mec-test-344202/logs/cloudaudit.googleapis.com%2Factivity",
"operation": {
"id": "operation-1666330758905-5eb84dcf6801e-d6151f23-a92800a7",
"producer": "compute.googleapis.com",
"last": true
},
"receiveTimestamp": "2022-10-21T05:40:29.109860985Z"
}
四、查看策略
查看放置策略
[wanggaoli@192:] ~ $ gcloud compute instances describe wanggaoli-msp --zone=asia-northeast1-b |grep -i resourcePolicies
resourcePolicies:
- https://www.googleapis.com/compute/v1/projects/mec-test-344202/regions/asia-northeast1/resourcePolicies/wanggaoli-policy
查看策略详情:
[wanggaoli@192:] ~ $ gcloud compute resource-policies describe wanggaoli-policy --region=asia-northeast1
creationTimestamp: '2022-10-17T23:09:32.087-07:00'
groupPlacementPolicy:
availabilityDomainCount: 4
id: '486574430083087859'
kind: compute#resourcePolicy
name: wanggaoli-policy
region: https://www.googleapis.com/compute/v1/projects/mec-test-344202/regions/asia-northeast1
selfLink: https://www.googleapis.com/compute/v1/projects/mec-test-344202/regions/asia-northeast1/resourcePolicies/wanggaoli-policy
status: READY
查看虚拟机放置拓扑:
gcloud compute instances describe wanggaoli-n2 --zone=asia-northeast1-b \
--format="table[box,title=VM-Topology](resourcePolicies.scope():sort=1, \
resourceStatus.physicalHost:label=location)"
#输出信息
VM-Topology
RESOURCE_POLICIES: asia-northeast1/resourcePolicies/wanggaoli-jincou']
location: /b829f1848ed07fc9ef5d4ade6b168321/b3580512a9d20b6f775139ec7ade476d/bcd8bd003d31219a957d103dcc4ac60f
PHYSICAL_HOST
的值由三个字段组成;这些字段包含表示虚拟机所在的集群、服务器机架和宿主机的哈希值。将此值与其他虚拟机的相应值进行比较时,具有相同字符串的字段越多,虚拟机彼此间的位置就越接近。例如,属于同一项目、集群和在同一机架上运行的两个虚拟机在 PHYSICAL_HOST
字段的前两个部分具有相同的值。
参考文档:使用虚拟机实例放置政策
文章目录
关闭