From 76a8fe610ade2e4c0d772e92b933a79df8a6089b Mon Sep 17 00:00:00 2001 From: savitaashture Date: Fri, 7 Aug 2020 21:44:10 +0530 Subject: [PATCH 1/8] [multi-container]Add samples for multi container support --- docs/serving/samples/README.md | 1 + .../serving/samples/multi-container/README.md | 274 ++++++++++++++++++ .../serving/samples/multi-container/_index.md | 6 + .../samples/multi-container/service.yaml | 27 ++ .../servingcontainer/Dockerfile | 31 ++ .../multi-container/servingcontainer/go.mod | 3 + .../servingcontainer/servingcontainer.go | 43 +++ .../sidecarcontainer/Dockerfile | 31 ++ .../multi-container/sidecarcontainer/go.mod | 3 + .../sidecarcontainer/sidecarcontainer.go | 34 +++ 10 files changed, 453 insertions(+) create mode 100644 docs/serving/samples/multi-container/README.md create mode 100644 docs/serving/samples/multi-container/_index.md create mode 100644 docs/serving/samples/multi-container/service.yaml create mode 100644 docs/serving/samples/multi-container/servingcontainer/Dockerfile create mode 100644 docs/serving/samples/multi-container/servingcontainer/go.mod create mode 100644 docs/serving/samples/multi-container/servingcontainer/servingcontainer.go create mode 100644 docs/serving/samples/multi-container/sidecarcontainer/Dockerfile create mode 100644 docs/serving/samples/multi-container/sidecarcontainer/go.mod create mode 100644 docs/serving/samples/multi-container/sidecarcontainer/sidecarcontainer.go diff --git a/docs/serving/samples/README.md b/docs/serving/samples/README.md index 003ea40d3bd..e8fa9691419 100644 --- a/docs/serving/samples/README.md +++ b/docs/serving/samples/README.md @@ -17,3 +17,4 @@ Serving resources and how they can be applied across common use cases. | REST API | A simple Restful service that exposes an endpoint defined by an environment variable described in the Knative Configuration. | [Go](./rest-api-go/README.md) | | Telemetry | This sample runs a simple web server that makes calls to other in-cluster services and responds to requests with "Hello World!". The purpose of this sample is to show generating metrics, logs, and distributed traces. | [Go](./telemetry-go/README.md) | | Traffic Splitting | This samples builds off the [Creating a RESTful Service](./rest-api-go) sample to illustrate applying a revision, then using that revision for manual traffic splitting. | [YAML](./traffic-splitting/README.md) | +| Multi Container | A quick introduction that highlights how to build and deploy an app using Knative Serving for multiple containers. | [Go](./multi-container/README.md) | diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md new file mode 100644 index 00000000000..cca5ddcc020 --- /dev/null +++ b/docs/serving/samples/multi-container/README.md @@ -0,0 +1,274 @@ +A simple web app written in Go that you can use for multi container testing. + +Follow the steps below to create the sample code and then deploy the app to your +cluster. You can also download a working copy of the sample, by running the +following commands: + +```shell +git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs +``` + +## Before you begin + +- A Kubernetes cluster with Knative installed and DNS configured. Follow the + [installation instructions](../../../../install/README.md) 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). +- Make sure multi-container flag is enabled as part of `config-features` configmap. + +## Recreating the sample code + +Multi container will have more than one container, so for testing we have taken two containers one will be the serving and another will be the sidecar +### ServingContainer +1. `cd knative-docs/docs/serving/samples/multi-container/servingcontainer` +1. Create a new file named `servingcontainer.go` and paste the following code. This + code creates a basic web server which listens on port 8881: + + ```go + package main + + import ( + "fmt" + "io/ioutil" + "log" + "net/http" + ) + + func handler(w http.ResponseWriter, r *http.Request) { + log.Println("serving container received a request.") + res, err := http.Get("http://127.0.0.1:8882") + if err != nil { + log.Fatal(err) + } + resp, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + fmt.Fprintln(w, string(resp)) + } + + func main() { + log.Print("serving container started...") + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8881", nil)) + } + ``` + +1. In your project directory, create a file named `Dockerfile` and copy the code + block below into it. For detailed instructions on dockerizing a Go app, see + [Deploying Go servers with Docker](https://blog.golang.org/docker). + + ```docker + # Use the official Golang image to create a build artifact. + # This is based on Debian and sets the GOPATH to /go. + # https://hub.docker.com/_/golang + FROM golang:1.13 as builder + + # Create and change to the app directory. + WORKDIR /app + + # Retrieve application dependencies using go modules. + # Allows container builds to reuse downloaded dependencies. + COPY go.* ./ + RUN go mod download + + # Copy local code to the container image. + COPY . ./ + + # Build the binary. + # -mod=readonly ensures immutable go.mod and go.sum in container builds. + RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o servingcontainer + + # Use the official Alpine image for a lean production container. + # https://hub.docker.com/_/alpine + # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds + FROM alpine:3 + RUN apk add --no-cache ca-certificates + + # Copy the binary to the production image from the builder stage. + COPY --from=builder /app/servingcontainer /servingcontainer + + # Run the web service on container startup. + CMD ["/servingcontainer"] + ``` + +### SidecarContainer +1. ```shell + cd - + cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer + ``` +1. Create a new file named `sidecarcontainer.go` and paste the following code. This + code creates a basic web server which listens on port 8882: + + ```go + package main + + import ( + "fmt" + "log" + "net/http" + ) + + func handler(w http.ResponseWriter, r *http.Request) { + log.Println("sidecar container received a request.") + fmt.Fprintln(w, "Yay!! multi-container works") + } + + func main() { + log.Print("sidecar container started...") + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8882", nil)) + } + ``` + +1. In your project directory, create a file named `Dockerfile` and copy the code + block below into it. For detailed instructions on dockerizing a Go app, see + [Deploying Go servers with Docker](https://blog.golang.org/docker). + + ```docker + # Use the official Golang image to create a build artifact. + # This is based on Debian and sets the GOPATH to /go. + # https://hub.docker.com/_/golang + FROM golang:1.13 as builder + + # Create and change to the app directory. + WORKDIR /app + + # Retrieve application dependencies using go modules. + # Allows container builds to reuse downloaded dependencies. + COPY go.* ./ + RUN go mod download + + # Copy local code to the container image. + COPY . ./ + + # Build the binary. + # -mod=readonly ensures immutable go.mod and go.sum in container builds. + RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o sidecarcontainer + + # Use the official Alpine image for a lean production container. + # https://hub.docker.com/_/alpine + # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds + FROM alpine:3 + RUN apk add --no-cache ca-certificates + + # Copy the binary to the production image from the builder stage. + COPY --from=builder /app/sidecarcontainer /sidecarcontainer + + # Run the web service on container startup. + CMD ["/sidecarcontainer"] + ``` + +Go to multi-conatiner directory + + ```shell + cd - + cd knative-docs/docs/serving/samples/multi-container/ + ``` + +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/v1 + kind: Service + metadata: + name: multi-container + namespace: default + spec: + template: + spec: + containers: + - image: docker.io/{username}/servingcontainer + ports: + - containerPort: 8881 + - image: docker.io/{username}/sidecarcontainer + ``` + +1. Use the go tool to create a + [`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest. + + ```shell + cd - + cd knative-docs/docs/serving/samples/multi-container/servingcontainer + go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer + + cd - + cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer + go mod init github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer + ``` + +## Building and deploying the sample + +Once you have recreated the sample code files (or used the files in the sample +folder) you're ready to build and deploy the sample app. + +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 + # Build the container on your local machine + cd - + cd knative-docs/docs/serving/samples/multi-container/servingcontainer + docker build -t {username}/servingcontainer . + + cd - + cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer + docker build -t {username}/sidecarcontainer . + + # Push the container to docker registry + docker push {username}/servingcontainer + docker push {username}/sidecarcontainer + ``` + +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 + cd - + cd knative-docs/docs/serving/samples/multi-container + kubectl apply --filename 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 balance + for your app. + - Automatically scale your pods up and down (including to zero active pods). + +1. Run the following command to find the domain URL for your service: + + ```shell + kubectl get ksvc multi-container --output=custom-columns=NAME:.metadata.name,URL:.status.url + ``` + + Example: + + ```shell + NAME URL + multi-container http://multi-container.default.1.2.3.4.xip.io + ``` + +1. Now you can make a request to your app and see the result. Replace + the URL below with the URL returned in the previous command. + + ```shell + curl http://multi-container.default.1.2.3.4.xip.io + Yay!! multi-container works + ``` + + > Note: Add `-v` option to get more detail if the `curl` command failed. + +## Removing the sample app deployment + +To remove the sample app from your cluster, delete the service record: + +```shell +kubectl delete --filename service.yaml +``` diff --git a/docs/serving/samples/multi-container/_index.md b/docs/serving/samples/multi-container/_index.md new file mode 100644 index 00000000000..e1a210600a5 --- /dev/null +++ b/docs/serving/samples/multi-container/_index.md @@ -0,0 +1,6 @@ +--- +title: "Knative 'Multi Container' samples" +linkTitle: "A simple golang web app" +weight: 1 +type: "docs" +--- diff --git a/docs/serving/samples/multi-container/service.yaml b/docs/serving/samples/multi-container/service.yaml new file mode 100644 index 00000000000..49f83cb8fbe --- /dev/null +++ b/docs/serving/samples/multi-container/service.yaml @@ -0,0 +1,27 @@ +# Copyright 2020 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. +# 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/v1 +kind: Service +metadata: + name: multi-container + namespace: default +spec: + template: + spec: + containers: + - image: docker.io/savita3020/servingcontainer + ports: + - containerPort: 8881 + - image: docker.io/savita3020/sidecarcontainer diff --git a/docs/serving/samples/multi-container/servingcontainer/Dockerfile b/docs/serving/samples/multi-container/servingcontainer/Dockerfile new file mode 100644 index 00000000000..4282de4054e --- /dev/null +++ b/docs/serving/samples/multi-container/servingcontainer/Dockerfile @@ -0,0 +1,31 @@ +# Use the official Golang image to create a build artifact. +# This is based on Debian and sets the GOPATH to /go. +# https://hub.docker.com/_/golang +FROM golang:1.13 as builder + +# Create and change to the app directory. +WORKDIR /app + +# Retrieve application dependencies using go modules. +# Allows container builds to reuse downloaded dependencies. +COPY go.* ./ +RUN go mod download + +# Copy local code to the container image. +COPY . ./ + +# Build the binary. +# -mod=readonly ensures immutable go.mod and go.sum in container builds. +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o servingcontainer + +# Use the official Alpine image for a lean production container. +# https://hub.docker.com/_/alpine +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM alpine:3 +RUN apk add --no-cache ca-certificates + +# Copy the binary to the production image from the builder stage. +COPY --from=builder /app/servingcontainer /servingcontainer + +# Run the web service on container startup. +CMD ["/servingcontainer"] diff --git a/docs/serving/samples/multi-container/servingcontainer/go.mod b/docs/serving/samples/multi-container/servingcontainer/go.mod new file mode 100644 index 00000000000..d59669defe3 --- /dev/null +++ b/docs/serving/samples/multi-container/servingcontainer/go.mod @@ -0,0 +1,3 @@ +module github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer + +go 1.14 diff --git a/docs/serving/samples/multi-container/servingcontainer/servingcontainer.go b/docs/serving/samples/multi-container/servingcontainer/servingcontainer.go new file mode 100644 index 00000000000..f469e4bf463 --- /dev/null +++ b/docs/serving/samples/multi-container/servingcontainer/servingcontainer.go @@ -0,0 +1,43 @@ +/* +Copyright 2020 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. +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. +*/ + +package main + +import ( + "fmt" + "io/ioutil" + "log" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + log.Println("serving container received a request.") + res, err := http.Get("http://127.0.0.1:8882") + if err != nil { + log.Fatal(err) + } + resp, err := ioutil.ReadAll(res.Body) + if err != nil { + log.Fatal(err) + } + fmt.Fprintln(w, string(resp)) +} + +func main() { + log.Print("serving container started...") + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8881", nil)) +} diff --git a/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile b/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile new file mode 100644 index 00000000000..a3f9267ab9c --- /dev/null +++ b/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile @@ -0,0 +1,31 @@ +# Use the official Golang image to create a build artifact. +# This is based on Debian and sets the GOPATH to /go. +# https://hub.docker.com/_/golang +FROM golang:1.13 as builder + +# Create and change to the app directory. +WORKDIR /app + +# Retrieve application dependencies using go modules. +# Allows container builds to reuse downloaded dependencies. +COPY go.* ./ +RUN go mod download + +# Copy local code to the container image. +COPY . ./ + +# Build the binary. +# -mod=readonly ensures immutable go.mod and go.sum in container builds. +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o sidecarcontainer + +# Use the official Alpine image for a lean production container. +# https://hub.docker.com/_/alpine +# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds +FROM alpine:3 +RUN apk add --no-cache ca-certificates + +# Copy the binary to the production image from the builder stage. +COPY --from=builder /app/sidecarcontainer /sidecarcontainer + +# Run the web service on container startup. +CMD ["/sidecarcontainer"] diff --git a/docs/serving/samples/multi-container/sidecarcontainer/go.mod b/docs/serving/samples/multi-container/sidecarcontainer/go.mod new file mode 100644 index 00000000000..3600aec429b --- /dev/null +++ b/docs/serving/samples/multi-container/sidecarcontainer/go.mod @@ -0,0 +1,3 @@ +module github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer + +go 1.13 diff --git a/docs/serving/samples/multi-container/sidecarcontainer/sidecarcontainer.go b/docs/serving/samples/multi-container/sidecarcontainer/sidecarcontainer.go new file mode 100644 index 00000000000..0e05839d18c --- /dev/null +++ b/docs/serving/samples/multi-container/sidecarcontainer/sidecarcontainer.go @@ -0,0 +1,34 @@ +/* +Copyright 2020 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. +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. +*/ + +package main + +import ( + "fmt" + "log" + "net/http" +) + +func handler(w http.ResponseWriter, r *http.Request) { + log.Println("sidecar container received a request.") + fmt.Fprintln(w, "Yay!! multi-container works") +} + +func main() { + log.Print("sidecar container started...") + http.HandleFunc("/", handler) + log.Fatal(http.ListenAndServe(":8882", nil)) +} From 3a714100b55e12dd580bdbc1af97dfe878ced6c2 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Mon, 31 Aug 2020 21:55:54 +0530 Subject: [PATCH 2/8] Fix review comments --- docs/serving/samples/multi-container/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index cca5ddcc020..ad81a4fa5d3 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -19,7 +19,7 @@ git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs ## Recreating the sample code -Multi container will have more than one container, so for testing we have taken two containers one will be the serving and another will be the sidecar +Multi container has more than one container, For testing, we will create two containers, the serving container and sidecar container. ### ServingContainer 1. `cd knative-docs/docs/serving/samples/multi-container/servingcontainer` 1. Create a new file named `servingcontainer.go` and paste the following code. This From 2ac8fc19d222853c11cbfd58b3bbebba7ce7b01a Mon Sep 17 00:00:00 2001 From: savitaashture Date: Mon, 31 Aug 2020 23:04:35 +0530 Subject: [PATCH 3/8] Fix CI issue --- docs/serving/samples/multi-container/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index ad81a4fa5d3..2bd2bd72f89 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -11,7 +11,7 @@ git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs ## Before you begin - A Kubernetes cluster with Knative installed and DNS configured. Follow the - [installation instructions](../../../../install/README.md) if you need to + [installation instructions](../../../install/README.md) 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). From 18365e24bfd53eaa9f28657b932f5769463aedeb Mon Sep 17 00:00:00 2001 From: savitaashture Date: Tue, 1 Sep 2020 22:49:12 +0530 Subject: [PATCH 4/8] Fix review comments --- .../serving/samples/multi-container/README.md | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index 2bd2bd72f89..7b9a0f972e3 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -2,13 +2,13 @@ A simple web app written in Go that you can use for multi container testing. Follow the steps below to create the sample code and then deploy the app to your cluster. You can also download a working copy of the sample, by running the -following commands: +following command: ```shell git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs ``` -## Before you begin +## Prerequisites - A Kubernetes cluster with Knative installed and DNS configured. Follow the [installation instructions](../../../install/README.md) if you need to @@ -19,10 +19,13 @@ git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs ## Recreating the sample code -Multi container has more than one container, For testing, we will create two containers, the serving container and sidecar container. -### ServingContainer +Multi container has more than one container. For testing, we will create two containers: a serving container and a sidecar container. +The `multi-container` directory already exist with predefined code and dockerfile for serving and sidecar container with service yaml. +If user wants to update code, dockerfile or service yaml they can follow below steps. + +### Serving Container 1. `cd knative-docs/docs/serving/samples/multi-container/servingcontainer` -1. Create a new file named `servingcontainer.go` and paste the following code. This +1. Update a file named `servingcontainer.go` and copy the code block below into it. This code creates a basic web server which listens on port 8881: ```go @@ -55,7 +58,7 @@ Multi container has more than one container, For testing, we will create two con } ``` -1. In your project directory, create a file named `Dockerfile` and copy the code +1. In your project directory, update 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). @@ -93,12 +96,10 @@ Multi container has more than one container, For testing, we will create two con CMD ["/servingcontainer"] ``` -### SidecarContainer -1. ```shell - cd - - cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer - ``` -1. Create a new file named `sidecarcontainer.go` and paste the following code. This +### Sidecar Container +1. Navigate to cloned code directory `cd -` +1. `cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer` +1. Update a file named `sidecarcontainer.go` and copy the code block below into it. This code creates a basic web server which listens on port 8882: ```go @@ -122,7 +123,7 @@ Multi container has more than one container, For testing, we will create two con } ``` -1. In your project directory, create a file named `Dockerfile` and copy the code +1. In your project directory, update 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). @@ -160,14 +161,13 @@ Multi container has more than one container, For testing, we will create two con CMD ["/sidecarcontainer"] ``` -Go to multi-conatiner directory +### Writing Knative Service YAML - ```shell - cd - - cd knative-docs/docs/serving/samples/multi-container/ - ``` +1. Go to multi-container directory where `service.yaml` already exist + 1. `cd -` + 2. `cd knative-docs/docs/serving/samples/multi-container/` -1. Create a new file, `service.yaml` and copy the following service definition +1. Update a file, `service.yaml` and copy the following service definition into the file. Make sure to replace `{username}` with your Docker Hub username. @@ -202,8 +202,8 @@ Go to multi-conatiner directory ## Building and deploying the sample -Once you have recreated the sample code files (or used the files in the sample -folder) you're ready to build and deploy the sample app. +Once you have recreated the sample code files, or used the files in the sample +folder, you're ready to build and deploy the sample app. 1. 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 @@ -224,7 +224,7 @@ folder) you're ready to build and deploy the sample app. docker push {username}/sidecarcontainer ``` -1. After the build has completed and the container is pushed to docker hub, you +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`: From 34520d4a8fe41e17390071534a5039aa927f10f8 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Tue, 1 Sep 2020 22:56:04 +0530 Subject: [PATCH 5/8] Fix review comments --- docs/serving/samples/multi-container/README.md | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index 7b9a0f972e3..cabda90bd08 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -29,15 +29,13 @@ If user wants to update code, dockerfile or service yaml they can follow below s code creates a basic web server which listens on port 8881: ```go - package main - + package main import ( "fmt" "io/ioutil" "log" "net/http" ) - func handler(w http.ResponseWriter, r *http.Request) { log.Println("serving container received a request.") res, err := http.Get("http://127.0.0.1:8882") @@ -50,14 +48,12 @@ If user wants to update code, dockerfile or service yaml they can follow below s } fmt.Fprintln(w, string(resp)) } - func main() { log.Print("serving container started...") http.HandleFunc("/", handler) log.Fatal(http.ListenAndServe(":8881", nil)) } ``` - 1. In your project directory, update 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). @@ -104,18 +100,15 @@ If user wants to update code, dockerfile or service yaml they can follow below s ```go package main - import ( "fmt" "log" "net/http" ) - func handler(w http.ResponseWriter, r *http.Request) { log.Println("sidecar container received a request.") fmt.Fprintln(w, "Yay!! multi-container works") } - func main() { log.Print("sidecar container started...") http.HandleFunc("/", handler) From c72953b8fb87814d2504b750cc1709016e3a41a1 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Fri, 11 Sep 2020 10:12:10 +0530 Subject: [PATCH 6/8] Removed trailing whitespaces --- .../serving/samples/multi-container/README.md | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index cabda90bd08..1175dd99b60 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -63,31 +63,24 @@ If user wants to update code, dockerfile or service yaml they can follow below s # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang FROM golang:1.13 as builder - # Create and change to the app directory. WORKDIR /app - # Retrieve application dependencies using go modules. # Allows container builds to reuse downloaded dependencies. COPY go.* ./ RUN go mod download - # Copy local code to the container image. COPY . ./ - # Build the binary. # -mod=readonly ensures immutable go.mod and go.sum in container builds. RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o servingcontainer - # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds FROM alpine:3 RUN apk add --no-cache ca-certificates - # Copy the binary to the production image from the builder stage. COPY --from=builder /app/servingcontainer /servingcontainer - # Run the web service on container startup. CMD ["/servingcontainer"] ``` @@ -125,31 +118,24 @@ If user wants to update code, dockerfile or service yaml they can follow below s # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang FROM golang:1.13 as builder - # Create and change to the app directory. WORKDIR /app - # Retrieve application dependencies using go modules. # Allows container builds to reuse downloaded dependencies. COPY go.* ./ RUN go mod download - # Copy local code to the container image. COPY . ./ - # Build the binary. # -mod=readonly ensures immutable go.mod and go.sum in container builds. RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o sidecarcontainer - # Use the official Alpine image for a lean production container. # https://hub.docker.com/_/alpine # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds FROM alpine:3 - RUN apk add --no-cache ca-certificates - + RUN apk add --no-cache ca-certificates # Copy the binary to the production image from the builder stage. - COPY --from=builder /app/sidecarcontainer /sidecarcontainer - + COPY --from=builder /app/sidecarcontainer /sidecarcontainer # Run the web service on container startup. CMD ["/sidecarcontainer"] ``` @@ -187,7 +173,6 @@ If user wants to update code, dockerfile or service yaml they can follow below s cd - cd knative-docs/docs/serving/samples/multi-container/servingcontainer go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer - cd - cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer go mod init github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer @@ -207,11 +192,9 @@ folder, you're ready to build and deploy the sample app. cd - cd knative-docs/docs/serving/samples/multi-container/servingcontainer docker build -t {username}/servingcontainer . - cd - cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer docker build -t {username}/sidecarcontainer . - # Push the container to docker registry docker push {username}/servingcontainer docker push {username}/sidecarcontainer From 8fbd03f0cba1487ad996b74841260f69a2db5ae8 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Mon, 5 Oct 2020 17:26:34 +0530 Subject: [PATCH 7/8] Addressed review comments and updated go version to use 1.15 instead of 1.13 --- .../serving/samples/multi-container/README.md | 81 ++++++++++--------- .../servingcontainer/Dockerfile | 2 +- .../multi-container/servingcontainer/go.mod | 2 +- .../sidecarcontainer/Dockerfile | 2 +- .../multi-container/sidecarcontainer/go.mod | 2 +- 5 files changed, 49 insertions(+), 40 deletions(-) diff --git a/docs/serving/samples/multi-container/README.md b/docs/serving/samples/multi-container/README.md index 1175dd99b60..82fae8040ab 100644 --- a/docs/serving/samples/multi-container/README.md +++ b/docs/serving/samples/multi-container/README.md @@ -1,13 +1,5 @@ A simple web app written in Go that you can use for multi container testing. -Follow the steps below to create the sample code and then deploy the app to your -cluster. You can also download a working copy of the sample, by running the -following command: - -```shell -git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs -``` - ## Prerequisites - A Kubernetes cluster with Knative installed and DNS configured. Follow the @@ -17,16 +9,30 @@ git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs and a Docker Hub account configured (we'll use it for a container registry). - Make sure multi-container flag is enabled as part of `config-features` configmap. -## Recreating the sample code +The following steps show how you can use the sample code and deploy the app to your +cluster. -Multi container has more than one container. For testing, we will create two containers: a serving container and a sidecar container. -The `multi-container` directory already exist with predefined code and dockerfile for serving and sidecar container with service yaml. -If user wants to update code, dockerfile or service yaml they can follow below steps. +You can download a working copy of the sample, by entering the +following command: + +```shell +git clone -b "{{< branch >}}" https://github.com/knative/docs knative-docs +``` + +## Using the sample code + +To test multi container functionality, you must create two containers: a serving container, and a sidecar container. + +The `multi-container` directory is provided in the sample code, and contains predefined code and dockerfiles for creating the containers. + +You can update the default files and YAML by using the steps outlined in this section. ### Serving Container -1. `cd knative-docs/docs/serving/samples/multi-container/servingcontainer` -1. Update a file named `servingcontainer.go` and copy the code block below into it. This - code creates a basic web server which listens on port 8881: +1. After you have cloned the sample repository, navigate to the servingcontainer directory: + + `cd knative-docs/docs/serving/samples/multi-container/servingcontainer` +1. Create a basic web server which listens on port 8881. +You can do this by copying the following code into the `servingcontainer.go` file: ```go package main @@ -54,15 +60,13 @@ If user wants to update code, dockerfile or service yaml they can follow below s log.Fatal(http.ListenAndServe(":8881", nil)) } ``` -1. In your project directory, update a file named `Dockerfile` and copy the code - block below into it. For detailed instructions on dockerizing a Go app, see - [Deploying Go servers with Docker](https://blog.golang.org/docker). +1. Copy the following code into the `Dockerfile` file: ```docker # Use the official Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang - FROM golang:1.13 as builder + FROM golang:1.15 as builder # Create and change to the app directory. WORKDIR /app # Retrieve application dependencies using go modules. @@ -86,10 +90,14 @@ If user wants to update code, dockerfile or service yaml they can follow below s ``` ### Sidecar Container -1. Navigate to cloned code directory `cd -` -1. `cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer` -1. Update a file named `sidecarcontainer.go` and copy the code block below into it. This - code creates a basic web server which listens on port 8882: +1. After you have cloned the sample repository, navigate to the sidecarcontainer directory: + ```text + cd - + cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer + ``` + +1. Create a basic web server which listens on port 8882. +You can do this by copying the following code into the `sidecarcontainer.go` file: ```go package main @@ -109,15 +117,13 @@ If user wants to update code, dockerfile or service yaml they can follow below s } ``` -1. In your project directory, update a file named `Dockerfile` and copy the code - block below into it. For detailed instructions on dockerizing a Go app, see - [Deploying Go servers with Docker](https://blog.golang.org/docker). +1. Copy the following code into the `Dockerfile` file: ```docker # Use the official Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang - FROM golang:1.13 as builder + FROM golang:1.15 as builder # Create and change to the app directory. WORKDIR /app # Retrieve application dependencies using go modules. @@ -142,13 +148,11 @@ If user wants to update code, dockerfile or service yaml they can follow below s ### Writing Knative Service YAML -1. Go to multi-container directory where `service.yaml` already exist +1. After you have cloned the sample repository, navigate to the `multi-container` directory: 1. `cd -` 2. `cd knative-docs/docs/serving/samples/multi-container/` -1. Update a file, `service.yaml` and copy the following service definition - into the file. Make sure to replace `{username}` with your Docker Hub - username. +1. Copy the following YAML service definition into the `service.yaml` file: ```yaml apiVersion: serving.knative.dev/v1 @@ -166,13 +170,19 @@ If user wants to update code, dockerfile or service yaml they can follow below s - image: docker.io/{username}/sidecarcontainer ``` -1. Use the go tool to create a - [`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest. +**NOTE:** Replace `{username}` with your Docker Hub username. +1. Use Go tool to create a + [`go.mod`](https://github.com/golang/go/wiki/Modules#gomod) manifest: + + servingcontainer ```shell cd - cd knative-docs/docs/serving/samples/multi-container/servingcontainer - go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer + go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer + ``` + sidecarcontainer + ```shell cd - cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer go mod init github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer @@ -180,8 +190,7 @@ If user wants to update code, dockerfile or service yaml they can follow below s ## Building and deploying the sample -Once you have recreated the sample code files, or used the files in the sample -folder, you're ready to build and deploy the sample app. +After you have modified the sample code files you can build and deploy the sample app. 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 diff --git a/docs/serving/samples/multi-container/servingcontainer/Dockerfile b/docs/serving/samples/multi-container/servingcontainer/Dockerfile index 4282de4054e..4f8bce92e7b 100644 --- a/docs/serving/samples/multi-container/servingcontainer/Dockerfile +++ b/docs/serving/samples/multi-container/servingcontainer/Dockerfile @@ -1,7 +1,7 @@ # Use the official Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang -FROM golang:1.13 as builder +FROM golang:1.15 as builder # Create and change to the app directory. WORKDIR /app diff --git a/docs/serving/samples/multi-container/servingcontainer/go.mod b/docs/serving/samples/multi-container/servingcontainer/go.mod index d59669defe3..db0c2859954 100644 --- a/docs/serving/samples/multi-container/servingcontainer/go.mod +++ b/docs/serving/samples/multi-container/servingcontainer/go.mod @@ -1,3 +1,3 @@ module github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer -go 1.14 +go 1.15 diff --git a/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile b/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile index a3f9267ab9c..1c60e74ae3d 100644 --- a/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile +++ b/docs/serving/samples/multi-container/sidecarcontainer/Dockerfile @@ -1,7 +1,7 @@ # Use the official Golang image to create a build artifact. # This is based on Debian and sets the GOPATH to /go. # https://hub.docker.com/_/golang -FROM golang:1.13 as builder +FROM golang:1.15 as builder # Create and change to the app directory. WORKDIR /app diff --git a/docs/serving/samples/multi-container/sidecarcontainer/go.mod b/docs/serving/samples/multi-container/sidecarcontainer/go.mod index 3600aec429b..8a4c490f5e7 100644 --- a/docs/serving/samples/multi-container/sidecarcontainer/go.mod +++ b/docs/serving/samples/multi-container/sidecarcontainer/go.mod @@ -1,3 +1,3 @@ module github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer -go 1.13 +go 1.15 From b9e4a5458a0aed52185f922c8034a1076ac34bc5 Mon Sep 17 00:00:00 2001 From: savitaashture Date: Mon, 5 Oct 2020 19:37:38 +0530 Subject: [PATCH 8/8] Addressed review comments --- docs/serving/samples/multi-container/_index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/serving/samples/multi-container/_index.md b/docs/serving/samples/multi-container/_index.md index e1a210600a5..97a3cffa664 100644 --- a/docs/serving/samples/multi-container/_index.md +++ b/docs/serving/samples/multi-container/_index.md @@ -1,5 +1,5 @@ --- -title: "Knative 'Multi Container' samples" +title: "Knative multi-container samples" linkTitle: "A simple golang web app" weight: 1 type: "docs"