บทแนะนำนี้แสดงวิธีจัดทำเว็บแอปพลิเคชันในอิมเมจคอนเทนเนอร์ Docker และเรียกใช้อิมเมจคอนเทนเนอร์นั้นในคลัสเตอร์ Google Kubernetes Engine (GKE) จากนั้น คุณปรับใช้เว็บแอปพลิเคชันเป็นชุดของแบบจำลองที่สมดุลกับภาระงานที่สามารถปรับขนาดตามความต้องการของผู้ใช้ของคุณ ##วัตถุประสงค์ - บรรจุเว็บแอปพลิเคชันตัวอย่างลงในอิมเมจ Docker - อัปโหลดอิมเมจ Docker ไปยัง Artifact Registry - สร้างคลัสเตอร์ GKE - ปรับใช้แอปตัวอย่างกับคลัสเตอร์ - จัดการการปรับขนาดอัตโนมัติสำหรับการปรับใช้ - เปิดเผยแอปตัวอย่างไปยังอินเทอร์เน็ต - ปรับใช้แอปตัวอย่างเวอร์ชันใหม่ ## ค่าใช้จ่าย บทแนะนำนี้ใช้องค์ประกอบที่เรียกเก็บเงินได้ต่อไปนี้ของ Google Cloud: ในการสร้างประมาณการค่าใช้จ่ายตามการใช้งานที่คาดการณ์ไว้ของคุณ ใช้เครื่องคำนวณราคา เมื่อคุณจบบทแนะนำนี้ คุณสามารถหลีกเลี่ยงการเรียกเก็บเงินต่อเนื่องได้โดยการลบทรัพยากรที่คุณสร้างขึ้น สำหรับข้อมูลเพิ่มเติม โปรดดูที่ การล้างข้อมูล ## ก่อนที่คุณจะเริ่มทำตามขั้นตอนต่อไปนี้เพื่อเปิดใช้งาน Kubernetes Engine API: - ลงชื่อเข้าใช้บัญชี Google Cloud ของคุณ หากคุณเพิ่งเริ่มใช้ Google Cloud ให้สร้างบัญชีเพื่อประเมินประสิทธิภาพของผลิตภัณฑ์ของเราในสถานการณ์จริง ลูกค้าใหม่ยังได้รับเครดิตฟรีมูลค่า $300 เพื่อเรียกใช้ ทดสอบ และปรับใช้เวิร์กโหลด - ใน Google Cloud Console ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud - ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ระบบคลาวด์แล้ว เรียนรู้วิธีตรวจสอบว่ามีการเปิดใช้งานการเรียกเก็บเงินในโครงการหรือไม่ - เปิดใช้งาน Artifact Registry และ Google Kubernetes Engine API - ใน Google Cloud Console ในหน้าตัวเลือกโปรเจ็กต์ ให้เลือกหรือสร้างโปรเจ็กต์ Google Cloud - ตรวจสอบว่าได้เปิดใช้การเรียกเก็บเงินสำหรับโปรเจ็กต์ระบบคลาวด์แล้ว เรียนรู้วิธีตรวจสอบว่ามีการเปิดใช้งานการเรียกเก็บเงินในโครงการหรือไม่ - เปิดใช้งาน Artifact Registry และ Google Kubernetes Engine API ตัวเลือก A: ใช้ Cloud Shell คุณสามารถทำตามบทช่วยสอนนี้ได้โดยใช้ Cloud Shell ซึ่งมาพร้อมกับ ติดตั้งมาพร้อมกับ จีคลาวด์, นักเทียบท่าและ ใช้เครื่องมือบรรทัดคำสั่ง kubectl ในบทช่วยสอนนี้ หากคุณใช้ Cloud Shell คุณไม่จำเป็นต้องติดตั้งสิ่งเหล่านี้ เครื่องมือบรรทัดคำสั่งบนเวิร์กสเตชันของคุณ หากต้องการใช้ Cloud Shell: - ไปที่คอนโซล Google Cloud คลิก เปิดใช้งาน Cloud Shell ปุ่มที่ด้านบนของหน้าต่างคอนโซล Google Cloud เซสชัน Cloud Shell จะเปิดขึ้นภายในเฟรมใหม่ที่ด้านล่างของ Google Cloud Console และแสดงพรอมต์บรรทัดคำสั่ง ตัวเลือก B: ใช้เครื่องมือบรรทัดคำสั่งในเครื่อง หากคุณต้องการทำตามบทช่วยสอนนี้บนเวิร์กสเตชันของคุณ ให้ทำตามขั้นตอนเหล่านี้เพื่อติดตั้งเครื่องมือที่จำเป็น ติดตั้ง Google Cloud CLI ใช้ gcloud CLI ติดตั้งเครื่องมือบรรทัดคำสั่ง Kubernetes kubectlis ใช้เพื่อสื่อสารกับ Kubernetes ซึ่งเป็นระบบการประสานคลัสเตอร์ของคลัสเตอร์ GKE: ส่วนประกอบ gcloud ติดตั้ง kubectl ติดตั้ง Docker Community Edition (CE) บนเวิร์กสเตชันของคุณ คุณใช้สิ่งนี้เพื่อสร้างอิมเมจคอนเทนเนอร์สำหรับแอปพลิเคชัน ติดตั้งเครื่องมือควบคุมแหล่งที่มาของ Git เพื่อดึงแอปพลิเคชันตัวอย่างจาก GitHub ## สร้างที่เก็บ ในบทแนะนำสอนการใช้งานนี้ คุณจะจัดเก็บอิมเมจไว้ใน Artifact Registry และปรับใช้ จากรีจิสทรี Artifact Registry เป็นรีจีสทรีคอนเทนเนอร์ที่แนะนำ Google คลาวด์ สำหรับการเริ่มต้นอย่างรวดเร็วนี้ คุณจะสร้างที่เก็บชื่อ สวัสดี repo ตั้ง PROJECT_IDตัวแปรสภาพแวดล้อมสำหรับรหัสโครงการ Google Cloud ของคุณ ( ). คุณจะใช้ตัวแปรสภาพแวดล้อมนี้เมื่อคุณสร้างอิมเมจคอนเทนเนอร์และส่งไปยังที่เก็บของคุณ PROJECT_ID ส่งออก PROJECT_ID= PROJECT_ID ยืนยันว่า ตัวแปร PROJECT_IDenvironment มีค่าที่ถูกต้อง: สะท้อน $PROJECT_ID ตั้งค่า ID โครงการของคุณสำหรับ Google Cloud CLI: gcloud config ตั้งค่าโปรเจ็กต์ $PROJECT_ID เอาท์พุต: อัปเดตพร็อพเพอร์ตี้ [คอร์/โปรเจ็กต์] สร้าง hello-reporepository ด้วยคำสั่งต่อไปนี้: ที่เก็บสิ่งประดิษฐ์ gcloud สร้าง hello-repo \ --repository-format=docker \ --location= REGION\ --description="ที่เก็บนักเทียบท่า"แทนที่ ด้วยขอบเขตสำหรับที่เก็บ เช่น ภูมิภาค เรา-ตะวันตก1. หากต้องการดูรายการตำแหน่งที่มีอยู่ ให้รันคำสั่ง: รายการตำแหน่งสิ่งประดิษฐ์ gcloud ## สร้างอิมเมจคอนเทนเนอร์ ในบทช่วยสอนนี้ คุณจะปรับใช้เว็บตัวอย่าง แอปพลิเคชันที่เรียกว่า สวัสดีแอปเว็บเซิร์ฟเวอร์ที่เขียนขึ้น ใน Go ที่ตอบสนองต่อคำขอทั้งหมดด้วยข้อความ สวัสดีชาวโลก! บนพอร์ต 8080 GKE ยอมรับอิมเมจ Docker เป็นรูปแบบการทำให้แอปพลิเคชันใช้งานได้ ก่อนปรับใช้ สวัสดีแอป GKE คุณต้องทำแพ็คเกจ เดอะ ซอร์สโค้ดของแอพ hello เป็นอิมเมจ Docker ในการสร้างอิมเมจ Docker คุณต้องมีซอร์สโค้ดและไฟล์ Docker Dockerfile มีคำแนะนำเกี่ยวกับวิธีสร้างอิมเมจ ดาวน์โหลด hello-appsource code และ Dockerfile โดยเรียกใช้คำสั่งต่อไปนี้: git โคลน httpsgithub.com/GoogleCloudPlatform/kubernetes-engine-samples cd kubernetes-engine-samples/hello-app สร้างและแท็กอิมเมจ Docker สำหรับ สวัสดีแอป: นักเทียบท่า build -t REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 คำสั่งนี้สั่งให้ Docker สร้างอิมเมจโดยใช้ไฟล์ Dockerfile ในไดเร็กทอรีปัจจุบัน บันทึกลงในสภาพแวดล้อมภายในเครื่องของคุณ และแท็กด้วยชื่อ เช่น us-west1-docker.pkg.dev/my-project/hello-repo/hello-app:v1 ภาพจะถูกส่งไปยัง Artifact Registry ในหัวข้อถัดไป - เดอะ PROJECT_IDvariable เชื่อมโยงอิมเมจคอนเทนเนอร์กับ hello-reporepository ในโครงการ Google Cloud ของคุณ - เดอะ us-west1-docker.pkg.devprefix อ้างถึง Artifact Registry ซึ่งเป็นโฮสต์ระดับภูมิภาคสำหรับพื้นที่เก็บข้อมูลของคุณ - เดอะ เรียกใช้ คำสั่ง docker images เพื่อยืนยันว่า build สำเร็จ: ภาพนักเทียบท่า เอาท์พุต: รหัสรูปภาพแท็กที่เก็บสร้างขนาด us-west1-docker.pkg.dev/my-project/hello-repo/hello-app v1 25cfadb1bf28 10 วินาทีที่แล้ว 54 MB ## เรียกใช้คอนเทนเนอร์ของคุณในเครื่อง (ไม่บังคับ) ทดสอบอิมเมจคอนเทนเนอร์ของคุณโดยใช้เครื่องมือ Docker ในเครื่องของคุณ: นักเทียบท่ารัน --rm -p 8080:8080 REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 หากคุณใช้ Cloud Shell ให้คลิก ปุ่มดูตัวอย่างเว็บ แล้วเลือก หมายเลขพอร์ต 8080 GKE เปิดตัวอย่าง URL บนบริการพร็อกซีในหน้าต่างเบราว์เซอร์ใหม่ มิฉะนั้น ให้เปิดหน้าต่างเทอร์มินัลใหม่ (หรือแท็บ Cloud Shell) และเรียกใช้คำสั่งต่อไปนี้เพื่อตรวจสอบว่าคอนเทนเนอร์ทำงานและตอบกลับคำขอด้วย "Hello, World ขด httplocalhost:8080 หลังจากที่คุณเห็นการตอบกลับสำเร็จ คุณสามารถกดคอนเทนเนอร์ลงได้โดยการกด Ctrl+Cin แท็บที่ คำสั่ง docker run กำลังทำงานอยู่ ## พุชอิมเมจ Docker ไปยัง Artifact Registry คุณต้องอัปโหลดอิมเมจคอนเทนเนอร์ไปยังรีจิสทรีเพื่อให้คลัสเตอร์ GKE ของคุณดาวน์โหลดและเรียกใช้อิมเมจคอนเทนเนอร์ได้ ในบทช่วยสอนนี้ คุณจะจัดเก็บคอนเทนเนอร์ของคุณใน Artifact Registry กำหนดค่าเครื่องมือบรรทัดคำสั่ง Docker เพื่อรับรองความถูกต้องของ Artifact Registry: gcloud auth กำหนดค่านักเทียบท่า REGION-docker.pkg.dev พุชอิมเมจ Docker ที่คุณเพิ่งสร้างไปยังที่เก็บ: ดันนักเทียบท่า REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 ## การสร้างคลัสเตอร์ GKE เมื่อเก็บอิมเมจ Docker ไว้ใน Artifact Registry แล้ว ให้สร้าง GKE กลุ่ม วิ่ง สวัสดีแอป คลัสเตอร์ GKE ประกอบด้วยพูลของอินสแตนซ์ Compute Engine VM เรียกใช้ Kubernetes ซึ่งเป็นโอเพ่นซอร์สคลัสเตอร์ Orchestration ระบบที่ขับเคลื่อน GKE คลาวด์เชลล์ ตั้งค่าโซนหรือภูมิภาคของ Compute Engine ขึ้นอยู่กับโหมดการทำงานที่คุณเลือกใช้ใน GKE ให้ระบุโซนหรือภูมิภาคเริ่มต้น หากคุณใช้โหมดมาตรฐาน คลัสเตอร์ของคุณจะเป็นโซน (สำหรับบทช่วยสอนนี้) ดังนั้นให้ตั้งค่าโซนการคำนวณเริ่มต้นของคุณ หากคุณใช้โหมด Autopilot คลัสเตอร์ของคุณจะเป็นภูมิภาค ดังนั้นให้ตั้งค่าภูมิภาคการคำนวณเริ่มต้นของคุณ เลือกโซนหรือภูมิภาคที่ใกล้กับที่เก็บ Artifact Registry ที่คุณสร้างขึ้นมากที่สุด Standardcluster เช่น เรา-west1-a: gcloud config ตั้งค่าการคำนวณ/โซน COMPUTE_ZONE Autopilotcluster เช่น เรา-ตะวันตก1: gcloud config ตั้งค่าการคำนวณ/ภูมิภาค คอมพิวเตอร์_ภูมิภาค - สร้างคลัสเตอร์ชื่อ สวัสดีคลัสเตอร์: คลัสเตอร์มาตรฐาน: คลัสเตอร์คอนเทนเนอร์ gcloud สร้างสวัสดีคลัสเตอร์ Autopilotcluster: คลัสเตอร์คอนเทนเนอร์ gcloud สร้างอัตโนมัติสวัสดีคลัสเตอร์ การสร้างและตรวจสอบสถานะของคลัสเตอร์ GKE จะใช้เวลาไม่กี่นาที - หลังจากคำสั่งเสร็จสิ้น ให้รันคำสั่งต่อไปนี้เพื่อดูโหนดสามโหนดของคลัสเตอร์: kubectl รับโหนด เอาท์พุต: ชื่อ สถานะ บทบาท รุ่นอายุ gke-hello-cluster-default-pool-229c0700-cbtd Ready 92s v1.18.12-gke.1210 gke-hello-cluster-default-pool-229c0700-fc5j Ready 91s v1.18.12-gke.1210 gke-hello-cluster-default-pool-229c0700-n9l7 Ready 92s v1.18.12-gke.1210 Console Go to the Google Kubernetes Enginepage in the Google Cloud console Go to Google Kubernetes Engine Click Create Choose Standard or Autopilot mode and click Configure In the Namefield, enter the name hello-cluster Select a zone or region: Standardcluster: Under Location type, select Zonaland then select a Compute Engine zone from the Zonedrop-down list, such as us-west1-a Autopilotcluster: Select a Compute Engine region from the Regiondrop-down list, such as us-west1 - Click Create. This creates a GKE cluster Wait for the cluster to be created. When the cluster is ready, a green check mark appears next to the cluster name ## Deploying the sample app to GKE You are now ready to deploy the Docker image you built to your GKE cluster Kubernetes represents applications as Pods, which are scalable units holding one or more containers. The Pod is the smallest deployable unit in Kubernetes. Usually, you deploy Pods as a set of replicas that can be scaled and distributed together across your cluster. One way to deploy a set of replicas is through a Kubernetes Deployment In this section, you create a Kubernetes Deployment to run hello-app on your cluster. This Deployment has replicas (Pods). One Deployment Pod contains only one container: the hello-app Docker image You also create a HorizontalPodAutoscaler resource that scales the number of Pods from 3 to a number between 1 and 5, based on CPU load Cloud Shell Ensure that you are connected to your GKE cluster gcloud container clusters get-credentials hello-cluster --zone COMPUTE_ZONE Create a Kubernetes Deployment for your hello-appDocker image kubectl create deployment hello-app --image= REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 Set the baseline number of Deployment replicas to 3 kubectl scale deployment hello-app --replicas=3 Create a HorizontalPodAutoscalerresource for your Deployment kubectl autoscale deployment hello-app --cpu-percent=80 --min=1 --max=5 To see the Pods created, run the following command: kubectl get pods Output: NAME READY STATUS RESTARTS AGE hello-app-784d7569bc-hgmpx 1/1 Running 0 10s hello-app-784d7569bc-jfkz5 1/1 Running 0 10s hello-app-784d7569bc-mnrrl 1/1 Running 0 15s Console Go to the Workloadspage in the Google Cloud console Click Deploy In the Specify containersection, select Existing container image In the Image pathfield, click Select In the Select container imagepane, select the hello-appimage you pushed to Artifact Registry and click Select In the Containersection, click Done, then click Continue In the Configurationsection, under Labels, enter appfor Keyand hello-appfor Value Under Configuration YAML, click View YAML. This opens a YAML configuration file representing the two Kubernetes API resources about to be deployed into your cluster: one Deployment, and one HorizontalPodAutoscalerfor that Deployment Click Close, then click Deploy When the Deployment Pods are ready, the Deployment detailspage opens Under Managed pods, note the three running Pods for the hello-appDeployment ## Exposing the sample app to the internet While Pods do have individually-assigned IP addresses, those IPs can only be reached from inside your cluster. Also, GKE Pods are designed to be ephemeral, starting or stopping based on scaling needs. And when a Pod crashes due to an error, GKE automatically redeploys that Pod, assigning a new Pod IP address each time What this means is that for any Deployment, the set of IP addresses corresponding to the active set of Pods is dynamic. We need a way to 1) group Pods together into one static hostname, and 2) expose a group of Pods outside the cluster, to the internet Kubernetes Services solve for both of these problems Services group Pods into one static IP address, reachable from any Pod inside the cluster GKE also assigns a DNS hostname to that static IP. For example, hello-app.default.svc.cluster.local The default Service type in GKE is called ClusterIP, where the Service gets an IP address reachable only from inside the cluster To expose a Kubernetes Service outside the cluster, create a Service of type LoadBalancer This type of Service spawns an External Load Balancer IP for a set of Pods, reachable through the internet In this section, you expose the hello-app Deployment to the internet using a Service of type LoadBalancer Cloud Shell Use the kubectl exposecommand to generate a Kubernetes Service for the hello-appdeployment: kubectl expose deployment hello-app --name=hello-app-service --type=LoadBalancer --port 80 --target-port 8080 Here, the --portflag specifies the port number configured on the Load Balancer, and the --target-portflag specifies the port number that the hello-appcontainer is listening on Run the following command to get the Service details for hello-app-service: kubectl get service Output: NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE hello-app-service 10.3.251.122 203.0.113.0 80:30877/TCP 10s Copy the EXTERNAL_IPaddress to the clipboard (for instance: 203.0.113.0) Console Go to the Workloadspage in the Google Cloud console Click hello-app From the Deployment details page, click Actions > Expose In the Exposedialog, set the Target portto 8080. This is the port the hello-appcontainer listens on From the Service typedrop-down list, select Load balancer Click Exposeto create a Kubernetes Service for hello-app When the Load Balancer is ready, the Service detailspage opens Scroll down to the External endpointsfield, and copy the IP address Now that the hello-app Pods are exposed to the internet through a Kubernetes Service, you can open a new browser tab, and navigate to the Service IP address you copied to the clipboard. A Hello, World! message appears, along with a Hostname field. The Hostname corresponds to one of the three hello-app Pods serving your HTTP request to your browser ## Deploying a new version of the sample app In this section, you upgrade hello-app to a new version by building and deploying a new Docker image to your GKE cluster GKE's rolling update feature lets you update your Deployments without downtime. During a rolling update, your GKE cluster incrementally replaces the existing hello-app Pods with Pods containing the Docker image for the new version During the update, your load balancer service routes traffic only into available Pods Return to Cloud Shell, where you have cloned the hello app source code and Dockerfile. Update the function hello()in the main.gofile to report the new version 2.0.0 Build and tag a new hello-appDocker image docker build -t REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v2 Push the image to Artifact Registry docker push REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v2 Now you're ready to update your hello-app Kubernetes Deployment to use a new Docker image Cloud Shell Apply a rolling update to the existing hello-appDeployment with an image update using the kubectl set imagecommand: kubectl set image deployment/hello-app hello-app= REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v2 Watch the running Pods running the v1image stop, and new Pods running the v2image start watch kubectl get pods Output: NAME READY STATUS RESTARTS AGE hello-app-89dc45f48-5bzqp 1/1 Running 0 2m42s hello-app-89dc45f48-scm66 1/1 Running 0 2m40s In a separate tab, navigate again to the hello-app-serviceExternal IP. You should now see the Versionset to 2.0.0 Console Go to the Workloadspage in the Google Cloud console Click hello-app On the Deployment detailspage, click Actions > Rolling update In the Rolling updatedialog, set the Image of hello-appfield to REGION-docker.pkg.dev/ PROJECT_ID/hello-repo/hello-app:v2 Click Update On the Deployment detailspage, inspect the Active Revisionssection. You should now see two Revisions, 1 and 2. Revision 1 corresponds to the initial Deployment you created earlier. Revision 2 is the rolling update you just started After a few moments, refresh the page. Under Managed pods, all of the replicas of hello-appnow correspond to Revision 2 In a separate tab, navigate again to the Service IP address you copied. The Versionshould be 2.0.0 ## 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 Delete the Service:This deallocates the Cloud Load Balancer created for your Service: kubectl delete service hello-app-service Delete the cluster:This deletes the resources that make up the cluster, such as the compute instances, disks, and network resources: gcloud container clusters delete hello-cluster --zone COMPUTE_ZONE Delete your container images:This deletes the Docker images you pushed to Artifact Registry gcloud artifacts docker images delete REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 \ --delete-tags --quiet gcloud artifacts docker images delete \ REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v2 \ --delete-tags --quiet ## What's next Learn about Pricing for GKE and use the Pricing Calculator to estimate costs Read the Load Balancers tutorial, which demonstrates advanced load balancing configurations for web applications Configure a static IP and domain name for your application Explore other Kubernetes Engine tutorials Explore reference architectures, diagrams, tutorials, and best practices about Google Cloud. Take a look at our Cloud Architecture Center ## Try it for yourself If you're new to Google Cloud, create an account to evaluate how GKE performs in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.Try GKE free