2.8.3. 启用自动 sidecar 注入

在部署应用程序时,您必须通过将 spec.template.metadata.annotations 中的 spec.template.metadata.annotations 中的注解 sidecar.istio.io/inject 配置为 true 来选择 注入。选择确保 sidecar 注入不会影响 OpenShift Container Platform 的其他功能,如 OpenShift Container Platform 生态系统中的多个框架使用的 builder pod。

先决条件

  • 识别作为服务网格一部分的命名空间,以及需要自动 sidecar 注入的部署。

流程

  1. 要查找部署,请使用 oc get 命令。

    $ oc get deployment -n <namespace>

    例如,若要查看 bookinfo 命名空间中 'ratings-v1' 微服务的部署文件,请使用以下命令以 YAML 格式查看资源。

    oc get deployment -n bookinfo ratings-v1 -o yaml
  2. 在编辑器中打开应用程序的部署配置 YAML 文件。
  3. spec.template.metadata.annotations.sidecar.istio/inject 添加到 Deployment YAML 中,并将 sidecar.istio.io/inject 设置为 true,如下例所示。

    bookinfo deployment-ratings-v1.yaml 中的代码片段示例

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: ratings-v1
      namespace: bookinfo
      labels:
        app: ratings
        version: v1
    spec:
      template:
        metadata:
          annotations:
            sidecar.istio.io/inject: 'true'

  4. 保存部署配置文件。
  5. 将文件添加回包含应用程序的项目。

    $ oc apply -n <namespace> -f deployment.yaml

    在本例中,bookinfo 是包含 ratings-v1 应用程序和 deployment-ratings-v1.yaml 的项目的名称,这是您编辑的文件。

    $ oc apply -n bookinfo -f deployment-ratings-v1.yaml
  6. 若要验证资源上传成功,请运行以下命令:

    $ oc get deployment -n <namespace> <deploymentName> -o yaml

    例如,

    $ oc get deployment -n bookinfo ratings-v1 -o yaml