การใช้กฎ Cloud NAT

1. ภาพรวม

Cloud NAT เป็นเครื่องมืออันทรงพลังเนื่องจาก Compute Engine และ Google Kubernetes Engine (GKE) จึงสามารถเข้าถึงทรัพยากรอินเทอร์เน็ตด้วยวิธีที่รองรับการปรับขนาดและปลอดภัย โดยไม่เปิดเผยภาระงานที่ทำงานบนทรัพยากรเหล่านั้นแก่บุคคลภายนอกโดยใช้ IP ภายนอก

Cloud NAT มีการออกแบบโดยไม่ต้องใช้พร็อกซี และใช้ NAT ที่เลเยอร์ Andromeda SDN ได้โดยตรง ด้วยเหตุนี้ ภาระงานของคุณจึงไม่ส่งผลกระทบต่อประสิทธิภาพ ทั้งยังปรับขนาดให้เหมาะกับ VM, ภูมิภาค และ VPC จำนวนมากได้อย่างง่ายดาย

กฎ NAT เป็นส่วนขยายของ Cloud NAT ฟีเจอร์กฎ NAT ใน Cloud NAT จะช่วยให้คุณสร้างกฎการเข้าถึงที่กำหนดวิธีใช้ Cloud NAT เพื่อเชื่อมต่อกับอินเทอร์เน็ตได้ ปัจจุบันกฎ NAT รองรับการเลือกที่อยู่ NAT ต้นทางตามที่อยู่ปลายทาง

หากไม่มีกฎ NAT จะทำให้ VM ที่เปิดใช้ Cloud NAT จะใช้ที่อยู่ IP ของ NAT ชุดเดียวกันในการเข้าถึงที่อยู่อินเทอร์เน็ตทั้งหมด

บางครั้ง Use Case ของ NAT จะเรียกร้องให้ Cloud NAT สามารถใช้ที่อยู่ IP ต้นทางที่แตกต่างกันสำหรับปลายทางหนึ่งๆ กฎ NAT จะกำหนดการจับคู่และการดำเนินการที่สอดคล้องกัน เมื่อระบุกฎ NAT แล้ว แพ็กเก็ตจะได้รับการจับคู่กับกฎ NAT แต่ละกฎ หากกฎตรงกัน ระบบจะดำเนินการที่เกี่ยวข้องกับการจับคู่นั้น

สำหรับข้อมูลเพิ่มเติม โปรดดูส่วนเอกสารประกอบเกี่ยวกับกฎ NAT

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

  • วิธีตั้งค่าเกตเวย์ Cloud NAT เพื่อเตรียมพร้อมสำหรับกฎ NAT
  • วิธีออกแบบกฎ NAT โดยใช้ Common Expression Language (CEL)
  • วิธีสร้างกฎ NAT และแนบกับเกตเวย์ NAT
  • วิธีทดสอบกฎ NAT จากอินสแตนซ์
  • วิธีอัปเดตกฎของเกตเวย์ NAT
  • วิธีลบกฎ NAT และเปลี่ยนกลับเป็นลักษณะการทำงานเริ่มต้นของ Cloud NAT

สิ่งที่ต้องมี

  • ความรู้พื้นฐานเกี่ยวกับ Google Compute Engine
  • ความรู้เกี่ยวกับเครือข่ายและ TCP/IP ขั้นพื้นฐาน
  • ความรู้พื้นฐานเกี่ยวกับบรรทัดคำสั่ง Unix/Linux
  • การสำรวจเครือข่ายใน GCP เช่น ห้องทดลองการสร้างเครือข่ายใน Google Cloud ก็จะเป็นประโยชน์อย่างมาก
  • ความเข้าใจเกี่ยวกับข้อมูลเบื้องต้นของ Cloud NAT

2. การใช้ Google Cloud Console และ Cloud Shell

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

คอนโซล Google Cloud

คุณติดต่อ Cloud Console ได้ที่ https://console.cloud.google.com

75eef5f6fd6d7e41.png

การตั้งค่าสภาพแวดล้อมตามเวลาที่สะดวก

  1. ลงชื่อเข้าใช้ Cloud Console และสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี

96a9c957bc475304.png

b9a10ebdf5b5a448.png

a1e3c01a38fa61c2.png

  • ชื่อโปรเจ็กต์คือตัวระบุส่วนตัวของคุณสำหรับโปรเจ็กต์นี้ ตราบใดที่คุณทำตามแบบแผนการตั้งชื่อ คุณก็สามารถใช้อะไรก็ได้ตามต้องการและอัปเดตชื่อได้ทุกเมื่อ
  • รหัสโปรเจ็กต์ต้องไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและจะเปลี่ยนแปลงไม่ได้ (เปลี่ยนแปลงไม่ได้เมื่อตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ปกติแล้วคุณไม่สนว่าอะไรเป็นอะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (ซึ่งปกติระบุไว้ว่าเป็น PROJECT_ID) ดังนั้นหากไม่ชอบ ให้สร้างรหัสแบบสุ่มขึ้นมาอีกรหัสหนึ่ง หรือคุณจะลองใช้รหัสโปรเจ็กต์ของคุณเองแล้วดูว่ารหัสโปรเจ็กต์พร้อมใช้งานหรือไม่ แล้วก็ "แช่แข็ง" เมื่อสร้างโปรเจ็กต์แล้ว
  1. ถัดไป คุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร Google Cloud

การใช้งาน Codelab นี้น่าจะไม่มีค่าใช้จ่ายใดๆ หากมี ตรวจสอบว่าคุณได้ทำตามวิธีการใน "การล้างข้อมูล" ซึ่งจะแนะนำคุณเกี่ยวกับวิธีปิดทรัพยากรเพื่อไม่ให้มีการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ ผู้ใช้ใหม่ของ Google Cloud จะมีสิทธิ์เข้าร่วมโปรแกรมทดลองใช้ฟรี$300 USD

เริ่มต้น Cloud Shell

แม้ว่าคุณจะดำเนินการ Google Cloud จากระยะไกลได้จากแล็ปท็อป แต่คุณจะใช้ Google Cloud Shell ซึ่งเป็นสภาพแวดล้อมแบบบรรทัดคำสั่งที่ทำงานในระบบคลาวด์ใน Codelab นี้

จากคอนโซล GCP ให้คลิกไอคอน Cloud Shell บนแถบเครื่องมือด้านขวาบนดังนี้

bce75f34b2c53987.png

การจัดสรรและเชื่อมต่อกับสภาพแวดล้อมนี้ควรใช้เวลาเพียงครู่เดียว เมื่อเสร็จแล้ว คุณจะเห็นข้อมูลต่อไปนี้

f6ef2b5f13479f3a.png

เครื่องเสมือนนี้เต็มไปด้วยเครื่องมือการพัฒนาทั้งหมดที่คุณต้องการ โดยมีไดเรกทอรีหลักขนาด 5 GB ที่ใช้งานได้ต่อเนื่องและทำงานบน Google Cloud ซึ่งช่วยเพิ่มประสิทธิภาพของเครือข่ายและการตรวจสอบสิทธิ์ได้อย่างมาก งานทั้งหมดใน Lab นี้สามารถทำได้โดยใช้เบราว์เซอร์

3. การตั้งค่าห้องทดลอง

สำหรับห้องทดลองนี้ คุณจะใช้โปรเจ็กต์ และสร้าง VPC 2 รายการที่มีซับเน็ตในแต่ละโปรเจ็กต์ คุณจะจองที่อยู่ IP ภายนอก จากนั้นสร้างและกำหนดค่าเกตเวย์ Cloud NAT (ด้วย Cloud Router) อินสแตนซ์ของผู้ผลิต 2 อินสแตนซ์ และอินสแตนซ์ของผู้บริโภค หลังจากตรวจสอบลักษณะการทำงาน Cloud NAT เริ่มต้นแล้ว คุณจะสร้างกฎ Cloud NAT ที่กำหนดเองและตรวจสอบลักษณะการทำงานของกฎดังกล่าว

ภาพรวมสถาปัตยกรรมเครือข่าย:

815147de3de0bd19.png

4. สำรองที่อยู่ IP ภายนอก

เรามาสำรองที่อยู่ IP ภายนอกทั้งหมดที่จะใช้ในห้องทดลองนี้กัน การดำเนินการนี้จะช่วยคุณเขียนกฎ NAT และไฟร์วอลล์ที่เกี่ยวข้องทั้งหมดใน VPC ของผู้บริโภคและผู้ผลิต

จาก Cloud Shell

gcloud compute addresses  create nat-address-1 nat-address-2 nat-address-3 producer-address-1 producer-address-2 --region us-east4

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-1].
Created [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-2].
Created [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-3].
Created [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/producer-address-1].
Created [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/producer-address-2].

ป้อนข้อมูลที่อยู่ IP ที่จองไว้เป็นตัวแปรสภาพแวดล้อม

export natip1=`gcloud compute addresses list --filter name:nat-address-1 --format="get(address)"`
export natip2=`gcloud compute addresses list --filter name:nat-address-2 --format="get(address)"`
export natip3=`gcloud compute addresses list --filter name:nat-address-3 --format="get(address)"`
export producerip1=`gcloud compute addresses list --filter name:producer-address-1 --format="get(address)"`
export producerip2=`gcloud compute addresses list --filter name:producer-address-2 --format="get(address)"`

คาดหวังว่าไม่พบผลลัพธ์ แต่เพื่อยืนยันว่าที่อยู่ได้รับการป้อนข้อมูลอย่างถูกต้อง ลองแสดงผลค่าของตัวแปรสภาพแวดล้อมทั้งหมด

$ env | egrep '^(nat|producer)ip[1-3]'

เอาต์พุต:

producerip1=<Actual Producer IP 1>
producerip2=<Actual Producer IP 2>
natip1=<NAT IP 1>
natip2=<NAT IP 2>
natip3=<NAT IP 3>

5. การตั้งค่า VPC และอินสแตนซ์ของผู้ผลิต

ต่อไปนี้เราจะสร้างทรัพยากรสำหรับทรัพยากรของผู้ผลิต อินสแตนซ์ที่ทำงานใน VPC ของผู้ผลิตจะเสนอบริการด้านอินเทอร์เน็ตโดยใช้ IP สาธารณะ 2 รายการ "manufacturers-address-1" และ "ผู้ผลิต-ที่อยู่-2" ที่ใช้เวลาเพียง 2 นาที

ก่อนอื่น ให้สร้าง VPC จาก Cloud Shell

gcloud compute networks create producer-vpc --subnet-mode custom

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/global/networks/producer-vpc].
NAME      SUBNET_MODE  BGP_ROUTING_MODE  IPV4_RANGE  GATEWAY_IPV4
producer-vpc  CUSTOM       REGIONAL

Instances on this network will not be reachable until firewall rules
are created. As an example, you can allow all internal traffic between
instances as well as SSH, RDP, and ICMP by running:

$ gcloud compute firewall-rules create <FIREWALL_NAME> --network producer-vpc --allow tcp,udp,icmp --source-ranges <IP_RANGE>
$ gcloud compute firewall-rules create <FIREWALL_NAME> --network producer-vpc --allow tcp:22,tcp:3389,icmp

ต่อไป ให้สร้างซับเน็ตใน us-east4 จาก Cloud Shell

gcloud compute networks subnets create producer-e4 \
   --network producer-vpc --range 10.0.0.0/24 --region us-east4

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/regions/us-east4/subnetworks/producer-e4].
NAME              REGION       NETWORK       RANGE
producer-e4       us-east4  producer-vpc  10.0.0.0/24

ถัดไป ให้สร้างไฟร์วอลล์ VPC เพื่ออนุญาตให้ที่อยู่ IP ของ NAT เข้าถึงอินสแตนซ์ของผู้ผลิตบนพอร์ต 8080 ได้

สำหรับกฎแรกจาก Cloud Shell ให้ทำดังนี้

gcloud compute firewall-rules create producer-allow-8080 \
  --network producer-vpc --allow tcp:8080 \
  --source-ranges $natip1,$natip2,$natip3

เอาต์พุต:

Creating firewall...⠹Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/global/firewalls/producer-allow-8080].
Creating firewall...done.
NAME                 NETWORK       DIRECTION  PRIORITY  ALLOW     DENY  DISABLED
producer-allow-8080  producer-vpc  INGRESS    1000      tcp:8080        False

ขั้นตอนถัดไปคือการสร้างอินสแตนซ์ของผู้ผลิต 2 รายการ

อินสแตนซ์ของผู้ผลิตจะเรียกใช้บริการ Echo ของ IP ในคอนเทนเนอร์ Docker Hub ที่พร้อมใช้งานใน Docker Hub (ซอร์สโค้ดจะอยู่ในที่เก็บ GitHub ของผู้เขียนบริการ

เราจะใช้ฟีเจอร์การติดตั้งใช้งานคอนเทนเนอร์บน Compute Engine เพื่อจัดสรรอินสแตนซ์ด้วยซอฟต์แวร์ที่จำเป็นทั้งหมดอย่างรวดเร็ว

เราจะจัดสรรแต่ละอินสแตนซ์ด้วยที่อยู่ IP ที่จองไว้ที่แตกต่างกันเพื่อให้เขียนกฎ NAT ได้

สร้างอินสแตนซ์แรก จาก Cloud Shell

gcloud compute instances create-with-container producer-instance-1 \
--zone=us-east4-a --machine-type=e2-medium \
--network-interface=address=producer-address-1,network-tier=PREMIUM,subnet=producer-e4 \
--container-image=mpolden/echoip --container-restart-policy=always

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/zones/us-east4-a/instances/producer-instance-1].
NAME                 ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
producer-instance-1  us-east4-a  e2-medium                  10.0.0.2     <producer IP 1>  RUNNING

จากนั้นสร้างอินสแตนซ์ที่ 2 จาก Cloud Shell

gcloud compute instances create-with-container producer-instance-2 \
 --zone=us-east4-a --machine-type=e2-medium \
--network-interface=address=producer-address-2,network-tier=PREMIUM,subnet=producer-e4 \
--container-image=mpolden/echoip --container-restart-policy=always

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/zones/us-east4-a/instances/producer-instance-2].
NAME                 ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
producer-instance-2  us-east4-a  e2-medium                  10.0.0.3     <producer IP 2>  RUNNING

6. ตั้งค่า VPC ของผู้บริโภค, Cloud NAT และอินสแตนซ์

ตอนนี้คุณสร้างบริการผู้ผลิตแล้ว ก็ถึงเวลาสร้าง VPC สำหรับผู้บริโภคและเกตเวย์ Cloud NAT

หลังจากสร้าง VPC และซับเน็ต เราจะเพิ่มกฎไฟร์วอลล์ขาเข้าแบบง่ายเพื่ออนุญาต IAP สำหรับช่วง IP ของแหล่งที่มา TCP ซึ่งจะทำให้เรา SSH ไปยังอินสแตนซ์ของผู้บริโภคได้โดยตรงโดยใช้ gcloud

จากนั้นเราจะสร้างเกตเวย์ Cloud NAT อย่างง่ายในโหมดการจัดสรรด้วยตนเองและที่อยู่ "nat-address-1" ที่สงวนไว้ ที่เกี่ยวข้อง ในส่วนต่อๆ มาของ Codelab เราจะอัปเดตการกำหนดค่าของเกตเวย์เพื่อเพิ่มกฎที่กำหนดเอง

ก่อนอื่น ให้สร้าง VPC จาก Cloud Shell

gcloud compute networks create consumer-vpc --subnet-mode custom

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/global/networks/consumer-vpc].
NAME      SUBNET_MODE  BGP_ROUTING_MODE  IPV4_RANGE  GATEWAY_IPV4
consumer-vpc  CUSTOM       REGIONAL

Instances on this network will not be reachable until firewall rules
are created. As an example, you can allow all internal traffic between
instances as well as SSH, RDP, and ICMP by running:

$ gcloud compute firewall-rules create <FIREWALL_NAME> --network consumer-vpc --allow tcp,udp,icmp --source-ranges <IP_RANGE>
$ gcloud compute firewall-rules create <FIREWALL_NAME> --network producer-vpc --allow tcp:22,tcp:3389,icmp

ต่อไป ให้สร้างซับเน็ตใน us-east4 จาก Cloud Shell

gcloud compute networks subnets create consumer-e4 \
   --network consumer-vpc --range 10.0.0.0/24 --region us-east4

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/regions/us-east4/subnetworks/consumer-e4].
NAME              REGION       NETWORK       RANGE
consumer-e4       us-east4  consumer-vpc  10.0.0.0/24

ถัดไป ให้สร้างกฎไฟร์วอลล์ VPC เพื่อให้ที่อยู่ของช่วง IAP เข้าถึงอินสแตนซ์ของผู้บริโภคบนพอร์ต 22 ได้

สำหรับกฎไฟร์วอลล์รายการแรก ให้เรียกใช้รายการต่อไปนี้จาก Cloud Shell

gcloud compute firewall-rules create consumer-allow-iap \
  --network consumer-vpc --allow tcp:22 \
  --source-ranges 35.235.240.0/20

เอาต์พุต:

Creating firewall...⠹Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/global/firewalls/consumer-allow-iap].
Creating firewall...done.
NAME                 NETWORK       DIRECTION  PRIORITY  ALLOW     DENY  DISABLED
consumer-allow-iap  consumer-vpc  INGRESS    1000      tcp:22        False

ก่อนที่จะสร้างเกตเวย์ NAT เราต้องสร้างอินสแตนซ์ Cloud Router ก่อน (เราใช้หมายเลข ASN ส่วนตัว แต่ไม่เกี่ยวข้องกับกิจกรรมของห้องทดลองนี้) จาก Cloud Shell

gcloud compute routers create consumer-cr \
--region=us-east4 --network=consumer-vpc \
 --asn=65501

เอาต์พุต:

Creating router [consumer-cr]...done.
NAME         REGION       NETWORK
consumer-cr  us-east4  consumer-vpc

จากนั้นสร้างอินสแตนซ์เกตเวย์ NAT จาก Cloud Shell

gcloud compute routers nats create consumer-nat-gw \
    --router=consumer-cr \
    --router-region=us-east4 \
    --nat-all-subnet-ip-ranges \
    --nat-external-ip-pool=nat-address-1

เอาต์พุต:

Creating NAT [consumer-nat-gw] in router [consumer-cr]...done.

สร้างอินสแตนซ์ทดสอบของผู้บริโภค เราจะป้อนข้อมูล IP ผู้ผลิตที่จองไว้ที่นี่เพื่อให้อ้างอิงถึงภายในอินสแตนซ์ได้ในภายหลัง จาก Cloud Shell

gcloud compute instances create consumer-instance --zone=us-east4-a \
--machine-type=e2-medium --network-interface=subnet=consumer-e4,no-address \
--metadata=producer-service-ip1=$producerip1,producer-service-ip2=$producerip2

เอาต์พุต:

Created [https://www.googleapis.com/compute/v1/projects/<Project-ID>/zones/us-east4-a/instances/consumer-instance].
NAME               ZONE           MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP  STATUS
consumer-instance  us-east4-a  e2-medium                  10.0.0.2                  RUNNING

7. ยืนยันลักษณะการทำงานเริ่มต้นของ Cloud NAT

ณ จุดนี้ อินสแตนซ์ของผู้บริโภคจะใช้ลักษณะการทำงานเริ่มต้นของ Cloud NAT ซึ่งใช้ IP "nat-address-1" ที่สงวนไว้เดียวกัน สำหรับสื่อสารกับที่อยู่ภายนอกทั้งหมด

มาตรวจสอบลักษณะการทำงานนี้ก่อนใช้งานฟีเจอร์กฎ NAT ใหม่ใน Cloud NAT กัน

SSH ไปยังอินสแตนซ์ของผู้บริโภค จาก Cloud Shell

gcloud compute ssh consumer-instance --zone=us-east4-a

ตอนนี้คุณควรอยู่ใน Shell ของอินสแตนซ์แล้ว

ตัวอย่างเอาต์พุต (เอาต์พุตแบบเต็มถูกตัดเพื่อความกระชับ)

No zone specified. Using zone [us-east4-a] for instance: [consumer-instance].
External IP address was not found; defaulting to using IAP tunneling.
...
...
<username>@consumer-instance:~$

จากภายในอินสแตนซ์ของผู้บริโภค ก่อนอื่นให้ดึงข้อมูลทั้ง IP ผู้ผลิตและป้อนข้อมูลเป็นตัวแปรสภาพแวดล้อม

export producerip1=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/producer-service-ip1" -H "Metadata-Flavor: Google"`

export producerip2=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/producer-service-ip2" -H "Metadata-Flavor: Google"`

จากนั้นลองเปลี่ยนเส้นทางไปยังอินสแตนซ์ของผู้ผลิตทั้ง 2 รายการและสังเกตที่อยู่ IP ต้นทางที่แสดงผล

<username>@consumer-instance:~$ curl http://$producerip1:8080
34.136.8.83
<username>@consumer-instance:~$ curl http://$producerip2:8080
34.136.8.83

คุณควรเห็นที่อยู่ IP เดียวกันแสดงขึ้นสำหรับปลายทางทั้ง 2 เครื่อง ซึ่งเท่ากับค่าของ IP ที่สงวนไว้ภายนอก "nat-address-1"

ในทำนองเดียวกัน Curl ไปยังบริการตัวสะท้อน IP ภายนอกควรแสดง IP เดียวกัน ตัวอย่างเช่น

<username>@consumer-instance:~$ curl http://ifconfig.co
34.136.8.83
<username>@consumer-instance:~$ curl http://ifconfig.me
34.136.8.83
<username>@consumer-instance:~$ curl http://ip.fyr.io
34.136.8.83

ออกจากเซสชัน SSH ของอินสแตนซ์ในตอนนี้ เราจะ SSH กลับหลังจากที่เรากำหนดค่ากฎ NAT แล้ว

8. สร้างกฎ Cloud NAT

กฎ NAT จะเขียนโดยใช้ไวยากรณ์ Common Expression Language โปรดดูข้อมูลเพิ่มเติมเกี่ยวกับภาษาของนิพจน์กฎที่หัวข้อภาษาของนิพจน์กฎ

คุณเพิ่มกฎ NAT ไปยังเกตเวย์ NAT ที่มีอยู่ได้โดยใช้คำสั่ง gcloud ด้วยเช่นกัน เราจะสำรวจทั้ง 2 ตัวเลือกในการสร้างกฎ Cloud NAT

ก่อนอื่น ให้สร้างไฟล์ YAML ของกฎ NAT

จาก Cloud Shell

export projectid=`gcloud config get-value project`

cat <<EOF >natrulesfile.txt
rules:
 - ruleNumber: 100
   match: destination.ip == '$producerip2'
   action:
     sourceNatActiveIps:
     -  /projects/$projectid/regions/us-east4/addresses/nat-address-2
EOF

จากนั้นอัปเดตเกตเวย์ NAT ที่มีอยู่โดยใช้ไฟล์กฎนี้ จาก Cloud Shell

gcloud alpha compute routers nats update consumer-nat-gw \
    --router=consumer-cr \
    --rules=natrulesfile.txt \
    --router-region=us-east4

คุณควรได้ผลลัพธ์ต่อไปนี้

Updating nat [consumer-nat-gw] in router [consumer-cr]...done.

ตรวจสอบว่ากำหนดค่ากฎเรียบร้อยแล้ว จาก Cloud Shell

gcloud alpha compute routers nats rules list \
--nat=consumer-nat-gw --router=consumer-cr \
--router-region=us-east4

คุณควรได้ผลลัพธ์ต่อไปนี้

RULE_NUMBER  MATCH
100          destination.ip == '35.192.142.134'

มาลองสร้างกฎเดิมอีกครั้งโดยใช้คำสั่ง gcloud เท่านั้นกัน ลบกฎที่มีอยู่ออกก่อน จาก Cloud Shell

gcloud alpha compute routers nats rules delete 100 \
--nat=consumer-nat-gw --router=consumer-cr \
--router-region=us-east4 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Updated [https://www.googleapis.com/compute/alpha/projects/ghaleb-s2/regions/us-east4/routers/consumer-cr]

จากนั้นสร้างกฎอีกครั้งโดยใช้คำสั่ง gcloud นี้ จาก Cloud Shell

gcloud alpha compute routers nats rules create 100 \
 --match='destination.ip == "'$producerip2'"' \
--source-nat-active-ips=nat-address-2 --nat=consumer-nat-gw \
 --router=consumer-cr --router-region=us-east4

คุณควรได้ผลลัพธ์ต่อไปนี้

Creating Rule [100] in NAT [consumer-nat-gw]...done.

ทำซ้ำคำสั่งก่อนหน้าเพื่อยืนยันว่าสร้างกฎสำเร็จแล้ว ในครั้งนี้เราจะเพิ่มสวิตช์การจัดรูปแบบ YAML เพื่อดูรายละเอียดทั้งหมดของกฎ

จาก Cloud Shell

gcloud alpha compute routers nats rules list\
 --nat=consumer-nat-gw --router=consumer-cr \
--router-region=us-east4  --format=yaml

คุณควรได้ผลลัพธ์ต่อไปนี้

---
action:
  sourceNatActiveIps:
  - https://www.googleapis.com/compute/alpha/projects/<Project-ID>/regions/us-east4/addresses/nat-address-2
match: destination.ip == <actual IP for producer-IP 2>
ruleNumber: 100

สุดท้ายนี้ ให้สังเกตว่าขณะนี้ทั้ง "nat-address1" และ "nat-address-2" ที่อยู่ภายนอกจะแสดงเป็น "IN_USE" หากต้องการดู ให้เรียกใช้คำสั่งนี้จาก Cloud Shell

$ gcloud compute addresses list

คุณจะได้รับผลลัพธ์ต่อไปนี้ (ที่อยู่ IP จริงควรตรงกับที่อยู่ที่คุณจองไว้) :

NAME                ADDRESS/RANGE   TYPE      PURPOSE  NETWORK  REGION       SUBNET  STATUS
nat-address-1       34.136.8.83     EXTERNAL                    us-east4          IN_USE
nat-address-2       34.70.137.35    EXTERNAL                    us-east4          IN_USE
nat-address-3       34.135.103.88   EXTERNAL                    us-east4          RESERVED
producer-address-1  34.66.0.105     EXTERNAL                    us-east4          IN_USE
producer-address-2  35.192.142.134  EXTERNAL                    us-east4          IN_USE

9. ยืนยันการทำงานของกฎ Cloud NAT

ในขั้นตอนนี้ อินสแตนซ์ของผู้บริโภคควรใช้กฎ Cloud NAT ที่สร้างขึ้นเพื่อใช้ nat-address-2 ในการสื่อสารกับ Producer-address-2

มาตรวจสอบลักษณะการทำงานนี้กัน SSH ไปยังอินสแตนซ์ของผู้บริโภค จาก Cloud Shell

gcloud compute ssh consumer-instance --zone=us-east4-a

ตอนนี้คุณควรอยู่ใน Shell ของอินสแตนซ์แล้ว

ตัวอย่างเอาต์พุต (เอาต์พุตแบบเต็มถูกตัดเพื่อความกระชับ)

No zone specified. Using zone [us-east4-a] for instance: [consumer-instance].
External IP address was not found; defaulting to using IAP tunneling.
...
...
<username>@consumer-instance:~$

จากภายในอินสแตนซ์ของผู้บริโภค ก่อนอื่นให้ดึงข้อมูลทั้ง IP ผู้ผลิตและป้อนข้อมูลเป็นตัวแปรสภาพแวดล้อม

export producerip1=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/producer-service-ip1" -H "Metadata-Flavor: Google"`

export producerip2=`curl -s "http://metadata.google.internal/computeMetadata/v1/instance/attributes/producer-service-ip2" -H "Metadata-Flavor: Google"`

จากนั้นลองขดตัวไปยังอินสแตนซ์ของผู้ผลิตทั้ง 2 อินสแตนซ์และสังเกตที่อยู่ IP ต้นทางที่แสดงผล

<username>@consumer-instance:~$ curl http://$producerip1:8080
34.136.8.83
<username>@consumer-instance:~$ curl http://$producerip2:8080
34.70.137.35

ตอนนี้คุณควรจะเห็นที่อยู่ IP ที่ต่างกันสำหรับปลายทางทั้ง 2 เครื่อง ที่อยู่ IP แรกควรจะเหมือนกับลักษณะการทำงานเริ่มต้น ที่อยู่ IP ที่ 2 ควรเท่ากับ "nat-address-2" หลังจากเพิ่มกฎ NAT ใหม่

การเปลี่ยนเส้นทางไปยังบริการตัวสะท้อน IP ภายนอกควรแสดง IP เดียวกันกับการทำงานเริ่มต้น เช่น

<username>@consumer-instance:~$ curl http://ifconfig.co
34.136.8.83
<username>@consumer-instance:~$ curl http://ifconfig.me
34.136.8.83
<username>@consumer-instance:~$ curl http://ip.fyr.io
34.136.8.83

ออกจากเซสชัน SSH ของอินสแตนซ์ในตอนนี้ เราจะ SSH กลับเพื่อทดสอบการระบายที่อยู่

10. อัปเดตและ ลบกฎ Cloud NAT

คุณอัปเดตกฎ Cloud NAT ที่มีอยู่ได้ ตัวอย่างเช่น คุณสามารถเชื่อมโยงที่อยู่ IP ใหม่และระบายที่อยู่ IP เดิมที่เชื่อมโยงกับกฎที่มีอยู่ออกไป

ลองอัปเดตไฟล์กฎ NAT ดังนี้

จาก Cloud Shell

export projectid=`gcloud config get-value project`

cat <<EOF >natrulesfile.txt
rules:
 - ruleNumber: 100
   match: destination.ip == '$producerip2'
   action:
     sourceNatDrainIps:
     -  /projects/$projectid/regions/us-east4/addresses/nat-address-2
     sourceNatActiveIps:
     -  /projects/$projectid/regions/us-east4/addresses/nat-address-3
EOF

สิ่งที่ไฟล์ใหม่นี้ทำในขณะนี้คือวาง "nat-address-2" ในสภาวะที่ประสิทธิภาพหมด แล้วเพิ่ม "nat-address-3" ในสถานะทำงานอยู่ ซึ่งจะทำให้การเชื่อมต่อที่มีอยู่ที่ใช้ nat-address-2 ยุติได้อย่างไม่มีปัญหา ขณะสร้างการเชื่อมต่อใหม่โดยใช้ nat-address-3 เท่านั้น

จากนั้นอัปเดตเกตเวย์ NAT ที่มีอยู่โดยใช้ไฟล์กฎนี้ จาก Cloud Shell

gcloud alpha compute routers nats update consumer-nat-gw \
    --router=consumer-cr \
    --rules=natrulesfile.txt \
    --router-region=us-east4

คุณควรได้ผลลัพธ์ต่อไปนี้

Updating nat [consumer-nat-gw] in router [consumer-cr]...done.

ตรวจสอบว่ากำหนดค่ากฎเรียบร้อยแล้ว จาก Cloud Shell

gcloud alpha compute routers nats rules list \
--nat=consumer-nat-gw --router=consumer-cr \
--router-region=us-east4 --format=yaml

คุณควรได้ผลลัพธ์ต่อไปนี้

---
action:
  sourceNatActiveIps:
  - https://www.googleapis.com/compute/alpha/projects/ghaleb-s2/regions/us-east4/addresses/nat-address-3
  sourceNatDrainIps:
  - https://www.googleapis.com/compute/alpha/projects/ghaleb-s2/regions/us-east4/addresses/nat-address-2
match: destination.ip == '35.192.142.134'
ruleNumber: 100

สังเกตว่า "nat-address-2" ถูกทำให้เข้าสู่สถานะ "ระบายน้ำแล้ว" เราขอให้ไว้เป็นการดำเนินการเพื่อยืนยันว่าการเชื่อมต่อใหม่จาก VPC ของผู้บริโภคในตอนนี้ใช้ IP ของ NAT ที่ถูกต้องแล้ว

สุดท้าย ให้ลบกฎ NAT ออกจากเกตเวย์ Cloud NAT และเปลี่ยนกลับเป็นลักษณะการทำงานเริ่มต้น คุณใช้คำสั่ง gcloud ต่อไปนี้ได้ จาก Cloud Shell

gcloud alpha compute routers nats rules delete 100 \
 --nat=consumer-nat-gw --router=consumer-cr \
 --router-region=us-east4 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Updated [https://www.googleapis.com/compute/alpha/projects/ghaleb-s2/regions/us-east4/routers/consumer-cr]

หากต้องการยืนยันว่าไม่มีกฎ NAT ให้ใช้คำสั่งอธิบายเกตเวย์ NAT

gcloud alpha compute routers nats describe consumer-nat-gw \
 --router=consumer-cr --router-region=us-east4

คุณควรได้ผลลัพธ์ต่อไปนี้

enableEndpointIndependentMapping: false
name: consumer-nat-gw
natIpAllocateOption: MANUAL_ONLY
natIps:
- https://www.googleapis.com/compute/alpha/projects/ghaleb-s2/regions/us-east4/addresses/nat-address-1
sourceSubnetworkIpRangesToNat: ALL_SUBNETWORKS_ALL_IP_RANGES

โปรดสังเกตที่ไม่มี "กฎ" ดังนี้ ใน YAML เอาต์พุต ไม่พบการกำหนดค่ากฎ NAT

11. ขั้นตอนการทำความสะอาด

คุณควรลบทรัพยากรทั้งหมดที่เชื่อมโยงกับ Codelab เพื่อหลีกเลี่ยงการเรียกเก็บเงินซ้ำ

ขั้นแรกให้ลบอินสแตนซ์ทั้งหมดก่อน

จาก Cloud Shell

gcloud compute instances delete consumer-instance \
producer-instance-1 producer-instance-2 \
 --zone us-east4-a --quiet

ผลลัพธ์ที่ต้องการ :

Deleted [https://www.googleapis.com/compute/v1/projects/<Project Id>/zones/us-east4-a/instances/consumer-instance].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project Id>/zones/us-east4-a/instances/producer-instance-1].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project Id>/zones/us-east4-a/instances/producer-instance-2].

จากนั้นให้ลบ Cloud Router จาก Cloud Shell

gcloud compute routers delete consumer-cr \
--region us-east4 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/routers/consumer-cr].

ปล่อยที่อยู่ IP ภายนอกทั้งหมด จาก Cloud Shell

gcloud compute addresses delete nat-address-1 \
 nat-address-2 nat-address-3 producer-address-1 \
producer-address-2 --region us-east4 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-1].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-2].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/nat-address-3].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/producer-address-1].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/addresses/producer-address-2].

ลบกฎไฟร์วอลล์ VPC จาก Cloud Shell

gcloud compute firewall-rules delete consumer-allow-iap \
 producer-allow-8080 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/global/firewalls/consumer-allow-iap].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/global/firewalls/producer-allow-8080].

ลบเครือข่ายย่อย จาก Cloud Shell

gcloud compute networks subnets delete consumer-e4 \
producer-e4 --region=us-east4 --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/subnetworks/consumer-e4].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/regions/us-east4/subnetworks/producer-e4].

สุดท้ายให้ลบ VPC จาก Cloud Shell

gcloud compute networks delete consumer-vpc \
producer-vpc --quiet

คุณควรได้ผลลัพธ์ต่อไปนี้

Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/global/networks/consumer-vpc].
Deleted [https://www.googleapis.com/compute/v1/projects/<Project ID>/global/networks/producer-vpc].

12. ยินดีด้วย

คุณทำภารกิจ Cloud NAT Rules Lab เสร็จสมบูรณ์แล้ว

สิ่งที่คุณได้พูดคุยกัน

  • วิธีตั้งค่าเกตเวย์ Cloud NAT เพื่อเตรียมพร้อมสำหรับกฎ NAT
  • วิธีออกแบบกฎ NAT โดยใช้ Common Expression Language (CEL)
  • วิธีสร้างกฎ NAT และแนบกับเกตเวย์ NAT
  • วิธีทดสอบกฎ NAT จากอินสแตนซ์
  • วิธีอัปเดตกฎของเกตเวย์ NAT
  • วิธีลบกฎ NAT และเปลี่ยนกลับเป็นลักษณะการทำงานเริ่มต้นของ Cloud NAT

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

  • ทดสอบการสร้างกฎ NAT ที่ซับซ้อนขึ้นอย่างเช่นตัวอย่างนี้
  • สำรวจการระบายที่อยู่ IP ของ NAT และสังเกตผลกระทบจากการเชื่อมต่อ
  • ดูข้อมูลเพิ่มเติมเกี่ยวกับการสร้างเครือข่ายบน Google Cloud Platform

©Google, Inc. หรือบริษัทในเครือ สงวนลิขสิทธิ์ ห้ามเผยแพร่