最後活躍 8 months ago

從專案切換、建立靜態 IP,到 VM 外部 IP 更換與 SSH 連線,全套 gcloud CLI 操作全收錄,超實用筆記!🛠️

timmy 已修改 8 months ago. 還原成這個修訂版本

沒有任何變更

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 13 insertions, 12 deletions

google_cloud_sdk_firewall_rules_script.sh

@@ -1,26 +1,27 @@
1 - # 列出目前專案中的所有防火牆規則
2 1 gcloud compute firewall-rules list
3 2
4 - # 查看特定防火牆規則的詳細資訊
5 3 gcloud compute firewall-rules describe <FIREWALL_RULE_NAME>
6 -
7 - # 例如,查看名為 "allow-http" 的防火牆規則
8 4 gcloud compute firewall-rules describe allow-http
9 5
10 - # 🔍 查找與特定 VM 標籤 (targetTags) 相關的防火牆規則
11 6 gcloud compute firewall-rules list --filter="targetTags:<VM_TAG>"
12 -
13 - # 例如,查找與 "web-server" 這個標籤相關的防火牆規則
14 7 gcloud compute firewall-rules list --filter="targetTags=web-server"
15 8
16 - # 📥 查詢所有「進站 (INGRESS)」的防火牆規則
17 9 gcloud compute firewall-rules list --filter="direction:INGRESS"
18 -
19 - # 📤 查詢所有「出站 (EGRESS)」的防火牆規則
20 10 gcloud compute firewall-rules list --filter="direction:EGRESS"
21 11
22 - # 🌐 查詢所有允許 TCP 80 (HTTP) 連線的防火牆規則
12 + # 查詢所有允許 TCP 80 (HTTP) 連線的規則
23 13 gcloud compute firewall-rules list --filter="allowed.tcp=80"
24 14
25 - # 🔑 查詢所有允許 TCP 22 (SSH) 連線的防火牆規則
15 + # 查詢所有允許 TCP 22 (SSH) 連線的規則
26 16 gcloud compute firewall-rules list --filter="allowed.tcp=22"
17 +
18 +
19 + gcloud compute firewall-rules create allow-tcp-7800 \
20 + --direction=INGRESS \
21 + --priority=1000 \
22 + --network=default \
23 + --action=ALLOW \
24 + --rules=tcp:7800 \
25 + --source-ranges=0.0.0.0/0
26 +
27 + gcloud compute firewall-rules list --filter="name=allow-tcp-7800"

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 12 insertions, 2 deletions

google_cloud_sdk_firewall_rules_script.sh

@@ -1,16 +1,26 @@
1 + # 列出目前專案中的所有防火牆規則
1 2 gcloud compute firewall-rules list
2 3
4 + # 查看特定防火牆規則的詳細資訊
3 5 gcloud compute firewall-rules describe <FIREWALL_RULE_NAME>
6 +
7 + # 例如,查看名為 "allow-http" 的防火牆規則
4 8 gcloud compute firewall-rules describe allow-http
5 9
10 + # 🔍 查找與特定 VM 標籤 (targetTags) 相關的防火牆規則
6 11 gcloud compute firewall-rules list --filter="targetTags:<VM_TAG>"
12 +
13 + # 例如,查找與 "web-server" 這個標籤相關的防火牆規則
7 14 gcloud compute firewall-rules list --filter="targetTags=web-server"
8 15
16 + # 📥 查詢所有「進站 (INGRESS)」的防火牆規則
9 17 gcloud compute firewall-rules list --filter="direction:INGRESS"
18 +
19 + # 📤 查詢所有「出站 (EGRESS)」的防火牆規則
10 20 gcloud compute firewall-rules list --filter="direction:EGRESS"
11 21
12 - # 查詢所有允許 TCP 80 (HTTP) 連線的規則
22 + # 🌐 查詢所有允許 TCP 80 (HTTP) 連線的防火牆規則
13 23 gcloud compute firewall-rules list --filter="allowed.tcp=80"
14 24
15 - # 查詢所有允許 TCP 22 (SSH) 連線的規則
25 + # 🔑 查詢所有允許 TCP 22 (SSH) 連線的防火牆規則
16 26 gcloud compute firewall-rules list --filter="allowed.tcp=22"

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 16 insertions

google_cloud_sdk_firewall_rules_script.sh(檔案已創建)

@@ -0,0 +1,16 @@
1 + gcloud compute firewall-rules list
2 +
3 + gcloud compute firewall-rules describe <FIREWALL_RULE_NAME>
4 + gcloud compute firewall-rules describe allow-http
5 +
6 + gcloud compute firewall-rules list --filter="targetTags:<VM_TAG>"
7 + gcloud compute firewall-rules list --filter="targetTags=web-server"
8 +
9 + gcloud compute firewall-rules list --filter="direction:INGRESS"
10 + gcloud compute firewall-rules list --filter="direction:EGRESS"
11 +
12 + # 查詢所有允許 TCP 80 (HTTP) 連線的規則
13 + gcloud compute firewall-rules list --filter="allowed.tcp=80"
14 +
15 + # 查詢所有允許 TCP 22 (SSH) 連線的規則
16 + gcloud compute firewall-rules list --filter="allowed.tcp=22"

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 10 deletions

google_cloud_sdk_compute_operations_script.sh

@@ -1,25 +1,19 @@
1 1 # 設定當前使用的 GCP 專案
2 -
3 2 gcloud config set project <YOUR_PROJECT_ID>
4 3 gcloud config set project my-gcp-project-123456
5 4
6 5 # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
7 -
8 6 gcloud compute instances list
9 7
10 8 # 在 GCP 中建立一個靜態 IP
11 -
12 9 gcloud compute addresses create <STATIC_IP_NAME> --region <REGION>
13 10 gcloud compute addresses create my-static-ip --region us-central1
14 11
15 12 # 查看目前專案中的所有靜態 IP
16 -
17 13 gcloud compute addresses list
18 14
19 15 # 刪除 VM "<YOUR_VM_NAME>" 目前的外部 IP 配置
20 -
21 16 # ⚠️ 執行後 VM 會暫時失去外部連線,請確保有內部網路或 Cloud Shell 可用!
22 -
23 17 gcloud compute instances delete-access-config <YOUR_VM_NAME> \
24 18 --zone <YOUR_VM_ZONE> \
25 19 --access-config-name "External NAT"
@@ -29,7 +23,6 @@ gcloud compute instances delete-access-config my-vm-1 \
29 23 --access-config-name "External NAT"
30 24
31 25 # 為 VM "<YOUR_VM_NAME>" 設定新的外部 IP
32 -
33 26 gcloud compute instances add-access-config <YOUR_VM_NAME> \
34 27 --zone <YOUR_VM_ZONE> \
35 28 --access-config-name "External NAT" \
@@ -41,14 +34,11 @@ gcloud compute instances add-access-config my-vm-1 \
41 34 --address 35.234.56.78
42 35
43 36 # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
44 -
45 37 gcloud compute instances list
46 38
47 39 # 透過 SSH 連線至 VM
48 -
49 40 gcloud compute ssh <YOUR_USERNAME>@<YOUR_VM_NAME> --zone <YOUR_VM_ZONE>
50 41 gcloud compute ssh timmy@my-vm-1 --zone us-central1-a
51 42
52 43 # 更新 SSH 設定
53 -
54 44 gcloud compute config-ssh

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 54 insertions

google_cloud_sdk_compute_operations_script.sh(檔案已創建)

@@ -0,0 +1,54 @@
1 + # 設定當前使用的 GCP 專案
2 +
3 + gcloud config set project <YOUR_PROJECT_ID>
4 + gcloud config set project my-gcp-project-123456
5 +
6 + # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
7 +
8 + gcloud compute instances list
9 +
10 + # 在 GCP 中建立一個靜態 IP
11 +
12 + gcloud compute addresses create <STATIC_IP_NAME> --region <REGION>
13 + gcloud compute addresses create my-static-ip --region us-central1
14 +
15 + # 查看目前專案中的所有靜態 IP
16 +
17 + gcloud compute addresses list
18 +
19 + # 刪除 VM "<YOUR_VM_NAME>" 目前的外部 IP 配置
20 +
21 + # ⚠️ 執行後 VM 會暫時失去外部連線,請確保有內部網路或 Cloud Shell 可用!
22 +
23 + gcloud compute instances delete-access-config <YOUR_VM_NAME> \
24 + --zone <YOUR_VM_ZONE> \
25 + --access-config-name "External NAT"
26 +
27 + gcloud compute instances delete-access-config my-vm-1 \
28 + --zone us-central1-a \
29 + --access-config-name "External NAT"
30 +
31 + # 為 VM "<YOUR_VM_NAME>" 設定新的外部 IP
32 +
33 + gcloud compute instances add-access-config <YOUR_VM_NAME> \
34 + --zone <YOUR_VM_ZONE> \
35 + --access-config-name "External NAT" \
36 + --address <STATIC_IP>
37 +
38 + gcloud compute instances add-access-config my-vm-1 \
39 + --zone us-central1-a \
40 + --access-config-name "External NAT" \
41 + --address 35.234.56.78
42 +
43 + # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
44 +
45 + gcloud compute instances list
46 +
47 + # 透過 SSH 連線至 VM
48 +
49 + gcloud compute ssh <YOUR_USERNAME>@<YOUR_VM_NAME> --zone <YOUR_VM_ZONE>
50 + gcloud compute ssh timmy@my-vm-1 --zone us-central1-a
51 +
52 + # 更新 SSH 設定
53 +
54 + gcloud compute config-ssh

timmy 已修改 8 months ago. 還原成這個修訂版本

1 file changed, 14 insertions

google_cloud_sdk_installation_script.sh(檔案已創建)

@@ -0,0 +1,14 @@
1 + # 用 Homebrew 安裝 Google Cloud SDK(適用 macOS)
2 + brew install google-cloud-sdk
3 +
4 + # 初始化 gcloud,進行登入與基本設定
5 + gcloud init
6 +
7 + # 設定當前使用的 GCP 專案(記得換成你的專案 ID)
8 + gcloud config set project [YOUR_PROJECT_ID]
9 +
10 + # 查看目前帳戶底下有哪些 GCP 專案
11 + gcloud projects list
12 +
13 + # 查詢目前帳戶下的所有 Compute Engine 虛擬機(VM)
14 + gcloud compute instances list
上一頁 下一頁