เอกสารนี้มีไว้สำหรับสถาปนิกและผู้ที่ทำงานในทีมปฏิบัติการและบริหาร เอกสารนี้จะอธิบายรูปแบบตัวอย่างที่คุณใช้สำหรับการปรับใช้ของคุณเองใน Google Cloud ในรูปแบบนี้ Cloud DNS กำหนดเส้นทางการรับส่งข้อมูลไปยังอินสแตนซ์ Compute Engine ในกลุ่มอินสแตนซ์ที่ได้รับการจัดการที่ให้บริการเนื้อหา ในกรณีที่ไฟดับ คุณจะต้องอัปเดตโซน Cloud DNS และเฟลโอเวอร์ไปยังไซต์คงที่ใน Cloud Storage เพื่อให้บทช่วยสอนนี้สมบูรณ์ คุณต้องมีชื่อโดเมนที่จดทะเบียนซึ่งคุณควบคุมและต้องการใช้กับเอกสารนี้ ในการปรับใช้จริง เว็บไซต์ของคุณน่าจะรวมไฟล์และโค้ดแอปพลิเคชันเพิ่มเติมบนเครื่องเสมือน (VM) กลุ่มอินสแตนซ์ที่มีการจัดการของคุณมากกว่าที่แสดงในเอกสารนี้ จากนั้น Cloud Storage จะโฮสต์เวอร์ชันคงที่ที่จำกัดมากขึ้นซึ่งมีฟังก์ชันการทำงานเพียงเล็กน้อย ในสถานการณ์เฟลโอเวอร์ที่อบอุ่น ผู้ใช้จะเห็นเว็บไซต์ที่จำกัดนี้จนกว่ากลุ่มอินสแตนซ์ที่ได้รับการจัดการจะกู้คืนและสามารถรองรับการรับส่งข้อมูลสำหรับประสบการณ์เว็บไซต์เต็มรูปแบบ ในบทช่วยสอนนี้ คุณจะปรับใช้ทรัพยากรเพื่อสร้างสภาพแวดล้อมดังที่แสดงในรูปภาพต่อไปนี้: เมื่อคุณต้องการเฟลโอเวอร์ คุณจะต้องอัปเดตการกำหนดค่า Cloud DNS เพื่อกำหนดเส้นทางการรับส่งข้อมูลไปยัง Cloud Storage ดังที่แสดงในรูปภาพต่อไปนี้: รูปแบบเฟลโอเวอร์ที่อบอุ่นนี้จะช่วยรักษาสมดุลต้นทุนในการเรียกใช้กลุ่มอินสแตนซ์ที่ได้รับการจัดการอื่นในภูมิภาคอื่นที่คุณใช้เมื่อภูมิภาคหลักล้มเหลวเท่านั้น ค่าใช้จ่ายของไซต์คงที่ที่ใช้ Cloud Storage ต่ำกว่าการเรียกใช้กลุ่มอินสแตนซ์ที่ได้รับการจัดการอื่น แต่มีความล่าช้าเล็กน้อยเมื่อคุณอัปเดต Cloud DNS ระหว่างตัวเลือกการโฮสต์ ประสบการณ์เว็บไซต์ที่จำกัดใน Cloud Storage ดีกว่าเว็บไซต์ที่ไม่สามารถใช้งานได้อย่างสมบูรณ์และประสบการณ์ของลูกค้าที่ไม่ดี สำหรับแนวทางอื่นที่ใช้ HTTP(S) Load Balancing ภายนอกแทน Cloud DNS เพื่อควบคุมการเปลี่ยนระบบ โปรดดูปรับใช้เว็บเซิร์ฟเวอร์ warm ที่กู้คืนได้ด้วย Compute Engine และ Cloud Storage รูปแบบนี้มีประโยชน์หากคุณไม่มีหรือไม่ต้องการใช้ Cloud DNS หากต้องการเรียกใช้แอปพลิเคชันที่เชื่อถือได้ใน Google Cloud เราขอแนะนำให้คุณออกแบบโครงสร้างพื้นฐานแอปพลิเคชันของคุณเพื่อรองรับการหยุดทำงาน ขึ้นอยู่กับแอปพลิเคชันและความต้องการทางธุรกิจของคุณ คุณอาจต้องใช้รูปแบบการเฟลโอเวอร์แบบ Cold, Warm Failover หรือรูปแบบการเฟลโอเวอร์แบบ Hot สำหรับข้อมูลเพิ่มเติมเกี่ยวกับวิธีการกำหนดแนวทางที่ดีที่สุดสำหรับแอปพลิเคชันของคุณ โปรดดูคู่มือการวางแผนการกู้คืนความเสียหาย เอกสารนี้ใช้เว็บเซิร์ฟเวอร์ Apache พื้นฐาน แต่แนวทางเดียวกันในการปรับใช้โครงสร้างพื้นฐานนั้นใช้กับสภาพแวดล้อมแอปพลิเคชันอื่น ๆ ที่คุณต้องสร้าง ## วัตถุประสงค์ - สร้างกลุ่มอินสแตนซ์ที่มีการจัดการระดับภูมิภาคด้วยอิมเมจ VM ที่กำหนดเอง - สร้างที่เก็บข้อมูลบนคลาวด์ - สร้างและกำหนดค่าโซน Cloud DNS - ทดสอบความล้มเหลวของเซิร์ฟเวอร์เว็บที่อบอุ่นด้วยบันทึก Cloud DNS ที่อัปเดตแล้ว - ทดสอบการกู้คืนและการย้อนกลับด้วยระเบียน Cloud DNS ที่อัปเดตแล้ว ## ค่าใช้จ่าย บทแนะนำนี้ใช้องค์ประกอบที่เรียกเก็บเงินได้ต่อไปนี้ของ Google Cloud: ในการสร้างประมาณการค่าใช้จ่ายตามการใช้งานที่คาดการณ์ไว้ของคุณ ใช้เครื่องคำนวณราคา ## ก่อนที่จะเริ่ม บางขั้นตอนในเอกสารนี้อาจทำงานไม่ถูกต้อง หากองค์กรของคุณใช้ข้อจำกัดกับสภาพแวดล้อม Google Cloud ในกรณีดังกล่าว คุณอาจทำงานต่างๆ เช่น สร้างที่อยู่ IP สาธารณะหรือคีย์บัญชีบริการไม่ได้ หากคุณส่งคำขอที่ส่งคืนข้อผิดพลาดเกี่ยวกับข้อจำกัด โปรดดูวิธีพัฒนาแอปพลิเคชันในสภาพแวดล้อม Google Cloud ที่มีข้อจำกัด - ลงชื่อเข้าใช้บัญชี Google Cloud ของคุณ หากคุณเพิ่งเริ่มใช้ Google Cloud ให้สร้างบัญชีเพื่อประเมินประสิทธิภาพของผลิตภัณฑ์ของเราในสถานการณ์จริง ลูกค้าใหม่ยังได้รับเครดิตฟรีมูลค่า $300 เพื่อเรียกใช้ ทดสอบ และปรับใช้เวิร์กโหลด - ใน Google Cloud Console ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud - ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ระบบคลาวด์แล้ว เรียนรู้วิธีตรวจสอบว่ามีการเปิดใช้งานการเรียกเก็บเงินในโครงการหรือไม่ - เปิดใช้งาน Compute Engine API - ติดตั้งและเริ่มต้น Google Cloud CLI - ใน Google Cloud Console ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud - ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ระบบคลาวด์แล้ว เรียนรู้วิธีตรวจสอบว่ามีการเปิดใช้งานการเรียกเก็บเงินในโครงการหรือไม่ - เปิดใช้งาน Compute Engine API - ติดตั้งและเริ่มต้น Google Cloud CLI คุณสามารถเรียกใช้ Google Cloud CLI ในคอนโซล Google Cloud โดยไม่ต้องติดตั้ง Google Cloud CLI หากต้องการเรียกใช้ gcloud CLI ใน Google Cloud Console ให้ใช้ Cloud Shell ##เตรียมสิ่งแวดล้อม ในส่วนนี้ คุณจะกำหนดตัวแปรบางอย่างสำหรับชื่อและตำแหน่งทรัพยากรของคุณ คำสั่ง Google Cloud CLI จะใช้ตัวแปรเหล่านี้เมื่อคุณปรับใช้ทรัพยากร ตลอดบทแนะนำนี้ คุณจะป้อนคำสั่งทั้งหมดใน Cloud Shell หรือสภาพแวดล้อมการพัฒนาในเครื่องของคุณ เว้นแต่จะระบุไว้เป็นอย่างอื่น แทนที่ ด้วยรหัสโครงการของคุณเอง หากต้องการ ให้ระบุคำต่อท้ายชื่อของคุณเองสำหรับแหล่งข้อมูลเพื่อช่วยในการค้นหาและระบุตัวตน เช่น PROJECT_ID แอป ระบุสองภูมิภาค เช่น และ เรา-ตะวันตก1 และโซนภายในหนึ่งในภูมิภาคเหล่านั้น เช่น เรา-ตะวันตก2 . โซนนี้กำหนดตำแหน่งที่สร้าง VM พื้นฐานเริ่มต้นที่ใช้สร้างอิมเมจสำหรับกลุ่มอินสแตนซ์ที่มีการจัดการ เรา-ตะวันตก1-ก สุดท้าย ตั้งค่าโดเมนที่ใช้สำหรับเว็บไซต์คงที่ของคุณ เช่น ตัวอย่าง.คอม PROJECT_ID= PROJECT_IDNAME_SUFFIX= appREGION1= us-west1REGION2= us-west2ZONE= us-west1-aDOMAIN= example.com ## สร้าง VPC และซับเน็ต เพื่อให้การเข้าถึงเครือข่ายไปยัง VM คุณต้องสร้าง Virtual Private Cloud (VPC) และซับเน็ต เนื่องจากคุณต้องการกลุ่มอินสแตนซ์ที่มีการจัดการในสองภูมิภาค คุณจึงสร้างหนึ่งเครือข่ายย่อยในแต่ละภูมิภาค สำหรับข้อมูลเพิ่มเติมเกี่ยวกับข้อดีของโหมดเครือข่ายย่อยที่กำหนดเองเพื่อจัดการช่วงที่อยู่ IP ที่ใช้งานในสภาพแวดล้อมของคุณ โปรดดูที่ ใช้เครือข่าย VPC ในโหมดกำหนดเอง สร้าง VPC ด้วยโหมดเครือข่ายย่อยที่กำหนดเอง: เครือข่ายการคำนวณ gcloud สร้างเครือข่าย-$NAME_SUFFIX --subnet-mode=custom ตอนนี้สร้างเครือข่ายย่อยสองเครือข่ายใน VPC ใหม่ หนึ่งเครือข่ายสำหรับแต่ละภูมิภาค กำหนดช่วงที่อยู่ของคุณเอง เช่น และ 10.1.0.0/20 ที่เหมาะกับช่วงเครือข่ายของคุณ: 10.2.0.0/20 เครือข่ายย่อยของเครือข่ายการคำนวณ gcloud สร้าง \ subnet-$NAME_SUFFIX-$REGION1 \ --network=network-$NAME_SUFFIX \ --range= 10.1.0.0/20\ --region=$REGION1 gcloud compute เครือข่ายย่อยสร้าง \ subnet-$NAME_SUFFIX-$REGION2 \ --network=network-$NAME_SUFFIX \ --range= 10.2.0.0/20\ --region=$ ภูมิภาค 2 ## สร้างกฎไฟร์วอลล์ เพื่อให้การรับส่งข้อมูลเครือข่ายไหลอย่างถูกต้องใน VPC ให้ใช้กฎไฟร์วอลล์ สร้างกฎไฟร์วอลล์เพื่ออนุญาตการรับส่งข้อมูลเว็บและการตรวจสอบความสมบูรณ์สำหรับโหลดบาลานเซอร์และกลุ่มอินสแตนซ์ที่มีการจัดการ: กฎไฟร์วอลล์คำนวณ gcloud สร้าง allow-http-$NAME_SUFFIX \ --network=network-$NAME_SUFFIX \ --direction=INGRESS \ --priority=1000 \ --action=ALLOW \ --rules=tcp:80 \ -- source-ranges=0.0.0.0/0 \ --target-tags=http-server gcloud compute firewall-rules create allow-health-check-$NAME_SUFFIX \ --network=network-$NAME_SUFFIX \ --action=allow \ - -direction=ingress \ --source-ranges=130.211.0.0/22,35.191.0.0/16 \ --target-tags=allow-health-check \ --rules=tcp:80 กฎ HTTP อนุญาตให้รับส่งข้อมูลไปยัง VM ใดๆ ที่ ใช้ http-servertag และจากแหล่งใด ๆ ที่ใช้ 0.0.0.0/0ช่วง สำหรับกฎการตรวจสอบความสมบูรณ์ มีการตั้งค่าช่วงเริ่มต้นสำหรับ Google Cloud เพื่อให้แพลตฟอร์มตรวจสอบความสมบูรณ์ของทรัพยากรได้อย่างถูกต้อง ในการอนุญาตการรับส่งข้อมูล SSH สำหรับการกำหนดค่าเริ่มต้นของอิมเมจ VM พื้นฐาน ให้กำหนดขอบเขตกฎไฟร์วอลล์ไปยังสภาพแวดล้อมของคุณโดยใช้ --source-rangeparameter. คุณอาจต้องทำงานร่วมกับทีมเครือข่ายของคุณเพื่อพิจารณาว่าองค์กรของคุณใช้ช่วงแหล่งที่มาใด แทนที่ ด้วยขอบเขตที่อยู่ IP ของคุณเอง: IP_ADDRESS_SCOPE กฎไฟร์วอลล์คำนวณ gcloud สร้าง allow-ssh-$NAME_SUFFIX \ --network=network-$NAME_SUFFIX \ --direction=INGRESS \ --priority=1000 \ --action=ALLOW \ --rules=tcp:22 \ -- ช่วงแหล่งที่มา = IP_ADDRESS_SCOPE หลังจากที่คุณสร้างกฎไฟร์วอลล์แล้ว ให้ตรวจสอบว่าได้เพิ่มกฎทั้งสามข้อแล้ว: รายการกฎไฟร์วอลล์คำนวณ gcloud \ --project=$PROJECT_ID \ --filter="NETWORK=network-$NAME_SUFFIX"ผลลัพธ์ตัวอย่างต่อไปนี้แสดงให้เห็นว่ากฎสามข้อได้ถูกสร้างขึ้นอย่างถูกต้อง: ลำดับความสำคัญของชื่อเครือข่าย อนุญาตให้อนุญาต-ตรวจสอบสุขภาพ-แอปเครือข่ายแอป INGRESS 1,000 tcp:80 อนุญาต-http-app แอปเครือข่าย INGRESS 1,000 tcp:80 อนุญาต-ssh-app แอปเครือข่าย INGRESS 1,000 tcp:22 ## สร้างและกำหนดค่าอิมเมจ VM พื้นฐาน หากต้องการสร้าง VM ที่เหมือนกันซึ่งคุณปรับใช้โดยไม่มีการกำหนดค่าเพิ่มเติม คุณจะต้องใช้อิมเมจ VM แบบกำหนดเอง อิมเมจนี้บันทึกการกำหนดค่าระบบปฏิบัติการและ Apache และใช้เพื่อสร้าง VM แต่ละรายการในกลุ่มอินสแตนซ์ที่มีการจัดการในขั้นตอนถัดไป บน VM คุณสร้างพื้นฐาน ไฟล์ index.html บนดิสก์ถาวรและ ติดมันไปที่ /var/www/example.com. ไฟล์การกำหนดค่า Apache ที่ /etc/apache2/sites-available/example.com.conf ให้บริการเนื้อหาเว็บจาก ตำแหน่งดิสก์ถาวรที่เมาท์ แผนภาพต่อไปนี้แสดงหน้า HTML พื้นฐานที่ให้บริการโดย Apache ซึ่งจัดเก็บไว้ในดิสก์ถาวร: คุณสร้างสภาพแวดล้อมนี้ในขั้นตอนต่อไปนี้ สร้าง VM ฐานพร้อมดิสก์ถาวรที่แนบ: อินสแตนซ์การประมวลผล gcloud สร้าง vm-base-$NAME_SUFFIX \ --zone=$ZONE \ --machine-type=n1-standard-1 \ --subnet=subnet-$NAME_SUFFIX-$REGION1 \ --tags=http-server \ --image=debian-10-buster-v20210420 \ --image-project=debian-cloud \ --boot-disk-size=10GB \ --boot-disk-type=pd-balanced \ --boot-disk- ชื่ออุปกรณ์=vm-base-$NAME_SUFFIX \ --create-disk=type=pd-ssd,name=disk-base-$NAME_SUFFIX,size=10GB,ชื่ออุปกรณ์=disk-base-$NAME_SUFFIX คุณใช้พารามิเตอร์ที่กำหนดไว้ตอนเริ่มต้นของเอกสารนี้เพื่อตั้งชื่อ VM และเชื่อมต่อกับเครือข่ายย่อยที่ถูกต้อง ชื่อยังถูกกำหนดจากพารามิเตอร์สำหรับดิสก์สำหรับบูตและดิสก์ข้อมูลอีกด้วย หากต้องการติดตั้งและกำหนดค่าเว็บไซต์แบบง่าย ขั้นแรกให้เชื่อมต่อกับ VM ฐานโดยใช้ SSH: gcloud คำนวณ ssh vm-base-$NAME_SUFFIX --zone=$ZONE ในเซสชัน SSH ของคุณไปยัง VM ให้สร้างสคริปต์เพื่อกำหนดค่า VM ในตัวแก้ไขที่คุณเลือก ตัวอย่างต่อไปนี้ใช้ Nano เป็นตัวแก้ไข: นาโนกำหนดค่า-vm. วางสคริปต์การกำหนดค่าต่อไปนี้ลงในไฟล์: ถัง/ทุบตี NAME_SUFFIX= แอป # สร้างไดเรกทอรีสำหรับไฟล์เว็บไซต์พื้นฐาน sudo mkdir -p /var/www/example.com sudo chmod a+w /var/www/example.com sudo chown -R www-data: /var/www/example.com # ค้นหาชื่อดิสก์ จากนั้นฟอร์แมตและติดตั้ง DISK_NAME="google-disk-base-$NAME_SUFFIX"DISK_PATHfind /dev/disk/by-id -name DISK_NAME}"| xargs -Ireadlink -fsudo mkfs.ext4 -m 0 - Elazy_itable_init=0,lazy_journal_init=0,ละทิ้ง $DISK_PATH sudo mount -o ละทิ้ง,ค่าเริ่มต้น $DISK_PATH /var/www/example.com # ติดตั้ง Apache sudo apt-get update&& sudo apt-get -y install apache2 # เขียนไฟล์ HTML พื้นฐานลงในดิสก์ถาวรที่เมาท์ sudo tee -a /var/ www/example.com/index.html >/dev/null EOF' ตัวอย่าง HA/DR

ยินดีต้อนรับสู่เว็บไซต์ Compute Engine ที่มี Warm Failover ไปยัง Cloud Storagep>

*:80> ServerName www.example.com ServerAdmin webmaster@localhost DocumentRoot /var/www/example.com ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined EOF # เปิดใช้งานไฟล์การกำหนดค่า Apache และบริการโหลดซ้ำ sudo a2dissite 000- ค่าเริ่มต้น sudo a2ensite example.com.conf sudo systemctl โหลด apache2 ใหม่ อัพเดต ตัวแปรให้ตรงกับค่าที่ตั้งไว้ตอนต้นของเอกสารนี้ เช่น แอป NAME_SUFFIX เขียนไฟล์และออกจากโปรแกรมแก้ไขของคุณ ตัวอย่างเช่น ในนาโนที่คุณใช้ Ctrl-Oto เขียนไฟล์แล้วออกด้วย Ctrl-X ทำให้สคริปต์การกำหนดค่าสามารถเรียกทำงานได้ จากนั้นรัน: chmod +x กำหนดค่า-vm../configure-vm. ออกจากเซสชัน SSH ไปยัง VM: ออก รับที่อยู่ IP ของ VM และใช้งาน curlเพื่อดูหน้าเว็บพื้นฐาน: curl $(อินสแตนซ์การประมวลผล gcloud อธิบาย vm-base-$NAME_SUFFIX \ --zone $ZONE \ --format="value(networkInterfaces.accessConfigs.[0].natIP เว็บไซต์พื้นฐานถูกส่งคืน ดังที่แสดงในผลลัพธ์ตัวอย่างต่อไปนี้: ตัวอย่าง HA/DR

ยินดีต้อนรับสู่เว็บไซต์ Compute Engine ที่มี Warm Failover ไปยัง Cloud Storagep>

# สร้างอิมเมจ VM พื้นฐาน gcloud compute image สร้าง image-$NAME_SUFFIX \ --source-disk=vm-base-$NAME_SUFFIX \ --source-disk-zone=$ZONE gcloud compute image create image-disk-$NAME_SUFFIX \ - -source-disk=disk-base-$NAME_SUFFIX \ --source-disk-zone=$ZONE # สร้างเทมเพลตอินสแตนซ์ gcloud คำนวณเทมเพลตอินสแตนซ์สร้างเทมเพลต-$NAME_SUFFIX-$REGION1 \ --machine-type=n1-standard- 1 \ --subnet=projects/$PROJECT_ID/regions/$REGION1/subnetworks/subnet-$NAME_SUFFIX-$REGION1 \ --region=$REGION1 \ --tags=http-server \ --metadatastartup-script /bin/bashn 'echo\ UUIDblkid\ -s\ UUID\ -o\ value\ /dev/sdb /var/www/example.com\ ext4\ discard,defaults,nofail\ 0\ 2 ee\ -a\ /etc/fstabn'mount \ -a \ --image=image-$NAME_SUFFIX \ --create-disk=image=image-disk-$NAME_SUFFIX,auto-delete=yes เทมเพลตอินสแตนซ์การคำนวณ gcloud สร้างเทมเพลต-$NAME_SUFFIX-$REGION2 \ --เครื่อง -type=n1-standard-1 \ --subnet=projects/$PROJECT_ID/regions/$REGION2/subnetworks/subnet-$NAME_SUFFIX-$REGION2 \ --region=$REGION2 \ --tags=http-server \ --metadatastartup-script /bin/bashn'echo\ UUIDblkid\ -s\ UUID\ -o\ value\ /dev/sdb /var/www/example.com\ ext4\ ละทิ้งค่าเริ่มต้น ,nofail\ 0\ 2 ee\ -a\ /etc/fstabn'mount\ -a \ --image=image-$NAME_SUFFIX \ --create-disk=image=image-disk-$NAME_SUFFIX,auto-delete=yes # สร้างการตรวจสอบความสมบูรณ์สำหรับอินสแตนซ์ VM การตรวจสอบความสมบูรณ์ของการประมวลผล gcloud สร้าง http http-basic-check-$NAME_SUFFIX \ --พอร์ต 80 # สร้างกลุ่มอินสแตนซ์ที่มีการจัดการ gcloud คำนวณกลุ่มอินสแตนซ์ที่มีการจัดการ สร้างกลุ่มอินสแตนซ์-$NAME_SUFFIX-$REGION1 \ --template=template-$NAME_SUFFIX-$REGION1 \ --size=2 \ --region=$REGION1 \ --health-check=http-basic-check-$NAME_SUFFIX gcloud compute กลุ่มอินสแตนซ์ที่จัดการสร้างกลุ่มอินสแตนซ์ -$NAME_SUFFIX-$REGION2 \ --template=template-$NAME_SUFFIX-$REGION2 \ --size=2 \ --region=$REGION2 \ --health-check=http-basic-check-$NAME_SUFFIX ## สร้างและกำหนดค่าโหลดบาลานเซอร์ สำหรับผู้ใช้ในการเข้าถึงเว็บไซต์ของคุณ คุณต้องอนุญาตการรับส่งข้อมูลผ่านไปยัง VM ที่ทำงานในกลุ่มอินสแตนซ์ที่มีการจัดการ นอกจากนี้ คุณยังต้องการเปลี่ยนเส้นทางการรับส่งข้อมูลไปยัง VM ใหม่โดยอัตโนมัติ หากมีโซนล้มเหลวในกลุ่มอินสแตนซ์ที่มีการจัดการ ในส่วนต่อไปนี้ คุณจะสร้างตัวจัดสรรภาระงาน HTTPS ภายนอกด้วยบริการแบ็กเอนด์สำหรับทราฟฟิก HTTP บนพอร์ต 80 ใช้การตรวจสอบความสมบูรณ์ที่สร้างขึ้นในขั้นตอนก่อนหน้า และแมปที่อยู่ IP ภายนอกผ่านไปยังบริการแบ็กเอนด์ สำหรับข้อมูลเพิ่มเติม โปรดดูวิธีตั้งค่าตัวจัดสรรภาระงาน HTTP ภายนอกอย่างง่าย สร้างและกำหนดค่าโหลดบาลานเซอร์สำหรับแอปพลิเคชันของคุณ: # กำหนดค่ากฎพอร์ตสำหรับพอร์ต HTTP 80 gcloud compute กลุ่มอินสแตนซ์ set-named-ports \ instance-group-$NAME_SUFFIX-$REGION1 \ --named-ports http:80 \ --region $REGION1 gcloud compute instance-groups set- ชื่อพอร์ต \ อินสแตนซ์กลุ่ม-$NAME_SUFFIX-$REGION2 \ --named-ports http:80 \ --region $REGION2 # สร้างบริการแบ็กเอนด์และเพิ่มกลุ่มอินสแตนซ์ที่มีการจัดการเข้าไป บริการแบ็กเอนด์คำนวณ gcloud สร้าง \ web- บริการแบ็กเอนด์-$NAME_SUFFIX \ --protocol=HTTP \ --port-name=http \ --health-checks=http-basic-check-$NAME_SUFFIX \ --global gcloud compute backend-services add-backend \ web- บริการแบ็กเอนด์-$NAME_SUFFIX \ --instance-group=instance-group-$NAME_SUFFIX-$REGION1 \ --instance-group-region=$REGION1 \ --global gcloud compute backend-services add-backend \ web-backend- service-$NAME_SUFFIX \ --instance-group=instance-group-$NAME_SUFFIX-$REGION2 \ --instance-group-region=$REGION2 \ --global # สร้างแมป URL สำหรับบริการแบ็กเอนด์ gcloud compute url-maps create เว็บแผนที่-http-$NAME_SUFFIX \ --default-service web-backend-service-$NAME_SUFFIX # กำหนดค่าการส่งต่อสำหรับการรับส่งข้อมูล HTTP gcloud compute target-http-proxies create \ http-lb-proxy-$NAME_SUFFIX \ --url-map web-map-http- $NAME_SUFFIX กฎการส่งต่อการคำนวณ gcloud สร้าง \ http-content-rule-$NAME_SUFFIX \ --global \ --target-http-proxy=http-lb-proxy-$NAME_SUFFIX \ --ports=80 รับที่อยู่ IP ของกฎการส่งต่อสำหรับการเข้าชมเว็บ: IP_ADDRESSgcloud คำนวณกฎการส่งต่ออธิบาย http-content-rule-$NAME_SUFFIX \ --global \ --format="value(IPAddress ใช้ curl หรือเปิดเว็บเบราว์เซอร์ของคุณ เพื่อดูเว็บไซต์โดยใช้ที่อยู่ IP ของตัวจัดสรรภาระงานจากขั้นตอนก่อนหน้า: ขด $IP_ADDRESS ใช้เวลาสองสามนาทีเพื่อให้โหลดบาลานเซอร์ใช้งานได้เสร็จสิ้นและกำหนดทิศทางทราฟฟิกไปยังแบ็กเอนด์ของคุณได้อย่างถูกต้อง ข้อผิดพลาด HTTP 404 จะถูกส่งกลับหากตัวจัดสรรภาระงานยังคงปรับใช้ หากจำเป็น ให้รอสักครู่แล้วลองเข้าเว็บไซต์อีกครั้ง เว็บไซต์พื้นฐานจะถูกส่งกลับ ดังที่แสดงในเอาต์พุตตัวอย่างต่อไปนี้: ตัวอย่าง HA/DR

ยินดีต้อนรับสู่เว็บไซต์ Compute Engine ที่มี Warm Failover ไปยัง Cloud Storagep>

< index.html HA / DR example

Welcome to a test static web server with warm failover from Cloud Storagep>

example.com Get the details of the Cloud DNS zone: gcloud dns managed-zones describe zone-$NAME_SUFFIX The following example output shows the nameServersfor the zone, such as ns-cloud-b1.googledomains.com kind: dns#managedZone name: zone-app nameServers: - ns-cloud-b1.googledomains.com. - ns-cloud-b2.googledomains.com. - ns-cloud-b3.googledomains.com. - ns-cloud-b4.googledomains.com Cloud DNS must be authoritative for your domain. Create nameserver (NS) records with your domain registrar that point to your Cloud DNS zone. Use the nameserver addresses returned in the previous step For more information and an example using Google Domains, see How to update name servers In your Cloud DNS zone, add a record for wwwusing the load balancer IP address obtained in a previous section: gcloud dns record-sets transaction start \ --zone=zone-$NAME_SUFFIX gcloud dns record-sets transaction add $IP_ADDRESS \ --name=www.$DOMAIN \ --ttl=300 \ --type=A \ --zone=zone-$NAME_SUFFIX This record directs user requests for the website through the load balancer to the managed instance groups. A TTL of 300 seconds is set to reduce the length of time the cached DNS record exists for a user Create a record to be used by the Cloud Storage bucket for the static website: gcloud dns record-sets transaction add c.storage.googleapis.com. \ --name=static-web.$DOMAIN \ --ttl=300 \ --type=CNAME \ --zone=zone-$NAME_SUFFIX This example uses static-webas the subdomain. Leave the c.storage.googleapis.com.Again, a TTL of 300 seconds is set to reduce the length of time the cached DNS record exists for a user Finally,the DNS record additions to the zone: gcloud dns record-sets transaction execute \ --zone=zone-$NAME_SUFFIX ## Verify and test the DNS zone and records Let's review the resource deployments before simulating a zone failure. All of the resources have been created to support the environment, as shown in the following image: - Cloud DNS zone records direct users to the load balancer for distribution across the managed instance group VMs - A Cloud Storage bucket is configured to host static web pages if there's an outage with the managed instance groups - The Cloud DNS zone is configured to use the static site in Cloud Storage, but doesn't currently resolve requests to the storage bucket To view the DNS records and test resolution, you must resolve addresses against the Cloud DNS servers. In production deployments, make sure you test and verify the addresses resolve correctly, then update your own DNS servers to resolve appropriately. This document doesn't detail the steps to update your own DNS servers, only how to verify traffic flows correctly under normal and failover conditions Get the details of the Cloud DNS zone again: gcloud dns managed-zones describe zone-$NAME_SUFFIX The following example output shows the nameServersfor the zone, such as ns-cloud-b1.googledomains.com kind: dns#managedZone name: zone-app nameServers: - ns-cloud-b1.googledomains.com. - ns-cloud-b2.googledomains.com. - ns-cloud-b3.googledomains.com. - ns-cloud-b4.googledomains.com To resolve the wwwrecord for your Cloud DNS zone against one of these name servers, use the digcommand: dig @ns-cloud-b1.googledomains.com www.$DOMAIN This example uses the ns-cloud-b1.googledomains.comnameserver address returned from the previous describecommand. Provide your own nameserver address shown in the output of the previous command The following example output shows that the record resolves to the IP address of the load balancer. If you used this nameserver to access the address, such as using curland the --resolveparameter with the Cloud DNS nameserver, the default page would be displayed from one of the managed instance groups behind the load balancer ;DiG [email protected] www.example.com ; (1 server found);; QUESTION SECTION: ;www.example.com. IN A ;; ANSWER SECTION: www.example.com. 300 IN A 35.227.253.90 Use the digcommand again to verify the DNS record for the static website in Cloud Storage: dig @ns-cloud-b1.googledomains.com static-web.$DOMAIN The following example output shows that the record resolves to Cloud Storage that can serve the static content from the storage bucket: ;DiG [email protected] static-web.example.com ; (1 server found);; QUESTION SECTION: ;static-web.example.com. IN A ;; ANSWER SECTION: static-web.example.com. 300 IN CNAME c.storage.googleapis.com ## Fail over to the Cloud Storage bucket In a production environment, you might get an alert using Cloud Monitoring or other monitoring solution when there's a problem with the managed instance groups. This alert prompts a human to understand the scope of the failure before you update the Cloud DNS records to redirect traffic to the Cloud Storage-hosted static website. An alternative approach is to use your monitoring solution to automatically respond to outages with the managed instance groups When you fail over, Cloud DNS resolves traffic to the Cloud Storage-hosted static website, as shown in the following image: When you or your monitoring solution determine the most appropriate action is to update the Cloud DNS records to direct traffic to Cloud Storage, update the existing DNS A record. In this document, you manually update the Cloud DNS records to redirect traffic to the Cloud Storage-hosted static website To fail over the Cloud DNS records, remove the existing Arecord that resolves to the load balancer: gcloud dns record-sets transaction start \ --zone=zone-$NAME_SUFFIX gcloud dns record-sets transaction remove $IP_ADDRESS \ --name=www.$DOMAIN \ --ttl=300 \ --type=A \ --zone=zone-$NAME_SUFFIX Create a CNAMErecord for wwwthat points to the Cloud Storage-hosted content: gcloud dns record-sets transaction add static-web.$DOMAIN \ --name=www.$DOMAIN. \ --ttl=30 \ --type=CNAME \ --zone=zone-$NAME_SUFFIX the updates to the Cloud DNS zone: gcloud dns record-sets transaction execute \ --zone=zone-$NAME_SUFFIX Use the digcommand to confirm the wwwrecord now resolves to the address of the Cloud Storage static website: dig @ns-cloud-b1.googledomains.com www.$DOMAIN The following example output shows that the www.example.comrecord resolves to the CNAME record of the Cloud Storage static website. Requests to access www.example.comare redirected to the Cloud Storage bucket, which displays the static website: ;DiG [email protected] www.example.com ; (1 server found);; QUESTION SECTION: ;www.example.com. IN A ;; ANSWER SECTION: www.example.com. 30 IN CNAME static-web.example.com. static-web.example.com. 300 IN CNAME c.storage.googleapis.com ## Fail back to the managed instance groups After issues with the managed instance groups are resolved, you can fail back to serving content from the load-balanced managed instance groups by updating the Cloud DNS records again. Again, a human might make this decision using Cloud Monitoring insights for the health of the managed instance groups. Or, you could use automation to respond to the restored health of the managed instance group. In this document, you manually update the Cloud DNS records When you fail back, Cloud DNS resolves traffic to the managed instance groups again, as shown in the following image: Remove the wwwCNAME record that redirects traffic to the Cloud Storage-hosted content: gcloud dns record-sets transaction start \ --zone=zone-$NAME_SUFFIX gcloud dns record-sets transaction remove static-web.$DOMAIN \ --name=www.$DOMAIN \ --ttl=30 \ --type=CNAME \ --zone=zone-$NAME_SUFFIX Add an Arecord to point to the load balancer in front of the managed instance groups again: gcloud dns record-sets transaction add $IP_ADDRESS \ --name=www.$DOMAIN \ --ttl=300 \ --type=A \ --zone=zone-$NAME_SUFFIX the updates to the Cloud DNS zone: gcloud dns record-sets transaction execute \ --zone=zone-$NAME_SUFFIX Use the digcommand one more time to confirm the wwwrecord resolves to the address of the load balancer in front of the managed instance groups again: dig @ns-cloud-b1.googledomains.com www.$DOMAIN The following example output shows that the record resolves to the IP address of the load balancer and traffic would be served from one of the managed instance groups: ;DiG [email protected] www.example.com ; (1 server found);; QUESTION SECTION: ;www.example.com. IN A ;; ANSWER SECTION: www.example.com. 300 IN A 35.227.253.90 ## Clean up To avoid incurring charges to your Google Cloud account for the resources used in this tutorial, either delete the project that contains the resources, or keep the project and delete the individual resources To delete the individual resources created in this document, complete the following steps: Delete the DNS zone and records: touch empty-file gcloud dns record-sets import -z zone-$NAME_SUFFIX \ --delete-all-existing \ empty-file rm empty-file gcloud dns managed-zones delete zone-$NAME_SUFFIX Delete the Cloud Storage bucket: gsutil rm -r gsstatic-web.$DOMAIN Delete the load balancer configuration: gcloud compute forwarding-rules delete \ http-content-rule-$NAME_SUFFIX --global --quiet gcloud compute target-http-proxies delete \ http-lb-proxy-$NAME_SUFFIX --quiet gcloud compute url-maps delete web-map-http-$NAME_SUFFIX --quiet gcloud compute backend-services delete \ web-backend-service-$NAME_SUFFIX --global --quiet Delete the managed instance groups and health check: gcloud compute instance-groups managed delete \ instance-group-$NAME_SUFFIX-$REGION1 \ --region=$REGION1 --quiet gcloud compute instance-groups managed delete \ instance-group-$NAME_SUFFIX-$REGION2 \ --region=$REGION2 --quiet gcloud compute health-checks delete http-basic-check-$NAME_SUFFIX --quiet Delete the instance templates, images, base VM, and persistent disks: gcloud compute instance-templates delete \ template-$NAME_SUFFIX-$REGION1 --quiet gcloud compute instance-templates delete \ template-$NAME_SUFFIX-$REGION2 --quiet gcloud compute images delete image-$NAME_SUFFIX --quiet gcloud compute images delete image-disk-$NAME_SUFFIX --quiet gcloud compute instances delete vm-base-$NAME_SUFFIX \ --zone=$ZONE --quiet Delete the firewall rules gcloud compute firewall-rules delete \ allow-health-check-$NAME_SUFFIX --quiet gcloud compute firewall-rules delete \ allow-ssh-$NAME_SUFFIX --quiet gcloud compute firewall-rules delete \ allow-http-$NAME_SUFFIX --quiet Delete the subnet and VPC gcloud compute networks subnets delete \ subnet-$NAME_SUFFIX-$REGION1 --region=$REGION1 --quiet gcloud compute networks subnets delete \ subnet-$NAME_SUFFIX-$REGION2 --region=$REGION2 --quiet gcloud compute networks delete network-$NAME_SUFFIX --quiet ## What's next - For an alternative approach that uses external HTTP(S) Load Balancing instead of Cloud DNS to control the failover, see Deploy a warm recoverable web server with Compute Engine and Cloud Storage. This pattern is useful if you don't have, or don't want to use, Cloud DNS - To learn how how to determine the best approach for your own applications and which recovery method to use, see the Disaster recovery planning guide - To see other patterns for applications, such as cold and hot failover, see Disaster recovery scenarios for applications - For more ways to handle scale and availability, see the Patterns for scalable and resilient apps - Explore reference architectures, diagrams, tutorials, and best practices about Google Cloud. Take a look at our Cloud Architecture Center.