Kubernetes native sidecars, in depth

Native sidecars, an init container with restartPolicy: Always, stable since Kubernetes 1.33: the three problems they fix, start-up and the startup probe, shutdown order, Jobs that finish, what kubectl shows, and Istio's native sidecars.

GazeekKUBERNETES SIDECARS·EPISODE 7
EPISODE 7

Kubernetes native sidecars, in depth

Native sidecars, an init container with restartPolicy: Always, stable since Kubernetes 1.33: the three problems they fix, start-up and the startup probe, shutdown order, Jobs that finish, what kubectl shows, and Istio's native sidecars.

0:00 / 3:53

Diagrams in this video · 2Free in archhy

Copy and edit in archhy: What native sidecars fix (new tab)
Copy and edit in archhy: A native sidecar's life (new tab)

Now try it on a real cluster

The videos are free. Gazeek's hands-on labs give you a real Kubernetes cluster in your browser for every lab, with checks that tell you when you've got it right: the practice the CKA and CKAD exams need.

After this lesson you can

  • Name the three problems native sidecars fix
  • Declare a native sidecar, and gate the app on it with a startup probe
  • Describe the shutdown order and why sidecar exit codes are ignored
  • Explain why a Job with a native sidecar completes
Transcript

Kubernetes native sidecars, in depth

Space Llama here. Back in episode three, we met native sidecars. Today, the deep dive. What they fix, how they start and stop, and the one probe that matters.

What native sidecars fix

First, the problem. For years, a sidecar was just a second container in the Pod. Kubernetes didn't know it was special. So at start-up, the app could start before its proxy was ready, and its first requests went nowhere. At shutdown, a log shipper could stop before the app did, and the last logs were lost. And in a Job, the sidecar kept running after the work was done, so the Job never finished. Native sidecars fix the order and the Jobs, and with a startup probe, the proxy is ready first too. First proposed in 2018, on by default since Kubernetes 1.29, stable since 1.33.

Declaring one

Here's one. A Job that writes a nightly report, with a log-shipping sidecar. The sidecar goes in the init containers list. Not containers. This one line makes it a native sidecar. Restart policy Always, on the container. And a startup probe. Remember that one. It matters in a minute. The report itself goes in containers, as usual.

A native sidecar's life

Now its life in the Pod. Everything in the init containers list starts in the order you write it. Here, a regular init container comes first, and runs to completion. Then the sidecar. It starts at its place in the list, but it doesn't have to finish. It runs for the whole Pod, and it's restarted if it exits. Here's the surprise. Starts first is nearly in parallel. A readiness probe on the sidecar does not make the app wait. Only a startup probe does, or a blocking post start hook. Once that probe passes, the kubelet moves on. To the next init container, or, as here, to the app. And at the end, the order reverses. The sidecars get their stop signal only after the last app container has finished, and they stop in reverse order.

Jobs, exit codes and probes

A few more things worth knowing. In a Job, the sidecar no longer holds things up. The Job completes when the main container finishes. At shutdown, a sidecar may be cut off, so a non-zero exit code from it is normal. Tools should ignore it. Sidecars support startup, readiness and liveness probes. And a sidecar's readiness counts towards the Pod's.

What kubectl shows

In kubectl get pods, the ready column counts native sidecars too. While the Pod starts, the status shows init, a number out of a number, and that count includes your sidecars. A sidecar that has started counts as done.

Istio uses them too

If you run Istio in sidecar mode, its proxy is a native sidecar now, on by default since Istio 1.27. With one catch. Istio only does it when the kubelet is 1.33 or later. For a scheduled Pod, that's the kubelet on its node. For a Pod that isn't scheduled yet, it checks every node in the cluster, so one old node switches it off.

Remember

Let's recap. A native sidecar is an init container with restart policy Always. Only a startup probe, or a blocking post start hook, makes the app wait for it. It stops last, after the app, in reverse order. And Jobs complete, and a sidecar's exit code at shutdown is ignored.

Practice and what's next

That's native sidecars. If you're studying for the CKAD, the Certified Kubernetes Application Developer exam, multi-container Pods are on the exam, so try one in a lab. See you next time. That's the series. There's more on the channel. Alpaca Mango. Go on, then. Mate.

Hands-on labs

Now try it on a real cluster

A real Kubernetes cluster in your browser for every lab, checked as you go.

See the labs

Gazeek Flash · free

Exam questions on your phone

Free questions on Application Design and Build: quick-fire batches and timed exams, on the web or Android.