Proper way to create sidecar containers

Hi guys,

I’m going through practice exams to prepare to CKA exam and I’m kinda confused about the sidecar container questions in it.
So the Kubernetes’ docs states that sidecar container is a special case of the initcontainer, so it could be described like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
  labels:
    app: myapp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
        - name: myapp
          image: alpine:latest
          command: ['sh', '-c', 'while true; do echo "logging" >> /opt/logs.txt; sleep 1; done']
          volumeMounts:
            - name: data
              mountPath: /opt
      initContainers:
        - name: logshipper
          image: alpine:latest
          restartPolicy: Always
          command: ['sh', '-c', 'tail -F /opt/logs.txt']
          volumeMounts:
            - name: data
              mountPath: /opt
      volumes:
        - name: data
          emptyDir: {}

at the same time I do see that the answer to the question about the sidecar container assumes this as a right answer:

piVersion: apps/v1
kind: Deployment
metadata:
  name: olive-app-cka10-str
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: olive-app-cka10-str
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                  - cluster1-node01
      containers:
      - name: python
        image: poroko/flask-demo-app
        ports:
        - containerPort: 5000
        volumeMounts:
        - name: python-data
          mountPath: /usr/share/
      - name: busybox
        image: busybox
        command:
          - "bin/sh"
          - "-c"
          - "sleep 10000"
        volumeMounts:
          - name: python-data
            mountPath: "/usr/src"
            readOnly: true
      volumes:
      - name: python-data
        persistentVolumeClaim:
          claimName: olive-pvc-cka10-str
  selector:
    matchLabels:
      app: olive-app-cka10-str

So question - what is the proper way for creating the sidecar container? Is it just an additional container in containers section or it’s initContainers though?

Here I’m going to say something I don’t say a lot: to ignore what you see in the docs. The new-fangled thing referred to in the docs as side-car? That’s NOT the same thing as the sidecar containers you will deal with in the CKAD exam. So those particular doc pages? For CKAD, you should ignore those. Somebody decided to reuse the term “sidecar” in a cute new way. It was a bad idea :slight_smile: Don’t be like that guy.

For CKAD purposes, a sidecar is a container that complements the functionality of the main container of the pod. Maybe it adds some functionality to the pod like logging. Maybe it acts to “translate” between the main container’s protocol and some similar protocol that you need to get the pod’s work done (the “ambassador” pattern). See this blog post for more information about the kind of sidecars that CKAD asks you to learn about.

Thanks @rob_kodekloud it was helpful! So just a new entry in the containers list, got it.