Inject sidecar containers into pods on virtual nodes
The SidecarSet feature of OpenKruise automatically injects sidecar containers into pods that are scheduled to virtual nodes, decoupling application containers from auxiliary function containers. This topic describes how to create a SidecarSet to define sidecar containers and automatically inject them into pods on virtual nodes.
Terms
Sidecar: a design approach that separates the auxiliary functionality of an application from the application itself and runs it as a separate process or container. This pattern lets you add various features to an application non-intrusively. It avoids adding extra configuration code to the application to meet the requirements of third-party components.
Sidecar container: an additional container in a pod that extends and enhances the main container without modifying it.
SidecarSet: one of the core features of OpenKruise, the cloud-native application automation engine open-sourced by Alibaba Cloud. SidecarSet automatically injects sidecar containers into eligible pods, which decouples the definition and lifecycle of sidecar containers (such as monitoring and logging agents) from application containers.
Scope
ACK virtual nodes support both ECI and ACS computing power. ACS supports both CPU and GPU computing power.
Prerequisites
The cluster version is 1.22 or later, and the cluster type is ACK managed Pro cluster or ACK dedicated cluster.
The ack-virtual-node component is installed, and its version is v2.10.0 or later. For more information, see ACK Virtual Node.
The ack-kruise component is installed, and its version is v1.3.0 or later. For more information, see OpenKruise.
ImportantOn virtual nodes, all SidecarSet features of OpenKruise v1.3.0 and earlier are supported, but the SidecarSet features added in versions later than 1.3.0 are not supported.
The parameter settings of the Kube API Server component are customized so that featureGates includes
SidecarSetServerlessPod=trueto enable the feature gate for the SidecarSet feature. For more information, see Customize control plane component parameters.
Features
SidecarSet
You can match all pods that are scheduled to virtual nodes in exactly the same way as the default SidecarSet, by specifying the label serverless.alibabacloud.com/virtual-node: "true". This label is added after a pod is confirmed to be scheduled to a virtual node. For information about how to use the default SidecarSet, see SidecarSet.
Another commonly used SidecarSet feature is referencing ConfigMaps and Secrets across namespaces. Running the core container of a DaemonSet often depends on a ConfigMap (such as configuration parameters). When the core container of a DaemonSet is injected into an application pod, the application pod and the ConfigMap are usually in different namespaces. Because virtual nodes do not support DaemonSets, sidecar containers are used instead. In the volume of a sidecar container, you can use the Namespace/Name format to reference ConfigMaps and Secrets across namespaces.
Accessing ConfigMaps and Secrets across namespaces requires authorization. For details, see SidecarSetResourceBinding.
SidecarSetResourceBinding
For security reasons, when you use ConfigMaps and Secrets from other namespaces in the volume of a sidecar container, you must explicitly grant authorization by using SidecarSetResourceBinding.
This authorization grants only read-only permissions (Get, List, and Watch) on ConfigMaps and Secrets.
Container startup and exit order
A sidecar container often needs to start before the application container and exit after the application container. You can achieve this by using Configure container startup and exit order (ECI) and Configure the startup and exit priorities of sidecar containers (ACS).
Automatically terminate sidecar containers
For Job-type pods, a sidecar container may prevent the Job from exiting after the application container completes. You can use Forcibly terminate the sidecar container and ignore the container exit code to resolve this issue.
Upgrade sidecar containers
After you use the sidecar pattern, you may have O&M requirements such as upgrading sidecar containers. You can use the existing Sidecar hot upgrade feature of OpenKruise. This method upgrades sidecar containers without affecting pod availability and is fully compatible with the current virtual node approach.
Collect standard output logs
You can collect the standard output logs of application containers by mounting the standard output logs of pods on virtual nodes (as a stdlog volume) to a specified directory in the sidecar container. For more information, see Mount container logs by using stdlog.
Example
The following example shows how to inject a sidecar container (a filebeat container) into an application pod (echo-server). You can use this example as a reference to design your own applications.
Deploy a SidecarSet, which is used later to automatically inject sidecar containers into application pods on virtual nodes.
Deploy a ConfigMap, which is later mounted to the sidecar container.
kubectl apply -f filebeat-config.yamlThe following example shows the content of filebeat-config.yaml. This example only mounts the configuration file (filebeat.yml) to the sidecar container and prints it. The related variables do not take effect and do not need to be replaced.
apiVersion: v1 kind: ConfigMap metadata: name: filebeat-config namespace: kube-system labels: k8s-app: filebeat data: filebeat.yml: |- filebeat.inputs: - type: log paths: - /var/log/std/*.log processors: - add_kubernetes_metadata: host: ${NODE_NAME} # This does not take effect and does not need to be modified. Use it as is. matchers: - logs_path: logs_path: "/var/log/std/" # To enable hints based autodiscover, remove `filebeat.inputs` configuration and uncomment this: #filebeat.autodiscover: # providers: # - type: kubernetes # node: ${NODE_NAME} # hints.enabled: true # hints.default_config: # type: container # paths: # - /var/log/containers/*${data.kubernetes.container.id}.log processors: - add_cloud_metadata: - add_host_metadata: cloud.id: ${ELASTIC_CLOUD_ID} # This does not take effect and does not need to be modified. Use it as is. cloud.auth: ${ELASTIC_CLOUD_AUTH} # This does not take effect and does not need to be modified. Use it as is. output.elasticsearch: hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}'] username: ${ELASTICSEARCH_USERNAME} # This does not take effect and does not need to be modified. Use it as is. password: ${ELASTICSEARCH_PASSWORD} # This does not take effect and does not need to be modified. Use it as is.Deploy a SidecarSet, which is used to describe the sidecar container.
kubectl apply -f sidecarset.yamlThe following example shows the content of sidecarset.yaml. In this example, the filebeat container that serves as the sidecar container prints the content of the configuration file and mounts stdlog to collect the standard output logs of the application pod.
apiVersion: apps.kruise.io/v1alpha1 kind: SidecarSet metadata: name: filebeat-sidecarset spec: selector: matchLabels: serverless.alibabacloud.com/virtual-node: "true" # Matches all pods that are scheduled to virtual nodes. updateStrategy: type: NotUpdate containers: # This example does not actually run filebeat. It is replaced with bosybox cat. #- name: filebeat # image: docker.elastic.co/beats/filebeat:8.6.1 # args: [ # "-c", "/etc/filebeat.yml", # "-e", # ] - name: filebeat image: busybox imagePullPolicy: IfNotPresent args: [ "/bin/sh", "-c", "cat /etc/filebeat.yml && sleep 36000", ] env: - name: ECI_SIDECAR_CONTAINER # Indicates that the sidecar container exits after the application container. value: "true" volumeMounts: - name: config mountPath: /etc/filebeat.yml readOnly: true subPath: filebeat.yml - name: stdlog # Mounts the standard output logs of the pod so that the sidecar container can read them. mountPath: /var/log/std readOnly: true volumes: - name: config configMap: name: kube-system/filebeat-config # Uses the Namespace/Name format to reference a ConfigMap in another namespace. - name: stdlog csi: driver: stdlogplugin.csi.alibabacloud.comAuthorize the sidecar container to access the ConfigMap.
If the application pod and the ConfigMap are in different namespaces, the sidecar container injected into the application pod requires explicit authorization by using SidecarSetResourceBinding to access the ConfigMap.
kubectl apply -f sidecarset-resourcebinding.yamlThe following example shows the content of sidecarset-resourcebinding.yaml. In this example, the application pod is to be deployed in the
defaultnamespace, whereas the ConfigMap is in thekube-systemnamespace. Therefore, authorization is required.# Grants authorization to filebeat-sidecarset so that the pods matched by the SidecarSet can access the filebeat-config ConfigMap in the kube-system namespace. apiVersion: sidecarset.alibabacloud.com/v1alpha1 kind: SidecarSetResourceBinding metadata: name: filebeat-sidecarset-resourcebinding namespace: kube-system # This SidecarSetResourceBinding authorizes only the resources in the kube-system namespace. labels: spec: subjects: - kind: SidecarSet name: filebeat-sidecarset resourceRefs: - kind: ConfigMap name: filebeat-config
Deploy an application pod that is scheduled to a virtual node.
kubectl apply -f echo-server.yamlThe following example shows the content of echo-server.yaml. The Deployment contains one replica, and the pod contains one container. After the
alibabacloud.com/eci: "true"label is added, the pod is scheduled to a virtual node.apiVersion: apps/v1 kind: Deployment metadata: name: echo-server labels: app: echo-server spec: replicas: 1 selector: matchLabels: app: echo-server template: metadata: labels: app: echo-server alibabacloud.com/eci: "true" spec: containers: - name: echo-server image: hashicorp/http-echo imagePullPolicy: IfNotPresent args: - -listen=:8080 - -text="hello world"Confirm that a sidecar container is automatically injected into the application pod, and verify the mounting result.
View the application pod.
kubectl get podThe expected output is as follows. The application pod contains two containers, which indicates that the sidecar container is injected.
NAME READY STATUS RESTARTS AGE echo-server-f8bdc5844-r44nj 2/2 Running 0 14mVerify that the sidecar container has mounted the standard output logs of the application pod.
kubectl exec echo-server-f8bdc5844-r44nj -c filebeat -- cat /var/log/std/echo-server/0.logThe expected output is as follows, which shows the standard output logs of the application pod.
2025-04-29T11:26:06.783205694+08:00 stderr F 2025/04/29 03:26:06 Server is listening on :8080Verify that the sidecar container has mounted the configuration file from another namespace.
kubectl exec echo-server-f8bdc5844-r44nj -c filebeat -- cat /etc/filebeat.ymlThe expected output is as follows, which indicates that the mounting works properly.