Skip to content

KFServing

Interactive Diagram

새 탭 ↗

KFServing provides Kubernetes CRDs for machine learning model serving. It exposes high-level, production-oriented interfaces for common ML frameworks such as TensorFlow, XGBoost, Scikit-Learn, PyTorch, and ONNX, making it possible to operate trained models as services on Kubernetes.

  • Features (https://github.com/kserve/kserve/blob/master/docs/samples/README.md)
    • Scale to and from Zero
    • Request based Autoscaling on CPU/GPU
    • Revision Management
    • Optimized Container
    • Batching
    • Request/Response logging
    • Scalable Multi Model Serving
    • Traffic management
    • Security with AuthN/AuthZ
    • Distributed Tracing
    • Out-of-the-box metrics
    • Ingress/Egress control

| KFServing can offer these serverless-style features because it builds on custom resources that are already provided by components such as Istio and Knative inside a Kubernetes cluster. You can also deploy Kubeflow, which includes KFServing as an embedded component, and use it as a fully managed MLOps framework.


KServe model inference platform: generative and predictive runtimes, GenAI integrations, autoscaling and networking integrations on Kubernetes Source: KServe GitHub README, The KServe Authors, Apache-2.0

See KFServing GitHub repository https://github.com/kserve/kserve See the KFServing v1beta1 RFC https://docs.google.com/document/d/1ktiO7gWohq19C_rixXH0T_D91TjkrQELlQjlkvSefVc/edit#heading=h.e1sgar1o8xoy

  • Architecture overview
    • Data plane : The path that actually delivers traffic, shown here with a single-model request flow.
    • Control plane : The layer that creates, configures, and controls the resources behind that data plane.

KFServing Architecture Overview https://github.com/kserve/kserve/blob/master/docs/README.md

A predictor container is the core runtime for basic model serving. Explainer and transformer components are optional and can be added when a serving workflow needs explanation or pre/post-processing.

  • Example request flow for a single model

KServe data plane: requests split into default and canary endpoints and passed to transformer, explainer and predictor Source: KServe GitHub, docs/diagrams, The KServe Authors, Apache-2.0

  • Concepts

    • Endpoint: An inference service is split into two endpoints, default and canary. The canary endpoint is optional, and the rollout strategy can be changed through detailed InferenceService settings. By default, traffic goes to the default endpoint.
    • Predictor: The mandatory component of an InferenceService. It is the model-serving runtime that exposes a model at a network endpoint.
    • Explainer: An optional data-plane component that provides model explanation in addition to ordinary prediction. It can be implemented with a user-defined container. KFServing commonly provides built-in explainers such as Alibi.
    • Transformer: Lets users run pre-processing and post-processing around the prediction or explanation flow. It can also be implemented with a user-defined container. KFServing commonly provides built-in transformers such as Feast.
  • Data Plane API (v2)

    • The older v1 API is being consolidated into v2. In v1, predict and explain were exposed separately as shown in the diagram above, while in v2 they are converging around the predict API.
    • Supports HTTP/REST and gRPC

See the v2 API docs https://github.com/kserve/kserve/tree/master/docs/predict-api/v2

Using Kubernetes custom resources such as InferenceService and TrainedModel, KFServing deploys predictor, explainer, and transformer containers and then performs actions such as autoscaling in response to workload traffic.

KServe InferenceService structure: ingress gateway, queue proxy, KServe agent, model server, and KPA or HPA autoscaling Source: KServe GitHub, docs/diagrams, The KServe Authors, Apache-2.0

  • Control plane components
    • Knative Serving Controller: Manages service revisions and creates network routing resources. By default it also injects the Queue Proxy sidecar, which exposes traffic metrics and enforces concurrency limits.
    • Knative Activator: Brings pods back after they have scaled to zero and forwards requests to them.
    • Knative Autoscaler (KPA): Watches application traffic and scales replicas up and down according to the configured metrics.
    • KFServing Controller: Creates the services needed for request/response logging, batching, model pooling, and the model-server and agent containers themselves.
    • Ingress Gateway: Routes external and internal requests.

At its core, KFServing is a serverless model-serving system, and Knative is the foundation that enables that behavior.

Knative handles ingress traffic through the gateway provided by Istio. When a service must be exposed externally, Istio VirtualService resources are created and linked to the ServerlessService objects that correspond to Knative-managed revisions.

The following is a compact summary of the Istio/Knative processing flow and the main API resources that KFServing relies on.

See the MLOps Community KFServing slides https://www.slideshare.net/theofpa/serving-models-using-kfserving

  • Istio routing API

Istio VirtualService routing requests to DestinationRule subsets for service versions Source: Istio blog, Introducing the Istio v1alpha3 routing API, Istio Authors, Apache-2.0

API groupResource
networking.istio.ioGateway
networking.istio.ioVirtualService
  • Knative traffic flow

API groupResource
autoscaling.internal.knative.devMetric
autoscaling.internal.knative.devPodAutoscaler
caching.internal.knative.devImage
networking.internal.knative.devIngress
networking.internal.knative.devServerlessService
serving.knative.devConfiguration
serving.knative.devRevision
serving.knative.devRoute
serving.knative.devService

The prerequisites from the official KFServing installation guide must be in place. It is also possible to install Kubeflow and consume KFServing as part of a larger ML pipeline, but the environment below was built as a standalone KFServing deployment.

See the KFServing install guide https://github.com/kserve/kserve#install-kserve

KFServing was moving from alpha toward beta when this was validated, and Knative was also changing quickly, so it was important to watch the deployment carefully. There can also be waiting periods while components such as cert-manager generate certificate secrets.

The prerequisite components were deployed as follows.

  • Istio: v1.9.0
    • Sidecar injection was not required here, so it was excluded from the installation.
    • The knative-local-gateway service created during Knative installation also appears in this namespace.
Terminal window
$ kubectl get all -n istio-system
NAME READY STATUS RESTARTS AGE
pod/istio-ingressgateway-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/istiod-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/istiod-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/istiod-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/istio-ingressgateway LoadBalancer 10.0.0.15 <External-IP> 15021:32554/TCP,80:32236/TCP,443:31368/TCP,15012:30705/TCP,15443:30149/TCP 2d22h
service/istiod ClusterIP 10.0.0.16 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 2d22h
service/knative-local-gateway ClusterIP 10.0.0.17 <none> 80/TCP 2d22h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/istio-ingressgateway 1/1 1 1 2d22h
deployment.apps/istiod 3/3 3 3 2d22h
  • Knative Serving: v0.22.0
    • The controller creates the serving containers used by KFServing and manages revisions and routes. Through the Queue Proxy sidecar it also exposes traffic metrics and enforces concurrency limits.
    • The autoscaler watches serving-container metrics and performs automatic scale-out and scale-in.
    • When a request arrives after scale-to-zero, the activator brings the pod back and forwards the request.
Terminal window
$ kubectl get all -n knative-serving
NAME READY STATUS RESTARTS AGE
pod/activator-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/autoscaler-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/controller-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/istio-webhook-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/networking-istio-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
pod/webhook-xxxxxxxxxx-xxxxx 1/1 Running 0 21h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/activator-service ClusterIP 10.0.0.18 <none> 9090/TCP,8008/TCP,80/TCP,81/TCP 2d22h
service/autoscaler ClusterIP 10.0.0.19 <none> 9090/TCP,8008/TCP,8080/TCP 2d22h
service/autoscaler-bucket-00-of-01 ClusterIP 10.0.0.20 <none> 8080/TCP 2d22h
service/controller ClusterIP 10.0.0.21 <none> 9090/TCP,8008/TCP 2d22h
service/istio-webhook ClusterIP 10.0.0.22 <none> 9090/TCP,8008/TCP,443/TCP 2d22h
service/webhook ClusterIP 10.0.0.23 <none> 9090/TCP,8008/TCP,443/TCP 2d22h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/activator 1/1 1 1 2d22h
deployment.apps/autoscaler 1/1 1 1 2d22h
deployment.apps/controller 1/1 1 1 2d22h
deployment.apps/istio-webhook 1/1 1 1 2d22h
deployment.apps/networking-istio 1/1 1 1 2d22h
deployment.apps/webhook 1/1 1 1 2d22h
  • Cert Manager: v1.3.1
Terminal window
$ kubectl get all -n cert-manager
NAME READY STATUS RESTARTS AGE
pod/cert-manager-xxxxxxxxxx-xxxxx 1/1 Running 0 22h
pod/cert-manager-cainjector-xxxxxxxxxx-xxxxx 1/1 Running 0 22h
pod/cert-manager-webhook-xxxxxxxxxx-xxxxx 1/1 Running 0 22h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/cert-manager ClusterIP 10.0.0.24 <none> 9402/TCP 2d3h
service/cert-manager-webhook ClusterIP 10.0.0.25 <none> 443/TCP 2d3h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/cert-manager 1/1 1 1 2d3h
deployment.apps/cert-manager-cainjector 1/1 1 1 2d3h
deployment.apps/cert-manager-webhook 1/1 1 1 2d3h
  • KFServing
    • kfserving-controller: creates the services used for request/response logging, batching, and model pooling, and also creates the model servers themselves.
Terminal window
$ kubectl get all -n kfserving-system
NAME READY STATUS RESTARTS AGE
pod/kfserving-controller-manager-0 2/2 Running 0 23h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kfserving-controller-manager-metrics-service ClusterIP 10.0.0.26 <none> 8443/TCP 2d3h
service/kfserving-controller-manager-service ClusterIP 10.0.0.27 <none> 443/TCP 2d3h
service/kfserving-webhook-server-service ClusterIP 10.0.0.28 <none> 443/TCP 2d3h
NAME READY AGE
statefulset.apps/kfserving-controller-manager 1/1 2d3h
  • KFServing request flow
+----------------------+ +-----------------------+ +----------------------------+
|Istio Virtual Service | |Istio Virtual Service | | K8S Service |
| | | | | |
|model-server | |model-server-predictor | | model-server-predictor |
| +---->| -default +---->| -default-$revision |
| | | | | |
| *KFServing Route* | | *Knative Route* | | *Knative Revision Service* |
+----------------------+ +-----------------------+ +----------------+-----------+
Istio Ingress Gateway Istio Local Gateway Kube Proxy
|
|
|
+-------------------------------------------------------+ |
| *Knative Revision Pod* | |
| | |
| +-------------------+ +-----------------+ | |
| | | | | | |
| |kfserving-container|<-----+ Queue Proxy | |<------------------+
| | | | | |
| +-------------------+ +--------------^--+ |
| | |
| | |
+-----------------------^-------------------------------+
| scale deployment |
+--------+--------+ | pull metrics
| Knative | |
| Autoscaler |-----------
| KPA/HPA |
+-----------------+

KFServing exposes two major custom resources, InferenceService and TrainedModel, but the core model-serving workflow built around a predictor can be implemented with InferenceService alone.

When you create an InferenceService on Kubernetes, resources like the following are created with it. The diagram below shows separate default and canary configurations, but an InferenceService can also be defined with only the default path.

InferenceService architecture

  • InferenceService
    • A resource provided by KFServing, that is, by Kubeflow.
    • Manages the overall lifecycle of the model server.
  • Configuration
    • A configuration resource provided by Knative.
    • Tracks deployment history for the model server.
  • Revision
    • A revision resource provided by Knative.
    • Created from configuration history; in practice it stores the serving-container spec.
  • Route
    • A route resource provided by Knative.
    • Serves as the endpoint for network-traffic management.
    • Routes traffic to the connected revision. (For example: default-service -> revision-00X-service)

With the KFServing API you create InferenceService, TrainedModel, and related resources while KFServing in turn manages the required Knative resources. A predictor spec is mandatory, and predictor-level settings such as autoscaling and canary rollout can be configured there.

KFServing v1beta1 API docs https://github.com/kserve/kserve/tree/master/docs/apis/v1beta1

The following shows the result of defining triton-inference-server as the predictor and deploying it through KFServing.


| During model serving, Triton can mount a model repository into the inference server and serve requests across multiple models. By contrast, runtimes such as TensorFlow appear to mount a single model into the predictor container. I did not dig far enough into real analyst workflows to compare those approaches in detail.


  • Basic Kubernetes objects
Terminal window
# A service is created for each revision, and the serving container is deployed as a Deployment.
$ kubectl get all
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/triton-model ExternalName <none> knative-local-gateway.istio-system.svc.cluster.local <none> 2d18h
service/triton-model-predictor-default ExternalName <none> knative-local-gateway.istio-system.svc.cluster.local 80/TCP 2d18h
service/triton-model-predictor-default-00001 ClusterIP 10.0.0.29 <none> 80/TCP 2d18h
service/triton-model-predictor-default-00001-private ClusterIP 10.0.0.30 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 2d18h
service/triton-model-predictor-default-00002 ClusterIP 10.0.0.31 <none> 80/TCP 2d18h
service/triton-model-predictor-default-00002-private ClusterIP 10.0.0.32 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 2d18h
service/triton-model-predictor-default-00003 ClusterIP 10.0.0.33 <none> 80/TCP 25h
service/triton-model-predictor-default-00003-private ClusterIP 10.0.0.34 <none> 80/TCP,9090/TCP,9091/TCP,8022/TCP 25h
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/triton-model-predictor-default-00001-deployment 0/0 0 0 2d18h
deployment.apps/triton-model-predictor-default-00002-deployment 0/0 0 0 2d18h
deployment.apps/triton-model-predictor-default-00003-deployment 0/0 0 0 25h
  • Knative-related resources
Terminal window
# Knative InferenceService
# - Query the canary rate, current revision, container status, and so on.
$ kubectl get ksvc
NAME URL READY PREV LATEST PREVROLLEDOUTREVISION LATESTREADYREVISION AGE
triton-model http://triton-model.inference.<custom.domain> True 100 triton-model-predictor-default-00003 2d1h
# Knative configuration
# - This configuration creates the revisions below.
$ kubectl get configuration
NAME LATESTCREATED LATESTREADY READY REASON
triton-model-predictor-default triton-model-predictor-default-00003 triton-model-predictor-default-00003 True
# Knative revision
$ kubectl revision configuration
NAME CONFIG NAME K8S SERVICE NAME GENERATION READY REASON
triton-model-predictor-default-00001 triton-model-predictor-default triton-model-predictor-default-00001 1 True
triton-model-predictor-default-00002 triton-model-predictor-default triton-model-predictor-default-00002 2 True
triton-model-predictor-default-00003 triton-model-predictor-default triton-model-predictor-default-00003 3 True
# Knative route
$ kubectl route configuration
NAME URL READY REASON
triton-model-predictor-default http://triton-model-predictor-default.inference.<custom.domain> True

Create an InferenceService, define a predictor, and KFServing will create the inference-server container for you.

  • predictor
    • You can use runtimes such as TFServing, TorchServe, Triton, MLServer/KFServer, and ONNX Runtime, each backed by an official image.
    • A custom image can also be defined.
  • storage
    • Public-cloud object storage such as Amazon S3 and Google GCS can be used.
    • PVC-based volume mounts are also supported.

To mount Amazon S3 as storage, the related Secret and ServiceAccount were created in advance.

apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
name: "triton-model"
namespace: inference
spec:
predictor:
serviceAccountName: s3-sa
triton:
resources:
limits:
cpu: "1"
memory: 2Gi
storageUri: s3://kloudbank-triton-inference-server-repository/model_repository

As described above, Knative serving workloads scale in and out through HPA under the control of KPA. By setting minReplicas to 0, you can let unused model servers scale all the way down.

  • minReplicas: minimum number of replicas

    • Default: 1
    • If set to 0, the pod is terminated after the period configured in Knative.
      Terminal window
      $ kubectl get po -w
      NAME READY STATUS RESTARTS AGE
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 Pending 0 0s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 Pending 0 0s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 Init:0/1 0 0s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 Init:0/1 0 1s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 PodInitializing 0 4s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 1/2 Running 0 5s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 1/2 Running 0 6s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 2/2 Running 0 6s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 2/2 Terminating 0 68s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 1/2 Terminating 0 70s
      triton-model-predictor-default-00003-deployment-689d9545ff7hvkg 0/2 Terminating 0 115s
  • maxReplicas: maximum number of replicas

  • containerConcurrency

By pointing to different storage locations, requests can be served by two or more model-server revisions.
In v1alpha2, default and canary predictor definitions were declared together in a single InferenceService. From v1beta1, it appears the pattern changed: first define the predictor used as the default, then add another predictor definition that specifies canaryTrafficPercent.

Because these InferenceService patches are all preserved and managed as revisions, the model-serving history is easier to roll back.

The following is the TensorFlow example from the KFServing samples.

  • default predictor definition
apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
name: "flower-sample"
namespace: kfserving-test
spec:
predictor:
minReplicas: 0
tensorflow:
storageUri: gs://kfserving-samples/models/tensorflow/flowers
  • canary predictor and traffic percent definition
apiVersion: "serving.kubeflow.org/v1beta1"
kind: "InferenceService"
metadata:
name: "flower-sample"
namespace: kfserving-test
spec:
predictor:
minReplicas: 0
# Indicates that this additional predictor is for canary handling.
canaryTrafficPercent: 20
tensorflow:
# Mount a different model
storageUri: "gs://kfserving-samples/models/tensorflow/flowers-2"
  • The canary rollout can be checked by querying the InferenceService
Terminal window
$ kubectl get isvc -owide
NAME URL READY PREV LATEST PREVROLLEDOUTREVISION LATESTREADYREVISION AGE
flower-sample http://flower-sample.kfserving-test.<custom-domain> True 80 20 flower-sample-predictor-default-00002 flower-sample-predictor-default-00003 25h

This section is still pending a follow-up pass after the environment is rebuilt.