1. ภาพรวม
ห้องทดลองนี้จะสอนวิธีใช้ Pulumi ซึ่งเป็นเครื่องมือโครงสร้างพื้นฐานเป็นโค้ดในการจัดสรรและจัดการทรัพยากร Google Cloud
สิ่งที่คุณจะได้เรียนรู้
ในบทแนะนำนี้ คุณจะได้เรียนรู้วิธีดำเนินการต่อไปนี้
- ติดตั้งและกำหนดค่า Pulumi
- เขียนโปรแกรม YAML เพื่อจำลองโครงสร้างพื้นฐานใน Google Cloud
- จัดสรรและจัดการทรัพยากรระบบคลาวด์โดยใช้ Pulumi
- ใช้ pulumi convert เพื่อแปลงโปรแกรม YAML เป็นโปรแกรม Python
2. การตั้งค่าและข้อกําหนด
การตั้งค่าสภาพแวดล้อมตามเวลาที่สะดวก
- ลงชื่อเข้าใช้ Google Cloud Console แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี
- ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
- รหัสโปรเจ็กต์จะไม่ซ้ำกันในทุกโปรเจ็กต์ของ Google Cloud และจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้หลังจากตั้งค่าแล้ว) คอนโซล Cloud จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นจะเป็นอะไร ในโค้ดแล็บส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น
PROJECT_ID
) หากไม่ชอบรหัสที่สร้างขึ้น คุณก็สร้างรหัสอื่นแบบสุ่มได้ หรือจะลองใช้อุปกรณ์ของคุณเองเพื่อดูว่าฟีเจอร์นี้พร้อมใช้งานหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงชื่อหลังจากขั้นตอนนี้ไม่ได้ และชื่อจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์ - สำหรับข้อมูลของคุณ ค่าที่ 3 คือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 รายการนี้ได้ในเอกสารประกอบ
- ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำงานในโค้ดแล็บนี้แทบจะไม่เสียค่าใช้จ่ายใดๆ เลย หากต้องการปิดใช้ทรัพยากรเพื่อไม่ให้มีการเรียกเก็บเงินหลังจากบทแนะนำนี้ คุณก็ลบทรัพยากรที่สร้างไว้หรือลบทั้งโปรเจ็กต์ได้ ผู้ใช้ใหม่ของ Google Cloud มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD
3. การตั้งค่าโครงสร้างพื้นฐาน
ติดตั้งและกำหนดค่า Pulumi
ใน Cloud Shell ให้เรียกใช้คำสั่งต่อไปนี้เพื่อติดตั้ง Pulumi
curl -fsSL https://get.pulumi.com | sh
เพิ่ม Pulumi ลงในเส้นทางและดูข้อความความช่วยเหลือจาก Pulumi
export PATH=${PATH}:~/.pulumi/bin
# view the help message to verify pulumi runs
pulumi -h
เรียกใช้คำสั่งต่อไปนี้เพื่อตั้งค่ารหัสโปรเจ็กต์และให้สิทธิ์เข้าถึง คุณต้องทําตามวิธีการที่คำสั่งระบุ
export PROJECT_ID=$(gcloud config get-value project)
gcloud auth application-default login
สร้างที่เก็บข้อมูล GCS ใน Cloud Shell และใช้เป็นที่เก็บข้อมูลหลังบ้าน
gsutil mb gs://pulumi-${PROJECT_ID}
pulumi login gs://pulumi-${PROJECT_ID}
สร้างโปรเจ็กต์ใหม่
สร้างไดเรกทอรีรูทของโปรเจ็กต์ใน Cloud Shell
mkdir pulumi-lab && cd pulumi-lab
กำหนดไฟล์โปรเจ็กต์(จุดแรกเข้าของ Pulumi)
cat <<EOT > Pulumi.yaml
name: pulumi-lab
description: Try Pulumi
runtime: yaml
main: yaml-repo/
EOT
กําหนดทรัพยากร YAML
สร้างไดเรกทอรีเพื่อเก็บคำจำกัดความทรัพยากรระบบคลาวด์ในรูปแบบ YAML
mkdir yaml-repo
สร้างไฟล์ yaml-repo/Pulumi.yaml
ด้วยคําจํากัดความของทรัพยากรต่อไปนี้
- ที่เก็บข้อมูล
- การผูก IAM
- ออบเจ็กต์ข้อความที่มีสตริง "Hello World!"
- และเอาต์พุตบางส่วน
resources:
# Create a GCP resource (Storage Bucket)
my-bucket:
type: gcp:storage:Bucket
properties:
location: US
website:
mainPageSuffix: index.html
uniformBucketLevelAccess: true
my-bucket-binding:
type: gcp:storage:BucketIAMBinding
properties:
bucket: ${my-bucket.name}
role: "roles/storage.objectViewer"
members: ["allUsers"]
index-object:
type: gcp:storage:BucketObject
properties:
bucket: ${my-bucket}
source:
fn::stringAsset: Hello World!
outputs:
bucketName: ${my-bucket.url}
ติดตั้งใช้งานทรัพยากร
เริ่มต้นและกำหนดค่าสแต็ก
export PULUMI_CONFIG_PASSPHRASE=pulumi-lab
pulumi stack init dev
pulumi config set gcp:project $PROJECT_ID
ตรวจสอบการกําหนดค่าสแต็ก แล้วคุณควรเห็นคีย์ gcp:project ที่มีรหัสโปรเจ็กต์เป็นค่า
pulumi config
ในจุดนี้ โครงสร้างไดเรกทอรีควรมีลักษณะดังต่อไปนี้
├── Pulumi.dev.yaml ├── Pulumi.yaml └── yaml-repo └── Pulumi.yaml
ทำให้สแต็กใช้งานได้
pulumi up
คําสั่งนี้จะประเมินโปรแกรมและระบุการอัปเดตทรัพยากรที่จะทํา ก่อนอื่น ระบบจะแสดงตัวอย่างที่สรุปการเปลี่ยนแปลงที่จะทำเมื่อคุณใช้คำสั่ง
(เอาต์พุต)
Previewing update (dev): Downloading plugin gcp v6.44.0: 45.69 MiB / 45.69 MiB [=============] 100.00% 1s Type Name Plan + pulumi:pulumi:Stack pulumi-lab-dev create + ├─ gcp:storage:Bucket my-bucket create + ├─ gcp:storage:BucketObject index-object create + └─ gcp:storage:BucketIAMBinding my-bucket-binding create Outputs: bucketName: output<string> Resources: + 4 to create Do you want to perform this update? [Use arrows to move, type to filter] yes > no details
เลือกใช่ จากนั้นระบบจะจัดสรรทรัพยากร เอาต์พุตควรมีลักษณะดังนี้
Do you want to perform this update? yes Updating (dev): Type Name Status + pulumi:pulumi:Stack pulumi-lab-dev created (3s) + ├─ gcp:storage:Bucket my-bucket created (1s) + ├─ gcp:storage:BucketObject index-object created (0.78s) + └─ gcp:storage:BucketIAMBinding my-bucket-binding created (5s) Outputs: bucketName: "gs://my-bucket-874aa08" Resources: + 4 created Duration: 11s
การเรียกใช้คำสั่งต่อไปนี้จะพิมพ์เอาต์พุตที่กำหนดไว้
pulumi stack output
เรียกใช้คําสั่งต่อไปนี้เพื่อยืนยันการเปลี่ยนแปลง
gsutil ls $(pulumi stack output bucketName)
เอาต์พุตจะมีลักษณะดังนี้
(เอาต์พุต)
gs://my-bucket-11a9046/index-object-77a5d80
4. แปลง YAML เป็น Python
มาแปลงตัวอย่างข้างต้นเป็นโปรแกรม Pulumi Python
pulumi convert --language python --out ./py-repo
ตรวจสอบโค้ดที่สร้างขึ้นใน py-repo
cat py-repo/__main__.py
(เอาต์พุต)
import pulumi import pulumi_gcp as gcp my_bucket = gcp.storage.Bucket("my-bucket", location="US", website=gcp.storage.BucketWebsiteArgs( main_page_suffix="index.html", ), uniform_bucket_level_access=True) my_bucket_binding = gcp.storage.BucketIAMBinding("my-bucket-binding", bucket=my_bucket.name, role="roles/storage.objectViewer", members=["allUsers"]) index_object = gcp.storage.BucketObject("index-object", bucket=my_bucket.id, source=pulumi.StringAsset("Hello World!")) pulumi.export("bucketName", my_bucket.url) .......
เปิดใช้งานสภาพแวดล้อมเสมือนของ Python
source py-repo/bin/activate
อัปเดตไฟล์โปรเจ็กต์ Pulumi.yaml ให้ชี้ไปยังโปรแกรม Python โปรดสังเกตว่ารันไทม์และรายการหลักมีการเปลี่ยนแปลง
cat <<EOT > Pulumi.yaml
name: pulumi-lab
description: Try Pulumi
runtime: python
main: py-repo/
EOT
ลองทำให้สแต็กใช้งานได้อีกครั้งและเลือกใช่
pulumi up
ไม่ควรมีการเปลี่ยนแปลงใดๆ และเอาต์พุตควรมีลักษณะคล้ายกับภาพนี้
(เอาต์พุต)
Previewing update (dev): Type Name Plan pulumi:pulumi:Stack pulumi-lab-dev Resources: 4 unchanged Do you want to perform this update? yes Updating (dev): Type Name Status pulumi:pulumi:Stack pulumi-lab-dev Outputs: bucketName: "gs://my-bucket-c2b49ad" Resources: 4 unchanged Duration: 6s
5. ลบทรัพยากร
ลบทรัพยากรที่สร้างไว้
pulumi destroy
การยืนยันจะมีลักษณะดังนี้
Previewing update (dev): Type Name Plan pulumi:pulumi:Stack pulumi-lab-dev Resources: 4 unchanged Do you want to perform this update? [Use arrows to move, type to filter] yes > no details Do you want to perform this destroy? yes Destroying (dev): Type Name Status - pulumi:pulumi:Stack pulumi-lab-dev deleted - ├─ gcp:storage:BucketIAMBinding my-bucket-binding deleted (5s) - ├─ gcp:storage:BucketObject index-object deleted (1s) - └─ gcp:storage:Bucket my-bucket deleted (0.73s) Outputs: - bucketName: "gs://my-bucket-874aa08" Resources: - 4 deleted Duration: 10s
6. ยินดีด้วย
ยินดีด้วย คุณทำแล็บเสร็จแล้ว