星期六, 11月 02, 2019

使用 gcloud 指令建立與調整 GCE 小記

使用 gcloud 指令建立與調整 GCE 小記


OS: Container with openSUSE Leap 15


工作上三個雲端平臺都會碰到, 所以就想要使用一些工具或是方式來節省時間
畢竟偷懶是工程師前進最大的動力 :p


有的時候會遇到因為業務需求, 要建立不同類型 / 自訂規格 的 VM, 或是, 變更 VM 的相關規格, 開關機器之類的要求

以下是一些自己在 GCP 上面的小記

感謝 宏庭科技 Joy 的幫忙, 讓我有方向前進, 來進行實驗還有撰寫 shell script

建立 自訂規格VM


# gcloud compute instances create --zone=asia-east1-b --custom-cpu 1 --custom-memory 4 --image-project=opensuse-cloud --image-family opensuse-leap --boot-disk-size=30GB test20191026 --project sakanatest


在 GCP 上面, gcloud 指令已經非常方便了, 我都覺得 Ansible 沒啥發揮空間了 :)
但是有的時候還是懶的慢慢打指令, 所以寫了 shell script 來方便執行
  • read 的部分目前在 openSUSE Leap 15 上面驗證過 
    • Mac 的部分可能還要調整, 不過後面再調整, 因為我都是跑在容器的 linux 內, 沒有影響


gcp_createCustom_gce.sh 檔案內容


#!/bin/bash


# edit by sakana 2019/10/26
# 建立 GCE


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


# 設定 cpu 數量
read -e -p "Please enter cpu number: " -i "1" cpu_number


# 設定 記憶體大小
read -e -p "Only use integer, Please enter memory GB: " -i "4" mem_size


# 顯示常用的 OS 資訊


echo ""
echo "常用的 image 參數"
echo " openSUSE Leap: --image-project=opensuse-cloud  --image-family opensuse-leap"
echo " CentOS 7:      --image-project=centos-cloud  --image-family centos-7 "
echo " Ubuntu 18.04:  --image-project=ubuntu-os-cloud  --image-family ubuntu-1804-lts"
echo ""


# 設定 OS
read -e -p "Please enter image project: " -i "opensuse-cloud" image_project
read -e -p "Please enter image family: "  -i "opensuse-leap" image_family


# 設定硬碟大小
read -e -p "Please enter HD size in GB: " -i "30" boot_hd_size


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始建立 GCE
gcloud compute instances create --zone $zone_name --custom-cpu $cpu_number --custom-memory $mem_size --image-project $image_project --image-family $image_family --boot-disk-size $boot_hd_size $gce_name --project $project_name


建立標準規格 VM


# gcloud compute instances create --zone=asia-east1-b --machine-type n1-standard-1 --image-project=opensuse-cloud --image-family opensuse-leap --boot-disk-size=30GB test20191026 --project sakanatest


gcp_createStandard_gce.sh 檔案內容


#!/bin/bash


# edit by sakana 2019/10/26
# 建立 GCE


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


# 顯示常用的機器類型


echo ""
echo "常用的 機器類型"
echo " n1-starndard-1: 1 vCPU and 3.75 GB Memory"
echo " n1-starndard-2: 2 vCPU and 7.5 GB Memory"
echo ""


# 設定 機器類型
read -e -p "Please enter machine type: " -i "n1-standard-1" machine_type


# 顯示常用的 OS 資訊


echo ""
echo "常用的 image 參數"
echo " openSUSE Leap: --image-project=opensuse-cloud  --image-family opensuse-leap"
echo " CentOS 7:      --image-project=centos-cloud  --image-family centos-7 "
echo " Ubuntu 18.04:  --image-project=ubuntu-os-cloud  --image-family ubuntu-1804-lts"
echo ""


# 設定 OS
read -e -p "Please enter image project: " -i "opensuse-cloud" image_project
read -e -p "Please enter image family: "  -i "opensuse-leap" image_family


# 設定硬碟大小
read -e -p "Please enter HD size in GB: " -i "30" boot_hd_size


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始建立 GCE
gcloud compute instances create --zone $zone_name --machine-type $machine_type --image-project $image_project --image-family $image_family --boot-disk-size $boot_hd_size $gce_name --project $project_name


停止 VM


# gcloud compute instances stop test20191026 --project sakanatest


gcp_stop_gce.sh 檔案內容


#!/bin/bash


# edit by sakana 2019/10/26
# 停止 GCE


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


echo ""
echo "If your want to stop multiple VM , just enter their name and split by space"
echo ""


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始停止 GCE
gcloud compute instances stop --zone $zone_name $gce_name --project $project_name


調整 VM 規格


# gcloud compute instances set-machine-type --custom-cpu 2 --custom-memory 7 test20191026 --project sakanatest


gcp_adjust_gce.sh 檔案內容


#!/bin/bash


# edit by sakana 2019/10/26
# 調整 GCE 規格


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


# 設定 cpu 數量
read -e -p "Please enter cpu number: " -i "1" cpu_number


# 設定 記憶體大小
read -e -p "Only use integer, Please enter memory GB: " -i "4" mem_size


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始調整 GCE, 一次只能針對一台 VM 調整
gcloud compute instances set-machine-type --zone $zone_name --custom-cpu $cpu_number --custom-memory $mem_size  $gce_name --project $project_name


啟動 VM


# gcloud compute instances start test20191026 --project sakanatest


gcp_start_gce.sh 檔案如下


#!/bin/bash


# edit by sakana 2019/10/26
# 啟動 GCE


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


echo ""
echo "If your want to start multiple VM , just enter their name and split by space"
echo ""


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始啟動 GCE
gcloud compute instances start --zone $zone_name $gce_name --project $project_name


刪除 VM


# gcloud compute instances delete test20191026 --project sakanatest


gcp_delete_gce.sh 檔案內容


#!/bin/bash


# edit by sakana 2019/10/26
# 刪除 GCE


# 設定 zone
read -e -p "Please enter zone name: " -i "asia-east1-b" zone_name


echo ""
echo "If your want to delete multiple VM , just enter their name and split by space"
echo ""


# 設定 VM 名稱
read -e -p "Please enter vm name: " -i "test" gce_name


# 設定使用專案
read -e -p "Please enter project name: " -i "sakanatest" project_name


# 開始刪除 GCE
gcloud compute instances delete --zone $zone_name $gce_name --project $project_name


下次應該要弄使用 image 建立 VM 的 shell script :)


先記下來


~ enjoy it

沒有留言: