arrow_back

Getting Started with Google Kubernetes Engine - Windows

Accédez à plus de 700 ateliers et cours

Getting Started with Google Kubernetes Engine - Windows

Atelier 1 heure universal_currency_alt 5 crédits show_chart Débutant
info Cet atelier peut intégrer des outils d'IA pour vous accompagner dans votre apprentissage.
Accédez à plus de 700 ateliers et cours

Overview

In this lab, you deploy a simple Windows app to Docker running on a Compute Engine instance. You will then push the same app into Google Container Registry and deploy it to Google Kubernetes Engine.

Objectives

In this lab, you learn how to perform the following tasks:

  • Create a simple Windows app.
  • Containerize the app using Docker.
  • Create your Kubernetes cluster on Kubernetes Engine.
  • Deploy your app to a pod.
  • Allow external traffic to your pod.

Task 1: Sign in to the Cloud Console

For each lab, you get a new Google Cloud project and set of resources for a fixed time at no cost.

  1. Sign in to Qwiklabs using an incognito window.

  2. Note the lab's access time (for example, 1:15:00), and make sure you can finish within that time.
    There is no pause feature. You can restart if needed, but you have to start at the beginning.

  3. When ready, click Start lab.

  4. Note your lab credentials (Username and Password). You will use them to sign in to the Google Cloud Console.

  5. Click Open Google Console.

  6. Click Use another account and copy/paste credentials for this lab into the prompts.
    If you use other credentials, you'll receive errors or incur charges.

  7. Accept the terms and skip the recovery resource page.

Task 2: Enable Google Container Registry API

  1. On the Google Cloud menu, click Activate Cloud Shell (Activate Cloud Shell) icon then click Continue.

  2. Make sure the Google Container Registry API is enabled, by running the following command:

gcloud services enable containerregistry.googleapis.com
  1. Click Authorize.

Task 3: RDP into the Windows VM

  1. On the Navigation menu (Navigation menu), click Compute Engine > VM instances.

  2. Click the Drop down arrow icon next to RDP button and select Set Windows password.

  3. Copy and save the Username value and click SET.

  4. Copy and save the New Windows Password and click CLOSE.

  5. Use credentials created in previous steps.

Once inside the Windows VM, you'll notice that it's a bare-minimum OS with minimal UI.

In the Windows command prompt (C:\Users\gcpstagingXXXXX_stud>), run the following to see the images that are installed by default:

docker images

(Output):

REPOSITORY TAG IMAGE ID mcr.microsoft.com/windows/servercore ltsc2019 29a2c2cb7e4d

Task 4: Create a Windows container app

For the app inside the Windows container, use an IIS Web Server. IIS has an image for Windows Server 2019. You can use the image as is and it will serve the default IIS page, but for this lab, do something more interesting and have IIS serve a page you define.

  1. Create a folder called my-windows-app and enter into the directory:
mkdir my-windows-app cd my-windows-app
  1. Create folder named content:
mkdir content
  1. Create a file named index.html in the content folder:
call > content\index.html
  1. Edit index.html:
notepad content\index.html
  1. Add the following content to index.html:
<html> <head> <title>Windows containers</title> </head> <body> <p>Windows containers are cool!</p> </body> </html>

Save the index.html. This is the page IIS will serve.

Task 5: Build Docker image

  1. Create a Dockerfile for the Docker image.
call > Dockerfile
  1. Edit the dockerfile:
notepad Dockerfile
  1. Add the following contents in the Dockerfile:
FROM mcr.microsoft.com/windows/servercore/iis:windowsservercore-ltsc2019 RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\* WORKDIR /inetpub/wwwroot COPY content/ .

Save the Dockerfile.

  1. Build the Docker image and tag it with Google Container Registry (GCR) and your project id. This will be useful when we push the image to GCR later (replace [PROJECT_ID] with your Qwiklabs project id):
docker build -t gcr.io/[PROJECT_ID]/iis-site-windows .
  1. Once the Docker image is built, you can see it along with its IIS dependency:
docker images

Sample output:

REPOSITORY TAG gcr.io/[PROJECT_ID]/iis-site-windows latest mcr.microsoft.com/windows/servercore/iis windowsservercore-ltsc2019 mcr.microsoft.com/windows/servercore ltsc2019
  1. Run the following command to run the Windows container from your VM instance:
docker run -d -p 80:80 gcr.io/[PROJECT_ID]/iis-site-windows
  1. Return to the Cloud Console. On the Navigation menu (Navigation menu), click Compute Engine > VM instances.

  2. Open a browser window enter the External IP of the Compute Engine instance to see the app:

GCE_docker_app.png

Task 6: Push container image to Google Container Registry

  1. Configure Docker to point to Google Container Registry (GCR):
gcloud auth configure-docker
  1. When prompted enter Y.

The output should look like this

Adding credentials for all GCR repositories. WARNING: A long list of credential helpers may cause delays running 'docker build'. We recommend passing the registry name to configure only the registry you are using. After update, the following will be written to your Docker config file located at [C:\Users\student_01_9786da300\.docker\config.json]: { "credHelpers": { "gcr.io": "gcloud", "us.gcr.io": "gcloud", "eu.gcr.io": "gcloud", "asia.gcr.io": "gcloud", "staging-k8s.gcr.io": "gcloud", "marketplace.gcr.io": "gcloud" } } Do you want to continue (Y/n)? Y Docker configuration file updated.
  1. Push the image to Container Repository. Remember to replace [PROJECT_ID] with your Qwiklabs project id.
docker push gcr.io/[PROJECT_ID]/iis-site-windows
  1. If you return to the Cloud Console and go Container Repository in the Navigation Menu you can see the image

image-in-gcr.png

Click Check my progress to verify the objective. Push Container image to Container Registry

Task 7: Start a Kubernetes Engine cluster

  1. In Cloud Console, on the Google Cloud menu, click Activate Cloud Shell (Activate Cloud Shell) icon then click Continue.

  2. Run the following command to set your environment variables for the project and zones and to set them as default:

export PROJECT_ID=[PROJECT_ID] gcloud config set project ${PROJECT_ID} export ZONE=us-central1-a gcloud config set compute/zone ${ZONE}
  1. Start a Kubernetes cluster managed by Kubernetes Engine. Name the cluster win-cluster and configure it to run 2 nodes:
export CLUSTER_NAME=win-cluster gcloud container clusters create ${CLUSTER_NAME} \ --enable-ip-alias \ --num-nodes=2

It takes several minutes to create a cluster as Kubernetes Engine provisions virtual machines for you.

Eventually, the cluster should be created.

Example output

Creating cluster win-cluster in us-central1-a... Cluster is being health-checked (master is healthy)...done. Created [https://container.googleapis.com/v1/projects/qwiklabs-gcp-03-52c197b33d26/zones/us-central1-a/clusters/win-cluster]. To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1-a/win-cluster?project=qwiklabs-gcp-03-52c197b33d26 kubeconfig entry generated for win-cluster. NAME: win-cluster LOCATION: us-central1-a MASTER_VERSION: 1.22.11-gke.400 MASTER_IP: 34.123.19.209 MACHINE_TYPE: e2-medium NODE_VERSION: 1.22.11-gke.400 NUM_NODES: 2 STATUS: RUNNING
  1. After the cluster is created, check your installed version of Kubernetes using the kubectl version command:
kubectl version

The gcloud container clusters create command automatically authenticated kubectl for you.

  1. View your running nodes in the Cloud Console. On the Navigation menu (Navigation menu), click Compute Engine > VM Instances.

    Your Kubernetes cluster is now ready for use.

Click Check my progress to verify the objective. Start a Kubernetes Engine cluster

Task 8: Add Windows Node to cluster

  1. Once we have the basic GKE cluster, we can go ahead and add a Windows pool for Windows nodes to it:
gcloud container node-pools create windows-node-pool \ --cluster=${CLUSTER_NAME} \ --image-type=WINDOWS_LTSC_CONTAINERD \ --no-enable-autoupgrade \ --machine-type=n1-standard-2

For Windows Server containers in GKE, you're already licensed for underlying Windows host VMs – containers need no additional licensing.

In the end, the GKE cluster is ready with 2 Linux nodes and 3 Windows nodes:

Click Check my progress to verify the objective. Add Windows Node to cluster

Task 9: Verify cluster initialization

Before running our Windows container as a pod on GKE, make sure the cluster is initialized.

  1. In Cloud Shell run the following command:
gcloud container clusters get-credentials ${CLUSTER_NAME}

kubectl is now setup to manage our cluster.

  1. Before using the cluster, wait for several seconds until windows.config.common-webhooks.networking.gke.io is created. This webhook adds scheduling tolerations to Pods created with the kubernetes.io/os: windows (or beta.kubernetes.io/os: windows) node selector to ensure they are allowed to run on Windows Server nodes. It also validates the Pod to ensure that it only uses features supported on Windows.
kubectl get mutatingwebhookconfigurations

Output:

neg-annotation.config.common-webhooks.networking.gke.io 1 14m pod-ready.config.common-webhooks.networking.gke.io 1 14m windows.config.common-webhooks.networking.gke.io 1 16s

Task 10: Run Windows container as a pod in GKE

  1. Create and open a file called iis-site-windows.yaml with nano using the following command:
nano iis-site-windows.yaml
  1. Once nano has opened, paste following into the file (Remember to replace [PROJECT_ID] with your Qwiklabs project id):
apiVersion: apps/v1 kind: Deployment metadata: name: iis-site-windows labels: app: iis-site-windows spec: replicas: 2 selector: matchLabels: app: iis-site-windows template: metadata: labels: app: iis-site-windows spec: nodeSelector: kubernetes.io/os: windows containers: - name: iis-site-windows image: gcr.io/[PROJECT_ID]/iis-site-windows ports: - containerPort: 80
  1. Press Ctrl+O, and then press Enter to save your edited file.

  2. Press Ctrl+X to exit the nano text editor.

  3. Create a deployment for the cluster with the following command:

kubectl apply -f iis-site-windows.yaml
  1. Check the deployment with the following command:
kubectl get deployment,pods

After a few minutes, you should see the deployment created and pods running:

NAME READY UP-TO-DATE AVAILABLE AGE deployment.extensions/iis-site-windows 2/2 2 2 3m4s NAME READY STATUS RESTARTS AGE pod/iis-site-windows-5b6dfdf64b-k5b6r 1/1 Running 0 3m4s pod/iis-site-windows-5b6dfdf64b-msr9s 1/1 Running 0

Task 11: Create a Kubernetes Service

  1. To make pods accessible to the outside world, we need to create a Kubernetes service with LoadBalancer type:
kubectl expose deployment iis-site-windows --type="LoadBalancer"
  1. In a few minutes, you should see a new service with an external IP:
kubectl get service

You should see an output like this:

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) iis-site-windows LoadBalancer 10.107.11.34 104.155.45.254 80:30035/TCP
  1. Open a browser window and go to the external IP of the load balancer, you will see the app:

GKE_cluster.png

Click Check my progress to verify the objective. Create a Kubernetes Service

Congratulations!

You have successfully configured and deployed a GKE cluster running a Windows Server workload.

In this lab, you learned how to perform the following tasks:

  • Create a simple Windows app.
  • Containerize the app using Docker.
  • Create your Kubernetes cluster on Kubernetes Engine.
  • Deploy your app to a pod.
  • Allow external traffic to your pod.

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.

Avant de commencer

  1. Les ateliers créent un projet Google Cloud et des ressources pour une durée déterminée.
  2. Les ateliers doivent être effectués dans le délai imparti et ne peuvent pas être mis en pause. Si vous quittez l'atelier, vous devrez le recommencer depuis le début.
  3. En haut à gauche de l'écran, cliquez sur Démarrer l'atelier pour commencer.

Utilisez la navigation privée

  1. Copiez le nom d'utilisateur et le mot de passe fournis pour l'atelier
  2. Cliquez sur Ouvrir la console en navigation privée

Connectez-vous à la console

  1. Connectez-vous à l'aide des identifiants qui vous ont été attribués pour l'atelier. L'utilisation d'autres identifiants peut entraîner des erreurs ou des frais.
  2. Acceptez les conditions d'utilisation et ignorez la page concernant les ressources de récupération des données.
  3. Ne cliquez pas sur Terminer l'atelier, à moins que vous n'ayez terminé l'atelier ou que vous ne vouliez le recommencer, car cela effacera votre travail et supprimera le projet.

Ce contenu n'est pas disponible pour le moment

Nous vous préviendrons par e-mail lorsqu'il sera disponible

Parfait !

Nous vous contacterons par e-mail s'il devient disponible

Un atelier à la fois

Confirmez pour mettre fin à tous les ateliers existants et démarrer celui-ci

Utilisez la navigation privée pour effectuer l'atelier

Ouvrez une fenêtre de navigateur en mode navigation privée pour effectuer cet atelier. Vous éviterez ainsi les conflits entre votre compte personnel et le compte temporaire de participant, qui pourraient entraîner des frais supplémentaires facturés sur votre compte personnel.