TensorFlow Serving
Out of date
This guide contains outdated information pertaining to Kubeflow 1.0. This guide needs to be updated for Kubeflow 1.1.
This Kubeflow component has stable status. See the .
To deploy a model we create following resources as illustrated below
- A deployment to deploy the model using TFServing
- A K8s service to create an endpoint a service
- An Istio virtual service to route traffic to the model and expose it through the Istio gateway
- An Istio DestinationRule is for doing traffic splitting.
Referring to the above example, you can customize your deployment by changing the following configurations in the YAML file:
In the deployment resource, the argument points to the model. Change the value to your own model.
The example contains three configurations for Google Cloud Storage (GCS) access: volumes (secret
user-gcp-sa
), volumeMounts, and env (GOOGLE_APPLICATION_CREDENTIALS). If your model is not at GCS (e.g. using S3 from AWS), See the section below on how to setup access.GPU. If you want to use GPU, add
nvidia.com/gpu: 1
in container resources, and use a GPU image, for example:tensorflow/serving:1.11.1-gpu
.resources:
limits:
cpu: "4"
memory: 4Gi
nvidia.com/gpu: 1
The resource
VirtualService
andDestinationRule
are for routing. With the example above, the model is accessible atHOSTNAME/tfserving/models/mnist
(HOSTNAME is your Kubeflow deployment hostname). To change the path, edit thehttp.match.uri
of VirtualService.
Depending where model file is located, set correct parameters
Change the deployment spec as follows:
spec:
selector:
matchLabels:
app: mnist
template:
metadata:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: mnist
version: v1
containers:
- args:
- --port=9000
- --rest_api_port=8500
- --model_base_path=gs://kubeflow-examples-data/mnist
command:
- /usr/bin/tensorflow_model_server
env:
- name: GOOGLE_APPLICATION_CREDENTIALS
value: /secret/gcp-credentials/user-gcp-sa.json
image: tensorflow/serving:1.11.1-gpu
imagePullPolicy: IfNotPresent
livenessProbe:
initialDelaySeconds: 30
periodSeconds: 30
tcpSocket:
port: 9000
name: mnist
ports:
- containerPort: 9000
- containerPort: 8500
resources:
limits:
cpu: "4"
memory: 4Gi
nvidia.com/gpu: 1
requests:
cpu: "1"
memory: 1Gi
volumeMounts:
name: config-volume
name: gcp-credentials
volumes:
- configMap:
name: mnist-v1-config
name: config-volume
- name: gcp-credentials
secret:
secretName: user-gcp-sa
The changes are:
- environment variable
GOOGLE_APPLICATION_CREDENTIALS
- volume
gcp-credentials
- volumeMount
gcp-credentials
We need a service account that can access the model. If you are using Kubeflow’s click-to-deploy app, there should be already a secret, user-gcp-sa
, in the cluster.
The model at gs://kubeflow-examples-data/mnist is publicly accessible. However, if your environment doesn’t have google cloud credential setup, TF serving will not be able to read the model. See this issue for example. To setup the google cloud credential, you should either have the environment variable GOOGLE_APPLICATION_CREDENTIALS
pointing to the credential file, or run gcloud auth login
. See for more detail.
S3
To use S3, first you need to create secret that will contain access credentials. Use base64 to encode your credentials and check details in the Kubernetes guide to creating a secret manually
apiVersion: v1
metadata:
name: secretname
data:
AWS_ACCESS_KEY_ID: bmljZSB0cnk6KQ==
AWS_SECRET_ACCESS_KEY: YnV0IHlvdSBkaWRuJ3QgZ2V0IG15IHNlY3JldCE=
kind: Secret
Then use the following manifest as an example:
If the service type is LoadBalancer, it will have its own accessible external ip. Get the external ip by:
kubectl get svc mnist-service
And then send the request
curl -X POST -d @input.json http://EXTERNAL_IP:8500/v1/models/mnist:predict
- Save the client ID that you used to as
IAP_CLIENT_ID
. Create a service account
gcloud iam service-accounts create --project=$PROJECT $SERVICE_ACCOUNT
Grant the service account access to IAP enabled resources:
Download the service account key:
gcloud iam service-accounts keys create ${KEY_FILE} \
--iam-account ${SERVICE_ACCOUNT}@${PROJECT}.iam.gserviceaccount.com
Export the environment variable
GOOGLE_APPLICATION_CREDENTIALS
to point to the key file of the service account.
Finally, you can send the request with an input file with this python script
To send a GET request:
Please look at the .
See the guide to logging and monitoring for instructions on getting logs and metrics using Stackdriver.