SidecarSet
控制器支持经过 admission webhook
来自动为集群中建立的符合条件的 Pod
注入 sidecar
容器。这个注入过程和 istio 的自动注入方式很相似。除了在 Pod
建立时候注入外,SidecarSet
还提供了为运行时 Pod
原地升级其中已经注入的 sidecar
容器镜像的能力。html
简单来讲,SidecarSet
将 sidecar
容器的定义和生命周期与业务容器解耦。它主要用于管理无状态的 sidecar
容器,好比监控、日志等 agent。nginx
SidecarSet spec
定义以下:web
type SidecarSetSpec struct { // selector is a label query over pods that should be injected Selector *metav1.LabelSelector `json:"selector,omitempty"` // Containers is the list of init containers to be injected into the selected pod // We will inject those containers by their name in ascending order // We only inject init containers when a new pod is created, it does not apply to any existing pod InitContainers []SidecarContainer `json:"initContainers,omitempty"` // Containers is the list of sidecar containers to be injected into the selected pod Containers []SidecarContainer `json:"containers,omitempty"` // List of volumes that can be mounted by sidecar containers Volumes []corev1.Volume `json:"volumes,omitempty"` // Paused indicates that the sidecarset is paused and will not be processed by the sidecarset controller. Paused bool `json:"paused,omitempty"` // The sidecarset strategy to use to replace existing pods with new ones. Strategy SidecarSetUpdateStrategy `json:"strategy,omitempty"`}type SidecarContainer struct { corev1.Container}
注意,sidecar
的注入只会发生在 Pod
建立阶段,而且只有 Pod spec
会被更新,不会影响 Pod
所属的 workload template
模板。json
SidecarSet
:以下的 sidecarset.yaml
定义了一个 SidecarSet
,其中包括了一个名为 sidecar1 的 sidecar
容器:centos
apiVersion: apps.kruise.io/v1alpha1kind: SidecarSetmetadata: name: test-sidecarsetspec: selector: matchLabels: app: nginx strategy: rollingUpdate: maxUnavailable: 2 containers: - name: sidecar1 image: centos:6.7 command: ["sleep", "999d"] # do nothing at all volumeMounts: - name: log-volume mountPath: /var/log volumes: # this field will be merged into pod.spec.volumes - name: log-volume emptyDir: {}
建立这个 YAML:api
kubectl apply -f sidecarset.yaml
Pod
:定义一个匹配 SidecarSet selector
的 Pod
:app
apiVersion: v1kind: Podmetadata: labels: app: nginx # matches the SidecarSet's selector name: test-podspec: containers: - name: app image: nginx:1.15.1
建立这个 Pod
,会发现其中被注入了 sidecar1 容器:ide
kubectl get pod NAME READY STATUS RESTARTS AGE test-pod 2/2 Running 0 118s
此时,SidecarSet status
被更新为:ui
kubectl get sidecarset test-sidecarset -o yaml | grep -A4 status
status: matchedPods: 1 observedGeneration: 1 readyPods: 1 updatedPods: 1
SidecarSet
:使用 kubectl edit sidecarset test-sidecarset
来将 SidecarSet
中的 image
从 centos:6.7 更新为 centos:6.8,会发现已经注入 Pod
中的 sidecar
镜像被原地升级。this
若是用户更新了 spec.containers
中除 image
以外的字段,那么 SidecarSet
是没法作到原地升级的,只能等到 Pod
下一次被删除、重建的时候从新注入(好比用 Deployment/CloneSet
作重建升级)。这种行为被 SidecarSet
称为 懒升级 模式。