iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 17
1
DevOps

Best Practice for DevOps on GitLab and GCP系列 第 17

Best Practice for DevOps on GitLab and GCP : GCP 執行個體管理與遠端操作 - Day 17 -

Imgur

前言

Cloud Compute 是 GCP 上最基礎的服務之一,主要功能就是提供使用者可以通過網頁或指令的方式,快速建立起所需要的實例 (instance),在管理或作業上文字的方式,可以做到有效的管理和備忘,所以本篇內容會以指令方式進行,如果直接看本篇文章的朋友可能不清楚怎麼開始,可以下滑到『環境建置』先將環境建立好後就可以開始正式上路。

雲端計算實例基本操作

取得計算實例列表

$ gcloud compute instances list

輸出

NAME  ZONE          MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP   STATUS
vm-0  asia-east1-a  n1-standard-1               10.140.0.7   35.234.46.55  RUNNING


建立單個計算實例

$ gcloud compute instances create vm-0

輸出

NAME  ZONE          MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
vm-0  asia-east1-a  n1-standard-1               10.140.0.2   35.194.193.73  RUNNING

建立多個計算實例

$ gcloud compute instances create vm-{1..4}

輸出

NAME  ZONE          MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
vm-4  asia-east1-a  n1-standard-1               10.140.0.4   104.155.198.35  RUNNING
vm-1  asia-east1-a  n1-standard-1               10.140.0.6   35.234.46.55    RUNNING
vm-2  asia-east1-a  n1-standard-1               10.140.0.5   104.199.160.46  RUNNING
vm-3  asia-east1-a  n1-standard-1               10.140.0.3   34.80.84.146    RUNNING

刪除計算實例

$ gcloud compute instances delete --quiet vm-{0..4}

輸出

Deleted [https://www.googleapis.com/compute/v1/projects/cookie-first-project/zones/asia-east1-a/instances/vm-0].
Deleted [https://www.googleapis.com/compute/v1/projects/cookie-first-project/zones/asia-east1-a/instances/vm-4].
Deleted [https://www.googleapis.com/compute/v1/projects/cookie-first-project/zones/asia-east1-a/instances/vm-2].
Deleted [https://www.googleapis.com/compute/v1/projects/cookie-first-project/zones/asia-east1-a/instances/vm-3].
Deleted [https://www.googleapis.com/compute/v1/projects/cookie-first-project/zones/asia-east1-a/instances/vm-1].

遠端登入連線

設定 config-ssh

gcloud compute config-ssh

輸出

Updating project ssh metadata...-Updated [https://www.googleapis.com/compute/v1/projects/cookie-first-project].
Updating project ssh metadata...done.
You should now be able to use ssh/scp with your instances.
For example, try running:

  $ ssh vm-0.asia-east1-a.cookie-first-project


觀看可用連線

$ cat ~/.ssh/config

輸出

# Google Compute Engine Section
#
# The following has been auto-generated by "gcloud compute config-ssh"
# to make accessing your Google Compute Engine virtual machines easier.
#
# To remove this blob, run:
#
#   gcloud compute config-ssh --remove
#
# You can also manually remove this blob by deleting everything from
# here until the comment that contains the string "End of Google Compute
# Engine Section".
#
# You should not hand-edit this section, unless you are deleting it.
#
Host vm-0.asia-east1-a.cookie-first-project
    HostName 35.234.46.55
    IdentityFile /root/.ssh/google_compute_engine
    UserKnownHostsFile=/root/.ssh/google_compute_known_hosts
    HostKeyAlias=compute.5670556533261508977
    IdentitiesOnly=yes
    CheckHostIP=no

# End of Google Compute Engine Section

使用順序建議

  1. 建立實例 vm-0
  2. 設定 config-ssh
  3. 觀看可用連線
  4. 使用 ~/.ssh/config 中的 host 進行 SSH 連線

結語

經過一連串的指令細細品嘗後,就可以完成計算實例的新增刪除,也可以進行遠端的連線操作。但在多人共同操作時是蠻不一樣的,在下一篇將介紹如何通過設定中繼資料中的 SSH 金鑰,來達到讓多人可以進行連線的方法。可以說在這一篇可以學到單人開發模式,而下一篇則是主要運用在協作開發上。

環境建置

請參考下列兩篇文章:

  1. GCP Google Cloud SDK 快速入手模式 (上)
  2. GCP Google Cloud SDK 快速入手模式 (下)

上一篇
Best Practice for DevOps on GitLab and GCP : GCP 服務帳戶與權限 - Day 16 -
下一篇
Best Practice for DevOps on GitLab and GCP : GCP 中繼資料 SSH 金鑰管理 - Day 18 -
系列文
Best Practice for DevOps on GitLab and GCP30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言