星期六, 11月 02, 2019

使用 Stackdriver 監控 GCE CPU 使用率, 超出監控值主動發出通知小記

使用 Stackdriver 監控 GCE CPU 使用率, 超出監控值主動發出通知小記


OS: Container with openSUSE Leap 15


以前管地端機器的時候, 因為大部分的專案跟自己有關係, 可以藉由 nagios 或其他方式進行監控.  現在業務上以雲端的資源為主, 所以就要多利用雲端原生的方式來進行監控.


需求: GCE CPU 超過一定使用量, 通知使用者


做法: 使用 Stackdriver Monitoring Alert 來達成


登入 GCP Console


點選 STACKDRIVER 的 Monitoring




這個時候會被導向到另外一個頁面 https://app.google.stackdriver.com/
如果之前專案沒有建立過 Starkdriver 的工作區, 那就按照引導建立工作區並將 GCP 上面的專案加入到工作區
  • Stackdriver 費用又是另外一個故事, 改天再來說


點選 Alerting -- > Create a Policy




點選 Add Condition 



Metrics 的部分我使用
compute.googleapis.com/instance/cpu/utilization
  • 另外有看到 agent.googleapis.com/cpu/utilization, 猜測是要安裝 agent, 然後由agent 傳回值 




Filter 的部分
剛開始是使用 instance_id 方式, 但是要一台一台指定, 轉向使用 name (VM 名稱)的方式來進行.
之後的目標是使用標籤的方式來 filter.




更新使用標籤的部分

  • 之前沒有看到標籤是因為時間差, 我一建立完 GCE, 然後套用 env: prod 的標籤就去設定 Stackdriver monitoring policy, 標籤的資料還沒同步過去, 當套用標籤後過一些時間, 再重新建立 filter 就會看到 env 可以選, 然後點選 prod 就可以了
  • 感謝 GCPUG.TW 的朋友幫忙

Configuration 部分設定 1分鐘超過 50 % 就警告




可以在右方觀察到基準線
也可以看到套用的 GCE VM

點選 SAVE 完成 Condition 設定


接下來設定通知方式
點選下拉式選單來選取, 這邊我是只能選 Email
  • 希望未來使用 Telegram 通知 :)



輸入要通知的 E-mail address -- > Add Notification Channel
輸入 Policy 名稱 -- > Save
完成設定



接下來進行驗證
我在GCP 上面建立兩台 openSUSE Leap 15, 並安裝 stress-ng 來進行壓測
  • 可以參考之前的文章 [1] 


使用 stress-ng 壓測
在 GCE VM 上面
# stress-ng  -c  4  --cpu-method all


可以從剛剛設定的 Policy 觀察到 CPU 超過監控值




達到門檻值, 也看到產生 Incident 與通知使用者






完成相關設定以及驗證


做完 Console 的方式, 接下來當然會想去評估 API 方式, 參考官方文件


看起來的邏輯是要
  • 建立通知管道 Notification
  • 然後才能建立 Policy


因為還要針對 YAML 或是 JSON 內容進行編輯, 不能直接使用 get 或是 describe 方式取得之後來套用, 所以這個部分就先跳過


先記下來


~ enjoy it


Reference:
  1. 宏庭科技 Joy 幫忙, 給我基本相關做法




使用 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