快速設定 App Engine IP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
$ gcloud compute networks subnets create [SUBNET_NAME] \ --range=10.124.0.0/28 \ --network=[VPC_NAME] \ --region=[REGION]
$ gcloud compute networks vpc-access connectors create [CONNECTOR_NAME] \ --region=[REGION] \ --subnet=[SUBNET_NAME]
$ gcloud depoly app
$ gcloud compute firewall-rules create [RULE_NAME] \ --action=ALLOW \ --rules=TCP \ --source-ranges=35.199.224.0/19 \ --target-tags=vpc-connector \ --direction=INGRESS \ --network=[SUBNET_NAME] \ --priority=0 \ --project=[PROJECT_ID]
$ gcloud compute routers create [ROUTER_NAME] \ --network=[SUBNET_NAME] \ --region=[REGION]
$ gcloud compute addresses create [STATIC_IP_NAME] \ --region=[REGION]
$ gcloud compute routers nats create [NAT_NAME] \ --router=[ROUTER_NAME] \ --region=[REGION] \ --nat-custom-subnet-ip-ranges=[SUBNET_NAME] \ --nat-external-ip-pool=[STATIC_IP_NAME]
|
刪除服務
1 2 3 4 5
| $ gcloud compute routers nats delete [NAT_NAME] --router=[ROUTER_NAME] --region=[REGION] $ gcloud compute addresses delete [STATIC_IP_NAME] --region=[REGION] $ gcloud compute routers delete [ROUTER_NAME] --region=[REGION] $ gcloud compute networks vpc-access connectors delete [CONNECTOR_NAME] --region=[REGION] $ gcloud compute networks subnets delete [SUBNET_NAME] --region=[REGION]
|
驗證設定影響練習
練習部署 Serverless 服務,驗證是否取得固定 IP。同時檢測在同一個 VPC 下 subnets 的影響。
快速部署 GKE 範例
建立一個簡易的 Python 專案或使用 Google 教學範例
新增專案目錄和建立 app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import requests from flask import Flask, request
app = Flask(__name__)
@app.route('/') def home(): response = requests.get('https://api.ipify.org?format=json') return response.json()
if __name__ == '__main__': app.run(host='0.0.0.0', port=80)
|
建立 Dockerfile
1 2 3 4 5 6 7 8 9 10
| FROM python:3.8-slim
WORKDIR /app
COPY . /app
RUN pip install Flask requests
ENTRYPOINT ["python"] CMD ["app.py"]
|
部署(前置作業請先註冊 GCP 帳號,取得權限,安裝 gcloud)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| $ gcloud projects list $ gcloud config set [PROJECT_ID]
$ gcloud services enable artifactregistry.googleapis.com $ gcloud services enable container.googleapis.com
$ gcloud projects add-iam-policy-binding [YOUR_PROJECT_ID] \ --member="user:[USER_EMAIL]" \ --role="roles/container.admin"
$ gcloud container clusters create-auto [CLUSTER_NAME] --location=[REGION]
$ cd [DEMO_PROJECT] $ docker build --platform=linux/amd64 -t [DEMO_PROJECT_NAME] . $ docker tag demo-app gcr.io/[PROJECT_ID]/[DEMO_PROJECT_NAME]:latest $ docker push gcr.io/[PROJECT_ID]/[DEMO_PROJECT_NAME]
$ gcloud container clusters get-credentials [CLUSTER_NAME] --location [REGION]
$ kubectl create deployment [DEPLOYMENT_NAME] --image=gcr.io/[PROJECT_ID]/[DEMO_PROJECT_NAME]:latest
$ kubectl expose deployment [DEPLOYMENT_NAME] --type= LoadBalancer --port 80 --target-port 80
$ kubectl get pods
$ kubectl get service [DEPLOYMENT_NAME]
$ kubectl set image deployment/[DEPLOYMENT_NAME] [DEPLOYMENT_NAME]=gcr.io/[PROJECT_ID]/[DEMO_PROJECT_NAME]:latest
|
快速部署 App Engine 範例
常用 gcloud 指令
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
$ gcloud projects list
$ gcloud config list
$ gcloud config get-value project
$ gcloud config set project [PROJECT_ID] $ export PROJECT_ID="$(gcloud config get-value project -q)" $ gcloud config set compute/zone [REGION]
$ gcloud config configurations list $ gcloud config configurations create [CONFIGURATION_NAME] $ gcloud config configurations activate [CONFIGURATION_NAME]
$ gcloud config set project [PROJECT_ID] $ gcloud config set compute/zone [ZONE] $ gcloud config set account [ACCOUNT]
$ gcloud services list --available
$ gcloud services enable [SERVICE_NAME]
|
參考資源