# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Scaling Examples
#
# ## Prerequisites
#
#  * A kubernetes cluster with kubectl configured
#  * curl
#  * grpcurl
#  * pygmentize
#  
#
# ## Setup Seldon Core
#
# Use the setup notebook to [Setup Cluster](seldon_core_setup.ipynb) to setup Seldon Core with an ingress - either Ambassador or Istio.
#
# Then port-forward to that ingress on localhost:8003 in a separate terminal either with:
#
#  * Ambassador: `kubectl port-forward $(kubectl get pods -n seldon -l app.kubernetes.io/name=ambassador -o jsonpath='{.items[0].metadata.name}') -n seldon 8003:8080`
#  * Istio: `kubectl port-forward $(kubectl get pods -l istio=ingressgateway -n istio-system -o jsonpath='{.items[0].metadata.name}') -n istio-system 8003:80`

# !kubectl create namespace seldon

# !kubectl config set-context $(kubectl config current-context) --namespace=seldon

# ## Replica Settings
#
# A deployment that illustrate the settings for
#
#   * `.spec.replicas`
#   * `.spec.predictors[].replicas`
#   * `.spec.predictors[].componentSpecs[].replicas`
#

# !pygmentize resources/model_replicas.yaml

# !kubectl create -f resources/model_replicas.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=test-replicas -o jsonpath='{.items[0].metadata.name}')
# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=test-replicas -o jsonpath='{.items[1].metadata.name}')
# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=test-replicas -o jsonpath='{.items[2].metadata.name}')

# Check each container is running in a deployment with correct number of replicas

# classifierReplicas=!kubectl get deploy test-replicas-example-0-classifier -o jsonpath='{.status.replicas}'
classifierReplicas = int(classifierReplicas[0])
assert(classifierReplicas==2)

# classifier2Replicas=!kubectl get deploy test-replicas-example-1-classifier2 -o jsonpath='{.status.replicas}'
classifier2Replicas = int(classifier2Replicas[0])
assert(classifier2Replicas==3)

# classifier3Replicas=!kubectl get deploy test-replicas-example2-0-classifier3 -o jsonpath='{.status.replicas}'
classifier3Replicas = int(classifier3Replicas[0])
assert(classifier3Replicas==1)

# !curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/test-replicas/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl delete -f resources/model_replicas.yaml

# ## Scale SeldonDeployment

# !pygmentize resources/model_scale.yaml

# !kubectl create -f resources/model_scale.yaml

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=seldon-scale -o jsonpath='{.items[0].metadata.name}')

# replicas=!kubectl get deploy seldon-scale-example-0-classifier -o jsonpath='{.status.replicas}'
replicas = int(replicas[0])
assert(replicas==1)

# !kubectl scale --replicas=2 sdep/seldon-scale

# !kubectl rollout status deploy/$(kubectl get deploy -l seldon-deployment-id=seldon-scale -o jsonpath='{.items[0].metadata.name}')

# replicas=!kubectl get deploy seldon-scale-example-0-classifier -o jsonpath='{.status.replicas}'
replicas = int(replicas[0])
assert(replicas==2)

# !curl -s -d '{"data": {"ndarray":[[1.0, 2.0, 5.0]]}}' \
#    -X POST http://localhost:8003/seldon/seldon/seldon-scale/api/v1.0/predictions \
#    -H "Content-Type: application/json"

# !kubectl delete -f resources/model_scale.yaml



StarcoderdataJupyterScriptsDedupFiltered