In this lab, you will learn how to:
- Create a simple Python Flask application.
- Use environment variables to configure the application.
- Deploy the Flask application on Kubernetes and pass environment variables to the app.
- Verify that the Flask app is correctly configured with the environment variables and can be accessed externally.
- Kubernetes Cluster (can be a local cluster like Minikube or a cloud-based one).
- kubectl installed and configured to interact with your Kubernetes cluster.
- Docker installed on your machine to build and push the Docker image of the Flask app.
- Docker Hub account to push the Docker image.
-
Create a directory for the Flask app:
- On your local machine, create a directory to hold your Flask application.
mkdir flask-k8s-app cd flask-k8s-app -
Create the Flask Application (
app.py):- Inside the
flask-k8s-appdirectory, create a Python file calledapp.py.
app.py:
from flask import Flask import os app = Flask(__name__) # Get the environment variable env_var = os.getenv('MY_ENV_VAR', 'Default Value') @app.route('/') def hello(): return f'Hello from Flask! The environment variable is: {env_var}' if __name__ == "__main__": app.run(host='0.0.0.0', port=5000)
This simple Flask app will display the value of an environment variable named
MY_ENV_VAR. If the environment variable is not set, it will display"Default Value". - Inside the
-
Create a
requirements.txtfile:- Create a
requirements.txtfile to list the dependencies for the Flask application.
requirements.txt:
Flask - Create a
-
Create a Dockerfile:
- In the same directory, create a file called
Dockerfileto containerize the Flask app.
Dockerfile:
# Use an official Python runtime as a parent image FROM python:3.8-slim # Set the working directory in the container WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --no-cache-dir -r requirements.txt # Expose port 5000 to the outside world EXPOSE 5000 # Run app.py when the container launches CMD ["python", "app.py"]
- In the same directory, create a file called
-
Build the Docker image:
- Build your Docker image using the following command:
docker build -t <your-dockerhub-username>/flask-app-env .
-
Push the Docker image to Docker Hub:
- After building the image, push it to Docker Hub:
docker login docker push <your-dockerhub-username>/flask-app-env
-
Create a Deployment YAML file for the Flask app (
flask-deployment.yaml):- The following YAML file will create a deployment for the Flask app and set an environment variable (
MY_ENV_VAR) for the container.
flask-deployment.yaml:
apiVersion: apps/v1 kind: Deployment metadata: name: flask-app spec: replicas: 2 selector: matchLabels: app: flask-app template: metadata: labels: app: flask-app spec: containers: - name: flask-app image: <your-dockerhub-username>/flask-app-env ports: - containerPort: 5000 env: - name: MY_ENV_VAR value: "This is the environment variable!"
In this example, the
MY_ENV_VARenvironment variable is passed to the Flask application with the value"This is the environment variable!". - The following YAML file will create a deployment for the Flask app and set an environment variable (
-
Create a Service YAML file to expose the Flask app (
flask-service.yaml):- Create a service to expose the Flask app so you can access it externally.
flask-service.yaml:
apiVersion: v1 kind: Service metadata: name: flask-app-service spec: selector: app: flask-app ports: - protocol: TCP port: 80 targetPort: 5000 type: LoadBalancer
-
Apply the Deployment and Service:
- Apply both the deployment and service to Kubernetes.
kubectl apply -f flask-deployment.yaml kubectl apply -f flask-service.yaml
-
Check the Deployment and Pods:
- Verify that the Flask app is deployed correctly and that the pods are running.
kubectl get pods kubectl get deployments
-
Verify the Service:
- Check if the service has been created successfully.
kubectl get svc
If you're using a LoadBalancer type service (on a cloud provider), it will take a few minutes to get an external IP address. Otherwise, you can use
kubectl port-forwardto access the service locally:kubectl port-forward svc/flask-app-service 8080:80
Then, access the Flask app by visiting http://localhost:8080.
-
Verify Environment Variable:
- Visit the Flask application in your browser or use
curlto make sure the environment variable is being correctly passed to the application:
curl http://localhost:8080
You should see something like this:
Hello from Flask! The environment variable is: This is the environment variable! - Visit the Flask application in your browser or use
-
Modify the Environment Variable in the Deployment:
- Update the
MY_ENV_VARin theflask-deployment.yamlfile to a new value, for example"Updated value from Kubernetes!".
- Update the
-
Apply the Updated Deployment:
- After making the change, apply the updated deployment to Kubernetes:
kubectl apply -f flask-deployment.yaml
-
Verify the Update:
- Check the logs to ensure that the application is using the new environment variable:
kubectl logs <pod-name>
Alternatively, you can test the Flask app again by visiting http://localhost:8080 to verify the new value is reflected.
- Docker Image: Submit the Docker image name (from Docker Hub) that you used for the Flask application.
- Kubernetes YAML Files:
flask-deployment.yamlflask-service.yaml
- Testing Results:
- Provide a screenshot or output from accessing the Flask app, showing that the environment variable is being passed and used.
- Log Outputs:
- Provide logs or evidence showing the environment variable update worked after the redeployment.
- Dockerization: Correctly building and pushing the Flask application Docker image to Docker Hub.
- Environment Variables: Correctly passing and using environment variables inside the Flask app.
- Kubernetes Deployment: Properly creating and deploying the Flask app using Kubernetes resources (Deployment, Service).
- Application Access: Successfully accessing the Flask application via the exposed service and verifying the environment variable is used.
By completing this lab, you will understand how to use environment variables in Kubernetes to configure applications dynamically and securely.