How to docker push app (flask-python + redis) to gcr.io & deploy app to google kubernetes

349 views Asked by At

How to push my app (using python-flask + redis) to gcr.io and deploy to google kubernetes (by yaml file)? And I want to set env variable for my app

import os
import redis

from flask import Flask
from flask import request, redirect, render_template, url_for
from flask import Response

app = Flask(__name__)
redis_host = os.environ['REDIS_HOST']
app.redis = redis.StrictRedis(host=redis_host, port=6379, charset="utf-8", decode_responses=True)

# Be super aggressive about saving for the development environment.
# This says save every second if there is at least 1 change.  If you use
# redis in production you'll want to read up on the redis persistence
# model.
app.redis.config_set('save', '1 1')

@app.route('/', methods=['GET', 'POST'])
def main_page():
    if request.method == 'POST':
        app.redis.lpush('entries', request.form['entry'])        
        return redirect(url_for('main_page'))
    else:
        entries = app.redis.lrange('entries', 0, -1)
        return render_template('main.html', entries=entries)

#Router my app by post and redirect to mainpage
@app.route('/clear', methods=['POST'])
def clear_entries():
    app.redis.ltrim('entries', 1, 0)
    return redirect(url_for('main_page'))

#use for docker on localhost
if __name__ == "__main__":
  app.run(host='0.0.0.0', port=5000)
1

There are 1 answers

0
Dawid Kruk On

Posting this answer as a community wiki to set more of a baseline approach to the question rather than to give a specific solution addressing the code included in the question.

Feel free to edit/expand.


This topic could be quite wide considering the fact it could be addressed in many different ways (as described in the question, by using Cloud Build, etc).

Addressing this question specifically on the part of:

  • Building the image and sending it to GCR.
  • Using newly built image in GKE.

Building the image and sending it to GCR.

Assuming that your code and your whole Docker image is running correctly, you can build/tag it in a following manner to then send it to GCR:

  • gcloud auth configure-docker

adds the Docker credHelper entry to Docker's configuration file, or creates the file if it doesn't exist. This will register gcloud as the credential helper for all Google-supported Docker registries.

  • docker tag YOUR_IMAGE gcr.io/PROJECT_ID/IMAGE_NAME
  • docker push gcr.io/PROJECT_ID/IMAGE_NAME

After that you can go to the:

  • GCP Cloud Console (Web UI) -> Container Registry

and see the image you've uploaded.


Using newly built image in GKE

To run earlier mentioned image you can either:

  • Create the Deployment in the Cloud Console (Kubernetes Engine -> Workloads -> Deploy)

A side note!

You can also add there the environment variables of your choosing (as pointed in the question)

  • Create it with a YAML manifest that will be similar to the one below:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: amazing-app
  labels:
    app: amazing-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: amazing-app
  template:
    metadata:
      labels:
        app: amazing-app
    spec:
      containers:
      - name: amazing-app
        image: gcr.io/PROJECT-ID/IMAGE-NAME # <-- IMPORTANT!
        env:
        - name: DEMO_GREETING
          value: "Hello from the environment"

Please take a specific look on following part:

        env:
        - name: DEMO_GREETING
          value: "Hello from the environment"

This part will create an environment variable inside of each container:

  • $ kubectl exec -it amazing-app-6db8d7478b-4gtxk -- /bin/bash -c 'echo $DEMO_GREETING'
Hello from the environment

Additional resources: