VOOZH about

URL: https://thenewstack.io/tensorflow-model-deployment-and-inferencing-with-kubeflow/

⇱ Tensorflow Model Deployment and Inferencing with Kubeflow - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2021-08-06 03:00:57
Tensorflow Model Deployment and Inferencing with Kubeflow
tutorial,
Cloud Native Ecosystem / Kubernetes

Tensorflow Model Deployment and Inferencing with Kubeflow

In this series on Kubeflow Jupyter Notebook Servers, we explore end-to-end MLOps scenario of configuring the environment, performing data preparation, training, deployment, and inference.
Aug 6th, 2021 3:00am by Janakiram MSV
👁 Featued image for: Tensorflow Model Deployment and Inferencing with Kubeflow
Feature image via Pixabay.
This tutorial is the last installment in an explanatory series on Kubeflow, Google’s popular open source machine learning platform for Kubernetes.

In the last part of this series, we trained a Tensorflow model to classify the images of cats and dogs. The model is stored in a shared Kubernetes persistent volume claim (PVC) which can be accessed by another Kubeflow Notebook Server to test the model.

Remember, this series aims not to build an extremely complex neural network but to demonstrate how Kubeflow helps organizations with machine learning operations (MLOps).

👁 Image

Launch a new CPU-based Jupyter Notebook Server and upload the notebook available on GitHub. This notebook validates the model by passing a few images.

👁 Image

Follow the same steps to launch the Notebook Server based on the image, janakiramm/infer. Make sure you mount the shared PVC – models.

This notebook loads the TensorFlow model and performs the classification based on sample images.

👁 Image

The infer function accepts a file and returns the prediction.

👁 Image

👁 Image

👁 Image

Let’s now deploy the model in TensorFlow Serving running in Kubernetes. Start by cloning the Github repository that has everything we need to run the inference code.

git clone https://github.com/janakiramm/kubeflow-notebook-tutorial.git

Navigate to the inference directory to find the YAML files and other related assets.

Let’s deploy TensorFlow Serving in the kubeflow-user-example-com namespace and expose it as a NodePort service. It’s the same namespace where the Jupyter Notebook Servers are running.

cd inference
kubectl apply -f tf-serve-deploy.yaml
kubectl apply -f tf-serve-service.yaml

👁 Image

Below are YAML specifications for the TF Serving deployment and service.

apiVersion: apps/v1
kind: Deployment
metadata:
 labels:
 app: dogs-vs-cats
 name: dogs-vs-cats-v1
 namespace: kubeflow-user-example-com
spec:
 selector:
 matchLabels:
 app: dogs-vs-cats
 template:
 metadata:
 labels:
 app: dogs-vs-cats
 version: v1
 spec:
 containers:
 - args:
 - --port=9000
 - --rest_api_port=8500
 - --model_name=dogs-vs-cats
 - --model_base_path=/models
 command:
 - /usr/bin/tensorflow_model_server
 image: tensorflow/serving:latest
 imagePullPolicy: IfNotPresent
 livenessProbe:
 initialDelaySeconds: 30
 periodSeconds: 30
 tcpSocket:
 port: 9000
 name: dogs-vs-cats
 ports:
 - containerPort: 9000
 - containerPort: 8500
 volumeMounts:
 - mountPath: /models
 name: model-serve-storage
 volumes:
 - name: model-serve-storage
 persistentVolumeClaim:
 claimName: models
apiVersion: v1
kind: Service
metadata:
 labels:
 app: dogs-vs-cats
 name: dogs-vs-cats-service
 namespace: kubeflow-user-example-com
spec:
 ports:
 - name: http-tf-serving
 port: 8500
 targetPort: 8500
 nodePort: 31000
 - name: grpc-tf-serving
 port: 9000
 targetPort: 9000
 nodePort: 31001 
 selector:
 app: dogs-vs-cats
 type: NodePort

We are essentially mounting the same PVC used by the Jupyter Notebook Servers to serve the model.

The TF Serving endpoint is available as a NodePort on the Kubeflow cluster.

Since Kubeflow relies on Istio for authorizing requests, we need to apply an authorization policy to allow requests to TF Serving.

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: default
 namespace: kubeflow-user-example-com
spec:
 rules:
 - to:
 - operation:
 methods: ["GET","POST"]
 paths: ["/v1/models/*"]
kubectl apply -f tf-serve-auth.yaml

It’s time to invoke the endpoint from a Python Client. Let’s create a virtual environment and install the required modules.

python3 -m venv inferenv
source inferenv/bin/activate
pip install -r requirements.txt

Below is the Python client code we use for inference.

import argparse
import json

import numpy as np
import requests
import tensorflow
import PIL
from tensorflow.keras.preprocessing import image

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
 help="path of the image")
ap.add_argument("-u", "--uri", required=True,
 help="URI of model server")

args = vars(ap.parse_args())

image_path = args['image']
uri = args['uri']

img = image.img_to_array(image.load_img(image_path, target_size=(128, 128))) / 255.

payload = {
 "instances": [{'conv2d_3_input': img.tolist()}]
}

r = requests.post(uri+'/v1/models/dogs-vs-cats:predict', json=payload)
pred = json.loads(r.content.decode('utf-8'))
predict=np.asarray(pred['predictions']).argmax(axis=1)[0]
print( "Dog" if predict==1 else "Cat" )

Let’s run the Python client by passing the TF Serving URL and a sample image. When sending sample1.jpg, we see the prediction as a dog and a cat when using sample2.jpg.

👁 Image

HOST=http://10.0.0.54:31000
python infer.py -i sample1.jpg -u $HOST

Replace HOST with an appropriate IP and port-based on your cluster and the TF Serving NodePort service.

👁 Image

HOST=http://10.0.0.54:31000
python infer.py -i sample2.jpg -u $HOST

👁 Image

As you can see, the classification is accurate for the images that we sent.

This concludes the series on Kubeflow Jupyter Notebook Servers where we explored the end-to-end MLOps scenario of configuring the environment, performing data preparation, training, deployment, and inference.

TRENDING STORIES
Janakiram MSV (Jani) is a practicing architect, research analyst, and advisor to Silicon Valley startups. He focuses on the convergence of modern infrastructure powered by cloud-native technology and machine intelligence driven by generative AI. Before becoming an entrepreneur, he spent...
Read more from Janakiram MSV
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.