Documentation-related updates to Go, Node, and C# samples (#21)

This commit is contained in:
Sam O'Dell 2018-06-26 14:57:53 -07:00 committed by Ryan Gregg
parent 35094b0bd5
commit 32f9363548
3 changed files with 115 additions and 56 deletions

View File

@ -1,19 +1,25 @@
# Hello World - .NET Core sample # Hello World - .NET Core sample
This sample application shows how to create a hello world application in C# using .NET Core 2.1. A simple web app written in C# using .NET Core 2.1 that you can use for testing.
It reads in an env variable 'TARGET' and prints "Hello World: ${TARGET}!" if It reads in an env variable `TARGET` and prints "Hello World: ${TARGET}!". If
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET. TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* You have a Kubernetes cluster with Knative installed. Follow the [installation instructions](https://github.com/knative/install/) if you need to do this. * A Kubernetes Engine cluster with Knative installed. Follow the
* You have installed and initalized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) and have created a project in Google Cloud. [installation instructions](https://github.com/knative/install/) if you need to create one.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. * The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. If you created your cluster using the Google Cloud SDK, this has already be done. If you created your cluster from the Google Cloud Console, run the following command, replacing `CLUSTER_NAME` with the name of your cluster:
```bash
gcloud containers clusters get-credentials CLUSTER_NAME
```
* You have installed [.NET Core SDK 2.1](https://www.microsoft.com/net/core). * You have installed [.NET Core SDK 2.1](https://www.microsoft.com/net/core).
## Steps to recreate the sample code ## Recreating the sample code
While you can clone all of the code from this directory, hello world apps are generally more useful if you build them step-by-step. The following instructions recreate the source files from this folder. While you can clone all of the code from this directory, hello world apps are
generally more useful if you build them step-by-step. The following instructions
recreate the source files from this folder.
1. From the console, create a new empty web project using the dotnet command: 1. From the console, create a new empty web project using the dotnet command:
@ -21,7 +27,8 @@ While you can clone all of the code from this directory, hello world apps are ge
dotnet new web -o helloworld-csharp dotnet new web -o helloworld-csharp
``` ```
1. Update the `CreateWebHostBuilder` definition in `Program.cs` by adding `.UseUrls("http://0.0.0.0:8080")` to define the serving port: 1. Update the `CreateWebHostBuilder` definition in `Program.cs` by adding
`.UseUrls("http://0.0.0.0:8080")` to define the serving port:
```csharp ```csharp
public static IWebHostBuilder CreateWebHostBuilder(string[] args) => public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
@ -39,7 +46,9 @@ While you can clone all of the code from this directory, hello world apps are ge
}); });
``` ```
1. In your project directory, create a file named `Dockerfile` and copy the code block below into it. For detailed instructions on dockerizing a .NET core app, see [dockerizing a .NET core app](https://docs.microsoft.com/en-us/dotnet/core/docker/docker-basics-dotnet-core#dockerize-the-net-core-application). 1. In your project directory, create a file named `Dockerfile` and copy the code
block below into it. For detailed instructions on dockerizing a .NET core app,
see [dockerizing a .NET core app](https://docs.microsoft.com/en-us/dotnet/core/docker/docker-basics-dotnet-core#dockerize-the-net-core-application).
```docker ```docker
FROM microsoft/dotnet:2.1-sdk FROM microsoft/dotnet:2.1-sdk
@ -55,7 +64,10 @@ While you can clone all of the code from this directory, hello world apps are ge
ENTRYPOINT ["dotnet", "out/helloworld-csharp.dll"] ENTRYPOINT ["dotnet", "out/helloworld-csharp.dll"]
``` ```
1. Create a new file, `service.yaml` and copy the following service definition into the file. Make sure to replace `{PROJECT_ID}` with the ID of your Google Cloud project. If you are using docker or another container registry instead, replace the entire image path. 1. Create a new file, `service.yaml` and copy the following service definition
into the file. Make sure to replace `{PROJECT_ID}` with the ID of your Google
Cloud project. If you are using docker or another container registry instead,
replace the entire image path.
```yaml ```yaml
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
@ -75,17 +87,22 @@ While you can clone all of the code from this directory, hello world apps are ge
value: "C# Sample v1" value: "C# Sample v1"
``` ```
## Build and deploy this sample ## Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample folder) you're ready to build and deploy the sample app. Once you have recreated the sample code files (or used the files in the sample
folder) you're ready to build and deploy the sample app.
1. For this example, we'll use Google Cloud Container Builder to build the sample into a container. To use container builder, execute the following gcloud command: 1. For this example, we'll use Google Cloud Container Builder to build the
sample into a container. To use container builder, execute the following gcloud
command:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-csharp gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-csharp
``` ```
1. After the build has completed, you can deploy the app into your cluster. Ensure that the container image value in `service.yaml` matches the container you build in the previous step. Apply the configuration using kubectl: 1. After the build has completed, you can deploy the app into your cluster. Ensure
that the container image value in `service.yaml` matches the container you built in
the previous step. Apply the configuration using `kubectl`:
```shell ```shell
kubectl apply -f service.yaml kubectl apply -f service.yaml
@ -96,7 +113,9 @@ Once you have recreated the sample code files (or used the files in the sample f
* Network programming to create a route, ingress, service, and load balance for your app. * Network programming to create a route, ingress, service, and load balance for your app.
* Automatically scale your pods up and down (including to zero active pods). * Automatically scale your pods up and down (including to zero active pods).
1. To find the URL and IP address for your service, use kubectl to list the ingress points in the cluster: 1. To find the URL and IP address for your service, use `kubectl get ing` to
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell ```shell
kubectl get ing kubectl get ing
@ -105,14 +124,15 @@ Once you have recreated the sample code files (or used the files in the sample f
helloworld-csharp-ingress helloworld-csharp.default.demo-domain.com 35.232.134.1 80 1m helloworld-csharp-ingress helloworld-csharp.default.demo-domain.com 35.232.134.1 80 1m
``` ```
1. Now you can make a request to your app to see the result. Replace `{IP_ADDRESS}` with the address you see returned in the previous step. 1. Now you can make a request to your app to see the result. Replace
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell ```shell
curl -H "Host: helloworld-csharp.default.demo-domain.com" http://{IP_ADDRESS} curl -H "Host: helloworld-csharp.default.demo-domain.com" http://{IP_ADDRESS}
Hello World! Hello World!
``` ```
## Remove the sample app deployment ## Removing the sample app deployment
To remove the sample app from your cluster, delete the service record: To remove the sample app from your cluster, delete the service record:

View File

@ -1,22 +1,27 @@
# Hello World - Go # Hello World - Go
A simple web app written in Go that you can use for testing. A simple web app written in Go that you can use for testing.
It reads in an env variable 'TARGET' and prints "Hello World: ${TARGET}!" if It reads in an env variable `TARGET` and prints "Hello World: ${TARGET}!". If
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET. TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* You have a Kubernetes cluster with Knative installed. Follow the [installation instructions](https://github.com/knative/install/) if you need to do this. * A Kubernetes Engine cluster with Knative installed. Follow the
* You have installed and initalized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) and have created a project in Google Cloud. [installation instructions](https://github.com/knative/install/) if you need to create one.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. * The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. If you created your cluster using the Google Cloud SDK, this has already be done. If you created your cluster from the Google Cloud Console, run the following command, replacing `CLUSTER_NAME` with the name of your cluster:
```bash
gcloud containers clusters get-credentials CLUSTER_NAME
```
## Steps to recreate the sample code ## Recreating the sample code
While you can clone all of the code from this directory, hello world While you can clone all of the code from this directory, hello world
apps are generally more useful if you build them step-by-step. The apps are generally more useful if you build them step-by-step. The
following instructions recreate the source files from this folder. following instructions recreate the source files from this folder.
1. Create a new file named `helloworld.go` and paste the following code. This code creates a basic web server which listens on port 8080: 1. Create a new file named `helloworld.go` and paste the following code. This
code creates a basic web server which listens on port 8080:
```go ```go
package main package main
@ -47,7 +52,9 @@ following instructions recreate the source files from this folder.
} }
``` ```
1. In your project directory, create a file named `Dockerfile` and copy the code block below into it. For detailed instructions on dockerizing a Go app, see [Deploying Go servers with Docker](https://blog.golang.org/docker). 1. In your project directory, create a file named `Dockerfile` and copy the code
block below into it. For detailed instructions on dockerizing a Go app, see
[Deploying Go servers with Docker](https://blog.golang.org/docker).
```docker ```docker
# Start from a Debian image with the latest version of Go installed # Start from a Debian image with the latest version of Go installed
@ -69,7 +76,10 @@ following instructions recreate the source files from this folder.
EXPOSE 8080 EXPOSE 8080
``` ```
1. Create a new file, `service.yaml` and copy the following service definition into the file. Make sure to replace `{PROJECT_ID}` with the ID of your Google Cloud project. If you are using docker or another container registry instead, replace the entire image path. 1. Create a new file, `service.yaml` and copy the following service definition
into the file. Make sure to replace `{PROJECT_ID}` with the ID of your Google
Cloud project. If you are using docker or another container registry instead,
replace the entire image path.
```yaml ```yaml
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
@ -89,44 +99,51 @@ following instructions recreate the source files from this folder.
value: "Go Sample v1" value: "Go Sample v1"
``` ```
## Build and deploy this sample ## Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample folder) you're ready to build and deploy the sample app. Once you have recreated the sample code files (or used the files in the sample
folder) you're ready to build and deploy the sample app.
1. For this example, we'll use Google Cloud Container Builder to build the sample into a container. To use container builder, execute the following gcloud command: 1. For this example, we'll use Google Cloud Container Builder to build the
sample into a container. To use container builder, execute the following gcloud
command:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-go gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-go
``` ```
1. After the build has completed, you can deploy the app into your cluster. Ensure that the container image value in `service.yaml` matches the container you build in the previous step. Apply the configuration using kubectl: 1. After the build has completed, you can deploy the app into your cluster.
Ensure that the container image value in `service.yaml` matches the container
you built in the previous step. Apply the configuration using kubectl:
```shell ```shell kubectl apply -f service.yaml ```
kubectl apply -f service.yaml
```
1. Now that your service is created, Knative will perform the following steps: 1. Now that your service is created, Knative will perform the following steps:
* Create a new immutable revision for this version of the app. * Create a new immutable revision for this version of the app.
* Network programming to create a route, ingress, service, and load balance for your app. * Network programming to create a route, ingress, service, and load balancer
for your app.
* Automatically scale your pods up and down (including to zero active pods). * Automatically scale your pods up and down (including to zero active pods).
1. To find the URL and IP address for your service, use kubectl to list the ingress points in the cluster. You may need to wait a few seconds for the ingress point to be created, if you don't see it right away. 1. To find the URL and IP address for your service, use `kubectl get ing` to
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell ```shell
kubectl get ing --watch kubectl get ing
NAME HOSTS ADDRESS PORTS AGE NAME HOSTS ADDRESS PORTS AGE
helloworld-go-ingress helloworld-go.default.demo-domain.com,*.helloworld-go.default.demo-domain.com 35.232.134.1 80 1m helloworld-go-ingress helloworld-go.default.demo-domain.com,*.helloworld-go.default.demo-domain.com 35.232.134.1 80 1m
``` ```
1. Now you can make a request to your app to see the result. Replace `{IP_ADDRESS}` with the address you see returned in the previous step. 1. Now you can make a request to your app to see the results. Replace
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell ```shell
curl -H "Host: helloworld-go.default.demo-domain.com" http://{IP_ADDRESS} curl -H "Host: helloworld-go.default.demo-domain.com" http://{IP_ADDRESS}
Hello World: NOT SPECIFIED Hello World: NOT SPECIFIED
``` ```
## Remove the sample app deployment ## Removing the sample app deployment
To remove the sample app from your cluster, delete the service record: To remove the sample app from your cluster, delete the service record:

View File

@ -1,19 +1,30 @@
# Hello World - Node.js # Hello World - Node.js
A simple web app in Node that you can use for testing. A simple web app written in Node.js that you can use for testing.
It reads in an env variable 'TARGET' and prints "Hello World: ${TARGET}!" if It reads in an env variable `TARGET` and prints "Hello World: ${TARGET}!". If
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET. TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* You have a Kubernetes cluster with Knative installed. Follow the [installation instructions](https://github.com/knative/install/) if you need to do this. * A Kubernetes Engine cluster with Knative installed. Follow the
* You have installed and initalized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) and have created a project in Google Cloud. [installation instructions](https://github.com/knative/install/) if you need to create one.
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. * You have `kubectl` configured to connect to the Kubernetes cluster running Knative.
If you created your cluster using the Google Cloud SDK, this has already be done. If you
created your cluster from the Google Cloud Console, run the following command, replacing
`CLUSTER_NAME` with the name of your cluster:
```bash
gcloud containers clusters get-credentials CLUSTER_NAME
```
* [Node.js](https://nodejs.org/en/) installed and configured. * [Node.js](https://nodejs.org/en/) installed and configured.
## Steps to recreate the sample code ## Recreating the sample code
1. Create a new directory and initalize npm. You can accept the defaults, but change the entry point to app.js to be consistent with the sample code here. While you can clone all of the code from this directory, hello world apps are
generally more useful if you build them step-by-step. The following instructions
recreate the source files from this folder.
1. Create a new directory and initalize `npm`. You can accept the defaults, but change the entry point to `app.js` to be consistent with the sample code here.
```shell ```shell
npm init npm init
@ -35,7 +46,7 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
npm install express --save npm install express --save
``` ```
1. Create a new file named 'app.js' and paste the following code: 1. Create a new file named `app.js` and paste the following code:
```js ```js
const express = require('express'); const express = require('express');
@ -54,7 +65,7 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
}); });
``` ```
1. Modify the package.json file to add a start command to the scripts section: 1. Modify the `package.json` file to add a start command to the scripts section:
```json ```json
{ {
@ -95,7 +106,10 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
CMD [ "npm", "start" ] CMD [ "npm", "start" ]
``` ```
1. Create a new file, `service.yaml` and copy the following service definition into the file. Make sure to replace `{PROJECT_ID}` with the ID of your Google Cloud project. If you are using docker or another container registry instead, replace the entire image path. 1. Create a new file, `service.yaml` and copy the following service
definitioninto the file. Make sure to replace `{PROJECT_ID}` with the ID of your
Google Cloud project. If you are using docker or another container registry
instead, replace the entire image path.
```yaml ```yaml
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
@ -115,17 +129,22 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
value: "Node.js Sample v1" value: "Node.js Sample v1"
``` ```
## Build and deploy this sample ## Building and deploying the sample
Once you have recreated the sample code files (or used the files in the sample folder) you're ready to build and deploy the sample app. Once you have recreated the sample code files (or used the files in the sample
folder) you're ready to build and deploy the sample app.
1. For this example, we'll use Google Cloud Container Builder to build the sample into a container. To use container builder, execute the following gcloud command: 1. For this example, we'll use Google Cloud Container Builder to build the
sample into a container. To use container builder, execute the following gcloud
command:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-nodejs gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-nodejs
``` ```
1. After the build has completed, you can deploy the app into your cluster. Ensure that the container image value in `service.yaml` matches the container you build in the previous step. Apply the configuration using kubectl: 1. After the build has completed, you can deploy the app into your cluster.
Ensure that the container image value in `service.yaml` matches the container
you built in the previous step. Apply the configuration using kubectl:
```shell ```shell
kubectl apply -f service.yaml kubectl apply -f service.yaml
@ -136,23 +155,26 @@ Once you have recreated the sample code files (or used the files in the sample f
* Network programming to create a route, ingress, service, and load balance for your app. * Network programming to create a route, ingress, service, and load balance for your app.
* Automatically scale your pods up and down (including to zero active pods). * Automatically scale your pods up and down (including to zero active pods).
1. To find the URL and IP address for your service, use kubectl to list the ingress points in the cluster. You may need to wait a few seconds for the ingress point to be created, if you don't see it right away. 1. To find the URL and IP address for your service, use `kubectl get ing` to
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell ```shell
kubectl get ing --watch kubectl get ing
NAME HOSTS ADDRESS PORTS AGE NAME HOSTS ADDRESS PORTS AGE
helloworld-nodejs-ingress helloworld-nodejs.default.demo-domain.com 35.232.134.1 80 1m helloworld-nodejs-ingress helloworld-nodejs.default.demo-domain.com 35.232.134.1 80 1m
``` ```
1. Now you can make a request to your app to see the result. Replace `{IP_ADDRESS}` with the address you see returned in the previous step. 1. Now you can make a request to your app to see the result. Replace
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell ```shell
curl -H "Host: helloworld-nodejs.default.demo-domain.com" http://{IP_ADDRESS} curl -H "Host: helloworld-nodejs.default.demo-domain.com" http://{IP_ADDRESS}
Hello World: NOT SPECIFIED Hello World: NOT SPECIFIED
``` ```
## Remove the sample app deployment ## Removing the sample app deployment
To remove the sample app from your cluster, delete the service record: To remove the sample app from your cluster, delete the service record: