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
* A Kubernetes Engine cluster with Knative installed. Follow the
[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. 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
```
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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).
* You have installed [.NET Core SDK 2.1](https://www.microsoft.com/net/core).
## Recreating the sample code
@ -28,7 +26,7 @@ recreate the source files from this folder.
```
1. Update the `CreateWebHostBuilder` definition in `Program.cs` by adding
`.UseUrls("http://0.0.0.0:8080")` to define the serving port:
`.UseUrls("http://0.0.0.0:8080")` to define the serving port:
```csharp
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
@ -36,19 +34,20 @@ recreate the source files from this folder.
.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
app.Run(async (context) =>
{
var target = Environment.GetEnvironmentVariable("TARGET") ?? "NOT SPECIFIED";
await context.Response.WriteAsync($"Hello World: {target}");
await context.Response.WriteAsync($"Hello World: {target}\n");
});
```
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).
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
FROM microsoft/dotnet:2.1-sdk
@ -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
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.
into the file. Make sure to replace `{username}` with your Docker Hub username.
```yaml
apiVersion: serving.knative.dev/v1alpha1
@ -81,7 +78,7 @@ replace the entire image path.
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-csharp
image: docker.io/{username}/helloworld-csharp
env:
- name: TARGET
value: "C# Sample v1"
@ -92,17 +89,22 @@ replace the entire image path.
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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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
that the container image value in `service.yaml` matches the container you built 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
kubectl apply -f service.yaml
@ -114,8 +116,8 @@ the previous step. Apply the configuration using `kubectl`:
* 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
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell
kubectl get ing
@ -125,7 +127,7 @@ ingress point to be created.
```
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.
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-csharp.default.demo-domain.com" http://{IP_ADDRESS}

View File

@ -28,7 +28,7 @@ namespace helloworld_csharp
app.Run(async (context) =>
{
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:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-csharp
image: docker.io/{username}/helloworld-csharp
env:
- name: TARGET
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.
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
* A Kubernetes Engine cluster with Knative installed. Follow the
[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. 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
```
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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).
## Recreating the sample code
@ -21,7 +19,7 @@ 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 file named `helloworld.go` and paste the following code. This
code creates a basic web server which listens on port 8080:
code creates a basic web server which listens on port 8080:
```go
package main
@ -53,8 +51,8 @@ code creates a basic web server which listens on port 8080:
```
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).
block below into it. For detailed instructions on dockerizing a Go app, see
[Deploying Go servers with Docker](https://blog.golang.org/docker).
```docker
# Start from a Debian image with the latest version of Go installed
@ -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
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.
into the file. Make sure to replace `{username}` with your Docker Hub username.
```yaml
apiVersion: serving.knative.dev/v1alpha1
@ -93,7 +89,7 @@ replace the entire image path.
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-go
image: docker.io/{username}/helloworld-go
env:
- name: TARGET
value: "Go Sample v1"
@ -104,29 +100,35 @@ replace the entire image path.
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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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.
Ensure that the container image value in `service.yaml` matches the container
you built 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 kubectl apply -f service.yaml ```
```shell
kubectl apply -f service.yaml
```
1. Now that your service is created, Knative will perform the following steps:
* Create a new immutable revision for this version of the 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).
* 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.
* 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
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell
kubectl get ing
@ -136,7 +138,7 @@ ingress point to be created.
```
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.
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-go.default.demo-domain.com" http://{IP_ADDRESS}

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");
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:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-go
image: docker.io/{username}/helloworld-go
env:
- name: TARGET
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
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
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites
* A Kubernetes Engine cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/docs/tree/master/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. 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
```
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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).
* You have installed [Java SE 8 or later JDK](http://www.oracle.com/technetwork/java/javase/downloads/index.html).
## Recreating the sample code
@ -54,28 +52,28 @@ recreate the source files from this folder.
@SpringBootApplication
public class HelloworldApplication {
@Value("${TARGET:NOT SPECIFIED}")
String target;
@Value("${TARGET:NOT SPECIFIED}")
String target;
@RestController
class HelloworldController {
@GetMapping("/")
String hello() {
return "Hello World: " + target;
}
}
@RestController
class HelloworldController {
@GetMapping("/")
String hello() {
return "Hello World: " + target;
}
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
```
1. In your project directory, create a file named `Dockerfile` and copy the code
block below into it. For detailed instructions on dockerizing a Spring Boot app,
see [Spring Boot with Docker](https://spring.io/guides/gs/spring-boot-docker/).
For additional information on multi-stage docker builds for Java see
[Creating Smaller Java Image using Docker Multi-stage Build](http://blog.arungupta.me/smaller-java-image-docker-multi-stage-build/).
block below into it. For detailed instructions on dockerizing a Spring Boot app,
see [Spring Boot with Docker](https://spring.io/guides/gs/spring-boot-docker/).
For additional information on multi-stage docker builds for Java see
[Creating Smaller Java Image using Docker Multi-stage Build](http://blog.arungupta.me/smaller-java-image-docker-multi-stage-build/).
```docker
FROM maven:3.5-jdk-8-alpine as build
@ -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
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.
into the file. Make sure to replace `{username}` with your Docker Hub username.
```yaml
apiVersion: serving.knative.dev/v1alpha1
@ -106,7 +102,7 @@ replace the entire image path.
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-java
image: docker.io/{username}/helloworld-java
env:
- name: TARGET
value: "Spring Boot Sample v1"
@ -117,17 +113,22 @@ replace the entire image path.
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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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
that the container image value in `service.yaml` matches the container you built 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
kubectl apply -f service.yaml
@ -139,8 +140,8 @@ the previous step. Apply the configuration using `kubectl`:
* 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
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell
kubectl get ing
@ -150,7 +151,7 @@ ingress point to be created.
```
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.
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-java.default.demo-domain.com" http://{IP_ADDRESS}

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-java
image: docker.io/{username}/helloworld-java
env:
- name: TARGET
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.
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
* A Kubernetes Engine cluster with Knative installed. Follow the
[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.
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
```
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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).
* [Node.js](https://nodejs.org/en/) installed and configured.
## Recreating the sample code
@ -24,23 +19,25 @@ 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.
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
npm init
package name: (helloworld-nodejs)
version: (1.0.0)
description:
package name: (helloworld-nodejs)
version: (1.0.0)
description:
entry point: (index.js) app.js
test command:
git repository:
keywords:
author:
test command:
git repository:
keywords:
author:
license: (ISC) Apache-2.0
```
1. Install the `express` dependency:
1. Install the `express` package:
```shell
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
FROM node:8
@ -106,10 +105,8 @@ recreate the source files from this folder.
CMD [ "npm", "start" ]
```
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.
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
apiVersion: serving.knative.dev/v1alpha1
@ -123,7 +120,7 @@ instead, replace the entire image path.
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-nodejs
image: docker.io/{username}/helloworld-nodejs
env:
- name: TARGET
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
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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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.
Ensure that the container image value in `service.yaml` matches the container
you built 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
kubectl apply -f service.yaml
@ -156,8 +158,8 @@ you built in the previous step. Apply the configuration using kubectl:
* 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
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
list the ingress points in the cluster. It may take a few seconds for the
ingress point to be created.
```shell
kubectl get ing
@ -167,7 +169,7 @@ ingress point to be created.
```
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.
`{IP_ADDRESS}` with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-nodejs.default.demo-domain.com" http://{IP_ADDRESS}

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");
you may not use this file except in compliance with the License.

View File

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

View File

@ -1,31 +1,30 @@
# 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
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites
* A Kubernetes Engine cluster with Knative installed. Follow the
[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. 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
```
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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).
## 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
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 cd into it:
````shell
mkdir app
cd app
````
1. Create a file named `index.php` and copy the code block below into it:
```php
@ -35,8 +34,8 @@ following instructions recreate the source files from this folder.
?>
```
1. Create a file named `Dockerfile` and copy the code block below into it.
See [official PHP docker image](https://hub.docker.com/_/php/) for more details.
1. Create a file named `Dockerfile` and copy the code block below into it.
See [official PHP docker image](https://hub.docker.com/_/php/) for more details.
```docker
FROM php:7.2.6-apache
@ -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
```
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.
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
apiVersion: serving.knative.dev/v1alpha1
@ -63,7 +61,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-php
image: docker.io/{username}/helloworld-php
env:
- name: TARGET
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)
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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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.
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
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
```
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
curl -H "Host: helloworld-php.default.demo-domain.com" http://{IP_ADDRESS}

View File

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

View File

@ -1,17 +1,16 @@
# Hello World - Python sample
This sample application shows how to create a hello world application in Python.
When called, this application reads an env variable 'TARGET'
and prints "Hello World: ${TARGET}!".
If TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
A simple web app written in Python that you can use for testing.
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.
## Prerequisites
* You have a Kubernetes cluster with Knative installed.
Follow the [installation instructions](https://github.com/knative/install/) if you need to do this.
* You have installed and initialized [Google Cloud SDK](https://cloud.google.com/sdk/docs/)
and have created a project in Google Cloud.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative.
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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
@ -20,6 +19,7 @@ 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 cd into it:
````shell
mkdir app
cd app
@ -42,8 +42,8 @@ The following instructions recreate the source files from this folder.
app.run(debug=True,host='0.0.0.0',port=8080)
```
1. Create a file named `Dockerfile` and copy the code block below into it.
See [official Python docker image](https://hub.docker.com/_/python/) for more details.
1. Create a file named `Dockerfile` and copy the code block below into it.
See [official Python docker image](https://hub.docker.com/_/python/) for more details.
```docker
FROM python
@ -58,9 +58,8 @@ See [official Python docker image](https://hub.docker.com/_/python/) for more de
CMD ["app.py"]
```
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.
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
apiVersion: serving.knative.dev/v1alpha1
@ -74,7 +73,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-python
image: docker.io/{username}/helloworld-python
env:
- name: TARGET
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
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. Make sure to replace `${PROJECT_ID}`
with the ID of your Google Cloud project.
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
Docker Hub username:
```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.
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
kubectl apply -f service.yaml
@ -116,7 +120,7 @@ Apply the configuration using kubectl:
```
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.
with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-python.default.demo-domain.com" http://{IP_ADDRESS}

View File

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

View File

@ -1,17 +1,16 @@
# Hello World - Ruby sample
This sample application shows how to create a hello world application in Ruby.
When called, this application reads an env variable 'TARGET'
and prints "Hello World: ${TARGET}!".
If TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
A simple wenb app written in Ruby that you can use for testing.
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.
## Prerequisites
* You have a Kubernetes cluster with Knative installed.
Follow the [installation instructions](https://github.com/knative/install/) if you need to do this.
* You have installed and initialized [Google Cloud SDK](https://cloud.google.com/sdk/docs/)
and have created a project in Google Cloud.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative.
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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
@ -20,10 +19,12 @@ 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 cd into it:
````shell
mkdir app
cd app
````
1. Create a file named `app.rb` and copy the code block below into it:
```ruby
@ -38,7 +39,7 @@ The following instructions recreate the source files from this folder.
```
1. Create a file named `Dockerfile` and copy the code block below into it.
See [official Ruby docker image](https://hub.docker.com/_/ruby/) for more details.
See [official Ruby docker image](https://hub.docker.com/_/ruby/) for more details.
```docker
FROM 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.
```
```gem
source 'https://rubygems.org'
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
bundle install
```
If you don't have bundler installed, copy the [Gemfile.lock](./Gemfile.lock) to your working directory.
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.
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
apiVersion: serving.knative.dev/v1alpha1
@ -89,7 +88,7 @@ If you are using docker or another container registry instead, replace the entir
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-ruby
image: docker.io/{username}/helloworld-ruby
env:
- name: TARGET
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)
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. Make sure to replace `${PROJECT_ID}`
with the ID of your Google Cloud project.
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
Docker Hub username:
```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.
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
kubectl apply -f service.yaml
@ -131,7 +135,7 @@ Apply the configuration using kubectl:
```
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.
with the address you see returned in the previous step.
```shell
curl -H "Host: helloworld-ruby.default.demo-domain.com" http://{IP_ADDRESS}

View File

@ -9,7 +9,7 @@ spec:
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-ruby
image: docker.io/{username}/helloworld-ruby
env:
- name: TARGET
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.
It reads in an env variable 'TARGET' and prints "Hello World: ${TARGET}" if
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
TARGET is not specified, it will use "NOT SPECIFIED" as the TARGET.
## Prerequisites
* You have a Kubernetes cluster with Knative installed. Follow the [installation instructions](https://github.com/knative/install/) if you need to do this.
* You have installed and initalized [Google Cloud SDK](https://cloud.google.com/sdk/docs/) and have created a project in Google Cloud.
* You have `kubectl` configured to connect to the Kubernetes cluster running Knative.
* A Kubernetes cluster with Knative installed. Follow the
[installation instructions](https://github.com/knative/install/) if you need
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
@ -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
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
[package]
@ -29,7 +31,9 @@ following instructions recreate the source files from this folder.
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
#![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
FROM rust:1.27.0
@ -84,7 +89,8 @@ following instructions recreate the source files from this folder.
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
apiVersion: serving.knative.dev/v1alpha1
@ -98,7 +104,7 @@ following instructions recreate the source files from this folder.
revisionTemplate:
spec:
container:
image: gcr.io/{PROJECT_ID}/helloworld-rust
image: docker.io/{username}/helloworld-rust
env:
- name: TARGET
value: "Rust Sample v1"
@ -106,17 +112,25 @@ following instructions recreate the source files from this folder.
## 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. 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. Use Docker to build the sample code into a container. To build and push with
Docker Hub, run these commands replacing `{username}` with your
Docker Hub username:
```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
kubectl apply -f service.yaml
@ -127,26 +141,29 @@ 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.
* 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
kubectl get ing --watch
kubectl get ing
NAME HOSTS ADDRESS PORTS AGE
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
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:
```shell
kubectl delete -f service.yaml
```
```

View File

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