Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

Using ConfigMaps on GKE

Part of good cloud-native application design is separating configuration from the deployable artifact — i.e., the container image. In this lab, you will deploy a web app that can be dynamically reconfigured based on the existence of both environment variables and a configuration file. The variables and the file can be added at runtime with the use of Kubernetes ConfigMaps.

Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 1h 0m
Published
Clock icon Apr 09, 2021

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Create and Connect to a GKE Cluster

    1. Using the main navigation menu, under COMPUTE, select Kubernetes Engine.
    2. Click ENABLE.
    3. Once enabled, click CREATE.
    4. To the right of In the Standard: You manage your cluster, click CONFIGURE.
    5. At the top, choose USE A SETUP GUIDE, and choose My first cluster.
    6. Click CREATE NOW.
    7. Once the cluster has been created, click the three vertical dots and choose Connect.
    8. Under Command-line access, click Run in Cloud Shell.
    9. When prompted, click Continue.
    10. When the terminal has launched, hit Return to run the command.
    11. When prompted, click Authorize.
  2. Challenge

    Build the Container and Create the Deployment

    1. In the Cloud Shell terminal, download the code for this lab from GitHub.

    2. From the content-google-certified-pro-cloud-developer/flask-configmaps/ directory, use Cloud Build to create the container image. Note: Replace <your-project-id> with the ID of your project.

    3. Create a deployment.yaml file to deploy the app. Note: Replace <your-project-id> with the ID of your project:

      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: flask-deployment
      spec:
        selector:
          matchLabels:
            app: flask
        template:
          metadata:
            labels:
              app: flask
          spec:
            containers:
            - name: flask
              image: gcr.io/<your-project-id>/flask-configmaps
              ports:
              - containerPort: 8080
      
    4. Create the deployment.

    5. Create a load balancer service to expose the deployment.

    6. After a few minutes, an external IP should be provisioned for the service. View the service and external IP.

    7. Open the external IP in a browser tab to confirm the deployment is working.

  3. Challenge

    Use a ConfigMap to Add Environment Variables

    1. Create a new ConfigMap called animal-config using the literal values dogs=10 and cats=5.
    2. Update the deployment YAML to create environment variables from this ConfigMap. Add the following under the container image, at the same indentation level. If you are using copy and paste, you may have to correct some indentation. Just make sure that env starts at the same indentation level as the image directive above it.
    3. Re-apply the deployment.
    4. Reload the web page, and the app should now display the environment variables.
  4. Challenge

    Use a ConfigMap to Add a Configuration File

    1. Create a file called animals.cfg with the following contents:

      catfood=kibble
      dogfood=mixer
      fussy_dog=derek
      latest_feed=10pm
      
    2. Create a new ConfigMap called animal-configfile using the file you just created.

    3. Update the deployment YAML to create a volume from this ConfigMap and mount it into the Pod. Add the following under the container image, at the same indentation level. If you are using copy and paste, you may have to correct some indentation. Just make sure that volumeMounts starts at the same indentation level as the image directive above it.

    4. Now add the volume to the end of the YAML file. The volumes directive should start at the same indentation level as the containers directive:

    5. Re-apply the deployment.

    6. Reload the web page, and the app should now display the contents of the config file as well.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans