Skip to content

Commit

Permalink
initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
raviranjan0309 committed Oct 10, 2018
1 parent 0f41a25 commit 04cd7cf
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# nginx-gunicorn-flaskRestful

FROM ubuntu:16.04

RUN apt-get update
RUN apt-get install -y python python-pip python-virtualenv nginx gunicorn supervisor

# Setup flask application
RUN mkdir -p /deploy/app
COPY app /deploy/app
RUN pip install -r /deploy/app/requirements.txt
RUN apt-get --assume-yes install python-opencv

# Setup nginx
RUN rm /etc/nginx/sites-enabled/default
COPY flask.conf /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/flask.conf /etc/nginx/sites-enabled/flask.conf
RUN echo "daemon off;" >> /etc/nginx/nginx.conf

# Setup supervisord
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY gunicorn.conf /etc/supervisor/conf.d/gunicorn.conf

# Start processes
CMD ["/usr/bin/supervisord"]
58 changes: 58 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#Constant value for build, run and deployment
image_name=face-emotion-app-1
project_id=nissan-helios-189503
cluster_name=emotion-api-cluster-nginx
num_nodes=1
zone=us-central1-b
port=80
version=v7
updated_version=v8

#Build and run docker image on local
deploy-local:
#build docker image
docker build -t gcr.io/${project_id}/${image_name}:${version} .
#run docker image
docker run -d --name ${image_name} -p $(port):$(port) gcr.io/${project_id}/${image_name}:${version}

#Build and deploy docker image on kubernetes GCP
deploy-gcp:
#Build the container image of face emotion application and tag it for uploading
docker build -t gcr.io/${project_id}/${image_name}:${version} .
#Using the gcloud command line tool, install the Kubernetes command-line tool
#kubectl is used to communicate with Kubernetes, which is the cluster orchestration system of GKE clusters
gcloud components install kubectl
#Configure Docker command-line tool to authenticate to Container Registry
gcloud auth configure-docker
#Use the Docker command-line tool to upload the image to your Container Registry
docker push gcr.io/${project_id}/${image_name}:${version}
#Use gcloud command-line tool set the project id:
gcloud config set project ${project_id}
#Use gcloud command-line tool set the zone:
gcloud config set compute/zone ${zone}
#Create a one-node kubernetes cluster named emotion-api-cluster-nginx on GCP
gcloud container ${cluster_name} create face-emotion --num-nodes=${num_nodes}
#Deploy face emotion application, listening on port 80:
kubectl run ${image_name} --image=gcr.io/${project_id}/${image_name}:v1 --port 80
#Expose face emotion application to traffic from the Internet
kubectl expose deployment ${image_name} --type=LoadBalancer --port $(port) --target-port $(port)

#Deploy a new version of app
update-deploy-new-version:
#Create an image for the v2 version of face emotion application by building the same source code and tagging it as v2
docker build -t gcr.io/${project_id}/${image_name}:${updated_version} .
#Push the image to the Google Container Registry
gcloud docker -- push gcr.io/${project_id}/${image_name}:${updated_version}
#Apply a rolling update to the existing deployment with an image update
kubectl set image deployment/${image_name} ${image_name}=gcr.io/${project_id}/${image_name}:${updated_version}

#Destroy the service and kubernetes cluster from gcp
destroy:
#Delete the Service and deallocate the Cloud Load Balancer created for face emotion Service
kubectl delete service ${image_name}
#Delete the container cluster and the resources that make up the container cluster,
#such as the compute instances, disks and network resources
gcloud container clusters delete ${cluster_name}



Binary file added app/.DS_Store
Binary file not shown.
32 changes: 32 additions & 0 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
from flask_cors import CORS
from flask import Flask, request, Response, jsonify
from flask_restful import reqparse, Resource, Api
import os
import json
import logging
import pyrebase

app = Flask(__name__)
api = Api(app)
CORS(app)

#Create and configure logger
logging.basicConfig(filename="newfile.log",
format='%(asctime)s %(message)s',
filemode='w')

#Creating an object
logger=logging.getLogger()

#Setting the threshold of logger to DEBUG
logger.setLevel(logging.DEBUG)

#test service
@app.route('/test',methods = ['GET'])
def test():
return "hello world"

if __name__ == '__main__':
logger.info("Service is up")
app.run()
18 changes: 18 additions & 0 deletions app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
certifi==2017.7.27.1
Flask-RESTful==0.3.6
gunicorn==19.7.1
Jinja2==2.9.6
requests==2.18.4
urllib3==1.22
Werkzeug==0.12.2
flask
flask-cors
keras==2.0.5
tensorflow==1.1.0
pandas==0.19.1
numpy==1.12.1
h5py==2.7.0
statistics
pyrebase
scipy
pillow
9 changes: 9 additions & 0 deletions flask.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
server {
listen 80;

location / {
proxy_pass http://localhost:5000/;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3 changes: 3 additions & 0 deletions gunicorn.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[program:gunicorn]
command=/usr/bin/gunicorn main:app -b localhost:5000
directory=/deploy/app
5 changes: 5 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[supervisord]
nodaemon=true

[program:nginx]
command=/usr/sbin/nginx

0 comments on commit 04cd7cf

Please sign in to comment.