|
| 1 | +# Hello World - Dart sample |
| 2 | + |
| 3 | +A simple web app written in the [Dart](www.dartlang.org) programming language |
| 4 | +that you can use for testing. It reads in the env variable `TARGET` and prints |
| 5 | +`"Hello $TARGET"`. If `TARGET` is not specified, it will use `"World"` as `TARGET`. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +* A Kubernetes cluster with Knative installed. Follow the |
| 10 | + [installation instructions](https://github.com/knative/docs/blob/master/install/README.md) if you need |
| 11 | + to create one. |
| 12 | +* [Docker](https://www.docker.com) installed and running on your local machine, |
| 13 | + and a Docker Hub account configured (we'll use it for a container registry). |
| 14 | +* [dart-sdk](https://www.dartlang.org/tools/sdk#install) installed and configured. |
| 15 | + |
| 16 | +## Recreating the sample code |
| 17 | + |
| 18 | +While you can clone all of the code from this directory, it is useful to know how |
| 19 | +to build a hello world Dart application step-by-step. This application can be |
| 20 | +created using the following instructions. |
| 21 | + |
| 22 | +1. Create a new directory and write `pubspec.yaml` as follows: |
| 23 | + |
| 24 | + ```yaml |
| 25 | + name: hello_world_dart |
| 26 | + private: True # let's not accidentally publish this to pub.dartlang.org |
| 27 | + description: >- |
| 28 | + Hello world server example in dart. |
| 29 | + dependencies: |
| 30 | + shelf: ^0.7.3 |
| 31 | + environment: |
| 32 | + sdk: '>=2.0.0 <3.0.0' |
| 33 | + ``` |
| 34 | +
|
| 35 | +2. Install dependencies |
| 36 | +
|
| 37 | + ```shell |
| 38 | + pub get |
| 39 | + ``` |
| 40 | + |
| 41 | +3. Create a new file `bin/main.dart` and write the following code: |
| 42 | + |
| 43 | + ```dart |
| 44 | + import 'dart:io'; |
| 45 | +
|
| 46 | + import 'package:shelf/shelf.dart'; |
| 47 | + import 'package:shelf/shelf_io.dart'; |
| 48 | +
|
| 49 | + void main() { |
| 50 | + // Find port to listen on from environment variable. |
| 51 | + var port = int.tryParse(Platform.environment['PORT']); |
| 52 | + |
| 53 | + // Read $TARGET from environment variable. |
| 54 | + var target = Platform.environment['TARGET'] ?? 'World'; |
| 55 | +
|
| 56 | + // Create handler. |
| 57 | + var handler = Pipeline().addMiddleware(logRequests()).addHandler((request) { |
| 58 | + return Response.ok('Hello $target'); |
| 59 | + }); |
| 60 | +
|
| 61 | + // Serve handler on given port. |
| 62 | + serve(handler, InternetAddress.anyIPv4, port).then((server) { |
| 63 | + print('Serving at http://${server.address.host}:${server.port}'); |
| 64 | + }); |
| 65 | + } |
| 66 | + ``` |
| 67 | + |
| 68 | +4. Create a new file named `Dockerfile`, this file defines instructions for |
| 69 | + dockerizing your applications, for dart apps this can be done as follows: |
| 70 | + |
| 71 | + ```Dockerfile |
| 72 | + FROM google/dart-runtime |
| 73 | + ``` |
| 74 | + |
| 75 | +5. Create a new file, `service.yaml` and copy the following service definition |
| 76 | + into the file. Make sure to replace `{username}` with your Docker Hub username. |
| 77 | + |
| 78 | + ```yaml |
| 79 | + apiVersion: serving.knative.dev/v1alpha1 |
| 80 | + kind: Service |
| 81 | + metadata: |
| 82 | + name: helloworld-dart |
| 83 | + namespace: default |
| 84 | + spec: |
| 85 | + runLatest: |
| 86 | + configuration: |
| 87 | + revisionTemplate: |
| 88 | + spec: |
| 89 | + container: |
| 90 | + image: docker.io/{username}/helloworld-dart |
| 91 | + env: |
| 92 | + - name: TARGET |
| 93 | + value: "Dart Sample v1" |
| 94 | + ``` |
| 95 | + |
| 96 | +## Building and deploying the sample |
| 97 | + |
| 98 | +Once you have recreated the sample code files (or used the files in the sample |
| 99 | +folder) you're ready to build and deploy the sample app. |
| 100 | + |
| 101 | +1. Use Docker to build the sample code into a container. To build and push with |
| 102 | + Docker Hub, run these commands replacing `{username}` with your |
| 103 | + Docker Hub username: |
| 104 | + |
| 105 | + ```shell |
| 106 | + # Build the container on your local machine |
| 107 | + docker build -t {username}/helloworld-dart . |
| 108 | +
|
| 109 | + # Push the container to docker registry |
| 110 | + docker push {username}/helloworld-dart |
| 111 | + ``` |
| 112 | + |
| 113 | +1. After the build has completed and the container is pushed to docker hub, you |
| 114 | + can deploy the app into your cluster. Ensure that the container image value |
| 115 | + in `service.yaml` matches the container you built in |
| 116 | + the previous step. Apply the configuration using `kubectl`: |
| 117 | + |
| 118 | + ```shell |
| 119 | + kubectl apply --filename service.yaml |
| 120 | + ``` |
| 121 | + |
| 122 | +1. Now that your service is created, Knative will perform the following steps: |
| 123 | + * Create a new immutable revision for this version of the app. |
| 124 | + * Network programming to create a route, ingress, service, and load balance for your app. |
| 125 | + * Automatically scale your pods up and down (including to zero active pods). |
| 126 | + |
| 127 | +1. To find the IP address for your service, use |
| 128 | + `kubectl get svc knative-ingressgateway -n istio-system` to get the ingress IP for your |
| 129 | + cluster. If your cluster is new, it may take sometime for the service to get asssigned |
| 130 | + an external IP address. |
| 131 | + |
| 132 | + ```shell |
| 133 | + kubectl get svc knative-ingressgateway --namespace istio-system |
| 134 | +
|
| 135 | + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 136 | + knative-ingressgateway LoadBalancer 10.23.247.74 35.203.155.229 80:32380/TCP,443:32390/TCP,32400:32400/TCP 2d |
| 137 | +
|
| 138 | + ``` |
| 139 | + |
| 140 | +1. To find the URL for your service, use |
| 141 | + ``` |
| 142 | + kubectl get services.serving.knative.dev helloworld-dart --output=custom-columns=NAME:.metadata.name,DOMAIN:.status.domain |
| 143 | + NAME DOMAIN |
| 144 | + helloworld-dart helloworld-dart.default.example.com |
| 145 | + ``` |
| 146 | + |
| 147 | +1. Now you can make a request to your app to see the result. Replace |
| 148 | + `{IP_ADDRESS}` with the address you see returned in the previous step. |
| 149 | + |
| 150 | + ```shell |
| 151 | + curl -H "Host: helloworld-dart.default.example.com" http://{IP_ADDRESS} |
| 152 | + Hello Dart Sample v1 |
| 153 | + ``` |
| 154 | + |
| 155 | +## Removing the sample app deployment |
| 156 | + |
| 157 | +To remove the sample app from your cluster, delete the service record: |
| 158 | + |
| 159 | +```shell |
| 160 | +kubectl delete --filename service.yaml |
| 161 | +``` |
| 162 | + |
0 commit comments