Knative multi-container samples
A simple web app written in Go that you can use for multi container testing.
Prerequisites
- A Kubernetes cluster with Knative installed and DNS configured. Follow the installation instructions if you need to create one.
- Docker 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.
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:
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
After you have cloned the sample repository, navigate to the servingcontainer directory:
cd knative-docs/docs/serving/samples/multi-container/servingcontainer
Create a basic web server which listens on port 8881. You can do this by copying the following code into the
servingcontainer.go
file: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))
}
Copy the following code into the
Dockerfile
file:# 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.15 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"]
Sidecar Container
After you have cloned the sample repository, navigate to the sidecarcontainer directory:
cd -
cd knative-docs/docs/serving/samples/multi-container/sidecarcontainer
Create a basic web server which listens on port 8882. You can do this by copying the following code into the
sidecarcontainer.go
file: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))
}
Copy the following code into the
Dockerfile
file:# 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.15 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"]
Writing Knative Service YAML
After you have cloned the sample repository, navigate to the
multi-container
directory:cd -
cd knative-docs/docs/serving/samples/multi-container/
Copy the following YAML service definition into the
service.yaml
file: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
NOTE: Replace {username}
with your Docker Hub username.
Use Go tool to create a
go.mod
manifest:servingcontainer
cd -
cd knative-docs/docs/serving/samples/multi-container/servingcontainer
go mod init github.com/knative/docs/docs/serving/samples/multi-container/servingcontainer
sidecarcontainer
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
After you have modified the sample code files you can build and deploy the sample app.
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:# 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
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 usingkubectl
:cd -
cd knative-docs/docs/serving/samples/multi-container
kubectl apply --filename service.yaml
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).
Run the following command to find the domain URL for your service:
kubectl get ksvc multi-container --output=custom-columns=NAME:.metadata.name,URL:.status.url
Example:
NAME URL
multi-container http://multi-container.default.1.2.3.4.xip.io
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.
curl http://multi-container.default.1.2.3.4.xip.io
Yay!! multi-container works
Note: Add
-v
option to get more detail if thecurl
command failed.
Removing the sample app deployment
To remove the sample app from your cluster, delete the service record:
kubectl delete --filename service.yaml
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.