Wczytuję…
Nie znaleziono wyników.

    Getting Started with Google Kubernetes Engine

    Sprawdź swoją wiedzę i podziel się nią ze społecznością.

    Working with Cloud Build

    Moduł 1 godz. universal_currency_alt Punkty: 5 show_chart Wprowadzające
    info Ten moduł może zawierać narzędzia AI, które ułatwią Ci naukę.
    Sprawdź swoją wiedzę i podziel się nią ze społecznością.

    Overview

    In this lab you will build a Docker container image from provided code and a Dockerfile using Cloud Build. You will then upload the container to the Artifact Registry.

    Objectives

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

    • Use Cloud Build to build and push containers
    • Use Artifact Registry to store and deploy containers

    Lab setup

    Access the lab

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

    1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

      • The Open Google Cloud console button
      • Time remaining
      • The temporary credentials that you must use for this lab
      • Other information, if needed, to step through this lab
    2. Click Open Google Cloud console (or right-click and select Open Link in Incognito Window if you are running the Chrome browser).

      The lab spins up resources, and then opens another tab that shows the Sign in page.

      Tip: Arrange the tabs in separate windows, side-by-side.

      Note: If you see the Choose an account dialog, click Use Another Account.
    3. If necessary, copy the Username below and paste it into the Sign in dialog.

      {{{user_0.username | "Username"}}}

      You can also find the Username in the Lab Details panel.

    4. Click Next.

    5. Copy the Password below and paste it into the Welcome dialog.

      {{{user_0.password | "Password"}}}

      You can also find the Password in the Lab Details panel.

    6. Click Next.

      Important: You must use the credentials the lab provides you. Do not use your Google Cloud account credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
    7. Click through the subsequent pages:

      • Accept the terms and conditions.
      • Do not add recovery options or two-factor authentication (because this is a temporary account).
      • Do not sign up for free trials.

    After a few moments, the Google Cloud console opens in this tab.

    Note: To view a menu with a list of Google Cloud products and services, click the Navigation menu at the top-left, or type the service or product name in the Search field.

    After you complete the initial sign-in steps, the project dashboard opens.

    Activate Google Cloud Shell

    Google Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud.

    Google Cloud Shell provides command-line access to your Google Cloud resources.

    1. In Cloud console, on the top right toolbar, click the Open Cloud Shell button.

    2. Click Continue.

    It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. For example:

    gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

    • You can list the active account name with this command:
    gcloud auth list

    Output:

    Credentialed accounts: - @.com (active)

    Example output:

    Credentialed accounts: - google1623327_student@qwiklabs.net
    • You can list the project ID with this command:
    gcloud config list project

    Output:

    [core] project =

    Example output:

    [core] project = qwiklabs-gcp-44776a13dea667a6 Note: Full documentation of gcloud is available in the gcloud CLI overview guide .

    Task 1. Confirm that needed APIs are enabled

    1. Make a note of the name of your Google Cloud project. This value is shown in the top bar of the Google Cloud console. It will be of the form qwiklabs-gcp- followed by hexadecimal numbers.

    2. In the Google Cloud console, in the Navigation menu(), click APIs & Services.

    3. Click Library.

    4. In the Search for APIs & Services box, type Cloud Build.

    5. In the resulting card for the Cloud Build API, if you do not see a message confirming that the Cloud Build API is enabled, click the Enable button.

    6. Use the Back button to return to the previous screen with a search box. In the search box, type Artifact Registry.

    7. In the resulting card for the Google Artifact Registry API, if you do not see a message confirming that the Artifact Registry API is enabled, click the Enable button.

    Task 2. Building containers with DockerFile and Cloud Build

    You can write build configuration files to provide instructions to Cloud Build as to which tasks to perform when building a container. These build files can fetch dependencies, run unit tests, analyses and more. In this task, you'll create a DockerFile and use it as a build configuration script with Cloud Build. You will also create a simple shell script (quickstart.sh) which will represent an application inside the container.

    1. On the Google Cloud console title bar, click Activate Cloud Shell.

    2. When prompted, click Authorize.

    Cloud Shell opens at the bottom of the Google Cloud console window.

    1. Create an empty quickstart.sh file using the nano text editor:
    nano quickstart.sh
    1. Add the following lines in to the quickstart.sh file:
    #!/bin/sh echo "Hello, world! The time is $(date)."
    1. Save the file and close nano by pressing the CTRL+X keys, then press Y and ENTER.

    2. Create an empty Dockerfile file using the nano text editor:

    nano Dockerfile
    1. Add the following Dockerfile command:
    FROM alpine

    This instructs the build to use the Alpine Linux base image.

    1. Add the following Dockerfile command to the end of the Dockerfile:
    COPY quickstart.sh /

    This adds the quickstart.sh script to the / directory in the image.

    1. Add the following Dockerfile command to the end of the Dockerfile:
    CMD ["/quickstart.sh"]

    This configures the image to execute the /quickstart.sh script when the associated container is created and run.

    The Dockerfile should now look like this:

    FROM alpine COPY quickstart.sh / CMD ["/quickstart.sh"]
    1. Save the file and close nano by pressing the CTRL+X keys, then press Y and ENTER.

    2. In Cloud Shell, run the following command to make the quickstart.sh script executable:

    chmod +x quickstart.sh
    1. Create a new Docker repository named quickstart-docker-repo in the location with the description "Docker repository"
    gcloud artifacts repositories create quickstart-docker-repo --repository-format=docker \ --location={{{project_0.default_region | "REGION"}}} --description="Docker repository"
    1. In Cloud Shell, run the following command to build the Docker container image in Cloud Build:
    gcloud builds submit --tag {{{project_0.default_region | "REGION"}}}-docker.pkg.dev/${DEVSHELL_PROJECT_ID}/quickstart-docker-repo/quickstart-image:tag1

    When the build completes, your Docker image is built and pushed to the Artifact Registry.

    1. In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Artifact Registry.

    2. Click the repository named quickstart-docker-repo.

    The quickstart-image Docker image appears in the list.

    Task 3. Building containers with a build configuration file and Cloud Build

    Cloud Build also supports custom build configuration files. In this task you will incorporate an existing Docker container using a custom YAML-formatted build file with Cloud Build.

    Let's create a sample custom cloud build configuration file called cloudbuild.yaml.

    1. Create and open a file called cloudbuild.yaml with nano using the following command:
    nano cloudbuild.yaml
    1. Once nano has opened, paste the following into the cloudbuild.yaml file:
    steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ] images: - 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'
    1. Press Ctrl+O, and then press Enter to save your edited file.

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

    3. Run the below command to set our region variable and insert that value into the yaml file.

    export REGION={{{project_0.default_region | "REGION"}}} sed -i "s/YourRegionHere/$REGION/g" cloudbuild.yaml
    1. In Cloud Shell, execute the following command to view the contents of cloudbuild.yaml:
    cat cloudbuild.yaml

    You will see the following:

    steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', '{{{project_0.default_region | "REGION"}}}-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ] images: - '{{{project_0.default_region | "REGION"}}}-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'

    This file instructs Cloud Build to use Docker to build an image using the Dockerfile specification in the current local directory, tag it with gcr.io/$PROJECT_ID/quickstart-image ($PROJECT_ID is a substitution variable automatically populated by Cloud Build with the project ID of the associated project), and then push that image to Artifact Registry.

    1. In Cloud Shell, execute the following command to start a Cloud Build using cloudbuild.yaml as the build configuration file:
    gcloud builds submit --config cloudbuild.yaml

    The build output to Cloud Shell should be the same as before. When the build completes, a new version of the same image is pushed to Artifact Registry.

    1. In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Artifact Registry.

    2. In search results, click Artifact Registry.

    3. Click the repository named quickstart-docker-repo > quickstart-image.

    Two versions of quickstart-image are now in the list.

    Click Check my progress to verify the objective. Build two container images in Cloud Build

    1. In the Google Cloud console, in the Search Bar (Located at the top of the console window), Search for Cloud Build.

    2. In search results, click Cloud Build.

    3. In Cloud Build, click History. Two builds appear in the list.

    4. Click the build ID for the build at the top of the list. The details of the build, including the build log, are displayed.

    Task 4. Building and testing containers with a build configuration file and Cloud Build

    The true power of custom build configuration files is their ability to perform other actions, in parallel or in sequence, in addition to simply building containers: running tests on your newly built containers, pushing them to various destinations, and even deploying them to Kubernetes Engine.

    In this task, we will see a simple example, a build configuration file that tests the container it built and reports the result to its calling environment. The first step is to alter the quickstart.sh file.

    1. In Cloud Shell, open quickstart.sh in nano.

      nano quickstart.sh
    2. Replace the existing with the following:

    #!/bin/sh if [ -z "$1" ] then echo "Hello, world! The time is $(date)." exit 0 else exit 1 fi
    1. Press Ctrl+O, and then press Enter to save your edited file.

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

    Let's create a new custom cloud build configuration file called cloudbuild2.yaml. This has been slightly modified to demonstrate Cloud Build's ability to test the containers it has built.

    1. Create and open a file called cloudbuild2.yaml with nano using the following command:
    nano cloudbuild2.yaml
    1. Once nano has opened, paste the following into the cloudbuild2.yaml file:
    steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ] - name: 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1' args: ['fail'] images: - 'YourRegionHere-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'
    1. Press Ctrl+O, and then press Enter to save your edited file.

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

    3. Run the below command to insert our region value into the yaml file.

    sed -i "s/YourRegionHere/$REGION/g" cloudbuild2.yaml
    1. In Cloud Shell, execute the following command to view the contents of cloudbuild2.yaml:
    cat cloudbuild2.yaml

    You will see the following:

    steps: - name: 'gcr.io/cloud-builders/docker' args: [ 'build', '-t', '{{{project_0.default_region | "REGION"}}}-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1', '.' ] - name: 'gcr.io/$PROJECT_ID/quickstart-image' args: ['fail'] images: - '{{{project_0.default_region | "REGION"}}}-docker.pkg.dev/$PROJECT_ID/quickstart-docker-repo/quickstart-image:tag1'

    In addition to its previous actions, this build configuration file runs the quickstart-image it has created. In this task, the quickstart.sh script has been modified so that it simulates a test failure when an argument ['fail'] is passed to it.

    1. In Cloud Shell, execute the following command to start a Cloud Build using cloudbuild.yaml as the build configuration file:
    gcloud builds submit --config cloudbuild2.yaml

    You will see output from the command that ends with text like this:

    Output

    BUILD FAILURE: Build step failure: build step 1 "us-east1-docker.pkg.dev/qwiklabs-gcp-02-1c7ba5c697a0/quickstart-docker-repo/quickstart-image:tag1" failed: starting step container failed: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: exec: "fail": executable file not found in $PATH: unknown ERROR: (gcloud.builds.submit) build 96c4a454-be06-4010-aa7c-da57c14165f4 completed with status "FAILURE"
    1. Confirm that your command shell knows that the build failed:
    echo $?

    The command will reply with a non-zero value. If you had embedded this build in a script, your script would be able to act up on the build's failure.

    Click Check my progress to verify the objective. Build and test containers with a build configuration file and Cloud Build

    End your lab

    When you have completed your lab, click End Lab. Google Cloud Skills Boost removes the resources you’ve used and cleans the account for you.

    You will be given an opportunity to rate the lab experience. Select the applicable number of stars, type a comment, and then click Submit.

    The number of stars indicates the following:

    • 1 star = Very dissatisfied
    • 2 stars = Dissatisfied
    • 3 stars = Neutral
    • 4 stars = Satisfied
    • 5 stars = Very satisfied

    You can close the dialog box if you don't want to provide feedback.

    For feedback, suggestions, or corrections, please use the Support tab.

    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.

    Wstecz Dalej

    Before you begin

    1. Labs create a Google Cloud project and resources for a fixed time
    2. Labs have a time limit and no pause feature. If you end the lab, you'll have to restart from the beginning.
    3. On the top left of your screen, click Start lab to begin

    Ta treść jest obecnie niedostępna

    Kiedy dostępność się zmieni, wyślemy Ci e-maila z powiadomieniem

    Świetnie

    Kiedy dostępność się zmieni, skontaktujemy się z Tobą e-mailem

    One lab at a time

    Confirm to end all existing labs and start this one

    Use private browsing to run the lab

    Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
    Podgląd