ห้องทดลอง: การนำไปใช้งาน PSC ของ NCC

1. บทนำ

ภาพรวม

ในแล็บนี้ ผู้ใช้จะได้สำรวจว่าฮับของ Network Connectivity Center จะเผยแพร่ปลายทาง Private Service Connect ไปยังกิ่ง VPC ได้อย่างไร

ทรัพยากรฮับมีรูปแบบการจัดการการเชื่อมต่อแบบรวมศูนย์เพื่อเชื่อมต่อการรับส่งข้อมูลของ Spoke VPC กับปลายทาง PSC

สิ่งที่คุณจะสร้าง

ในโค้ดแล็บนี้ คุณจะได้สร้างเครือข่าย NCC ที่จะเผยแพร่ปลายทาง Private Service Connect ไปยังอินสแตนซ์ Cloud SQL

74bf390e323ea3bb.png

สิ่งที่คุณจะได้เรียนรู้

  • ใช้ Private Service Connect เพื่อเชื่อมต่อกับอินสแตนซ์ Cloud SQL
  • ใช้ฮับ NCC เพื่อเผยแพร่ซับเน็ต PSC ไปยัง VPC Spoke ทั้งหมดเพื่อให้เชื่อมต่อเครือข่ายจากเครือข่าย VPC หลายเครือข่ายได้

สิ่งที่คุณต้องมี

  • ความรู้เกี่ยวกับ Cloud Networking ของ GCP
  • ความรู้พื้นฐานเกี่ยวกับ Cloud SQL
  • โปรเจ็กต์ Google Cloud
  • ตรวจสอบโควต้า: เครือข่ายและขอเครือข่ายเพิ่มเติมหากจำเป็น ภาพหน้าจอด้านล่าง

6bc606cb34bce7e8.png

วัตถุประสงค์

  • ตั้งค่าสภาพแวดล้อม GCP
  • ตั้งค่าอินสแตนซ์ Cloud SQL สำหรับ MySql ด้วย Private Service Connect
  • กำหนดค่าฮับ Network Connectivity Center เพื่อเผยแพร่ปลายทาง PSC
  • กำหนดค่า Network Connectivity Center โดยใช้ VPC เป็น Spoke
  • ตรวจสอบเส้นทางข้อมูล
  • สำรวจฟีเจอร์ความพร้อมในการให้บริการของ NCC
  • ล้างข้อมูลทรัพยากร

ก่อนเริ่มต้น

Google Cloud Console และ Cloud Shell

หากต้องการโต้ตอบกับ GCP เราจะใช้ทั้ง Google Cloud Console และ Cloud Shell ตลอดแล็บนี้

คอนโซล Google Cloud ของโปรเจ็กต์ฮับ NCC

คุณเข้าถึง Cloud Console ได้ที่ https://console.cloud.google.com

ตั้งค่ารายการต่อไปนี้ใน Google Cloud เพื่อให้กำหนดค่า Network Connectivity Center ได้ง่ายขึ้น

ใน Google Cloud Console ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud ในหน้าตัวเลือกโปรเจ็กต์

เปิด Cloud Shell Codelab นี้ใช้ $variables เพื่อช่วยในการติดตั้งใช้งานการกำหนดค่า gcloud ใน Cloud Shell

gcloud auth list
gcloud config list project
gcloud config set project $project
project=[YOUR-PROJECT-NAME]
echo $project

บทบาท IAM

NCC กำหนดให้ใช้บทบาท IAM เพื่อเข้าถึง API บางรายการ อย่าลืมกำหนดค่าผู้ใช้ด้วยบทบาท IAM ของ NCC ตามที่จำเป็น

บทบาท/คำอธิบาย

สิทธิ์

networkconnectivity.networkAdmin - อนุญาตให้ผู้ดูแลระบบเครือข่ายจัดการฮับและสโป๊ก

networkconnectivity.hubs.networkconnectivity.spokes.

networkconnectivity.networkSpokeManager - อนุญาตให้เพิ่มและจัดการ Spoke ในฮับ ใช้ใน VPC ที่แชร์ซึ่งโปรเจ็กต์โฮสต์เป็นเจ้าของฮับ แต่ผู้ดูแลระบบอื่นๆ ในโปรเจ็กต์อื่นๆ สามารถเพิ่ม Spoke สำหรับการเชื่อมต่อของตนเองไปยังฮับได้

networkconnectivity.spokes.**

networkconnectivity.networkUsernetworkconnectivity.networkViewer - อนุญาตให้ผู้ใช้เครือข่ายดูแอตทริบิวต์ต่างๆ ของฮับและสโป๊ก

networkconnectivity.hubs.getnetworkconnectivity.hubs.listnetworkconnectivity.spokes.getnetworkconnectivity.spokes.listnetworkconnectivity.spokes.aggregatedList

2. ตั้งค่าสภาพแวดล้อมเครือข่าย

ภาพรวม

ในส่วนนี้ เราจะทำให้เครือข่าย VPC 2 เครือข่ายและกฎไฟร์วอลล์ใช้งานได้ในโปรเจ็กต์เดียว แผนภาพเชิงตรรกะแสดงสภาพแวดล้อมเครือข่ายที่จะตั้งค่าในขั้นตอนนี้

8884a55988881e17.png

สร้าง VPC1 และซับเน็ต

เครือข่าย VPC มีซับเน็ตที่คุณจะติดตั้ง VM ของ GCE เพื่อตรวจสอบเส้นทางข้อมูล

vpc_spoke_network_name="vpc1-spoke"
vpc_spoke_subnet_name="subnet1"
vpc_spoke_subnet_ip_range="10.0.1.0/24"
region="us-central1"
zone="us-central1-a"

gcloud compute networks create "${vpc_spoke_network_name}" \
--subnet-mode=custom \

gcloud compute networks subnets create "${vpc_spoke_subnet_name}" \
--network="${vpc_spoke_network_name}" \
--range="${vpc_spoke_subnet_ip_range}" \
--region="${region}" 

สร้างซับเน็ต PSC ใน VPC

ใช้คำสั่งด้านล่างเพื่อสร้างซับเน็ตใน VPC Spoke ที่จะจัดสรรให้กับ PSC-EP

vpc_spoke_network_name="vpc1-spoke"
vpc_spoke_subnet_name="csql-psc-subnet"
region="us-central1"
vpc_spoke_subnet_ip_range="192.168.0.0/24"

gcloud compute networks subnets create "${vpc_spoke_subnet_name}" \
--network="${vpc_spoke_network_name}" \
--range="${vpc_spoke_subnet_ip_range}" \
--region="${region}" 

สร้าง VPC3 และซับเน็ต

vpc_spoke_network_name="vpc3-spoke"
vpc_spoke_subnet_name="subnet3"
vpc_spoke_subnet_ip_range="10.0.3.0/24"
region="us-central1"
zone="us-central1-a"

gcloud compute networks create "${vpc_spoke_network_name}" \
--subnet-mode=custom \

gcloud compute networks subnets create "${vpc_spoke_subnet_name}" \
--network="${vpc_spoke_network_name}" \
--range="${vpc_spoke_subnet_ip_range}" \
--region="${region}"

กำหนดค่ากฎไฟร์วอลล์ของ VPC1

กฎเหล่านี้จะอนุญาตการเชื่อมต่อเครือข่ายจากช่วง RFC1918 และ Identity Access Proxy

vpc_spoke_network_name="vpc1-spoke"

gcloud compute firewall-rules create vpc1-allow-all \
--network="${vpc_spoke_network_name}" \
--allow=all \
--source-ranges=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16

gcloud compute firewall-rules create vpc1-allow-iap \
--network="${vpc_spoke_network_name}" \
--allow all \
--source-ranges 35.235.240.0/20

กำหนดค่า VPC การกำหนดเส้นทางและกฎไฟร์วอลล์ VPC

vpc_spoke_network_name="vpc3-spoke"

gcloud compute firewall-rules create vpc3-allow-all \
--network="${vpc_spoke_network_name}" \
--allow=all \
--source-ranges=10.0.0.0/8,172.16.0.0/12,192.168.0.0/16

gcloud compute firewall-rules create vpc3-allow-iap \
--network="${vpc_spoke_network_name}" \
--allow all \
--source-ranges 35.235.240.0/20

กำหนดค่า GCE VM ใน VPC1

คุณจะต้องมีสิทธิ์เข้าถึงอินเทอร์เน็ตชั่วคราวเพื่อติดตั้งแพ็กเกจ ดังนั้นให้กำหนดค่าอินสแตนซ์เพื่อใช้ที่อยู่ IP ภายนอก

vm_vpc1_spoke_name="csql-vpc1-vm"
vpc_spoke_network_name="vpc1-spoke"
vpc_spoke_subnet_name="subnet1"
region="us-central1"
zone="us-central1-a"

gcloud compute instances create "${vm_vpc1_spoke_name}" \
--machine-type="e2-medium" \
--subnet="${vpc_spoke_subnet_name}" \
--zone="${zone}" \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
sudo apt-get update
sudo apt-get install -y default-mysql-client'

กำหนดค่า GCE VM ใน VPC3

คุณจะต้องมีสิทธิ์เข้าถึงอินเทอร์เน็ตชั่วคราวเพื่อติดตั้งแพ็กเกจ ดังนั้นให้กำหนดค่าอินสแตนซ์เพื่อใช้ที่อยู่ IP ภายนอก

vm_vpc_spoke_name="csql-vpc3-vm"
vpc_spoke_network_name="vpc3-spoke"
vpc_spoke_subnet_name="subnet3"
region="us-central1"
zone="us-central1-a"

gcloud compute instances create "${vm_vpc_spoke_name}" \
--machine-type="e2-medium" \
--subnet="${vpc_spoke_subnet_name}" \
--zone="${zone}" \
--image-family=debian-11 \
--image-project=debian-cloud \
--metadata=startup-script='#!/bin/bash
sudo apt-get update
sudo apt-get install -y default-mysql-client'

3. สร้างอินสแตนซ์ Cloud SQL

ใช้คำสั่งด้านล่างเพื่อสร้างอินสแตนซ์และเปิดใช้ Private Service Connect

การดำเนินการนี้จะใช้เวลาสักครู่

gcloud config set project ${project}

gcloud sql instances create mysql-instance \
--project="${project}" \
--region=us-central1 \
--enable-private-service-connect \
--allowed-psc-projects="${project}" \
--availability-type=zonal \
--no-assign-ip \
--tier=db-f1-micro \
--database-version=MYSQL_8_0 \
--enable-bin-log

ระบุ URI ของการเชื่อมต่อบริการของอินสแตนซ์ Cloud SQL

ใช้คำสั่ง gcloud sql instances describe เพื่อดูข้อมูลเกี่ยวกับอินสแตนซ์ที่เปิดใช้ Private Service Connect จดบันทึกฟิลด์ pscServiceAttachmentLink ซึ่งแสดง URI ที่ชี้ไปยังไฟล์แนบบริการของอินสแตนซ์ เราจะต้องใช้ข้อมูลนี้ในส่วนถัดไป

gcloud sql instances describe mysql-instance \
--format='value(pscServiceAttachmentLink)'

4. ปลายทาง PSC ไปยัง Cloud SQL

จองที่อยู่ IP ภายในสำหรับปลายทาง PSC

ใช้คำสั่งด้านล่างเพื่อจองที่อยู่ IP ภายในสำหรับปลายทาง Private Service Connect

region="us-central1"
vpc_spoke_subnet_name="csql-psc-subnet"

gcloud compute addresses create csql-psc-ip \
--subnet="${vpc_spoke_subnet_name}" \
--region="${region}" \
--addresses=192.168.0.253

ค้นหาชื่อที่เชื่อมโยงกับที่อยู่ IP ที่สงวนไว้ ซึ่งจะใช้ในการกำหนดค่ากฎการส่งต่อ

gcloud compute addresses list \
--filter="name=csql-psc-ip"

สร้างกฎการส่งต่อ Private Service Connect ใน VPC1

ใช้คำสั่งด้านล่างเพื่อสร้างปลายทาง Private Service Connect และชี้ไปยังการแนบบริการ Cloud SQL

vpc_spoke_network_name="vpc1-spoke"
vpc_spoke_subnet_name="csql-psc-subnet"
region="us-central1"
csql_psc_ep_name="csql-psc-ep"
sa_uri=$(gcloud sql instances describe mysql-instance \
  --format='value(pscServiceAttachmentLink)')
echo "$sa_uri"

gcloud compute forwarding-rules create "${csql_psc_ep_name}" \
--address=csql-psc-ip \
--region="${region}" \
--network="${vpc_spoke_network_name}" \
--target-service-attachment="${sa_uri}" \
--allow-psc-global-access

ใช้คำสั่งด้านล่างเพื่อยืนยันว่าการเชื่อมต่อบริการ cSQL ยอมรับปลายทาง

gcloud compute forwarding-rules describe csql-psc-ep \
--region=us-central1 \
--format='value(pscConnectionStatus)'

ยืนยันเส้นทางข้อมูลไปยัง MySQL จาก VPC1

เมื่อสร้างอินสแตนซ์ Cloud SQL ใหม่ คุณต้องตั้งรหัสผ่านสำหรับบัญชีผู้ใช้เริ่มต้นก่อนจึงจะเชื่อมต่อกับอินสแตนซ์ได้

gcloud sql users set-password root \
--host=% \
--instance=mysql-instance \
--prompt-for-password

ใช้คำสั่งด้านล่างเพื่อค้นหาที่อยู่ IP ของปลายทาง PSC ที่เชื่อมโยงกับไฟล์แนบบริการของ Cloud SQL

gcloud compute addresses describe csql-psc-ip \
--region=us-central1 \
--format='value(address)'

เชื่อมต่อกับอินสแตนซ์ Cloud SQL จาก VM ใน VPC1

เปิดเซสชัน SSH ไปยัง csql-vpc1-vm

gcloud compute ssh csql-vpc1-vm \
--zone=us-central1-a \
--tunnel-through-iap

ใช้คำสั่งด้านล่างเพื่อเชื่อมต่อกับอินสแตนซ์ Cloud SQL เมื่อได้รับข้อความแจ้ง ให้ป้อนรหัสผ่านที่สร้างไว้ในขั้นตอนด้านบน

mysql -h 192.168.0.253 -u root -p 

เอาต์พุตด้านล่างจะแสดงเมื่อเข้าสู่ระบบสำเร็จ

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8350
Server version: 8.0.31-google (Google)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> 

ใช้คำสั่ง show databases; เพื่อยืนยันฐานข้อมูลที่สร้างขึ้นโดยค่าเริ่มต้นใน MySql

MySQL [(none)]> show databases;

เชื่อมต่อกับอินสแตนซ์ Cloud SQL จาก VM ใน VPC3

เปิดเซสชัน SSH ไปยัง csql-vpc3-vm

gcloud compute ssh csql-vpc3-vm \
--zone=us-central1-a \
--tunnel-through-iap

ใช้คำสั่งด้านล่างเพื่อเชื่อมต่อกับอินสแตนซ์ Cloud SQL เมื่อได้รับข้อความแจ้ง ให้ป้อนรหัสผ่านที่สร้างไว้ในขั้นตอนด้านบน

mysql -h 192.168.0.253 -u root -p 

เซสชันจาก VM ที่อยู่ใน VPC3 จะล้มเหลวเนื่องจากไม่มีเส้นทางข้อมูลจาก VPC3 ไปยังปลายทาง Private Service Connect ใช้การกดแป้นเพื่อออกจากเซสชัน

Ctrl + C

5. ฮับ Network Connectivity Center

ภาพรวม

ในส่วนนี้ เราจะกำหนดค่า NCC Hub โดยใช้คำสั่ง gcloud NCC Hub จะทำหน้าที่เป็นระนาบควบคุมที่รับผิดชอบในการสร้างเส้นทางข้อมูลจาก VPC Spoke ไปยังปลายทาง Private Service Connect

b615efa4bad5f86.png

เปิดใช้บริการ API

เปิดใช้ API การเชื่อมต่อเครือข่ายในกรณีที่ยังไม่ได้เปิดใช้

gcloud services enable networkconnectivity.googleapis.com

สร้าง NCC Hub

ใช้คำสั่ง gcloud ด้านล่างเพื่อสร้างฮับ NCC แฟล็ก "–export-psc" จะสั่งให้ NCC Hub เผยแพร่ปลายทาง PSC ที่รู้จักไปยัง VPC Spoke ทั้งหมด

hub_name="ncc-hub"
gcloud network-connectivity hubs create "${hub_name}" \
--export-psc

อธิบายฮับ NCC ที่สร้างขึ้นใหม่ จดชื่อและเส้นทางที่เชื่อมโยงไว้

gcloud network-connectivity hubs describe ncc-hub

กำหนดค่า VPC1 เป็น Spoke ของ NCC

hub_name="ncc-hub"
vpc_spoke_name="sql-vpc1-spoke"
vpc_spoke_network_name="vpc1-spoke"

gcloud network-connectivity spokes linked-vpc-network create "${vpc_spoke_name}" \
--hub="${hub_name}" \
--vpc-network="${vpc_spoke_network_name}" \
--global

กำหนดค่า VPC3 เป็น Spoke ของ NCC

hub_name="ncc-hub"
vpc_spoke_name="sql-vpc3-spoke"
vpc_spoke_network_name="vpc3-spoke"

gcloud network-connectivity spokes linked-vpc-network create "${vpc_spoke_name}" \
--hub="${hub_name}" \
--vpc-network="${vpc_spoke_network_name}" \
--global

ใช้คำสั่งด้านล่างเพื่อตรวจสอบตารางเส้นทางของ NCC Hub สำหรับเส้นทางไปยังเครือข่ายย่อย PSC

gcloud network-connectivity hubs route-tables routes list \
--route_table=default \
--hub=ncc-hub

6. ตรวจสอบเส้นทางข้อมูล NCC

ในขั้นตอนนี้ เราจะตรวจสอบเส้นทางข้อมูลระหว่าง NCC แบบไฮบริดและ VPC Spoke

ยืนยันเส้นทางข้อมูลที่กำหนดค่า NCC ไปยังปลายทาง PSC ของอินสแตนซ์ Cloud SQL

ใช้เอาต์พุตจากคำสั่ง gcloud เหล่านี้เพื่อเข้าสู่ระบบ VM ในองค์กร

 gcloud compute instances list --filter="name=csql-vpc3-vm" 

เข้าสู่ระบบอินสแตนซ์ VM ที่อยู่ในเครือข่ายในองค์กร

gcloud compute ssh csql-vpc3-vm \
--zone=us-central1-a \
--tunnel-through-iap

ใช้คำสั่ง mysql ด้านล่างเพื่อเชื่อมต่อกับอินสแตนซ์ Cloud SQL เมื่อได้รับข้อความแจ้ง ให้ป้อนรหัสผ่านที่สร้างไว้ในขั้นตอนด้านบน

mysql -h 192.168.0.253 -u root -p 

เอาต์พุตด้านล่างจะแสดงเมื่อเข้าสู่ระบบสำเร็จ

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 8501
Server version: 8.0.31-google (Google)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.005 sec)

ใช้คำสั่ง show databases; เพื่อยืนยันฐานข้อมูลที่สร้างขึ้นโดยค่าเริ่มต้นใน MySql

MySQL [(none)]> show databases;

7. ล้าง

เข้าสู่ระบบ Cloud Shell แล้วลบทรัพยากร GCP

ลบปลายทาง PSC ของ Cloud SQL

gcloud compute forwarding-rules delete csql-psc-ep \
--region=us-central1 \
--quiet

gcloud compute addresses delete csql-psc-ip \
--region=us-central1 \
--quiet

gcloud compute networks subnets delete csql-psc-subnet \
--region=us-central1 \
--quiet

ลบอินสแตนซ์ Cloud SQL

gcloud sql instances delete mysql-instance --quiet

ลบกฎไฟร์วอลล์

vpc_spoke_network_name="vpc3-spoke"

gcloud compute firewall-rules delete vpc3-allow-all \ --network="${vpc_spoke_network_name}" 
gcloud compute firewall-rules delete vpc3-allow-iap \ --network="${vpc_spoke_network_name}"
vpc_spoke_network_name="vpc1-spoke"

gcloud compute firewall-rules delete vpc1-allow-all \ --network="${vpc_spoke_network_name}"
gcloud compute firewall-rules delete vpc1-allow-iap \ --network="${vpc_spoke_network_name}"

ลบอินสแตนซ์ GCE ใน VPC1 และ VPC3

vm_vpc1_spoke_name="csql-vpc1-vm"
zone="us-central1-a"
gcloud compute instances delete "${vm_vpc1_spoke_name}" \
--zone="${zone}" \
--quiet


vm_vpc_spoke_name="csql-vpc3-vm"
zone="us-central1-a"
gcloud compute instances delete "${vm_vpc_spoke_name}" \
--zone="${zone}" --quiet

ลบอุปกรณ์ปลายทาง NCC

vpc_spoke_name="sql-vpc1-spoke"
gcloud network-connectivity spokes delete "${vpc_spoke_name}" \
--global \  
--quiet
vpc_spoke_name="sql-vpc3-spoke"
gcloud network-connectivity spokes delete "${vpc_spoke_name}" \
--global \  
--quiet

ลบ NCC Hub

hub_name="ncc-hub"
gcloud network-connectivity hubs delete "${hub_name}" \
  --project=${project}

ลบซับเน็ตใน VPC ทั้งหมด

vpc_spoke_subnet_name="csql-psc-subnet"
region="us-central1"

gcloud compute networks subnets delete "${vpc_spoke_subnet_name}" \ 
--region="${region}" \
--quiet
vpc_spoke_subnet_name="subnet1"
region="us-central1"

gcloud compute networks subnets delete "${vpc_spoke_subnet_name}" \ 
--region="${region}" \
--quiet
vpc_spoke_subnet_name="subnet3"
region="us-central1"

gcloud compute networks subnets delete "${vpc_spoke_subnet_name}" \ 
--region="${region}" \
--quiet

ลบ VPC1 และ VPC3

gcloud compute networks delete vpc1-spoke vpc3-spoke 

8. ยินดีด้วย

คุณดำเนินการเผยแพร่ Private Service Connect ด้วย Network Connectivity Center Lab เสร็จแล้ว

สิ่งที่คุณครอบคลุม

  • การเผยแพร่ปลายทาง Private Service Connect ด้วย Network Connectivity Center

ขั้นตอนถัดไป