在 Cloud Service Mesh 中,如果使用 Istio API 来处理 Kubernetes 工作负载,则自动双向 TLS(自动 mTLS)默认处于启用状态。通过自动 mTLS,客户端边车代理会自动检测服务器是否具有边车。客户端边车会将 mTLS 发送到具有边车的工作负载,并将纯文本发送到没有边车的工作负载。但请注意,服务会接受纯文本和 mTLS 流量。在向 Pod 注入边车代理时,我们还建议您将服务配置为仅接受 mTLS 流量。
借助 Cloud Service Mesh,您可以通过应用 PeerAuthentication 政策将服务配置为仅接受 mTLS。借助 Cloud Service Mesh,您可以灵活地将政策应用于整个服务网格、命名空间或单个工作负载。为特定工作负载指定政策后,需以该政策为准。例如,特定于工作负载的政策将优先于特定于命名空间的政策。如果没有为工作负载指定政策,则工作负载将从命名空间或网格继承政策。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-27。"],[],[],null,["# Configure transport security\n============================\n\n| **Note:** This guide only supports Cloud Service Mesh with Istio APIs and does not support Google Cloud APIs. For more information see, [Cloud Service Mesh overview](/service-mesh/v1.25/docs/overview).\n\nIn Cloud Service Mesh with Istio APIs for Kubernetes workloads, auto mutual\nTLS (auto mTLS) is enabled by default. With auto mTLS, a client sidecar proxy\nautomatically detects if the server has a sidecar. The client sidecar sends mTLS\nto workloads with sidecars and sends plaintext to workloads without sidecars.\nNote, however, services *accept* both plaintext and mTLS traffic. As you inject\nsidecar proxies to your Pods, we recommend that you also configure your services\nto only accept mTLS traffic.\n\nWith Cloud Service Mesh, you can configure your services to only accept mTLS\nby applying a `PeerAuthentication` policy. Cloud Service Mesh gives you the\nflexibility to apply the policy to the entire service mesh, to a namespace, or\nto an individual workload. When you specify a policy for a specific workload,\nthat policy takes precedence. For example, a workload-specific policy takes\nprecedence over a namespace-specific one. If no policy is specified for the\nworkload, it inherits the policy from the namespace or the mesh.\n\nSee [Supported features](/service-mesh/v1.25/docs/supported-features) for details of which\nfields of the `PeerAuthentication` CR are supported by platform.\n\nEnable mutual TLS per namespace\n-------------------------------\n\nTo enable mTLS for all workloads within a particular namespace, use a\nnamespace-wide authentication policy. You specify the namespace it applies to\nunder `metadata`. \n\n kubectl apply -f - \u003c\u003cEOF\n apiVersion: \"security.istio.io/v1beta1\"\n kind: \"PeerAuthentication\"\n metadata:\n name: \"\u003cvar translate=\"no\"\u003eAUTH_POLICY_NAME\u003c/var\u003e\"\n namespace: \"\u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e\"\n spec:\n mtls:\n mode: STRICT\n EOF\n\nExpected output: \n\n```\npeerauthentication.security.istio.io/AUTH_POLICY_NAME created\n```\n\n\u003cbr /\u003e\n\nEnable mutual TLS per workload\n------------------------------\n\nTo set a `PeerAuthentication` policy for a specific workload, you must configure\nthe `selector` section and specify the labels that match the desired workload.\nHowever, Cloud Service Mesh can't aggregate workload-level policies for\noutbound mTLS traffic to a service. You need to configure a destination rule to\nmanage that behavior.\n\n1. Apply an authentication policy to a specific workload in your namespace:\n\n cat \u003c\u003cEOF | kubectl apply -n \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e -f -\n apiVersion: \"security.istio.io/v1beta1\"\n kind: \"PeerAuthentication\"\n metadata:\n name: \"\u003cvar translate=\"no\"\u003eAUTH_POLICY_NAME\u003c/var\u003e\"\n namespace: \"\u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e\"\n spec:\n selector:\n matchLabels:\n app: \u003cvar translate=\"no\"\u003eWORKLOAD\u003c/var\u003e\n mtls:\n mode: STRICT\n EOF\n\n Expected output: \n\n ```\n peerauthentication.security.istio.io/AUTH_POLICY_NAME created\n ```\n2. Configure a matching destination rule:\n\n cat \u003c\u003cEOF | kubectl apply -n \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e -f -\n apiVersion: \"networking.istio.io/v1alpha3\"\n kind: \"DestinationRule\"\n metadata:\n name: \"\u003cvar translate=\"no\"\u003eDEST_RULE_NAME\u003c/var\u003e\"\n spec:\n host: \"\u003cvar translate=\"no\"\u003eWORKLOAD\u003c/var\u003e.\u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e.svc.cluster.local\"\n trafficPolicy:\n tls:\n mode: ISTIO_MUTUAL\n EOF\n\n Expected output: \n\n ```\n destinationrule.networking.istio.io/WORKLOAD created\n ```\n\nEnforce mesh-wide mTLS\n----------------------\n\nTo prevent all your services in the mesh from accepting plain-text traffic, set\na mesh-wide `PeerAuthentication` policy with the mTLS mode set to `STRICT` (the\ndefault is `PERMISSIVE`). The mesh-wide `PeerAuthentication` policy shouldn't\nhave a selector and must be applied in the root namespace, `istio-system`. When\nyou deploy the policy, the control plane automatically provisions TLS\ncertificates so that workloads can authenticate with each other.\n\nTo enforce mesh-wide mTLS: \n\n kubectl apply -f - \u003c\u003cEOF\n apiVersion: \"security.istio.io/v1beta1\"\n kind: \"PeerAuthentication\"\n metadata:\n name: \"\u003cvar translate=\"no\"\u003eAUTH_POLICY_NAME\u003c/var\u003e\"\n namespace: \"istio-system\"\n spec:\n mtls:\n mode: STRICT\n EOF\n\nExpected output: \n\n```\npeerauthentication.security.istio.io/AUTH_POLICY_NAME created\n```\n\n\u003cbr /\u003e\n\nFind and delete `PeerAuthentication` policies\n---------------------------------------------\n\nFor a list of all the `PeerAuthentication` policies in the service mesh: \n\n kubectl get peerauthentication --all-namespaces\n\nIf there is a `PeerAuthentication` policy in force, you can delete it with\n`kubectl delete`: \n\n kubectl delete peerauthentication -n \u003cvar translate=\"no\"\u003eNAMESPACE\u003c/var\u003e \u003cvar translate=\"no\"\u003eAUTH_POLICY_NAME\u003c/var\u003e\n\nWhat's next\n-----------\n\n- [Cloud Service Mesh by example: mTLS](/service-mesh/v1.25/docs/tutorials/mtls)\n- [Learn about security in Cloud Service Mesh](/service-mesh/v1.25/docs/security/security-overview)"]]