Using ConfigMaps on GKE

30 minutes
  • 4 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create and Connect to a GKE Cluster
  1. From the GCP menu, select Kubernetes Engine.
  2. Select Kubernetes Engine. Enable the Kubernetes Engine API.
  3. Under Cluster set-up guides toward the right, click CONFIGURE for the Standard Cluster option and choose My first cluster
  4. Click Create Now.
  5. It will take a few minutes for the cluster to spin up. Once it is available, from the Clusters page, click Connect next to your cluster.
  6. Under Command-line access, click Run in Cloud Shell.
  7. When the Cloud Shell has spawned, hit return to run the command.
  8. If prompted, click Authorize.
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.
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.
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.

Additional Resources

To get started, log in to Google Cloud Platform by opening https://console.cloud.google.com/ in a private browser window. Then sign in using the credentials provided on the lab page.

What are Hands-on Labs

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.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?