SidecarSet

SidecarSet

SidecarSet 控制器支持經過 admission webhook 來自動爲集羣中建立的符合條件的 Pod 注入 sidecar 容器。這個注入過程和 istio 的自動注入方式很相似。除了在 Pod 建立時候注入外,SidecarSet 還提供了爲運行時 Pod 原地升級其中已經注入的 sidecar 容器鏡像的能力。html

簡單來講,SidecarSetsidecar 容器的定義和生命週期與業務容器解耦。它主要用於管理無狀態的 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 selectorPodapp

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 稱爲 懶升級 模式。

相關文章
相關標籤/搜索