Lifecycle Management of GCP Instance IP Addresses

Lifecycle Management of GCP Instance IP Addresses
一、Background
In GCP, IP addresses are assigned to instances using DHCP by default, and the default lease period of DHCP is one hour. When an instance is shut down for more than one hour, the originally assigned IP address may be allocated to other instances.
In order to ensure that the IP address does not change during the lifecycle of the instance, this default allocation rule needs to be modified. Currently, there are mainly three types:
- Upgrade the temporary IP to a static IP address
- Extend the default DHCP lease period
- Specify the subnet IP to be used when creating an instance
二、Advantages and disadvantages of three methods
1. Upgrade the temporary IP to a static IP address
This method will set the IP address to a static IP. After setting a static IP, the IP has an independent lifecycle. Once an instance selects a static IP, this IP will accompany the instance throughout its entire lifecycle. Even if the instance’s lifecycle ends, the static IP address still exists. Only when the static IP is manually deleted will the IP be reclaimed by the DHCP resource pool.
In projects with auto-scaling, this method will lead to an increase in unused static IPs, gradually depleting the IP addresses in the resource pool.
2. Extend the default DHCP lease period
The default lease period of DHCP is 1 hour. Currently, it is not supported to modify (there are relevant configuration items in the code, but they are not exposed for customers to customize).
If an appropriate lease period is not set, extending the default lease period can also lead to a failure similar to the first one, exhausting the IP addresses in the resource pool.
3. Specify the subnet IP to be used when creating an instance
When specifying an unused IP address in the subnet during instance creation, this IP address will not be automatically released when the instance is shut down. The IP address will only be released when the instance is deleted.
This method increases the workload when creating a virtual host, but there is no need to worry that the IP cannot be automatically released to the resource pool when the instance is deleted.
三、Configuration method
1. Upgrade the temporary IP to a static IP address
# 获取主机IP信息
gcloud compute instances describe INSTANCE_NAME --zone ZONE | grep "networkIP"
# 创建静态IP地址
gcloud compute addresses create ADDRESS_NAME_1 [ADDRESS_NAME_2..] \
--addresses IP_ADDRESS_1,[IP_ADDRESS_2,..] \--region REGION \--subnet SUBNETWORK
# API方式创建静态IP地址
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses
{
"addressType": "INTERNAL",
"address": "IP_ADDRESS",
"name": "ADDRESS_NAME",
"subnetwork": "regions/REGION/subnetworks/SUBNETWORK"
}
# 获取所有主机的IP地址信息
gcloud compute instances list --format="value(name,networkInterfaces[].networkIP,networkInterfaces[].subnetwork)"
# 获取所有静态地址信息
gcloud compute addresses list --filter="addressType:INTERNAL AND purpose:GCE_ENDPOINT" --format="value(name,address,status)"
# 获取所有的静态地址IP地址信息
gcloud compute addresses list
# API方式获取所有的静态地址IP地址信息
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses
GET https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/aggregated/addresses
# 删除指定的静态地址
gcloud compute addresses delete ADDRESS_NAME \
--region REGION
# API方式删除指定的静态地址
DELETE https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/regions/REGION/addresses/ADDRESS_NAME
DELETE https://compute.googleapis.com/compute/v1/projects/myproject/regions/us-west1/addresses/example-address-to-delete
#!/bin/bash
gcloud compute addresses list --format=json | jq -c '.[]' | while read line
do
echo $line | jq '.addressType'
echo $line | jq '.status'
echo "================================="
done
2. Specify the subnet IP to be used when creating an instance
# 创建实例的时候指定IP地址
gcloud compute instances create VM_NAME
--private-network-ip IP_ADDRESS
# API方式创建实例时指定IP地址
POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances
{
"name": "VM_NAME",
"machineType": "zones/us-central1-f/machineTypes/e2-micro",
"networkInterfaces": [{
"accessConfigs": [{
"type": "ONE_TO_ONE_NAT",
"name": "External NAT",
}],
"network": "global/networks/default",
"networkIP": "IP_ADDRESS"
}],
"disks": [{
"autoDelete": "true",
"boot": "true",
"type": "PERSISTENT",
"initializeParams": {
"sourceImage": "projects/debian-cloud/global/images/v20150818"
}
}]
}
Comments (4)
Owen Christ
Thanks for always keeping your WordPress themes up to date. Your level of support and dedication is second to none.
Krrish
棒! good
2KcoD4SUJlikH8JXBSFBB1DqJju
2KcoD4SUJlikH8JXBSFBB1DqJju
2KcwuurBMstBtgc0uwp478VtQLK
2KcwuurBMstBtgc0uwp478VtQLK