All Products
Search
Document Center

Container Service for Kubernetes:inject sidecar containers into virtual node pods

Last Updated:Feb 06, 2026

Anda dapat menggunakan fitur OpenKruise SidecarSet untuk secara otomatis menyuntikkan kontainer sidecar ke dalam Pod yang dijadwalkan ke node virtual. Pendekatan ini memisahkan kontainer aplikasi dari kontainer fungsi tambahan. Topik ini menjelaskan cara membuat SidecarSet guna mendefinisikan kontainer sidecar dan menyuntikkannya secara otomatis ke dalam Pod node virtual.

Istilah

  • Sidecar: Pola desain yang memisahkan fungsi aplikasi tambahan dari aplikasi utama ke dalam proses atau kontainer terpisah. Pola ini memungkinkan penambahan berbagai fitur ke aplikasi secara non-intrusif, sehingga menghindari modifikasi kode konfigurasi untuk memenuhi persyaratan komponen pihak ketiga.

  • Sidecar container: Kontainer tambahan yang ditambahkan ke dalam Pod untuk memperluas dan meningkatkan kontainer utama tanpa mengubahnya.

  • SidecarSet: Fitur inti dari mesin otomatisasi aplikasi cloud-native open source Alibaba Cloud OpenKruise. SidecarSet secara otomatis menyuntikkan kontainer sidecar ke dalam Pod yang memenuhi kriteria tertentu, serta memisahkan definisi dan lifecycle kontainer sidecar (seperti agen pemantauan dan log) dari kontainer aplikasi.

Scope

Node virtual ACK mendukung daya komputasi ECI dan ACS. Saat menggunakan ACS, daya komputasi CPU maupun GPU didukung.

Prasyarat

  • Versi kluster adalah 1.22 atau lebih baru, dan tipe kluster adalah ACK managed cluster Pro atau ACK dedicated cluster.

  • Anda telah menginstal komponen ack-virtual-node, versi v2.10.0 atau lebih baru. Untuk informasi selengkapnya, lihat ACK Virtual Node.

  • Anda telah menginstal komponen ack-kruise, versi v1.3.0 atau lebih baru. Untuk informasi selengkapnya, lihat OpenKruise.

    Penting

    Catatan: Dalam skenario node virtual, semua fitur SidecarSet pada OpenKruise v1.3.0 dan versi sebelumnya didukung. Namun, fitur SidecarSet baru pada versi setelah 1.3.0 tidak didukung.

  • Anda telah menyesuaikan pengaturan parameter komponen Kube API Server dan menyetel SidecarSetServerlessPod=true di featureGates untuk mengaktifkan feature gate untuk fitur SidecarSet. Untuk informasi selengkapnya, lihat Customize Control Plane Component Parameters.

Fitur

SidecarSet

Anda dapat mencocokkan semua Pod yang dijadwalkan ke node virtual dengan menentukan label serverless.alibabacloud.com/virtual-node: "true", seperti pada SidecarSet default. Label ini ditambahkan setelah Pod dikonfirmasi dijadwalkan ke node virtual. Untuk informasi selengkapnya tentang penggunaan SidecarSet default, lihat SidecarSet.

Fitur SidecarSet lainnya adalah kemampuan mereferensikan ConfigMap dan Secret lintas-namespace. Kontainer inti DaemonSet sering bergantung pada ConfigMap (misalnya, untuk pengaturan parameter). Saat menyuntikkan kontainer inti DaemonSet ke dalam Pod aplikasi, Pod aplikasi dan ConfigMap biasanya berada di namespace yang berbeda. Karena skenario node virtual tidak mendukung DaemonSet, kontainer sidecar digunakan sebagai gantinya. Anda dapat mereferensikan ConfigMap dan Secret lintas-namespace dalam volume kontainer sidecar menggunakan format Namespace/Name.

Catatan

Untuk mereferensikan ConfigMap dan Secret lintas-namespace, Anda harus menyelesaikan otorisasi terlebih dahulu. Untuk informasi selengkapnya, lihat SidecarSetResourceBinding.

Expand to view a YAML example of SidecarSet mounting a ConfigMap

apiVersion: apps.kruise.io/v1alpha1
kind: SidecarSet
metadata:
  name: filebeat-sidecarset
spec:
  selector:
    matchLabels:      
      serverless.alibabacloud.com/virtual-node: "true" # Mencocokkan semua pod yang dijadwalkan ke node virtual.
  updateStrategy:
    type: NotUpdate
  containers:
  - name: filebeat
    image: busybox
    imagePullPolicy: IfNotPresent    
    args: [
      "/bin/sh",
      "-c",
      "cat /etc/filebeat.yml && sleep 36000", # Contoh ini hanya mencetak konten konfigurasi filebeat.
    ]    
    volumeMounts:
    - name: config
      mountPath: /etc/filebeat.yml
      readOnly: true
      subPath: filebeat.yml    
  volumes:
  - name: config
    configMap:
      name: kube-system/filebeat-config # Gunakan format namespace/nama untuk mereferensikan ConfigMap di namespace lain.

SidecarSetResourceBinding

Untuk alasan keamanan, Anda harus secara eksplisit mengotorisasi SidecarSetResourceBinding saat memasukkan ConfigMap dan Secret dari namespace lain ke dalam volume kontainer sidecar.

Catatan

Otorisasi ini memberikan izin read-only (Get, List, Watch) untuk ConfigMap dan Secret.

Expand to view a YAML example of SidecarSetResourceBinding

# Authorizes filebeat-sidecarset. Pods matching 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 only authorizes resources in the kube-system namespace.
  labels:
spec:
  subjects:
    - kind: SidecarSet
      name: filebeat-sidecarset
  resourceRefs:
    - kind: ConfigMap # Only grants read-only permission (Get, List, Watch).
      name: filebeat-config

Container Startup and Shutdown Order

Kontainer sidecar sering kali perlu dijalankan sebelum kontainer aplikasi dan keluar setelahnya. Anda dapat mencapai hal ini dengan merujuk ke Configure container startup and shutdown order (ECI) dan Configure the startup and shutdown order of Sidecar containers (ACS).

Automatically Terminate Sidecar Containers

Untuk Pod jenis Job, kontainer sidecar mungkin mencegah Job berhenti setelah kontainer aplikasi selesai. Anda dapat mengatasi masalah ini dengan memaksa menghentikan kontainer sidecar dan mengabaikan kode keluar kontainer.

Upgrade Sidecar Containers

Setelah menerapkan pola Sidecar, Anda mungkin perlu melakukan upgrade kontainer sidecar. Anda dapat menggunakan fitur Sidecar hot upgrade OpenKruise yang sudah tersedia, yang memungkinkan upgrade kontainer sidecar secara mulus tanpa memengaruhi ketersediaan Pod dan sepenuhnya kompatibel dengan metode node virtual saat ini.

Collect Standard Output Logs

Anda dapat mengumpulkan log standard output dari kontainer aplikasi dengan memasang log standard output Pod node virtual (sebagai volume stdlog) ke direktori tertentu dalam kontainer sidecar. Untuk informasi selengkapnya, lihat Mount an stdlog volume to collect standard output logs from a container.

Expand to view a YAML example of SidecarSet mounting stdlog

apiVersion: apps.kruise.io/v1alpha1
kind: SidecarSet
metadata:
  name: filebeat-sidecarset
spec:
  selector:
    matchLabels:
      serverless.alibabacloud.com/virtual-node: "true" # Matches all pods scheduled to virtual nodes.
  updateStrategy:
    type: NotUpdate
  containers:
  # This example only prints the sidecar container log content.
  - name: filebeat
    image: busybox
    imagePullPolicy: IfNotPresent
    args: [
      "/bin/sh",
      "-c",
      "cat /var/log/std/filebeat/0.log && sleep 36000",
    ]    
    volumeMounts:    
    - name: stdlog # Mounts the pod's standard output log volume /var/log/std directory for the sidecar container to read.
      mountPath: /var/log/std
      readOnly: true
  volumes:  
  - name: stdlog
    csi:
      driver: stdlogplugin.csi.alibabacloud.com

Operation Example

Bagian ini menunjukkan cara menyuntikkan kontainer sidecar (kontainer filebeat) ke dalam Pod aplikasi (echo-server). Contoh ini dapat digunakan sebagai panduan untuk merancang aplikasi Anda.

  1. Terapkan SidecarSet untuk secara otomatis menyuntikkan kontainer sidecar ke dalam Pod aplikasi pada node virtual.

    1. Terapkan ConfigMap yang akan dipasang ke kontainer sidecar nanti.

      kubectl apply -f filebeat-config.yaml

      Isi file filebeat-config.yaml adalah sebagai berikut. Contoh ini hanya memasang dan mencetak file konfigurasi (filebeat.yml) ke kontainer sidecar. Variabel terkait tidak efektif atau perlu diganti.

      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} # Not effective. Do not modify. Use directly.
                  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}  # Not effective. Do not modify. Use directly.
          cloud.auth: ${ELASTIC_CLOUD_AUTH}  # Not effective. Do not modify. Use directly.
      
          output.elasticsearch:
            hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
            username: ${ELASTICSEARCH_USERNAME}  # Not effective. Do not modify. Use directly.
            password: ${ELASTICSEARCH_PASSWORD}  # Not effective. Do not modify. Use directly.
    2. Terapkan SidecarSet untuk mendeskripsikan kontainer sidecar.

      kubectl apply -f sidecarset.yaml

      Isi file sidecarset.yaml adalah sebagai berikut. Dalam contoh ini, kontainer filebeat—yang bertindak sebagai kontainer sidecar—mencetak isi file konfigurasi dan memasang volume stdlog untuk mengumpulkan log standard output Pod aplikasi.

      apiVersion: apps.kruise.io/v1alpha1
      kind: SidecarSet
      metadata:
        name: filebeat-sidecarset
      spec:
        selector:
          matchLabels:
            serverless.alibabacloud.com/virtual-node: "true" # Matches all pods scheduled to virtual nodes.
        updateStrategy:
          type: NotUpdate
        containers:
        # This example does not actually run filebeat; it uses busybox cat instead.
        #- 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 pod's standard output logs for the sidecar container to read.
            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.com
    3. Otorisasi akses kontainer sidecar ke ConfigMap.

      Jika Pod aplikasi dan ConfigMap berada di namespace yang berbeda, Anda harus secara eksplisit mengotorisasi kontainer sidecar yang disuntikkan ke dalam Pod aplikasi untuk mengakses ConfigMap menggunakan SidecarSetResourceBinding.

      kubectl apply -f sidecarset-resourcebinding.yaml

      Isi file sidecarset-resourcebinding.yaml adalah sebagai berikut. Contoh ini berencana menerapkan Pod aplikasi di namespace default, sedangkan ConfigMap berada di namespace kube-system. Oleh karena itu, otorisasi diperlukan.

      # Authorizes filebeat-sidecarset. Pods matching 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 only authorizes resources in the kube-system namespace.
        labels:
      spec:
        subjects:
          - kind: SidecarSet
            name: filebeat-sidecarset
        resourceRefs:
          - kind: ConfigMap
            name: filebeat-config
  2. Terapkan Pod aplikasi yang dijadwalkan ke node virtual.

    kubectl apply -f echo-server.yaml

    Isi file echo-server.yaml adalah sebagai berikut. Deployment ini berisi satu replika, dan Pod berisi satu kontainer. Setelah menambahkan label alibabacloud.com/eci: "true", Pod dijadwalkan ke node virtual.

    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"
  3. Konfirmasi bahwa kontainer sidecar secara otomatis disuntikkan ke dalam Pod aplikasi dan verifikasi hasil pemasangan.

    1. Lihat Pod aplikasi.

      kubectl get pod

      Output yang diharapkan adalah sebagai berikut. Pod aplikasi berisi dua kontainer, menunjukkan penyuntikan kontainer sidecar berhasil.

      NAME                          READY   STATUS    RESTARTS   AGE
      echo-server-f8bdc5844-r44nj   2/2     Running   0          14m
    2. Verifikasi bahwa kontainer sidecar telah memasang log standard output Pod aplikasi.

      kubectl exec echo-server-f8bdc5844-r44nj -c filebeat -- cat /var/log/std/echo-server/0.log

      Output yang diharapkan adalah sebagai berikut. Anda dapat melihat log standard output Pod aplikasi.

      2025-04-29T11:26:06.783205694+08:00 stderr F 2025/04/29 03:26:06 Server is listening on :8080
    3. Verifikasi bahwa kontainer sidecar telah memasang file konfigurasi lintas-namespace.

      kubectl exec echo-server-f8bdc5844-r44nj -c filebeat -- cat /etc/filebeat.yml

      Output yang diharapkan adalah sebagai berikut, menunjukkan pemasangan normal.

      Expand to view example output

      filebeat.inputs:
      - type: log
        paths:
          - /var/log/std/*.log
        processors:
          - add_kubernetes_metadata:
              host: ${NODE_NAME} # Not effective. Do not modify. Use directly.
              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}  # Not effective. Do not modify. Use directly.
      cloud.auth: ${ELASTIC_CLOUD_AUTH}  # Not effective. Do not modify. Use directly.
      
      output.elasticsearch:
        hosts: ['${ELASTICSEARCH_HOST:elasticsearch}:${ELASTICSEARCH_PORT:9200}']
        username: ${ELASTICSEARCH_USERNAME}  # Not effective. Do not modify. Use directly.
        password: ${ELASTICSEARCH_PASSWORD}  # Not effective. Do not modify. Use directly.