Professional Documents
Culture Documents
2. Generating a Model
import pickle
iris = datasets.load_iris()
validation_size = 0.20
seed = 100
test_size=validation_size,
random_state=seed)
knn = KNeighborsClassifier()
knn.fit(X_train, Y_train)
pickle.dump(knn, model_file)
● Run the 'model_generator.py' script. It creates the required model, and stores
it in the models folder.
python3 model_generator.py
import pickle
app = Flask(__name__)
api = Api(app)
model = pickle.load(model_file)
class IrisPredict(Resource):
def get(self):
sl = float(request.args.get('sl'))
sw = float(request.args.get('sw'))
pl = float(request.args.get('pl'))
pw = float(request.args.get('pw'))
return {'sepal_length':sl,
'sepal_width':sw,
'petal_length':pl,
'petal_width':pw,
'species':result}
api.add_resource(IrisPredict, '/classify/')
export LANG=C.UTF-8
● Open a new terminal and try to access the API with the 'classify' end point.
curl "http://0.0.0.0:8000/classify/?sl=5.1&sw=3.5&pl=1.4&pw=0.3"
_________________________________________________________________________
Hands-on Scenario
Minikube installation
sudo apt install docker.io -y
sudo systemctl unmask docker
sudo service docker restart
curl -LO
https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd
64.deb
sudo dpkg -i minikube_latest_amd64.deb
rm -rf minikube_latest_amd64.deb
1. Start Cluster : Launch the minikube cluster with the command 'minikube start'
2. Use the following yaml to deploy 'iris-classifier-site' to the cluster.
echo "apiVersion: apps/v1
kind: Deployment
metadata:
name: iris-classifier-site
labels:
app: web
spec:
replicas : 1
selector :
matchLabels:
app : iris-classifier
template :
metadata :
labels : { app : iris-classifier }
spec:
containers:
- name: mlexample
image: gpcplay/playimages:mlexample
ports:
- containerPort: 5000">iris-classifier-deployment.yaml
Hint: Use kubectl on the 'iris-classifier-deployment.yaml' file and wait till the pods are
created.
3. Create a Service for App Use the following yaml to expose port 30800 outside the
cluster. This service will be used by the iris-classifier-site app.
echo "apiVersion: v1
kind: Service
metadata:
name: iris-classifier-svc
spec:
selector:
app: iris-classifier
type: NodePort
ports:
- port: 5000
nodePort : 30800
targetPort: 5000">iris-classifier-service.yaml
4. Verify if the application is running with the command 'kubectl get pods'
● List the service in the current cluster with the command 'kubectl get services'
● Create an environment variable 'NODE_PORT' by using the following
expression:
export NODE_PORT=$(kubectl get services/iris-classifier-svc -o go-
template='{{(index .spec.ports 0).nodePort}}')
echo NODE_PORT=$NODE_PORT