SideCar mode introduction

A SideCar is a small vehicle attached to a motorcycle and used to carry passengers. In the programming world, its main function is to decouple the main application from the peripheral auxiliary services, providing a more flexible application deployment mode. The concept is consistent with the single responsibility principle in the design pattern, separating the main application from the ancillary services and focusing more on their own functions.


Several usages of SideCar mode in K8S environment

Shared storage

Based on the K8S Pod feature, the same Pod can share a Volume mounted in the root container. Based on this feature, we can think of the following SideCar applications:

1. Upload logs

We can mount logs to a shared Volume. The service container writes logs, and the SideCar container reads logs and uploads logs to the log analysis platform, decoupling them in producer-consumer mode.

2. Mount the Jar package

Because Java applications rely on having a Java runtime environment, most use images such as the Open-JDK as the base images. And this kind of mirror image mostly hundreds of M. With shared storage, we can use an image like BusyBox, which is only a few megabytes in size, as the base image and then copy the JAR package to the shared Volume. The jar image is used as an InitContainer. The primary service container uses the JAR package in the shared Volume to start services. For subsequent application version updates, only the JAR package image needs to be updated. The jar package image is a SideCar.

Sharing network

The same POD in K8S also shares an IP. Based on this feature, we can use SideCar mode as follows:

1. Container proxy

Apply containers through the SideCar container agent.

2. Container fit

If the SideCar container needs to provide access to multiple existing services but the data interaction formats of different services are different, you can refer to the adapter mode and use the SideCar container as an adapter to provide services without modifying the original service code.


An example of Java application deployment based on SideCar mode

Using InitContainer as our SideCar container, jar packages are mounted to the main business container through shared storage, and the home container provides the Java runtime environment.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: demo-depl
  labels:
    app: demo
spec:
  selector:
    matchLabels:
      app: demo
  replicas: 1
  template:
    metadata:
      labels:
        app: demo
    spec:
      containers:
      - name: business-container
        image: java:8-jdk
        imagePullPolicy: IfNotPresent
        command:
        - java
        - -Djava.security.egd=file:/dev/./urandom
        - -Dspring.profiles.active=k8s
        - -jar
        - / temp/demo - 1.0.0 - the SNAPSHOT. The jar
        ports:
        - containerPort: 8062
        volumeMounts:
        - mountPath: /temp
          name: jar-volume
        - name: TZ
          value: Asia/Shanghai
      initContainers:
      - image: registry.demo.com/demo:1.0.0
        imagePullPolicy: IfNotPresent
        name: demo-jar
        command:
        - cp
        - / demo - 1.0.0 - the SNAPSHOT. The jar
        - / app/demo - 1.0.0 - the SNAPSHOT. The jar
        volumeMounts:
        - mountPath: /app
          name: jar-volume
      volumes:
      - emptyDir: {}
        name: jar-volume
Copy the code

The resources

1.CNCF X Alibaba Cloud native technology open class gitbook.cn/gitchat/col…