Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the web-ui for the mnist example #473

Merged
merged 3 commits into from
Jan 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mnist/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build/**
build/**
web-ui/static/tmp/**
140 changes: 48 additions & 92 deletions mnist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,32 @@ There are various ways to monitor workflow/training job. In addition to using `k

TODO: This section needs to be updated

Tensorboard is deployed just before training starts. To connect:
Configure TensorBoard to point to your model location

```
PODNAME=$(kubectl get pod -l app=tensorboard-${JOB_NAME} -o jsonpath='{.items[0].metadata.name}')
kubectl port-forward ${PODNAME} 6006:6006
ks param set tensorboard --env=${KSENV} logDir ${LOGDIR}

```

Assuming you followed the directions above if you used GCS you can use the following value

```
LOGDIR=gs://${BUCKET}/${MODEL_PATH}
```

Then you can deploy tensorboard

```
ks apply ${KSENV} -c tensorboard
```

Tensorboard can now be accessed at [http://127.0.0.1:6006](http://127.0.0.1:6006).
To access tensorboard using port-forwarding

```
kubectl -n jlewi port-forward service/tensorboard-tb 8090:80
```
Tensorboard can now be accessed at [http://127.0.0.1:8090](http://127.0.0.1:8090).


## Serving the model

Expand Down Expand Up @@ -534,95 +552,33 @@ TODO: Add instructions

TODO: Add instructions

### Create the K8s service

Next we need to create a K8s service to route traffic to our model

```
ks apply jlewi -c mnist-service
```

By default the workflow deploys our model via Tensorflow Serving. Included in this example is a client that can query your model and provide results:

```
POD_NAME=$(kubectl get pod -l=app=mnist-${JOB_NAME} -o jsonpath='{.items[0].metadata.name}')
kubectl port-forward ${POD_NAME} 9000:9000 &
TF_MNIST_IMAGE_PATH=data/7.png python mnist_client.py
```

This should result in output similar to this, depending on how well your model was trained:
```
outputs {
key: "classes"
value {
dtype: DT_UINT8
tensor_shape {
dim {
size: 1
}
}
int_val: 7
}
}
outputs {
key: "predictions"
value {
dtype: DT_FLOAT
tensor_shape {
dim {
size: 1
}
dim {
size: 10
}
}
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 0.0
float_val: 1.0
float_val: 0.0
float_val: 0.0
}
}


............................
............................
............................
............................
............................
............................
............................
..............@@@@@@........
..........@@@@@@@@@@........
........@@@@@@@@@@@@........
........@@@@@@@@.@@@........
........@@@@....@@@@........
................@@@@........
...............@@@@.........
...............@@@@.........
...............@@@..........
..............@@@@..........
..............@@@...........
.............@@@@...........
.............@@@............
............@@@@............
............@@@.............
............@@@.............
...........@@@..............
..........@@@@..............
..........@@@@..............
..........@@................
............................
Your model says the above number is... 7!
```

You can also omit `TF_MNIST_IMAGE_PATH`, and the client will pick a random number from the mnist test data. Run it repeatedly and see how your model fares!
## Web Front End

The example comes with a simple web front end that can be used with your model.

To deploy the web front end

```
ks apply ${ENV} -c web-ui
```

### Connecting via port forwarding

To connect to the web app via port-forwarding

```
kubectl -n ${NAMESPACE} port-forward svc/web-ui 8080:80
```

You should now be able to open up the web app at [http://localhost:8080](http://localhost:8080).

### Using IAP on GCP

If you are using GCP and have set up IAP then you can access the web UI at

```
https://${DEPLOYMENT}.endpoints.${PROJECT}.cloud.goog/${NAMESPACE}/mnist/
```

## Conclusion and Next Steps

Expand Down
10 changes: 8 additions & 2 deletions mnist/image_build.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@
contextDir: "."
},

steps: modelSteps.steps + ksonnetSteps.steps,
images: modelSteps.images + ksonnetSteps.images,
local uiSteps = subGraphTemplate {
name: "web-ui",
dockerFile: "./web-ui/Dockerfile",
contextDir: "./web-ui"
},

steps: modelSteps.steps + ksonnetSteps.steps + uiSteps.steps,
images: modelSteps.images + ksonnetSteps.images + uiSteps.images,
}
15 changes: 14 additions & 1 deletion mnist/ks_app/components/params.libsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,23 @@
"mnist-service": {
enablePrometheus: 'true',
injectIstio: 'false',
modelName: 'null',
modelName: 'mnist',
name: 'mnist-service',
serviceType: 'ClusterIP',
trafficRule: 'v1:100',
},
"tensorboard": {
image: "tensorflow/tensorflow:1.11.0",
logDir: "gs://example/to/model/logdir",
name: "tensorboard",
},
"web-ui": {
containerPort: 5000,
image: "gcr.io/kubeflow-examples/mnist/web-ui:v20190112-v0.2-142-g3b38225",
name: "web-ui",
replicas: 1,
servicePort: 80,
type: "ClusterIP",
},
},
}
114 changes: 114 additions & 0 deletions mnist/ks_app/components/tensorboard.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
// TODO: Generalize to use S3. We can follow the pattern of training that
// takes parameters to specify environment variables and secret which can be customized
// for GCS, S3 as needed.
local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components.tensorboard;

local k = import "k.libsonnet";

local name = params.name;
local namespace = env.namespace;
local service = {
apiVersion: "v1",
kind: "Service",
metadata: {
name: name + "-tb",
namespace: env.namespace,
annotations: {
"getambassador.io/config":
std.join("\n", [
"---",
"apiVersion: ambassador/v0",
"kind: Mapping",
"name: " + name + "_mapping",
"prefix: /" + env.namespace + "/tensorboard/mnist",
"rewrite: /",
"service: " + name + "-tb." + namespace,
"---",
"apiVersion: ambassador/v0",
"kind: Mapping",
"name: " + name + "_mapping_data",
"prefix: /" + env.namespace + "/tensorboard/mnist/data/",
"rewrite: /data/",
"service: " + name + "-tb." + namespace,
]),
}, //annotations
},
spec: {
ports: [
{
name: "http",
port: 80,
targetPort: 80,
},
],
selector: {
app: "tensorboard",
"tb-job": name,
},
},
};

local deployment = {
apiVersion: "apps/v1beta1",
kind: "Deployment",
metadata: {
name: name + "-tb",
namespace: env.namespace,
},
spec: {
replicas: 1,
template: {
metadata: {
labels: {
app: "tensorboard",
"tb-job": name,
},
name: name,
namespace: namespace,
},
spec: {
containers: [
{
command: [
"/usr/local/bin/tensorboard",
"--logdir=" + params.logDir,
"--port=80",
],
image: params.image,
name: "tensorboard",
ports: [
{
containerPort: 80,
},
],
env: [
{
name: "GOOGLE_APPLICATION_CREDENTIALS",
value: "/secret/gcp-credentials/user-gcp-sa.json",
},
],
volumeMounts: [
{
mountPath: "/secret/gcp-credentials",
name: "gcp-credentials",
},
],
},
],

volumes: [
{
name: "gcp-credentials",
secret: {
secretName: "user-gcp-sa",
},
},
],
},
},
},
};

std.prune(k.core.v1.list.new([service, deployment]))

72 changes: 72 additions & 0 deletions mnist/ks_app/components/web-ui.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local env = std.extVar("__ksonnet/environments");
local params = std.extVar("__ksonnet/params").components["web-ui"];
[
{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": params.name,
"namespace": env.namespace,
annotations: {
"getambassador.io/config":
std.join("\n", [
"---",
"apiVersion: ambassador/v0",
"kind: Mapping",
"name: " + params.name + "_mapping",
"prefix: /" + env.namespace + "/mnist/",
"rewrite: /",
"service: " + params.name + "." + env.namespace,
]),
}, //annotations
},
"spec": {
"ports": [
{
"port": params.servicePort,
"targetPort": params.containerPort
}
],
"selector": {
"app": params.name
},
"type": params.type
}
},
{
"apiVersion": "apps/v1beta2",
"kind": "Deployment",
"metadata": {
"name": params.name,
"namespace": env.namespace,
},
"spec": {
"replicas": params.replicas,
"selector": {
"matchLabels": {
"app": params.name
},
},
"template": {
"metadata": {
"labels": {
"app": params.name
}
},
"spec": {
"containers": [
{
"image": params.image,
"name": params.name,
"ports": [
{
"containerPort": params.containerPort
}
]
}
]
}
}
}
}
]
Loading