GCP VPS Setup

當作 GCP 的 VPS 教學就好
不要傻傻拿免費額度開 VPS 來寫 code !!!

心得:

真的不要拿香香的 9000 元額度在這個雞毛小事
那時候有夠讓浪費 = =
殺雞焉用牛刀的感覺
畢竟遲早都會把環境弄好的

不應該拿 GCP 額度來開最基本的 VPS 的 😵
應該拿去跑 ML , 開 GKE 玩玩看
或是開 cloud database 之類的 🤕

真正應該要做的是:
想辦法用一些工具把環境設定變更容易搬遷
如: 用stow 管理我們的 dotfile
當下次我們拿到新電腦時可以很快 setup 成我們原本的環境

用 MAC 但發現環境很難打 code

直接架 GCP 主機跑 Linux

起因

換了安靜的 MAC 新筆電,原本以為不會跟 Ubuntu 的環境差太多。沒想到在打 FB HackerCup 才發現 MAC 是用 clang 而不是在 Linux 熟悉的 g++

一般在寫題目來說,最大的差別是 #include<bits/stdc++.h> 不能引入這個萬用標頭檔 ( 不過可以手動將這個 header file 加入系統的 include path )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

不果最重要的是: 用clang compile 有時候會跑出一些奇怪的 Bug ( 也有可能是 M1 晶片的問題 ? )

What is GCP

GCP 的全名是 Google Cloud Platform , 提供各種雲端的服務 。 而這次是用其中的 VM 執行個體服務 來架設一個 Linux 主機當作我的外出工作環境。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

免費試用 GCP

只要有金融卡( 或信用卡 ) 就可試用 GCP , 並且會提供 300 USD 為期 90 天的服務( 當然在 90 天內用完 300 USD 會自動結束 )

在免費試用期間,GCP 並不會向使用者收費,所以可以放心使用。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

建立 Linux 主機

進入 GCP 後,可以在左側 navbar 看到 Compute Engine , 再選取 VM 執行個體

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著選取上方的建立執行個體

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

就可以配置自己的主機規格( 費用也會根據硬體規格不同有所差別 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

然後這是我配置的主機規格:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開機磁碟 可以更改主機的 OS ( 如果選擇需要授權的 OS , 在開機時間需要支付額外費用 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

成功建立 Linux 主機後,應該可以在後台看到剛剛建立的 VM

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

SSH 登入 GCP 主機

固定外部 IP

原先 GCP 主機的對外 IP 是浮動的( 每次對外 IP 可能不同,這會導致 SSH 連線設定很常需要更動 )

先選取 VM 右側的其他操作項目

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再選取 查看詳細網路資訊
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

會開啟 虛擬私有雲網路,再選取左側的 IP 位置 , 再選取外部 IP 位置 做設定 ( 把對外 IP 連接到 VM )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定 SSH Key

在 local 的電腦使用 ssh-keygen command ( Mac 內件就有了 ) 來產生 SSH 的私鑰和公鑰。

因為 ssh-keygen 會在當前的檔案位置生成私鑰和公鑰,所以應該先進入~/.ssh 的 directory 再執行。

執行結果如下:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • Enter file in which to save the key ... .ssh/id_rsa): 可以輸入自訂的公鑰私鑰名稱。( 預設為 Users/USERNAME/.ssh/id_rsa

  • Enter passphrase ... 可以輸入SSH密碼 ( 可以 enter 略過 )

將 Public Key 加入 GCP 主機

先在 GCP 後台的上方 navbar 搜尋 ssh , 並選取 SSH Keys

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

就可以將 SSH 的 public key 加入 GCP 主機了( 把 filename.pub 的內容複製到後台 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

SSH 進 GCP

設定完就可以用 SSH 登入 GCP 了, command 如下 :
ssh -i ~/.ssh/SSH_NAME USSRNAME@EXTERNAL_IP

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

應該就可以成功進入 GCP 主機

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

設定 SSH config

剛剛的 SSH 登入方式還需要記得 UserName 和 IP 才能登入, 這對於使用者非常不方便。

我們可以在 ~/.ssh 的目錄下新增 config 設定檔,格式如下:
( # 後是註解,不是設定檔內容 )

Host            GCP                 # host name
Hostname        192.168.11.25       # IP or Domain name
Port            22                  # ssh default port
User            jason               # user name
identityfile    ~/.ssh/id_rsa       # path to private key

現在只需要使用 ssh Host 就可以進入 GCP 主機了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

VSCode 設定 SSH

在預設情況下,我們在 terminal 中 SSH 進入 GCP 的主機後,如果需要修改檔案時,只能透過 vimnano 這兩個可以在 terminal 運作的editor 。 如果要用 local 的電腦使用Visual Studio Code 來打 code 還需要額外設定:

  1. 在開始設定前,需要先下載好 Remote - SSH 這個 extension

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  2. 首先選取左下角藍色 >< 字樣的 icon

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  3. 接著會在上方 navbar 跳出 remote connect 的選項

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

並選取Connect Current Window to Host ( 第二個 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 選取先前設定好的 SSH

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

VSCode 就成功連線至的 GCP 主機了

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

( 這邊會顯示報錯是因為 Vscode 是根據 local 的 include path 做搜尋,而不是連限至的 GCP 主機 )

在測試 C++ 環境的時候發現主機並沒有載 g++ , 所以也需要自己 sudo apt install g++一下 ( 有可能是因為那時候 image 選 minimal

其他注意事項

Linux 關機

如果從 GCP 後台關機的話會顯示可能會導致檔案損毀的警告,所以關機就使用 linux command : sudo shutdown -h 筍now 就可以立即關機了 ( 必須使用 sudo 才能關機 )

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

開機的話一樣從 GCP 後台來開機

( 如果要定時開機關機的話 , 就可以從 GCP 設定 )

硬體儲存空間收費

即使我們在每次使用都有關機,但是硬體空間還是持續收費

(不過沒有到很貴,開 10G 磁碟一個月約 1 USD )

使用心得

有第三台電腦了( X
過一個月再補ㄅ

過了快一年才補

真的不要拿香香的 9000 元額度在這個雞毛小事
那時候有夠讓浪費 = =
殺雞焉用牛刀的感覺
畢竟遲早都會把環境弄好的

不應該拿 GCP 額度來開最基本的 VPS 的 😵
應該拿去跑 ML , 開 GKE 玩玩看
或是開 cloud database 之類的 🤕

真正應該要做的是:
想辦法用一些工具把環境設定變更容易搬遷
如: 用stow 管理我們的 dotfile
當下次我們拿到新電腦時可以很快 setup 成我們原本的環境