یہ ٹیوٹوریل آپ کو دکھاتا ہے کہ ڈوکر کنٹینر امیج میں ویب ایپلیکیشن کو کیسے پیک کیا جائے، اور اس کنٹینر کی تصویر کو Google Kubernetes Engine (GKE) کلسٹر پر کیسے چلایا جائے۔ اس کے بعد، آپ ویب ایپلیکیشن کو نقلوں کے بوجھ سے متوازن سیٹ کے طور پر تعینات کرتے ہیں جو آپ کے صارفین کی ضروریات کو پورا کر سکتے ہیں۔ ## مقاصد - ایک نمونہ ویب ایپلیکیشن کو ڈوکر امیج میں پیک کریں۔ - ڈوکر امیج کو آرٹفیکٹ رجسٹری میں اپ لوڈ کریں۔ - ایک GKE کلسٹر بنائیں - نمونہ ایپ کو کلسٹر میں تعینات کریں۔ - تعیناتی کے لیے آٹو اسکیلنگ کا انتظام کریں۔ - نمونہ ایپ کو انٹرنیٹ پر بے نقاب کریں۔ - نمونہ ایپ کا ایک نیا ورژن تعینات کریں۔ ## اخراجات اس ٹیوٹوریل میں گوگل کلاؤڈ کے درج ذیل قابل بل اجزاء استعمال کیے گئے ہیں: آپ کے متوقع استعمال کی بنیاد پر لاگت کا تخمینہ تیار کرنے کے لیے، قیمت کا کیلکولیٹر استعمال کریں۔ جب آپ اس ٹیوٹوریل کو ختم کرتے ہیں، تو آپ اپنے بنائے گئے وسائل کو حذف کرکے مسلسل بلنگ سے بچ سکتے ہیں۔ مزید معلومات کے لیے، کلین اپ دیکھیں ## شروع کرنے سے پہلے Kubernetes Engine API کو فعال کرنے کے لیے درج ذیل اقدامات کریں: - اپنے گوگل کلاؤڈ اکاؤنٹ میں سائن ان کریں۔ اگر آپ گوگل کلاؤڈ میں نئے ہیں تو اس بات کا اندازہ کرنے کے لیے ایک اکاؤنٹ بنائیں کہ حقیقی دنیا کے منظرناموں میں ہماری مصنوعات کیسی کارکردگی دکھاتی ہے۔ نئے صارفین کو کام کے بوجھ کو چلانے، جانچنے اور تعینات کرنے کے لیے $300 مفت کریڈٹس بھی ملتے ہیں۔ - گوگل کلاؤڈ کنسول میں، پروجیکٹ سلیکٹر پیج پر، گوگل کلاؤڈ پروجیکٹ کو منتخب کریں یا تخلیق کریں۔ - یقینی بنائیں کہ آپ کے کلاؤڈ پروجیکٹ کے لیے بلنگ فعال ہے۔ کسی پروجیکٹ پر بلنگ کو فعال کرنے کا طریقہ جانیں۔ - آرٹفیکٹ رجسٹری اور Google Kubernetes Engine APIs کو فعال کریں۔ - گوگل کلاؤڈ کنسول میں، پروجیکٹ سلیکٹر پیج پر، گوگل کلاؤڈ پروجیکٹ کو منتخب کریں یا تخلیق کریں۔ - یقینی بنائیں کہ آپ کے کلاؤڈ پروجیکٹ کے لیے بلنگ فعال ہے۔ کسی پروجیکٹ پر بلنگ کو فعال کرنے کا طریقہ جانیں۔ - آرٹفیکٹ رجسٹری اور Google Kubernetes Engine APIs کو فعال کریں۔ آپشن A: کلاؤڈ شیل استعمال کریں۔ آپ کلاؤڈ شیل کا استعمال کرتے ہوئے اس ٹیوٹوریل کی پیروی کر سکتے ہیں، جو آتا ہے۔ کے ساتھ پہلے سے نصب gcloud ڈاکر، اور kubectl کمانڈ لائن ٹولز استعمال کیے گئے۔ اس ٹیوٹوریل میں اگر آپ کلاؤڈ شیل استعمال کرتے ہیں، تو آپ کو ان کو انسٹال کرنے کی ضرورت نہیں ہے۔ آپ کے ورک سٹیشن پر کمانڈ لائن ٹولز کلاؤڈ شیل استعمال کرنے کے لیے: - گوگل کلاؤڈ کنسول پر جائیں۔ پر کلک کریں۔ کلاؤڈ شیل کو چالو کریں۔ گوگل کلاؤڈ کنسول ونڈو کے اوپری حصے میں بٹن ایک کلاؤڈ شیل سیشن گوگل کلاؤڈ کنسول کے نیچے ایک نئے فریم کے اندر کھلتا ہے اور کمانڈ لائن پرامپٹ دکھاتا ہے۔ اختیار B: مقامی طور پر کمانڈ لائن ٹولز استعمال کریں۔ اگر آپ اپنے ورک سٹیشن پر اس ٹیوٹوریل کی پیروی کرنا پسند کرتے ہیں، تو ضروری ٹولز انسٹال کرنے کے لیے ان اقدامات پر عمل کریں۔ گوگل کلاؤڈ CLI انسٹال کریں۔ gcloud CLI کا استعمال کرتے ہوئے، Kubernetes کمانڈ لائن ٹول انسٹال کریں۔ kubectlis کا استعمال Kubernetes کے ساتھ بات چیت کے لیے کیا جاتا ہے، جو GKE کلسٹرز کا کلسٹر آرکیسٹریشن سسٹم ہے: gcloud اجزاء kubectl انسٹال کرتے ہیں۔ اپنے ورک سٹیشن پر Docker Community Edition (CE) انسٹال کریں۔ آپ اسے ایپلیکیشن کے لیے کنٹینر امیج بنانے کے لیے استعمال کرتے ہیں۔ GitHub سے نمونہ ایپلیکیشن لانے کے لیے Git سورس کنٹرول ٹول انسٹال کریں۔ ## ایک ذخیرہ بنائیں اس ٹیوٹوریل میں، آپ آرٹفیکٹ رجسٹری میں ایک تصویر محفوظ کرتے ہیں اور اسے تعینات کرتے ہیں۔ رجسٹری سے آرٹفیکٹ رجسٹری تجویز کردہ کنٹینر رجسٹری آن ہے۔ گوگل کلاؤڈ۔ اس کوئیک سٹارٹ کے لیے، آپ نام کا ذخیرہ بنائیں گے۔ ہیلو ریپو مقرر PROJECT_IDماحولیاتی متغیر آپ کے گوگل کلاؤڈ پروجیکٹ ID ( )۔ آپ اس ماحولیاتی متغیر کو استعمال کریں گے جب آپ کنٹینر کی تصویر بنائیں گے اور اسے اپنے ذخیرہ میں دھکیلیں گے۔ PROJECT_ID PROJECT_ID= برآمد کریں۔ PROJECT_ID تصدیق کریں کہ PROJECT_IDenvironment متغیر کی صحیح قدر ہے: echo $PROJECT_ID گوگل کلاؤڈ CLI کے لیے اپنی پروجیکٹ ID سیٹ کریں: gcloud config سیٹ پروجیکٹ $PROJECT_ID آؤٹ پٹ: اپ ڈیٹ شدہ پراپرٹی [core/project] بنائیں درج ذیل کمانڈ کے ساتھ ہیلو ریپوزٹری: gcloud نمونے کے ذخیرے ہیلو ریپو بناتے ہیں \ --repository-format=docker \ --location= REGION\ --description="Docker repository"بدل دیں۔ ذخیرے کے لیے ایک علاقے کے ساتھ، جیسے علاقہ us-west1. دستیاب مقامات کی فہرست دیکھنے کے لیے، کمانڈ چلائیں: gcloud نمونے کے مقامات کی فہرست ## کنٹینر کی تصویر بنانا اس ٹیوٹوریل میں، آپ ایک نمونہ ویب لگاتے ہیں۔ درخواست کہا جاتا ہے ہیلو ایپ، ایک ویب سرور لکھا ہوا ہے۔ گو میں جو پیغام کے ساتھ تمام درخواستوں کا جواب دیتا ہے۔ ہیلو، دنیا! پورٹ 8080 پر GKE ڈوکر امیجز کو ایپلیکیشن کی تعیناتی کی شکل کے طور پر قبول کرتا ہے۔ تعینات کرنے سے پہلے Hello-app to GKE، آپ کو پیکیج کرنا ہوگا۔ دی ہیلو ایپ سورس کوڈ بطور ڈاکر امیج ڈوکر امیج بنانے کے لیے، آپ کو سورس کوڈ اور ڈاکر فائل کی ضرورت ہے۔ ایک ڈاکر فائل میں اس بارے میں ہدایات ہوتی ہیں کہ تصویر کیسے بنتی ہے۔ ڈاؤن لوڈ کریں۔ hello-appsource کوڈ اور Dockerfile درج ذیل کمانڈز چلا کر: git clone 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 یہ کمانڈ ڈوکر کو استعمال کرکے تصویر بنانے کی ہدایت کرتی ہے۔ موجودہ ڈائرکٹری میں Dockerfile، اسے اپنے مقامی ماحول میں محفوظ کریں، اور اسے کسی نام کے ساتھ ٹیگ کریں، جیسے us-west1-docker.pkg.dev/my-project/hello-repo/hello-app:v1۔ تصویر کو اگلے حصے میں آرٹیفیکٹ رجسٹری میں دھکیل دیا گیا ہے۔ --.دی PROJECT_ID متغیر کنٹینر کی تصویر کو کے ساتھ منسلک کرتا ہے۔ آپ کے گوگل کلاؤڈ پروجیکٹ میں ہیلو ریپوزٹری --.دی us-west1-docker.pkg.devprefix سے مراد آرٹفیکٹ رجسٹری ہے، آپ کے ریپوزٹری کے لیے علاقائی میزبان --.دی چلائیں docker imagescommand اس بات کی تصدیق کرنے کے لیے کہ تعمیر کامیاب تھی: ڈاکر کی تصاویر آؤٹ پٹ: REPOSITORY TAG IMAGE ID CREATED SIZE us-west1-docker.pkg.dev/my-project/hello-repo/hello-app v1 25cfadb1bf28 10 سیکنڈ پہلے 54 MB ## اپنے کنٹینر کو مقامی طور پر چلانا (اختیاری) اپنے مقامی ڈوکر انجن کا استعمال کرتے ہوئے اپنے کنٹینر کی تصویر کی جانچ کریں: ڈاکر رن --rm -p 8080:8080 REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 اگر آپ کلاؤڈ شیل استعمال کر رہے ہیں تو، پر کلک کریں۔ ویب پیش نظارہ بٹن اور پھر منتخب کریں 8080پورٹ نمبر۔ GKE اپنی پراکسی سروس پر پیش نظارہ URL کو ایک نئی براؤزر ونڈو میں کھولتا ہے۔ دوسری صورت میں، ایک نئی ٹرمینل ونڈو (یا کلاؤڈ شیل ٹیب) کھولیں اور اس بات کی تصدیق کرنے کے لیے درج ذیل کمانڈ کو چلائیں کہ کنٹینر کام کرتا ہے اور "ہیلو، ورلڈ"کے ساتھ درخواستوں کا جواب دیتا ہے۔ curl httplocalhost:8080 ایک کامیاب جواب دیکھنے کے بعد، آپ دبا کر کنٹینر کو نیچے کر دیتے ہیں۔ Ctrl+Cin ٹیب جہاں docker runcommand چل رہا ہے۔ ## ڈوکر امیج کو آرٹیفیکٹ رجسٹری میں دھکیلنا آپ کو کنٹینر کی تصویر کو رجسٹری میں اپ لوڈ کرنا چاہیے تاکہ آپ کا GKE کلسٹر کنٹینر کی تصویر کو ڈاؤن لوڈ اور چلا سکے۔ اس ٹیوٹوریل میں، آپ اپنے کنٹینر کو آرٹفیکٹ رجسٹری میں محفوظ کریں گے۔ آرٹفیکٹ رجسٹری کی توثیق کرنے کے لیے ڈوکر کمانڈ لائن ٹول کو کنفیگر کریں: gcloud auth configure-docker REGION-docker.pkg.dev ڈوکر امیج کو پش کریں جو آپ نے ابھی مخزن میں بنایا ہے: ڈاکر دھکا REGION-docker.pkg.devPROJECT_ID}/hello-repo/hello-app:v1 ## ایک GKE کلسٹر بنانا اب جب کہ ڈوکر امیج آرٹفیکٹ رجسٹری میں محفوظ ہے، ایک GKE بنائیں جھرمٹ دوڑنا ہیلو ایپ۔ ایک GKE کلسٹر کمپیوٹ انجن VM مثالوں کے ایک پول پر مشتمل ہوتا ہے۔ Kubernetes چلا رہا ہے، اوپن سورس کلسٹر آرکیسٹریشن نظام جو GKE کو طاقت دیتا ہے۔ کلاؤڈ شیل اپنا کمپیوٹ انجن زون یا علاقہ سیٹ کریں۔ آپریشن کے موڈ پر منحصر ہے جسے آپ GKE میں استعمال کرنے کا انتخاب کرتے ہیں، پہلے سے طے شدہ زون یا علاقہ کی وضاحت کریں۔ اگر آپ سٹینڈرڈ موڈ استعمال کرتے ہیں، تو آپ کا کلسٹر زونل ہے (اس ٹیوٹوریل کے لیے)، اس لیے اپنا ڈیفالٹ کمپیوٹ زون سیٹ کریں۔ اگر آپ آٹو پائلٹ موڈ استعمال کرتے ہیں، تو آپ کا کلسٹر علاقائی ہے، اس لیے اپنا ڈیفالٹ کمپیوٹ ریجن سیٹ کریں۔ ایک ایسا زون یا علاقہ منتخب کریں جو آپ کی تخلیق کردہ آرٹفیکٹ رجسٹری ریپوزٹری کے قریب ترین ہو۔ سٹینڈرڈ کلسٹر، جیسے us-west1-a: gcloud config سیٹ کمپیوٹ/زون COMPUTE_ZONE آٹو پائلٹ کلسٹر، جیسے us-west1: gcloud config سیٹ کمپیوٹ/ریجن COMPUTE_REGION - نام کا ایک کلسٹر بنائیں ہیلو کلسٹر: سٹینڈرڈ کلسٹر: gcloud کنٹینر کلسٹر ہیلو کلسٹر بناتے ہیں۔ آٹو پائلٹ کلسٹر: gcloud کنٹینر کلسٹرز تخلیق آٹو ہیلو کلسٹر آپ کے GKE کلسٹر کو بننے اور صحت کی جانچ کرنے میں چند منٹ لگتے ہیں۔ - کمانڈ مکمل ہونے کے بعد، کلسٹر کے تین نوڈس کو دیکھنے کے لیے درج ذیل کمانڈ کو چلائیں۔ kubectl نوڈس حاصل کریں آؤٹ پٹ: NAME اسٹیٹس رولز AGE VERSION gke-hello-cluster-default-pool-229c0700-cbtd تیار 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