Skip to content

Latest commit

 

History

History
192 lines (146 loc) · 6.59 KB

ks4.md

File metadata and controls

192 lines (146 loc) · 6.59 KB

Kubernetes series part 4

The objective here is to update our app so that we can detect server code changes on the fly. This will speed up our backend development cycles while still working with kubernetes in a development environment.

The flask development server has a nice reload experience:

The flask command line script (Command Line Interface) is strongly recommended for development because it provides a superior reload experience due to how it loads the application. The basic usage is like this:

export FLASK_APP=my_application
export FLASK_DEBUG=1
flask run

From: Source: Development Server

We will use this behaviour to get reload exeprience with the app we are building.

  1. navigate to ks4

    pwd
        ~/dev/github/redgate/ks/ks4
  2. start minikube

    ➜ minikube start
  3. switch to minikube context

    eval $(minikube docker-env)

    If you ever need to switch back to your machine's context do:

    eval $(docker-machine env -u)
  4. build web docker image

    ➜ docker build -f ./web/Dockerfile -t ks4webimage .
  5. build web server docker image

    ➜ docker build -f ./server/Dockerfile -t ks4webserverimage .
  6. mount ks4 source code

    In a separate terminal, in the root of the project (this terminal needs to keep running the whole time you're debugging...).

    pwd
        ~/dev/github/redgate/ks/ks4
    ➜ minikube mount .:/mounted-ks4-src
        Mounting . into /mounted-ks4-app-src on the minikube VM
        This daemon process needs to stay alive for the mount to still be accessible...
        ufs starting

    For more information about mounting volumes read these docs

  7. add environment variable to webserver section in deployment yaml file

    As described in the flask docs we will add the FLASK_DEBUG environment variable to get the reloading experience we want.

    command: ["python"]
    args: ["-m", "flask", "run"]
    env:
    - name: FLASK_DEBUG
        value: "1"
    ports:
    - containerPort: 5000
  8. add the web server volumes to the deployment yaml file

    Update the global volume section of the ./config/dev.ks.deployment.yaml with a python-server volume that points to the python backend so that changes are picked up on save.

    volumes:
    - name: python-server
    hostPath:
        path: /mounted-ks4-src/server
    - name: frontend
    hostPath:
        path: /mounted-ks4-src/app/src

    Then reference it in the webserver image section

    volumeMounts:
    - mountPath: /server
    name: python-server
  9. create deployment and service

    ➜ kubectl create -f ./config/dev.ks.deployment.yaml
        deployment "ks4web" created
    
    ➜ kubectl create -f ./config/dev.ks.service.yaml
        service "ks4web" created
    ➜ kubectl get all
        NAME            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
        deploy/ks4web   1         1         1            1           31s
    
        NAME                   DESIRED   CURRENT   READY     AGE
        rs/ks4web-2671084145   1         1         1         31s
    
        NAME            DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
        deploy/ks4web   1         1         1            1           31s
    
        NAME                         READY     STATUS    RESTARTS   AGE
        po/ks4web-2671084145-f0f71   2/2       Running   0          31s
  10. get web server logs

    ➜ kubectl logs ks4web-2671084145-f0f71 ks4webserver
        * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
        * Restarting with stat
        /usr/local/lib/python3.5/runpy.py:125: RuntimeWarning: 'flask.cli' found in sys.modules after import of package 'flask', but prior to execution of 'flask.cli'; this may result in unpredictable behaviour
        warn(RuntimeWarning(msg))
        * Debugger is active!
        * Debugger PIN: 207-014-748
  11. service ks4web

    ➜ minikube service ks4web --url
  12. check backend updates by changing the hello.py controller

    If you attach the logs with -f and update ./server/controllers/hello.py you should see

    Detected change in '/server/controllers/hello.py', reloading` in the logs.

    Full logs:

    ➜ kubectl logs ks4web-2671084145-gtz9q ks4webserver -f
        * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
        * Restarting with stat
        /usr/local/lib/python3.5/runpy.py:125: RuntimeWarning: 'flask.cli' found in sys.modules after import of package 'flask', but prior to execution of 'flask.cli'; this may result in unpredictable behaviour
        warn(RuntimeWarning(msg))
        * Debugger is active!
        * Debugger PIN: 207-014-748
        --------------------------------------------------------------------------------
        INFO in hello [/server/controllers/hello.py:8]:
        hello controller called
        --------------------------------------------------------------------------------
        127.0.0.1 - - [23/Oct/2017 09:53:50] "GET /api/hello HTTP/1.1" 200 -
        * Detected change in '/server/controllers/hello.py', reloading
        * Restarting with stat
        /usr/local/lib/python3.5/runpy.py:125: RuntimeWarning: 'flask.cli' found in sys.modules after import of package 'flask', but prior to execution of 'flask.cli'; this may result in unpredictable behaviour
        warn(RuntimeWarning(msg))
        * Debugger is active!
        * Debugger PIN: 207-014-748
        --------------------------------------------------------------------------------
        INFO in hello [/server/controllers/hello.py:8]:
        hello controller called

    If you then refresh the browser you should see the changes in the UI.

  13. minikube mounted volume command should detect this changes

    ➜ minikube mount .:/mounted-ks4-src
        Mounting . into /mounted-ks4-src on the minikube VM
        This daemon process needs to stay alive for the mount to still be accessible...
        ufs starting
        Rename ./server/controllers/__pycache__/hello.cpython-35.pyc.140423133386000 to hello.cpython-35.pyc
        rel  results in server/controllers/__pycache__/hello.cpython-35.pyc
        rename ./server/controllers/__pycache__/hello.cpython-35.pyc.140423133386000 to server/controllers/__pycache__/hello.cpython-35.pyc gets <nil>