By Yuanyi
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.
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:
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.
Knative V0.16.0 and later versions support multiple containers because of the native features of Kubernetes pods.
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
$ kubectl -n knative-serving get configmap config-features -oyaml
......
multi-container: "enabled"
......
Now, configure a multi-container service that contains two containers:
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.
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!
Setting a Whitelist to Specified Pod Segments in Terway for ACK
173 posts | 31 followers
FollowAlibaba Container Service - July 22, 2021
Alibaba Developer - February 3, 2020
Alibaba Container Service - May 12, 2021
Alibaba Container Service - July 22, 2021
Alibaba Developer - September 7, 2020
Alibaba Cloud Community - July 11, 2024
173 posts | 31 followers
FollowAlibaba Cloud Container Service for Kubernetes is a fully managed cloud container management service that supports native Kubernetes and integrates with other Alibaba Cloud products.
Learn MoreProvides a control plane to allow users to manage Kubernetes clusters that run based on different infrastructure resources
Learn MoreMSE provides a fully managed registration and configuration center, and gateway and microservices governance capabilities.
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreMore Posts by Alibaba Container Service