使用 Cloud Functions 和 Cloud Scheduler定期清理未使用的IP 資源

閱讀時間約 10 分鐘

Overview

使用 Cloud FunctionsCloud Scheduler 來識別和清理浪費的雲端資源。

raw-image
  • 創建used-ip-address和unused-ip-address
  • static-ip-instance使用used-ip-address
  • 創建cloud function(unused_ip_function)檢查並刪除沒有被使用的ip資源
  • 創建Cloud Scheduler定期執行cloud function

啟用 Cloud Scheduler API 和clone github repo

  1. 啟用 Cloud Scheduler API
gcloud services enable cloudscheduler.googleapis.com
raw-image
  1. clone 原廠的github程式碼
git clone <https://github.com/GoogleCloudPlatform/gcf-automated-resource-cleanup.git> && cd gcf-automated-resource-cleanup/


  1. 設定環境變數(PROJECT_ID, region, WORKDIR)
export PROJECT_ID=$(gcloud config list --format 'value(core.project)' 2>/dev/null)
export region=us-east1
export WORKDIR=$(pwd)


建立 IP 位址

cd $WORKDIR/unused-ip
  1. 設定環境變數(USED_IP, UNUSED_IP)
export USED_IP=used-ip-address
export UNUSED_IP=unused-ip-address


  1. 使用剛剛的環境變數建立兩個靜態IP
gcloud compute addresses create $USED_IP --project=$PROJECT_ID --region=us-east1
gcloud compute addresses create $UNUSED_IP --project=$PROJECT_ID --region=us-east1


  1. 檢查環境中的ip資源
gcloud compute addresses list --filter="region:(us-east1)"


  1. 將USED_IP的ip設定環境變數(USED_IP_ADDRESS)
export USED_IP_ADDRESS=$(gcloud compute addresses describe $USED_IP --region=us-east1 --format=json | jq -r '.address')


建立 VM

  1. 使用USED_IP_ADDRESS建立VM(static-ip-instance)
gcloud compute instances create static-ip-instance \\
--zone=us-east1-d \\
--machine-type=e2-medium \\
--subnet=default \\
--address=$USED_IP_ADDRESS
raw-image
  1. 檢查IP資源使用情況
gcloud compute addresses list --filter="region:(us-east1)"


查看 github repo 的程式碼

  1. grep "const compute" -A 31 → 查找"const compute"並往下顯示31行
cat $WORKDIR/unused-ip/function.js | grep "const compute" -A 31

output:

const compute = new Compute();
compute.getAddresses(function(err, addresses){ // gets all addresses across regions
if(err){
console.log("there was an error: " + err);
}
if (addresses == null) {
console.log("no addresses found");
return;
}
console.log("there are " + addresses.length + " addresses");

// iterate through addresses
for (let item of addresses){

// get metadata for each address
item.getMetadata(function(err, metadata, apiResponse) {

// if the address is not used:
if (metadata.status=='RESERVED'){

// compute age by convering ISO 8601 timestamps to Date
var creationDate = new Date(metadata.creationTimestamp);
var currDate = new Date();
var addressAge = Math.floor((currDate - creationDate)/86400e3);;

// delete address
item.delete(function(err, operation, apiResponse2){
if (err) {
console.log("could not delete address: " + err);
}
})
}
  • compute.getAddresses(function(err, addresses) uses the getAddresses method to retrieve IP addresses across all regions in the project.
  • item.getMetadata(function(err, metadata, apiResponse) gets the metadata for each IP address and checks its STATUS field.
  • if ((metadata.status=='RESERVED') & (calculateAge(metadata.creationTimestamp) >= ageToDelete)){ checks whether the IP address is in use, calculates its age by using a helper function, and compares its age against a constant (set to 0 for the purposes of the lab).
  • item.delete(function(err, operation, apiResponse2){ deletes the IP address.

部署Cloud Function

  1. 部署Cloud Function
gcloud functions deploy unused_ip_function --trigger-http --runtime=nodejs12 --region=us-east1
raw-image
  1. 將Cloud Function的Trigger URL設定環境變數(FUNCTION_URL)
export FUNCTION_URL=$(gcloud functions describe unused_ip_function --region=us-east1 --format=json | jq -r '.httpsTrigger.url')
raw-image

使用Cloud Scheduler

  1. 建立一個App Engine 以使用 Cloud Scheduler(舊版Scheduler需先起一個App Engine)
gcloud app create --region us-east1


  1. 建立一個 Cloud Scheduler 任務以在每晚凌晨 2 點(UTC+0)執行 Cloud Function
gcloud scheduler jobs create http unused-ip-job \\
--schedule="* 2 * * *" \\
--uri=$FUNCTION_URL \\
--location=us-east1

手動觸發Cloud Scheduler定時作業

  1. 手動觸發Cloud Scheduler
gcloud scheduler jobs run unused-ip-job \\
--location=us-east1
raw-image

檢查是否成功刪除

  1. 檢查ip資源狀態只剩下used-ip-address
gcloud compute addresses list --filter="region:(us-east1)"



如果你喜歡這篇文章歡迎幫我按愛心鼓勵一下喔!~閱讀愉快!~

延伸閱讀

其他學習資訊


17會員
34內容數
歡迎來到「Marcos的方格子」!目前在「Marcos談科技」撰寫在職涯上學習到的知識,在「Marcos談書」分享我在日常的閱讀和心得,歡迎您的到來!!
留言0
查看全部
發表第一個留言支持創作者!