Knative multi-container samples

    • A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
    • 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 configmap.

    The following steps show how you can use the sample code and deploy the app to your cluster.

    You can download a working copy of the sample, by entering the following command:

    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.

    1. After you have cloned the sample repository, navigate to the servingcontainer directory:

      cd knative-docs/docs/serving/samples/multi-container/servingcontainer

    2. Create a basic web server which listens on port 8881. You can do this by copying the following code into the servingcontainer.go file:

      1. package main
      2. import (
      3. "fmt"
      4. "io/ioutil"
      5. "log"
      6. "net/http"
      7. )
      8. func handler(w http.ResponseWriter, r *http.Request) {
      9. log.Println("serving container received a request.")
      10. res, err := http.Get("http://127.0.0.1:8882")
      11. if err != nil {
      12. log.Fatal(err)
      13. }
      14. resp, err := ioutil.ReadAll(res.Body)
      15. if err != nil {
      16. log.Fatal(err)
      17. }
      18. fmt.Fprintln(w, string(resp))
      19. }
      20. func main() {
      21. log.Print("serving container started...")
      22. http.HandleFunc("/", handler)
      23. log.Fatal(http.ListenAndServe(":8881", nil))
      24. }
      1. # Use the official Golang image to create a build artifact.
      2. # This is based on Debian and sets the GOPATH to /go.
      3. # https://hub.docker.com/_/golang
      4. FROM golang:1.15 as builder
      5. # Create and change to the app directory.
      6. # Retrieve application dependencies using go modules.
      7. # Allows container builds to reuse downloaded dependencies.
      8. COPY go.* ./
      9. RUN go mod download
      10. COPY . ./
      11. # Build the binary.
      12. # -mod=readonly ensures immutable go.mod and go.sum in container builds.
      13. RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o servingcontainer
      14. # Use the official Alpine image for a lean production container.
      15. # https://hub.docker.com/_/alpine
      16. # https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
      17. FROM alpine:3
      18. RUN apk add --no-cache ca-certificates
      19. # Copy the binary to the production image from the builder stage.
      20. COPY --from=builder /app/servingcontainer /servingcontainer
      21. # Run the web service on container startup.
      22. CMD ["/servingcontainer"]
    1. After you have cloned the sample repository, navigate to the sidecarcontainer directory:

      1. cd -
      2. cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer
    2. Create a basic web server which listens on port 8882. You can do this by copying the following code into the sidecarcontainer.go file:

      1. package main
      2. import (
      3. "fmt"
      4. "log"
      5. "net/http"
      6. )
      7. func handler(w http.ResponseWriter, r *http.Request) {
      8. log.Println("sidecar container received a request.")
      9. fmt.Fprintln(w, "Yay!! multi-container works")
      10. }
      11. func main() {
      12. log.Print("sidecar container started...")
      13. http.HandleFunc("/", handler)
      14. log.Fatal(http.ListenAndServe(":8882", nil))
      15. }
    3. Copy the following code into the Dockerfile file:

    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/
    2. Copy the following YAML service definition into the service.yaml file:

      1. kind: Service
      2. name: multi-container
      3. namespace: default
      4. spec:
      5. template:
      6. spec:
      7. containers:
      8. - image: docker.io/{username}/servingcontainer
      9. ports:
      10. - containerPort: 8881
      11. - image: docker.io/{username}/sidecarcontainer

    NOTE: Replace {username} with your Docker Hub username.

    1. Use Go tool to create a go.mod manifest:

      servingcontainer

      1. cd -
      2. cd knative-docs/docs/serving/samples/multi-container/servingcontainer
      3. go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer
      1. cd -
      2. cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer
      3. go mod init github.com/knative/docs/docs/serving/samples/multi-container/sidecarcontainer

    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 username:

      1. # Build the container on your local machine
      2. cd -
      3. cd knative-docs/docs/serving/samples/multi-container/servingcontainer
      4. docker build -t {username}/servingcontainer .
      5. cd -
      6. cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer
      7. docker build -t {username}/sidecarcontainer .
      8. # Push the container to docker registry
      9. docker push {username}/servingcontainer
      10. docker push {username}/sidecarcontainer
    2. 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:

    3. 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).
    4. Run the following command to find the domain URL for your service:

      1. kubectl get ksvc multi-container --output=custom-columns=NAME:.metadata.name,URL:.status.url

      Example:

      1. NAME URL
      2. multi-container http://multi-container.default.1.2.3.4.sslip.io
    5. 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.

      1. curl http://multi-container.default.1.2.3.4.sslip.io
      2. Yay!! multi-container works