×
Community Blog Introduction to Knative Multi-Container Support

Introduction to Knative Multi-Container Support

This article introduces the multi-container feature supported in Knative V0.16.0 and later versions and different methods to use multiple containers in the Knative Service.

By Yuanyi

Introduction

Microservices and containerization lead to the requirement of breaking down an application into small and reusable units, which generally run as separate processes or in separate containers. The pod model of Kubernetes allows users to create a deployment unit that can package multiple containers as a single instance of an application.

Currently, Knative users have the same requirement to deploy multiple containers into a single pod. Users can deploy a wider range of workloads in the Knative Serving model easily with multiple container support. Therefore, Knative V0.16.0 and later versions support multiple containers.

Multi-Container Support

Introduction to Single-Container Support

In versions earlier than Knative V0.16.0, only one container can be configured in Knative Service. When a service is configured, a queue container is added to the pod by default. The container takes over the inbound traffic for collecting metrics based on the traffic in Knative pod autoscaler (KPA). A typical Knative Service is listed below:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: helloworld-go
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/helloworld-go:73fbdd56
        env:
        - name: TARGET
          value: "Knative"

The following figure shows a running pod after configuration:

1

If users want to add a custom sidecar container for network interoperability, file download and copy, and other auxiliary functions, it is not supported. So, the practices are restricted.

Introduction to Multiple-Container Support

Knative V0.16.0 and later versions support multiple containers because of the native features of Kubernetes pods.

2

How can you use multiple containers? The example configuration is listed below:

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

Enable Multi-Container Feature

  • The multi-container feature of Alibaba Cloud Knative V0.18.3 is enabled by default.
  • The multi-container feature of Knative V0.16.0 for the community is disabled and is enabled from V0.17.0 by default. Run the following statements to check whether it is enabled:
$ kubectl -n knative-serving get configmap config-features -oyaml
 ......
 multi-container: "enabled"
 ......

Multi-Container Practices

Prerequisites

Configure a Service

Now, configure a multi-container service that contains two containers:

  • Serving Container
  • Sidecar Container

Execute the following sample code to call the sidecar container in the serving container:

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))
}

The sidecar container is used to print the information "Yay!! multi-container works." The sample code is listed below:

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))
}

Here, configure a multi-container service:

apiVersion: serving.knative.dev/v1
kind: Service
metadata:
  name: multi-container
  namespace: default
spec:
  template:
    spec:
      containers:
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/servingcontainer:v1
        ports:
          - containerPort: 8881
      - image: registry.cn-hangzhou.aliyuncs.com/knative-sample/sidecarcontainer:v1

Run the following deployment command:

kubectl apply -f multi-container.yaml

When viewing the pod information, there are three containers with queue container, serving container, and sidecar container:

richard@B-N3TEMD6P-1650 multi-container % kubectl get po
NAME                                                READY   STATUS    RESTARTS   AGE
multi-container-dfqtv-deployment-799c4f694c-bkc8t   3/3     Running   0          9s

Access the service:

richard@B-N3TEMD6P-1650 multi-container % curl -H "host: multi-container.default.example.com" http://182.92.208.172
Yay!! multi-container works

Now, multi-container access has taken effect.

Summary

This article introduces the multi-container feature supported in Knative V0.16.0 and later versions and different methods to use multiple containers in the Knative Service. You are welcome to try it out today!

0 0 0
Share on

Alibaba Container Service

93 posts | 23 followers

You may also like

Comments

Alibaba Container Service

93 posts | 23 followers

Related Products