@@ -9,21 +9,43 @@ weight: 50
9
9
10
10
Sidecar containers are the secondary containers that run along with the main
11
11
application container within the same {{< glossary_tooltip text="Pod" term_id="pod" >}}.
12
- These containers are used to enhance or to extend the functionality of the main application
13
- container by providing additional services, or functionality such as logging, monitoring,
12
+ These containers are used to enhance or to extend the functionality of the primary _ app
13
+ container _ by providing additional services, or functionality such as logging, monitoring,
14
14
security, or data synchronization, without directly altering the primary application code.
15
15
16
+ Typically, you only have one app container in a Pod. For example, if you have a web
17
+ application that requires a local webserver, the local webserver is a sidecar and the
18
+ web application itself is the app container.
19
+
16
20
<!-- body -->
17
21
18
- ## Enabling sidecar containers
22
+ ## Sidecar containers in Kubernetes {#pod-sidecar-containers}
23
+
24
+ Kubernetes implements sidecar containers as a special case of
25
+ [ init containers] ( /docs/concepts/workloads/pods/init-containers/ ) ; sidecar containers remain
26
+ running after Pod startup. This document uses the term _ regular init containers_ to clearly
27
+ refer to containers that only run during Pod startup.
28
+
29
+ Provided that your cluster has the ` SidecarContainers `
30
+ [ feature gate] ( /docs/reference/command-line-tools-reference/feature-gates/ ) enabled
31
+ (the feature is active by default since Kubernetes v1.29), you can specify a ` restartPolicy `
32
+ for containers listed in a Pod's ` initContainers ` field.
33
+ These restartable _ sidecar_ containers are independent from other init containers and from
34
+ the main application container(s) within the same pod.
35
+ These can be started, stopped, or restarted without effecting the main application container
36
+ and other init containers.
37
+
38
+ You can also run a Pod with multiple containers that are not marked as init or sidecar
39
+ containers. This is appropriate if the containers within the Pod are required for the
40
+ Pod to work overall, but you don't need to control which containers start or stop first.
41
+ You could also do this if you need to support older versions of Kubernetes that don't
42
+ support a container-level ` restartPolicy ` field.
43
+
44
+ ### Example application {#sidecar-example}
45
+
46
+ Here's an example of a Deployment with two containers, one of which is a sidecar:
19
47
20
- Enabled by default with Kubernetes 1.29, a
21
- [ feature gate] ( /docs/reference/command-line-tools-reference/feature-gates/ ) named
22
- ` SidecarContainers ` allows you to specify a ` restartPolicy ` for containers listed in a
23
- Pod's ` initContainers ` field. These restartable _ sidecar_ containers are independent with
24
- other [ init containers] ( /docs/concepts/workloads/pods/init-containers/ ) and main
25
- application container within the same pod. These can be started, stopped, or restarted
26
- without effecting the main application container and other init containers.
48
+ {{% code_sample language="yaml" file="application/deployment-sidecar.yaml" %}}
27
49
28
50
## Sidecar containers and Pod lifecycle
29
51
@@ -35,8 +57,8 @@ If a `readinessProbe` is specified for this init container, its result will be u
35
57
to determine the ` ready ` state of the Pod.
36
58
37
59
Since these containers are defined as init containers, they benefit from the same
38
- ordering and sequential guarantees as other init containers, allowing them to
39
- be mixed with other init containers into complex Pod initialization flows.
60
+ ordering and sequential guarantees as regular init containers, allowing you to mix
61
+ sidecar containers with regular init containers for complex Pod initialization flows.
40
62
41
63
Compared to regular init containers, sidecars defined within ` initContainers ` continue to
42
64
run after they have started. This is important when there is more than one entry inside
@@ -46,30 +68,28 @@ next init container from the ordered `.spec.initContainers` list.
46
68
That status either becomes true because there is a process running in the
47
69
container and no startup probe defined, or as a result of its ` startupProbe ` succeeding.
48
70
49
- Here's an example of a Deployment with two containers, one of which is a sidecar:
50
-
51
- {{% code_sample language="yaml" file="application/deployment-sidecar.yaml" %}}
71
+ ### Jobs with sidecar containers
52
72
53
- This feature is also useful for running Jobs with sidecars, as the sidecar
54
- container will not prevent the Job from completing after the main container
55
- has finished.
73
+ If you define a Job that uses sidecar using Kubernetes-style init containers,
74
+ the sidecar container in each Pod does not prevent the Job from completing after the
75
+ main container has finished.
56
76
57
77
Here's an example of a Job with two containers, one of which is a sidecar:
58
78
59
79
{{% code_sample language="yaml" file="application/job/job-sidecar.yaml" %}}
60
80
61
- ## Differences from regular containers
81
+ ## Differences from application containers
62
82
63
- Sidecar containers run alongside regular containers in the same pod. However, they do not
83
+ Sidecar containers run alongside _ app containers _ in the same pod. However, they do not
64
84
execute the primary application logic; instead, they provide supporting functionality to
65
85
the main application.
66
86
67
87
Sidecar containers have their own independent lifecycles. They can be started, stopped,
68
- and restarted independently of regular containers. This means you can update, scale, or
88
+ and restarted independently of app containers. This means you can update, scale, or
69
89
maintain sidecar containers without affecting the primary application.
70
90
71
91
Sidecar containers share the same network and storage namespaces with the primary
72
- container This co-location allows them to interact closely and share resources.
92
+ container. This co-location allows them to interact closely and share resources.
73
93
74
94
## Differences from init containers
75
95
@@ -112,8 +132,10 @@ for resource usage apply:
112
132
Quota and limits are applied based on the effective Pod request and
113
133
limit.
114
134
115
- Pod level control groups (cgroups) are based on the effective Pod request and
116
- limit, the same as the scheduler.
135
+ ### Sidecar containers and Linux cgroups {#cgroups}
136
+
137
+ On Linux, resource allocations for Pod level control groups (cgroups) are based on the effective Pod
138
+ request and limit, the same as the scheduler.
117
139
118
140
## {{% heading "whatsnext" %}}
119
141
0 commit comments