Making Sense Out of Native Sidecar Containers in Kubernetes


I spent some time playing with the new type of container in Kubernetes and came up with a post and a few challenging yet fun practical exercises that may help you master the new feature faster πŸ‘‡

Kubernetes 1.28 introduced a new type of container - "native" sidecars. Why this addition was needed? How do the new sidecar containers compare to regular and init containers? What use cases does this new type of container enable? And most importantly, how can I get my hands dirty with the hot new feature? Let's find out!

First, a potentially disturbing surprising fact: the 1.28 release didn't add a new Sidecar type or even a sidecarContainers field to the Pod spec - the so-called "native sidecar" containers are just the same old init containers but with some new properties, which make them behave unlike any other type of containers. At first glance, this may seem like a bad design decision, but when you wrap your head around it, you'll see that it's actually a clever and future-proof solution to a rather delicate architectural problem.

Before diving into the details of the new sidecar containers, let's quickly recap the original difference between the regular and init containers in Kubernetes.

Regular vs. init containers

Every Pod must have at least one regular container defined in its .spec.containers list. When a Pod has multiple regular containers, they all start and run concurrently, and if some of the Pod's containers terminate, they become subject to the Pod-wide restart policy:

Multiple concurrent and restartable containers deployed together as a single "unit" is a powerful abstraction, but there are also situations when some of the containers in such a group may need to:

  • Start before others (i.e., the startup order becomes important).
  • Run to completion (i.e., restart upon termination is undesirable).

Enforcing the startup order of regular containers is tricky but doable - a bunch of ugly-looking shell scripts usually do the trick. However, running some of the containers to completion while keeping others restartable is a much harder problem to solve. Simply setting the restart policy to OnFailure is not good enough because when one of the regular containers terminates, the Pod stops being ready, meaning that it's no longer able to serve traffic even if its primary container is still up and running.

A proper solution was needed to address the above problems, and that's how the init containers were born.

Since a while ago, the Kubernetes Pod spec has gotten another list - .spec.initContainers:

Even though the elements of this new list have a literally identical set of attributes to the regular containers, the init containers behave differently.

Init containers:

  • Triggered before regular containers.
  • Started one by one in the order defined by their list.
  • Run to completion before the next init container starts.

...and because of the above design, init containers:

  • Can't respect the Pod's restart policy if it's set to Always - init containers then fallback to OnFailure.
  • After their (successful) termination, don't affect the Pod's readiness - otherwise, the Pod with init containers would never become ready.
  • Don't allow specifying the startup, liveness, and readiness probes - because they would be useless anyway.

Sidecar containers

Historically, init containers were used to perform some auxiliary "one-off" tasks before the main application container startup:

  • Retrieving secrets and establishing the Pod's identity.
  • Fetching application configuration from a remote source.
  • Running scripts using tools and privileges that are too insecure for a long-running container.
  • etc.

But there is more auxiliary functionality that is beneficial to keep outside of the main application container but still in the same Pod:

  • Network proxies, adapters, and data transformers.
  • Logging, metrics, and tracing collectors.
  • Various monitoring agents.

And such functionality doesn't fit the init container model because the above containers:

  • Should start before the main container(s).
  • Have to live as long as the main application runs.
  • May or may not need to affect the Pod's readiness.
  • Should not block the Pod's termination.

Expectedly, over time, another architectural pattern, perfectly describing the above requirements, emerged - Sidecar Containers. However, up until the 1.28 release, sidecars in Kubernetes were implemented using regular containers. But, since there is no startup order guarantee for regular containers, and there is just one restart policy to rule them all, engineers had to come up with various workarounds to make sidecar containers behave as expected.

So, how has Kubernetes 1.28 changed the situation?

Native sidecar containers

The 1.28 release didn't add a new Sidecar type or even a .spec.sidecarContainers list. Instead, it introduced a new restartPolicy attribute for... containers!

In addition to the Pod-wide restart policy, now containers can have their own restart policy, but only if:

  • It's an init container.
  • The value is set to Always.

So, what's the difference between traditional init and init containers with the restartPolicy: Always attribute?

The new type of init containers:

  • Don't block the next (init or regular) container startup.
  • Restarted upon termination.
  • Support startup, readiness, and liveness probes!
  • Terminate after all regular containers are done.

In other words, the only "true init" thing about the new type of containers is that they still respect the startup order. The rest of the behavior seems very different (if not opposite) to the traditional init logic. However, the new behavior is exactly what one would expect from a Sidecar container.

Yes, it's not the most obvious way to achieve the desired behavior. But this design paves the way to a more advanced type of containers - KEP-753: Sidecar containers explains the motivation for reusing the initContainers list and even mentions a new type of container called infrastructureContainers, which might be used to unify the behavior of the old init and new Sidecar containers in the future:

For this KEP it is important to have sidecar containers be defined among other init containers to be able to express the initialization order of containers. The name initContainers is not a good fit for sidecar containers as they typically do more than initialization. The better name can be β€œinfrastructure” containers. The current idea is to implement sidecars as a part of initContainers and if this introduces too much trouble, the new collection name may replace the old collection name in future.

Here's how the new behavior can be visualized using a single Pod with carefully crafted containers:

Get your hands dirty

And now, for the most exciting part! I've prepared a few challenging yet fun problems to help you better internalize your knowledge of sidecars. Ready to tackle them? Then continue on at iximiuz Labs πŸš€

Help iximiuz Labs evolve faster by supporting my work on Patreon. As a patron, you'll get more powerful playgrounds, access to premium content, and an invite to a private Discord community. Does your company have a learning and development budget? Then this expense most likely can be reimbursed 😎

Ivan Velichko

Building labs.iximiuz.com - a place to help you learn Containers and Kubernetes the fun way πŸš€

Read more from Ivan Velichko

Hey there πŸ‘‹ I spent a few weeks deep diving into cgroup v2, and I'm happy to share my findings with you! Everyone knows that Docker and Kubernetes use cgroups to limit the resources of containers and Pods. But did you know that it's very easy to run an arbitrary Linux process in a cgroup using much more basic tools? The only kernel's interface for cgroups is the virtual filesystem called cgroupfs typically mounted at /sys/fs/cgroup. Creating folders there and writing to files in them is...

Hello friends! Ivan's here with the June roundup of all things Linux, Containers, Kubernetes, and Server-Side craft πŸ§™ What I was working on The first two lessons (and a few challenges) of my "Alternative Introduction to Dagger" course have not sparked much interest among my students, so I had to put this work on pause. With a heavy heart, though, because I do like Dagger, and I was enjoying working on the content about it. But no interest means fewer iximiuz Labs Premium subscribers, and I...

Hello friends! It's time for my traditional monthly roundup of all things Linux, Containers, Kubernetes, and Server-Side craft πŸ§™ Before we get started, I want you to know that this newsletter's previous issue (dispatched mid-May) was delivered to only about 1/5th of my usual email audience due to an unfortunate DNS misconfiguration. The good news is that you can still find it and all previous issues on newsletter.iximiuz.com. Also, if you reply to this email, it'd help to restore the domain's...