Chapter 3. Sidecar injection
Sidecar proxies are deployed into each application pod to intercept network traffic and enable service mesh features like security, observability, and traffic management.
3.1. About sidecar injection Copy linkLink copied to clipboard!
Sidecar injection is enabled using labels at the namespace or pod level. These labels also indicate the specific control plane managing the proxy. When you apply a valid injection label to the pod template defined in a deployment, any new pods created by that deployment automatically receive a sidecar. Similarly, applying a pod injection label at the namespace level ensures any new pods in that namespace include a sidecar.
Injection happens at pod creation through an admission controller, so changes appear on individual pods rather than the deployment resources. To confirm sidecar injection, check the pod details directly using
oc describe
3.2. Identifying the revision name Copy linkLink copied to clipboard!
The label required to enable sidecar injection is determined by the specific control plane instance, known as a revision. Each revision is managed by an
IstioRevision
Istio
IstioRevision
The naming of an
IstioRevision
spec.updateStrategy.type
Istio
InPlace
Istio
RevisionBased
<Istio resource name>-v<version>
Istio
IstioRevision
IstioRevision
To see available revision names, use the following command:
$ oc get istiorevisions
You should see output similar to the following example:
Example output
NAME READY STATUS IN USE VERSION AGE
my-mesh-v1-23-0 True Healthy False v1.23.0 114s
3.2.1. Enabling sidecar injection with default revision Copy linkLink copied to clipboard!
When the service mesh’s
IstioRevision
default
| Resource | Label | Enabled value | Disabled value |
|---|---|---|---|
| Namespace |
|
|
|
| Pod |
|
|
|
You can also enable injection by setting the
istio.io/rev: default
3.2.2. Enabling sidecar injection with other revisions Copy linkLink copied to clipboard!
When the
IstioRevision
default
IstioRevision
istio.io/rev
istio.io/rev: default
For example, with the revision shown above, the following labels would enable sidecar injection:
| Resource | Enabled label | Disabled label |
|---|---|---|
| Namespace |
|
|
| Pod |
|
|
When both
istio-injection
istio.io/rev
istio-injection
3.3. Enabling sidecar injection Copy linkLink copied to clipboard!
To demonstrate different approaches for configuring sidecar injection, the following procedures use the Bookinfo application.
Prerequisites
-
You have installed the Red Hat OpenShift Service Mesh Operator, created an resource, and the Operator has deployed Istio.
Istio -
You have created the resource, and the Operator has deployed the necessary
IstioCNIpods.IstioCNI - You have created the namespaces that are to be part of the mesh, and they are discoverable by the Istio control plane.
-
Optional: You have deployed the workloads to be included in the mesh. In the following examples, the Bookinfo has been deployed to the namespace, but sidecar injection (step 5) has not been configured. For more information, see "Deploying the Bookinfo application".
bookinfo
3.3.1. Enabling sidecar injection with namespace labels Copy linkLink copied to clipboard!
In this example, all workloads within a namespace receive a sidecar proxy injection, making it the best approach when the majority of workloads in the namespace should be included in the mesh.
Procedure
Verify the revision name of the Istio control plane using the following command:
$ oc get istiorevisionsYou should see output similar to the following example:
Example output
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy False v1.23.0 4m57sSince the revision name is default, you can use the default injection labels without referencing the exact revision name.
Verify that workloads already running in the desired namespace show
containers as1/1by using the following command. This confirms that the pods are running without sidecars.READY$ oc get pods -n bookinfoYou should see output similar to the following example:
Example output
NAME READY STATUS RESTARTS AGE details-v1-65cfcf56f9-gm6v7 1/1 Running 0 4m55s productpage-v1-d5789fdfb-8x6bk 1/1 Running 0 4m53s ratings-v1-7c9bd4b87f-6v7hg 1/1 Running 0 4m55s reviews-v1-6584ddcf65-6wqtw 1/1 Running 0 4m54s reviews-v2-6f85cb9b7c-w9l8s 1/1 Running 0 4m54s reviews-v3-6f5b775685-mg5n6 1/1 Running 0 4m54sTo apply the injection label to the
namespace, run the following command at the CLI:bookinfo$ oc label namespace bookinfo istio-injection=enabled namespace/bookinfo labeledTo ensure sidecar injection is applied, redeploy the existing workloads in the
namespace. Use the following command to perform a rolling update of all workloads:bookinfo$ oc -n bookinfo rollout restart deployments
Verification
Verify the rollout by checking that the new pods display
containers as2/2, confirming successful sidecar injection by running the following command:READY$ oc get pods -n bookinfoYou should see output similar to the following example:
Example output
NAME READY STATUS RESTARTS AGE details-v1-7745f84ff-bpf8f 2/2 Running 0 55s productpage-v1-54f48db985-gd5q9 2/2 Running 0 55s ratings-v1-5d645c985f-xsw7p 2/2 Running 0 55s reviews-v1-bd5f54b8c-zns4v 2/2 Running 0 55s reviews-v2-5d7b9dbf97-wbpjr 2/2 Running 0 55s reviews-v3-5fccc48c8c-bjktn 2/2 Running 0 55s
3.3.2. Exclude a workload from the mesh Copy linkLink copied to clipboard!
You can exclude specific workloads from sidecar injection within a namespace where injection is enabled for all workloads.
This example is for demonstration purposes only. The bookinfo application requires all workloads to be part of the mesh for proper functionality.
Procedure
-
Open the application’s resource in an editor. In this case, exclude the
Deploymentservice.ratings-v1 Modify the
section of yourspec.template.metadata.labelsresource to include the labelDeploymentto disable sidecar injection.sidecar.istio.io/inject: falsekind: Deployment apiVersion: apps/v1 metadata: name: ratings-v1 namespace: bookinfo labels: app: ratings version: v1 spec: template: metadata: labels: sidecar.istio.io/inject: 'false'NoteAdding the label to the top-level
section of thelabelsdoes not affect sidecar injection.DeploymentUpdating the deployment triggers a rollout, creating a new ReplicaSet with updated pod(s).
Verification
Verify that the updated pod(s) do not contain a sidecar container and show
containers as1/1by running the following command:Running$ oc get pods -n bookinfoYou should see output similar to the following example:
Example output
NAME READY STATUS RESTARTS AGE details-v1-6bc7b69776-7f6wz 2/2 Running 0 29m productpage-v1-54f48db985-gd5q9 2/2 Running 0 29m ratings-v1-5d645c985f-xsw7p 1/1 Running 0 7s reviews-v1-bd5f54b8c-zns4v 2/2 Running 0 29m reviews-v2-5d7b9dbf97-wbpjr 2/2 Running 0 29m reviews-v3-5fccc48c8c-bjktn 2/2 Running 0 29m
3.3.3. Enabling sidecar injection with pod labels Copy linkLink copied to clipboard!
This approach allows you to include individual workloads for sidecar injection instead of applying it to all workloads within a namespace, making it ideal for scenarios where only a few workloads need to be part of a service mesh. This example also demonstrates the use of a revision label for sidecar injection, where the
Istio
my-mesh
Istio
Procedure
Verify the revision name of the Istio control plane by running the following command:
$ oc get istiorevisionsYou should see output similar to the following example:
Example output
NAME TYPE READY STATUS IN USE VERSION AGE my-mesh Local True Healthy False v1.23.0 47sSince the revision name is
, use the revision labelmy-meshto enable sidecar injection.istio.io/rev=my-meshVerify that workloads already running show
containers as1/1, indicating that the pods are running without sidecars by running the following command:READY$ oc get pods -n bookinfoYou should see output similar to the following example:
Example output
NAME READY STATUS RESTARTS AGE details-v1-65cfcf56f9-gm6v7 1/1 Running 0 4m55s productpage-v1-d5789fdfb-8x6bk 1/1 Running 0 4m53s ratings-v1-7c9bd4b87f-6v7hg 1/1 Running 0 4m55s reviews-v1-6584ddcf65-6wqtw 1/1 Running 0 4m54s reviews-v2-6f85cb9b7c-w9l8s 1/1 Running 0 4m54s reviews-v3-6f5b775685-mg5n6 1/1 Running 0 4m54s-
Open the application’s resource in an editor. In this case, update the
Deploymentservice.ratings-v1 Update the
section of yourspec.template.metadata.labelsto include the appropriate pod injection or revision label. In this case,Deployment:istio.io/rev: my-meshkind: Deployment apiVersion: apps/v1 metadata: name: ratings-v1 namespace: bookinfo labels: app: ratings version: v1 spec: template: metadata: labels: istio.io/rev: my-meshNoteAdding the label to the top-level
section of thelabelsresource does not impact sidecar injection.DeploymentUpdating the deployment triggers a rollout, creating a new ReplicaSet with the updated pod(s).
Verification
Verify that only the ratings-v1 pod now shows
containers2/2, indicating that the sidecar has been successfully injected by running the following command:READY$ oc get pods -n bookinfoYou should see output similar to the following example:
Example output
NAME READY STATUS RESTARTS AGE details-v1-559cd49f6c-b89hw 1/1 Running 0 42m productpage-v1-5f48cdcb85-8ppz5 1/1 Running 0 42m ratings-v1-848bf79888-krdch 2/2 Running 0 9s reviews-v1-6b7444ffbd-7m5wp 1/1 Running 0 42m reviews-v2-67876d7b7-9nmw5 1/1 Running 0 42m reviews-v3-84b55b667c-x5t8s 1/1 Running 0 42m- Repeat for other workloads that you wish to include in the mesh.
3.4. Enabling sidecar injection with namespace labels and an IstioRevisionTag resource Copy linkLink copied to clipboard!
To use the
istio-injection=enabled
default
IstioRevisionTag
default
Istio
Prerequisites
-
You have installed the Red Hat OpenShift Service Mesh Operator, created an resource, and the Operator has deployed Istio.
Istio -
You have created the resource, and the Operator has deployed the necessary
IstioCNIpods.IstioCNI - You have created the namespaces that are to be part of the mesh, and they are discoverable by the Istio control plane.
-
Optional: You have deployed the workloads to be included in the mesh. In the following examples, the Bookinfo has been deployed to the namespace, but sidecar injection (step 5 in "Deploying the Bookinfo application" procedure) has not been configured. For more information, see "Deploying the Bookinfo application".
bookinfo
Procedure
Find the name of your
resource by running the following command:Istio$ oc get istioExample output
NAME REVISIONS READY IN USE ACTIVE REVISION STATUS VERSION AGE default 1 1 1 default-v1-24-3 Healthy v1.24.3 11sIn this example, the
resource has the nameIstio, but the underlying revision is calleddefault.default-v1-24-3Create the
resource in a YAML file:IstioRevisionTagExample
IstioRevistionTagresource YAML fileapiVersion: sailoperator.io/v1 kind: IstioRevisionTag metadata: name: default spec: targetRef: kind: Istio name: defaultApply the
resource by running the following command:IstioRevisionTag$ oc apply -f istioRevisionTag.yamlVerify that the
resource has been created successfully by running the following command:IstioRevisionTag$ oc get istiorevisiontags.sailoperator.ioExample output
NAME STATUS IN USE REVISION AGE default Healthy True default-v1-24-3 4m23sIn this example, the new tag is referencing your active revision,
. Now you can use thedefault-v1-24-3label as if your revision was calledistio-injection=enabled.defaultConfirm that the pods are running without sidecars by running the following command. Any workloads that are already running in the desired namespace should show
containers in the1/1column.READY$ oc get pods -n bookinfoExample output
NAME READY STATUS RESTARTS AGE details-v1-65cfcf56f9-gm6v7 1/1 Running 0 4m55s productpage-v1-d5789fdfb-8x6bk 1/1 Running 0 4m53s ratings-v1-7c9bd4b87f-6v7hg 1/1 Running 0 4m55s reviews-v1-6584ddcf65-6wqtw 1/1 Running 0 4m54s reviews-v2-6f85cb9b7c-w9l8s 1/1 Running 0 4m54s reviews-v3-6f5b775685-mg5n6 1/1 Running 0 4m54sApply the injection label to the
namespace by running the following command:bookinfo$ oc label namespace bookinfo istio-injection=enabled \ namespace/bookinfo labeledTo ensure sidecar injection is applied, redeploy the workloads in the
namespace by running the following command:bookinfo$ oc -n bookinfo rollout restart deployments
Verification
Verify the rollout by running the following command and confirming that the new pods display
containers in the2/2column:READY$ oc get pods -n bookinfoExample output
NAME READY STATUS RESTARTS AGE details-v1-7745f84ff-bpf8f 2/2 Running 0 55s productpage-v1-54f48db985-gd5q9 2/2 Running 0 55s ratings-v1-5d645c985f-xsw7p 2/2 Running 0 55s reviews-v1-bd5f54b8c-zns4v 2/2 Running 0 55s reviews-v2-5d7b9dbf97-wbpjr 2/2 Running 0 55s reviews-v3-5fccc48c8c-bjktn 2/2 Running 0 55s