Enabling Sidecar Injection
The following procedures utilize the Bookinfo application to illustrate various methods for configuring sidecar injection.
TOC
Prerequisites
- The Alauda Service Mesh v2 Operator is installed, an
Istioresource has been created, and the Operator has successfully deployed Istio. - The
IstioCNIresource has been created, and the Operator has deployed the requiredIstioCNIpods. - The namespaces intended for the mesh have been created and are discoverable by the Istio control plane.
- Optional: The workloads to be included in the mesh are already deployed. For the subsequent examples, the Bookinfo application is deployed in the
bookinfonamespace, but sidecar injection (as described in step 2) is not yet configured. See "Deploying the Bookinfo application" for more details.
Enabling Sidecar Injection Using Namespace Labels
This method injects a sidecar proxy into all workloads within a given namespace. It is the ideal approach when most workloads in that namespace need to be part of the mesh.
Procedure
-
Check the revision name of the Istio control plane with the following command:
You should see output similar to the following example:
Example output
Because the revision name is
default, you can use the standard injection labels without specifying the exact revision. -
Confirm that existing workloads in the target namespace show
1/1ready containers by running the command below. This verifies the pods are currently running without sidecars.You should see output similar to the following example:
Example output
-
Apply the injection label to the
bookinfonamespace by executing the following command:Example output
-
To apply the sidecar injection, redeploy the workloads in the
bookinfonamespace. Initiate a rolling update for all deployments with this command:
Verification
-
To verify the rollout, check that the new pods show
2/2containers in theREADYstate, which confirms a successful sidecar injection. Use the following command:You should see output similar to the following example:
Example output
Excluding a Workload from the Mesh
It is possible to prevent sidecar injection for a specific workload even when injection is enabled for its entire namespace.
This example serves only as a demonstration. For the Bookinfo application to function correctly, all of its workloads must be part of the mesh.
Procedure
-
Edit the
Deploymentresource for the application. In this example, we will exclude theratings-v1service. -
In the
spec.template.metadata.labelssection of theDeployment, add the labelsidecar.istio.io/inject: "false"to disable sidecar injection.NOTEThe sidecar injection process is not affected if this label is added to the top-level
labelssection of theDeployment.When the deployment is updated, a rollout is triggered, which creates a new ReplicaSet containing the modified pod(s).
Verification
-
Confirm that the updated pod(s) lack a sidecar container and display
1/1running containers by executing this command:You should see output similar to the following example:
Example output
Enabling Sidecar Injection Using Pod Labels
With this method, you can select individual workloads for sidecar injection rather than enabling it for an entire namespace. This is best suited for cases where only a small number of workloads require inclusion in the service mesh. The example also shows how to use a revision label for sidecar injection, where the Istio resource is named my-mesh. Using a distinct Istio resource name is necessary when multiple Istio control planes exist in one cluster or during a control plane upgrade that is based on revisions.
Procedure
-
Check the Istio control plane's revision name by running this command:
You should see output similar to the following example:
Example output
Because the revision name is
my-mesh, the revision labelistio.io/rev=my-meshmust be used to activate sidecar injection. -
Confirm that existing pods are running without sidecars by checking that they show
1/1containers in theREADYstate. Use this command:You should see output similar to the following example:
Example output
-
Edit the
Deploymentresource of the application. For this example, modify theratings-v1service. -
Modify the
spec.template.metadata.labelssection of theDeploymentto add the required pod injection or revision label. Here, it isistio.io/rev: my-mesh:NOTEPlacing the label in the top-level
labelssection of theDeploymentresource will not affect sidecar injection.This update to the deployment initiates a rollout, which results in a new ReplicaSet with the changed pod(s).
Verification
-
Confirm that the sidecar was injected successfully by verifying that only the
ratings-v1pod displays2/2ready containers. Run the following command:You should see output similar to the following example:
Example output
-
Follow the same process for any other workloads you want to add to the mesh.