Convert hello world samples to using Docker (#42)

This commit is contained in:
Ryan Gregg 2018-06-28 16:03:16 -07:00 committed by GitHub
parent 031584d128
commit 723b407c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 289 additions and 327 deletions

View File

@ -1 +1,13 @@
# Samples for knative/serving project # Samples for knative/serving project
* Hello World - Quick introduction that highlights how to deploy an app using
Knative Serving.
* [C#](helloworld-csharp/README.md)
* [Go](helloworld-go/README.md)
* [Java](helloworld-java/README.md)
* [Node.js](helloworld-nodejs/README.md)
* [PHP](helloworld-php/README.md)
* [Python](helloworld-python/README.md)
* [Ruby](helloworld-ruby/README.md)
* [Rust](helloworld-rust/README.md)

View File

@ -6,13 +6,11 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* A Kubernetes Engine cluster with Knative installed. Follow the * A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need to create one. [installation instructions](https://github.com/knative/install/) if you need
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized. to create one.
* 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: * [Docker](https://www.docker.com) installed and running on your local machine,
```bash and a Docker Hub account configured (we'll use it for a container registry).
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).
## Recreating the sample code ## Recreating the sample code
@ -36,13 +34,14 @@ recreate the source files from this folder.
.UseStartup<Startup>().UseUrls("http://0.0.0.0:8080"); .UseStartup<Startup>().UseUrls("http://0.0.0.0:8080");
``` ```
1. Update the `app.Run(...)` statement in `Startup.cs` to read and return the TARGET environment variable: 1. Update the `app.Run(...)` statement in `Startup.cs` to read and return the
TARGET environment variable:
```csharp ```csharp
app.Run(async (context) => app.Run(async (context) =>
{ {
var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED"; var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED";
await context.Response.WriteAsync($"Hello World: {target}"); await context.Response.WriteAsync($"Hello World: {target}\n");
}); });
``` ```
@ -65,9 +64,7 @@ see [dockerizing a .NET core app](https://docs.microsoft.com/en-us/dotnet/core/d
``` ```
1. Create a new file, `service.yaml` and copy the following service definition 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 into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -81,7 +78,7 @@ replace the entire image path.
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-csharp image: docker.io/{username}/helloworld-csharp
env: env:
- name: TARGET - name: TARGET
value: "C# Sample v1" value: "C# Sample v1"
@ -92,16 +89,21 @@ replace the entire image path.
Once you have recreated the sample code files (or used the files in 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. 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 1. Use Docker to build the sample code into a container. To build and push with
sample into a container. To use container builder, execute the following gcloud Docker Hub, run these commands replacing `{username}` with your
command: Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-csharp # Build the container on your local machine
docker build -t {username}/helloworld-csharp .
# Push the container to docker registry
docker push {username}/helloworld-csharp
``` ```
1. After the build has completed, you can deploy the app into your cluster. Ensure 1. After the build has completed and the container is pushed to docker hub, you
that the container image value in `service.yaml` matches the container you built in 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`: the previous step. Apply the configuration using `kubectl`:
```shell ```shell

View File

@ -28,7 +28,7 @@ namespace helloworld_csharp
app.Run(async (context) => app.Run(async (context) =>
{ {
var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED"; var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED";
await context.Response.WriteAsync($"Hello World: {target}"); await context.Response.WriteAsync($"Hello World: {target}\n");
}); });
} }
} }

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-csharp image: docker.io/{username}/helloworld-csharp
env: env:
- name: TARGET - name: TARGET
value: "C# Sample v1" value: "C# Sample v1"

View File

@ -1,4 +1,4 @@
# Hello World - Go # Hello World - Go sample
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
@ -6,13 +6,11 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* A Kubernetes Engine cluster with Knative installed. Follow the * A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need to create one. [installation instructions](https://github.com/knative/install/) if you need
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized. to create one.
* 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: * [Docker](https://www.docker.com) installed and running on your local machine,
```bash and a Docker Hub account configured (we'll use it for a container registry).
gcloud containers clusters get-credentials CLUSTER_NAME
```
## Recreating the sample code ## Recreating the sample code
@ -77,9 +75,7 @@ block below into it. For detailed instructions on dockerizing a Go app, see
``` ```
1. Create a new file, `service.yaml` and copy the following service definition 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 into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -93,7 +89,7 @@ replace the entire image path.
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-go image: docker.io/{username}/helloworld-go
env: env:
- name: TARGET - name: TARGET
value: "Go Sample v1" value: "Go Sample v1"
@ -104,24 +100,30 @@ replace the entire image path.
Once you have recreated the sample code files (or used the files in 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. 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 1. Use Docker to build the sample code into a container. To build and push with
sample into a container. To use container builder, execute the following gcloud Docker Hub, run these commands replacing `{username}` with your
command: Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-go # Build the container on your local machine
docker build -t {username}/helloworld-go .
# Push the container to docker registry
docker push {username}/helloworld-go
``` ```
1. After the build has completed, you can deploy the app into your cluster. 1. After the build has completed and the container is pushed to docker hub, you
Ensure that the container image value in `service.yaml` matches the container can deploy the app into your cluster. Ensure that the container image value
you built in the previous step. Apply the configuration using kubectl: in `service.yaml` matches the container you built in
the previous step. Apply the configuration using `kubectl`:
```shell kubectl apply -f service.yaml ``` ```shell
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 balancer * Network programming to create a route, ingress, service, and load balance for your app.
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 get ing` to 1. To find the URL and IP address for your service, use `kubectl get ing` to

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2018 Google LLC Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View File

@ -1,48 +0,0 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: serving.knative.dev/v1alpha1
kind: Configuration
metadata:
name: configuration-example
namespace: default
spec:
revisionTemplate:
metadata:
labels:
knative.dev/type: app
spec:
container:
# This is the Go import path for the binary to containerize
# and substitute here.
image: github.com/knative/serving/sample/helloworld
env:
- name: TARGET
value: shiniestnewestversion
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 3
periodSeconds: 3
---
apiVersion: serving.knative.dev/v1alpha1
kind: Route
metadata:
name: route-example
namespace: default
spec:
traffic:
- configurationName: configuration-example
percent: 100

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-go image: docker.io/{username}/helloworld-go
env: env:
- name: TARGET - name: TARGET
value: "Go Sample v1" value: "Go Sample v1"

View File

@ -1,38 +0,0 @@
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
apiVersion: serving.knative.dev/v1alpha1
kind: Configuration
metadata:
name: configuration-example
namespace: default
spec:
revisionTemplate:
metadata:
labels:
knative.dev/type: app
spec:
container:
# This is the Go import path for the binary to containerize
# and substitute here.
image: github.com/knative/serving/sample/helloworld
env:
- name: TARGET
value: updatedversion
readinessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 3
periodSeconds: 3

View File

@ -1,18 +1,16 @@
# Hello World - Spring Boot Java sample # Hello World - Spring Boot Java sample
A simple web app written in Java using Spring Boot 2.0 that you can use for testing Knative. A simple web app written in Java using Spring Boot 2.0 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
* A Kubernetes Engine cluster with Knative installed. Follow the * A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/docs/tree/master/install) if you need to create one. [installation instructions](https://github.com/knative/install/) if you need
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized. to create one.
* 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: * [Docker](https://www.docker.com) installed and running on your local machine,
```bash and a Docker Hub account configured (we'll use it for a container registry).
gcloud containers clusters get-credentials CLUSTER_NAME
```
* You have installed [Java SE 8 or later JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html). * You have installed [Java SE 8 or later JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html).
## Recreating the sample code ## Recreating the sample code
@ -90,9 +88,7 @@ For additional information on multi-stage docker builds for Java see
``` ```
1. Create a new file, `service.yaml` and copy the following service definition 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 into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -106,7 +102,7 @@ replace the entire image path.
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-java image: docker.io/{username}/helloworld-java
env: env:
- name: TARGET - name: TARGET
value: "Spring Boot Sample v1" value: "Spring Boot Sample v1"
@ -117,16 +113,21 @@ replace the entire image path.
Once you have recreated the sample code files (or used the files in 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. 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 1. Use Docker to build the sample code into a container. To build and push with
sample into a container. To use container builder, execute the following gcloud Docker Hub, run these commands replacing `{username}` with your
command: Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-java # Build the container on your local machine
docker build -t {username}/helloworld-java .
# Push the container to docker registry
docker push {username}/helloworld-java
``` ```
1. After the build has completed, you can deploy the app into your cluster. Ensure 1. After the build has completed and the container is pushed to docker hub, you
that the container image value in `service.yaml` matches the container you built in 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`: the previous step. Apply the configuration using `kubectl`:
```shell ```shell

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-java image: docker.io/{username}/helloworld-java
env: env:
- name: TARGET - name: TARGET
value: "Spring Boot Sample v1" value: "Spring Boot Sample v1"

View File

@ -1,4 +1,4 @@
# Hello World - Node.js # Hello World - Node.js sample
A simple web app written in Node.js 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
@ -6,16 +6,11 @@ TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* A Kubernetes Engine cluster with Knative installed. Follow the * A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need to create one. [installation instructions](https://github.com/knative/install/) if you need
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized. to create one.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. * [Docker](https://www.docker.com) installed and running on your local machine,
If you created your cluster using the Google Cloud SDK, this has already be done. If you and a Docker Hub account configured (we'll use it for a container registry).
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.
## Recreating the sample code ## Recreating the sample code
@ -24,7 +19,9 @@ 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 generally more useful if you build them step-by-step. The following instructions
recreate the source files from this folder. 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. 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
@ -40,7 +37,7 @@ recreate the source files from this folder.
license: (ISC) Apache-2.0 license: (ISC) Apache-2.0
``` ```
1. Install the `express` dependency: 1. Install the `express` package:
```shell ```shell
npm install express --save npm install express --save
@ -82,7 +79,9 @@ recreate the source files from this folder.
} }
``` ```
1. In your project directory, create a file named `Dockerfile` and paste the following code. For detailed instructions on dockerizing a Node.js app, see [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/). 1. In your project directory, create a file named `Dockerfile` and copy the code
block below into it. For detailed instructions on dockerizing a Node.js app,
see [Dockerizing a Node.js web app](https://nodejs.org/en/docs/guides/nodejs-docker-webapp/).
```docker ```docker
FROM node:8 FROM node:8
@ -106,10 +105,8 @@ recreate the source files from this folder.
CMD [ "npm", "start" ] CMD [ "npm", "start" ]
``` ```
1. Create a new file, `service.yaml` and copy the following service 1. Create a new file, `service.yaml` and copy the following service definition
definitioninto the file. Make sure to replace `{PROJECT_ID}` with the ID of your into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -123,7 +120,7 @@ instead, replace the entire image path.
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-nodejs image: docker.io/{username}/helloworld-nodejs
env: env:
- name: TARGET - name: TARGET
value: "Node.js Sample v1" value: "Node.js Sample v1"
@ -134,17 +131,22 @@ instead, replace the entire image path.
Once you have recreated the sample code files (or used the files in 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. 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 1. Use Docker to build the sample code into a container. To build and push with
sample into a container. To use container builder, execute the following gcloud Docker Hub, run these commands replacing `{username}` with your
command: Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-nodejs # Build the container on your local machine
docker build -t {username}/helloworld-nodejs .
# Push the container to docker registry
docker push {username}/helloworld-nodejs
``` ```
1. After the build has completed, you can deploy the app into your cluster. 1. After the build has completed and the container is pushed to docker hub, you
Ensure that the container image value in `service.yaml` matches the container can deploy the app into your cluster. Ensure that the container image value
you built in the previous step. Apply the configuration using kubectl: 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

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2018 Google LLC Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-nodejs image: docker.io/{username}/helloworld-nodejs
env: env:
- name: TARGET - name: TARGET
value: "Node.js Sample v1" value: "Node.js Sample v1"

View File

@ -1,19 +1,16 @@
# Hello World - PHP # Hello World - PHP sample
A simple web app written in PHP that you can use for testing.application in PHP. A simple web app written in PHP 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
* A Kubernetes Engine cluster with Knative installed. Follow the * A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need to create one. [installation instructions](https://github.com/knative/install/) if you need
* The [Google Cloud SDK](https://cloud.google.com/sdk/docs/) is installed and initalized. to create one.
* 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: * [Docker](https://www.docker.com) installed and running on your local machine,
and a Docker Hub account configured (we'll use it for a container registry).
```bash
gcloud containers clusters get-credentials CLUSTER_NAME
```
## Recreating the sample code ## Recreating the sample code
@ -22,10 +19,12 @@ 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 directory and cd into it: 1. Create a new directory and cd into it:
````shell ````shell
mkdir app mkdir app
cd app cd app
```` ````
1. Create a file named `index.php` and copy the code block below into it: 1. Create a file named `index.php` and copy the code block below into it:
```php ```php
@ -47,9 +46,8 @@ See [official PHP docker image](https://hub.docker.com/_/php/) for more details.
RUN sed -i 's/80/8080/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf RUN sed -i 's/80/8080/g' /etc/apache2/sites-available/000-default.conf /etc/apache2/ports.conf
``` ```
1. Create a file named `service.yaml` and copy the following service definition into the file. 1. Create a new file, `service.yaml` and copy the following service definition
Make sure to replace `{PROJECT_ID}` with the ID of your Google Cloud project. into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -63,7 +61,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-php image: docker.io/{username}/helloworld-php
env: env:
- name: TARGET - name: TARGET
value: "PHP Sample v1" value: "PHP Sample v1"
@ -74,16 +72,22 @@ If you are using docker or another container registry instead, replace the entir
Once you have recreated the sample code files (or used the files in the sample folder) 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. 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. 1. Use Docker to build the sample code into a container. To build and push with
To use container builder, execute the following gcloud command: Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-php # Build the container on your local machine
docker build -t {username}/helloworld-php .
# Push the container to docker registry
docker push {username}/helloworld-php
``` ```
1. After the build has completed, you can deploy the app into your cluster. 1. After the build has completed and the container is pushed to docker hub, you
Ensure that the container image value in `service.yaml` matches the container can deploy the app into your cluster. Ensure that the container image value
you build in the previous step. Apply the configuration using kubectl: 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
@ -103,8 +107,8 @@ you build in the previous step. Apply the configuration using kubectl:
helloworld-php-ingress helloworld-php.default.demo-domain.com,*.helloworld-php.default.demo-domain.com 35.232.134.1 80 1m helloworld-php-ingress helloworld-php.default.demo-domain.com,*.helloworld-php.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}` 1. Now you can make a request to your app to see the result. Replace
with the address you see returned in the previous step. `{IP_ADDRESS}` with the address you see returned in the previous step.
```shell ```shell
curl -H "Host: helloworld-php.default.demo-domain.com" http://{IP_ADDRESS} curl -H "Host: helloworld-php.default.demo-domain.com" http://{IP_ADDRESS}

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-php image: docker.io/{username}/helloworld-php
env: env:
- name: TARGET - name: TARGET
value: "PHP Sample v1" value: "PHP Sample v1"

View File

@ -1,17 +1,16 @@
# Hello World - Python sample # Hello World - Python sample
This sample application shows how to create a hello world application in Python. A simple web app written in Python that you can use for testing.
When called, this application reads an env variable 'TARGET' It reads in an env variable `TARGET` and prints "Hello World: ${TARGET}!". If
and prints "Hello World: ${TARGET}!". TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
If TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* You have a Kubernetes cluster with Knative installed. * A Kubernetes cluster with Knative installed. Follow the
Follow the [installation instructions](https://github.com/knative/install/) if you need to do this. [installation instructions](https://github.com/knative/install/) if you need
* You have installed and initialized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) to create one.
and have created a project in Google Cloud. * [Docker](https://www.docker.com) installed and running on your local machine,
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. and a Docker Hub account configured (we'll use it for a container registry).
## Steps to recreate the sample code ## Steps to recreate the sample code
@ -20,6 +19,7 @@ generally more useful if you build them step-by-step.
The following instructions recreate the source files from this folder. The following instructions recreate the source files from this folder.
1. Create a new directory and cd into it: 1. Create a new directory and cd into it:
````shell ````shell
mkdir app mkdir app
cd app cd app
@ -58,9 +58,8 @@ See [official Python docker image](https://hub.docker.com/_/python/) for more de
CMD ["app.py"] CMD ["app.py"]
``` ```
1. Create a file named `service.yaml` and copy the following service definition into the file. 1. Create a new file, `service.yaml` and copy the following service definition
Make sure to replace `{PROJECT_ID}` with the ID of your Google Cloud project. into the file. Make sure to replace `{username}` with your Docker Hub username.
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
@ -74,7 +73,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-python image: docker.io/{username}/helloworld-python
env: env:
- name: TARGET - name: TARGET
value: "Python Sample v1" value: "Python Sample v1"
@ -82,20 +81,25 @@ If you are using docker or another container registry instead, replace the entir
## Build and deploy this sample ## Build and deploy this sample
Once you have recreated the sample code files (or used the files in the sample folder) Once you have recreated the sample code files (or used the files in the sample
you're ready to build and deploy the sample app. 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. 1. Use Docker to build the sample code into a container. To build and push with
To use container builder, execute the following gcloud command. Make sure to replace `${PROJECT_ID}` Docker Hub, run these commands replacing `{username}` with your
with the ID of your Google Cloud project. Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-python # Build the container on your local machine
docker build -t {username}/helloworld-python .
# Push the container to docker registry
docker push {username}/helloworld-python
``` ```
1. After the build has completed, you can deploy the app into your cluster. 1. After the build has completed and the container is pushed to docker hub, you
Ensure that the container image value in `service.yaml` matches the container you build in the previous step. can deploy the app into your cluster. Ensure that the container image value
Apply the configuration using kubectl: 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

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-python image: docker.io/{username}/helloworld-python
env: env:
- name: TARGET - name: TARGET
value: "Python Sample v1" value: "Python Sample v1"

View File

@ -1,17 +1,16 @@
# Hello World - Ruby sample # Hello World - Ruby sample
This sample application shows how to create a hello world application in Ruby. A simple wenb app written in Ruby that you can use for testing.
When called, this application reads an env variable 'TARGET' It reads in an env variable `TARGET` and prints "Hello World: ${TARGET}!". If
and prints "Hello World: ${TARGET}!". TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
If TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites ## Prerequisites
* You have a Kubernetes cluster with Knative installed. * A Kubernetes cluster with Knative installed. Follow the
Follow the [installation instructions](https://github.com/knative/install/) if you need to do this. [installation instructions](https://github.com/knative/install/) if you need
* You have installed and initialized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) to create one.
and have created a project in Google Cloud. * [Docker](https://www.docker.com) installed and running on your local machine,
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. and a Docker Hub account configured (we'll use it for a container registry).
## Steps to recreate the sample code ## Steps to recreate the sample code
@ -20,10 +19,12 @@ generally more useful if you build them step-by-step.
The following instructions recreate the source files from this folder. The following instructions recreate the source files from this folder.
1. Create a new directory and cd into it: 1. Create a new directory and cd into it:
````shell ````shell
mkdir app mkdir app
cd app cd app
```` ````
1. Create a file named `app.rb` and copy the code block below into it: 1. Create a file named `app.rb` and copy the code block below into it:
```ruby ```ruby
@ -59,23 +60,21 @@ See [official Ruby docker image](https://hub.docker.com/_/ruby/) for more detail
1. Create a file named `Gemfile` and copy the text block below into it. 1. Create a file named `Gemfile` and copy the text block below into it.
``` ```gem
source 'https://rubygems.org' source 'https://rubygems.org'
gem 'sinatra' gem 'sinatra'
``` ```
1. Run: 1. Run bundle. If you don't have bundler installed, copy the
[Gemfile.lock](./Gemfile.lock) to your working directory.
```shell ```shell
bundle install bundle install
``` ```
If you don't have bundler installed, copy the [Gemfile.lock](./Gemfile.lock) to your working directory. 1. Create a new file, `service.yaml` and copy the following service definition
into the file. Make sure to replace `{username}` with your Docker Hub username.
1. Create a file named `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,7 +88,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-ruby image: docker.io/{username}/helloworld-ruby
env: env:
- name: TARGET - name: TARGET
value: "Ruby Sample v1" value: "Ruby Sample v1"
@ -100,17 +99,22 @@ If you are using docker or another container registry instead, replace the entir
Once you have recreated the sample code files (or used the files in the sample folder) 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. 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. 1. Use Docker to build the sample code into a container. To build and push with
To use container builder, execute the following gcloud command. Make sure to replace `${PROJECT_ID}` Docker Hub, run these commands replacing `{username}` with your
with the ID of your Google Cloud project. Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-ruby # Build the container on your local machine
docker build -t {username}/helloworld-ruby .
# Push the container to docker registry
docker push {username}/helloworld-ruby
``` ```
1. After the build has completed, you can deploy the app into your cluster. 1. After the build has completed and the container is pushed to docker hub, you
Ensure that the container image value in `service.yaml` matches the container you build in the previous step. can deploy the app into your cluster. Ensure that the container image value
Apply the configuration using kubectl: 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

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-ruby image: docker.io/{username}/helloworld-ruby
env: env:
- name: TARGET - name: TARGET
value: "Ruby Sample v1" value: "Ruby Sample v1"

View File

@ -1,14 +1,16 @@
# Hello World -Rust # Hello World - Rust sample
A simple web app in Rust that you can use for testing. A simple web app written in Rust 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 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
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative. to create one.
* [Docker](https://www.docker.com) installed and running on your local machine,
and a Docker Hub account configured (we'll use it for a container registry).
## Steps to recreate the sample code ## Steps to recreate the sample code
@ -16,7 +18,7 @@ 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 `Cargo.toml` and paste the following code. This code 1. Create a new file named `Cargo.toml` and paste the following code:
```toml ```toml
[package] [package]
@ -29,7 +31,9 @@ following instructions recreate the source files from this folder.
pretty_env_logger = "0.2.3" pretty_env_logger = "0.2.3"
``` ```
1. In an `src` folder, Create a new file named `main.rs` and paste the following code. This code creates a basic web server which listens on port 8080: 1. Create a `src` folder, then create a new file named `main.rs` in that folder
and paste the following code. This code creates a basic web server which
listens on port 8080:
```rust ```rust
#![deny(warnings)] #![deny(warnings)]
@ -69,7 +73,8 @@ following instructions recreate the source files from this folder.
} }
``` ```
1. In your project directory, create a file named `Dockerfile`. 1. In your project directory, create a file named `Dockerfile` and copy the code
block below into it.
```docker ```docker
FROM rust:1.27.0 FROM rust:1.27.0
@ -84,7 +89,8 @@ following instructions recreate the source files from this folder.
CMD ["hellorust"] CMD ["hellorust"]
``` ```
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 `{username}` with your Docker Hub username.
```yaml ```yaml
apiVersion: serving.knative.dev/v1alpha1 apiVersion: serving.knative.dev/v1alpha1
@ -98,7 +104,7 @@ following instructions recreate the source files from this folder.
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-rust image: docker.io/{username}/helloworld-rust
env: env:
- name: TARGET - name: TARGET
value: "Rust Sample v1" value: "Rust Sample v1"
@ -106,17 +112,25 @@ following instructions recreate the source files from this folder.
## Build and deploy this sample ## Build and deploy this 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. Clone this repository and navigate into the `serving/samples/helloworld-rust` directory. 1. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
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: Docker Hub username:
```shell ```shell
gcloud container builds submit --tag gcr.io/${PROJECT_ID}/helloworld-rust # Build the container on your local machine
docker build -t {username}/helloworld-rust .
# Push the container to docker registry
docker push {username}/helloworld-rust
``` ```
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 and the container is pushed to docker hub, 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
@ -127,23 +141,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-rust-ingress helloworld-rust.default.demo-domain.com 35.232.134.1 80 1m helloworld-rust-ingress helloworld-rust.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-rust.default.demo-domain.com" http://{IP_ADDRESS} curl -H "Host: helloworld-rust.default.demo-domain.com" http://{IP_ADDRESS}
Hello World: NOT SPECIFIED 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

@ -9,7 +9,7 @@ spec:
revisionTemplate: revisionTemplate:
spec: spec:
container: container:
image: gcr.io/{PROJECT_ID}/helloworld-rust image: docker.io/{username}/helloworld-rust
env: env:
- name: TARGET - name: TARGET
value: "Rust Sample v1" value: "Rust Sample v1"