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
- Using the main navigation menu, under COMPUTE, select Kubernetes Engine.
- Click ENABLE.
- Once enabled, click CREATE.
- To the right of In the Standard: You manage your cluster, click CONFIGURE.
- At the top, choose USE A SETUP GUIDE, and choose My first cluster.
- Click CREATE NOW.
- Once the cluster has been created, click the three vertical dots and choose Connect.
- Under Command-line access, click Run in Cloud Shell.
- When prompted, click Continue.
- When the terminal has launched, hit Return to run the command.
- When prompted, click Authorize.
- Build the Container and Create the Deployment
In the Cloud Shell terminal, download the code for this lab from GitHub.
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.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
Create the deployment.
Create a load balancer service to expose the deployment.
After a few minutes, an external IP should be provisioned for the service. View the service and external IP.
Open the external IP in a browser tab to confirm the deployment is working.
- Use a ConfigMap to Add Environment Variables
- Create a new ConfigMap called
animal-config
using the literal values dogs=10 and cats=5. - 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 theimage
directive above it. - Re-apply the deployment.
- Reload the web page, and the app should now display the environment variables.
- Create a new ConfigMap called
- Use a ConfigMap to Add a Configuration File
Create a file called
animals.cfg
with the following contents:catfood=kibble dogfood=mixer fussy_dog=derek latest_feed=10pm
Create a new ConfigMap called
animal-configfile
using the file you just created.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 theimage
directive above it.Now add the volume to the end of the YAML file. The
volumes
directive should start at the same indentation level as thecontainers
directive:Re-apply the deployment.
Reload the web page, and the app should now display the contents of the config file as well.