Giter Club home page Giter Club logo

flask-ml-service's Introduction

Agile Development with Azure Project: Building a CI/CD Pipeline

Python application test with Github Actions

Overview

This project demonstrates the advantages of automating DevOps with CI/CD pipelines. A Machine Learning web application is built, test, and deployed using Github Actions and Azure pipelines.

The project plan is followed by instructions that detail the following steps:

Further improvements to the project are discussed in enhancements.

A short demo concludes the documentation.

Status

Python application test with Github Actions

Project Plan

The yearly project plan depicts that DevOps automation is one essential step in the context of the overall project planned for Q2.

The quarterly project plan details the steps necessary for building the CI-CD pipeline and estimates the week by week deliverables as well as the effort associated to each deliverable in T-Shirt-Sizes (S,M,L).

The Trello board is then used for task planning and tracking.

Instructions

The overall architecture of this project is as follows:

Architecture

The code resides in a GitHub repo together with test cases that can be used for quality assurance.

A manual code checkout allows for local integration and testing, see Cloning and testing locally.

Continuous Integration (CI) of the code is performed upon each commit via GitHub Actions, see Setting up CI using Github Actions.

The code can be executed in the cloud as a webapp in Azure App Services, see Deploying to Azure App Services.

Finally, the combination of Continuous integration and Continuous Delivery (CI/CD) via Azure Pipelines is used to automatically reflect all comitted changes in the running webapp, see Setting up CI/CD using Azure Pipelines.

Cloning and Testing Locally

Head to https://github.com and clone the repo https://github.com/taofeekaoyusuf/flask-ml-service. The following instructions will use the original repo, you should replace them with your cloned repo.

Login to Azure and open the Azure cloud shell.

Clone this project from github and change to the project directory:

azureuser@Azure:~$ git clone https://github.com/taofeekaoyusuf/flask-ml-service.git
azureuser@Azure:~$ cd flask-ml-service

Create a virtual environment and source it:

azureuser@Azure:~/flask-ml-service$ make setup
azureuser@Azure:~/flask-ml-service$ source ~/.flask-ml-service/bin/activate

Clone repo / Create virtual environment

Build locally using 'make all' which will install all dependencies, perform linting and testing:

azureuser@Azure:~/flask-ml-service$ make all

Build project

Run the application locally:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ flask run

Run project

Test it locally in a new Azure cloud shell (as your first shell is blocked by 'flask run') :

azureuser@Azure:~$ source .flask-ml-service/bin/activate
(.flask-ml-service) azureuser@Azure:~$ cd flask-ml-service/
(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ ./make_prediction.sh

Test locally

Close the second cloud shell and stop flask by hitting CRTL-C in the first cloud shell.

Setting up CI using Github Actions

If you directly jumped to this section: Clone the repo:

azureuser@Azure:~$ git clone [email protected]:taofeekaoyusuf/flask-ml-service.git

Clone repo Setup a virtual environment and activate it:

azureuser@Azure:~/flask-ml-service$ make setup
azureuser@Azure:~/flask-ml-service$ source ~/.flask-ml-service/bin/activate

Ensure that all tests pass locally:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ make all

Passing tests locally

Enable Github Actions by clicking on 'Actions', then click on "set up a workflow yourself' and use the GitHub Actions yaml file .github/workflows/main.yml as a template.

Once the workflow is created it is automatically triggered and should show a passing build: Passing build

Deploying to Azure App Services

Install the app to Azure app services using the free tier:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ az webapp up --name myflaskmlwebappy --resource-group Azuredevops --runtime "PYTHON:3.7"

Install to Azure app services

Check if the app is up and running by opening the URL containing the webapp name provided in the previous step: https://myflaskmlwebappy.azurewebsites.net/

Running webapp

Edit file 'make_predict_azure_app.sh' and replace '< yourappname >' with your webapp name (e.g. flask-ml-service).

Test the remote webapp:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ ./make_predict_azure_app.sh

Test remotely

Logs of your running webapp can be accessed in two ways:

A. Via the public endpoint by appending '/api/logs/docker': https://myflaskmlwebappy.scm.azurewebsites.net/api/logs/docker

B. Via your cloud shell as a stream:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ az webapp log tail

Streamed webapp logs

Performance validation of the webapp can be performed via a load test using locust.
Replace '< yourappname >' in the provided configuration and call locust:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ nano locustfile.py
(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ locust -f locustfile.py --headless -u 20 -r 5 -t 20s

Using the parameters above locust will use 20 users with a spawn rate of 5 users per second and run for 20 seconds: Load test using locust

Setting up CI/CD using Azure Pipelines

At the time of writing the following steps were sufficient to setup a pipeline. As things change quite fast in a cloud environment please consult the official documentation provided by Microsoft in case of unclarities.

Open Azure devops in a browser https://dev.azure.com.

Create a new project or selecting an existing project.

Head to Pipelines and click on 'New pipeline'.

When you are asked 'Where is your code?' select Github and choose your cloned repo.

Configure your pipeline as a "Python to Linux Web App on Azure", select your subscription and the webapp name you used to deploy the webapp earlier (e.g. flask-ml-service). Click on 'Validate and configure'.

Created pipeline

Under 'Pipelines' you should now see a first successfull run of the pipeline including the 'Build stage' and the 'Deploy Web App' stage:

pipeline run

Coming back to the Pipeline page, we will observe successful build and deployment:

pipeline success

From now on every change to your code will trigger the CI/CD pipeline and update your webapp accordingly:

Change the application name in app.py from 'Sklearn Prediction Home' to 'Sklearn Prediction Home via Azure CI/CD Pipeline' and commit it:

(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ nano app.py
(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ git add app.py
(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ git commit -m "Changed application name"
(.flask-ml-service) azureuser@Azure:~/flask-ml-service$ git push

Change application name

The pipeline is triggered by the commit: Next pipeline run

After the pipeline run completed successfully the changed application name is visible: Updated webapp

Enhancements

Future improvements include but are not limited to:

  • Additional testing using pytest: Testcases could be added that are automatically checked by a Makefile target.
  • Code coverage using pytest-cov: In addition to testing code coverage could be checked in an automated way via a Makefile target.
  • Automation of the load testing using locust: While the instructions in the Deploying to Azure App Services section depict the initial use of locust for load testing it could be included as an additional target in the Makefile.

Demo

A short demo demonstrates the main steps of the project can be found at this YouTube link: Demo Video

The demo covers the following steps:

Welcome to the demo of the Agile project development. We built a machine learning application and automated testing and provisioning by continuous integration and continuous deployment using Microsoft Azure pipelines.

As shown in the project architecture the source code is maintained in Github. The project can be manually checked out in cloud shell and tested by calling 'make all'.

GitHub Actions are used for Continuous Integration to check the project build status upon every commit.

Azure Pipelines are used for Continuous Integration and Continuous Deployment of our machine learning application to App Services. The application can be accessed via a public endpoint. The logs are accessible via that endpoint or can be streamed in the cloud shell. The main functionality of the application is exposed via an API and can be accessed from the cloud shell.

flask-ml-service's People

Contributors

taofeekaoyusuf avatar lifiuni avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.