借助 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-11。"],[],[],null,["# Configure transport security\n============================\n\nIn Cloud Service Mesh 1.5 and later, auto\n[mutual TLS](/service-mesh/v1.19/docs/glossary#mutual_tls) (auto mTLS) is enabled by\ndefault. With auto mTLS, a client sidecar proxy automatically detects if the\nserver has a sidecar. The client sidecar sends mTLS to workloads with sidecars\nand sends plaintext to workloads without sidecars. Note, however, services\n*accept* both plaintext and mTLS traffic. As you\n[inject sidecar proxies](/service-mesh/v1.19/docs/anthos-service-mesh-proxy-injection) to\nyour Pods, we recommend that you also configure your services to only accept\nmTLS traffic.\n\nWith Cloud Service Mesh, you can configure your services to only accept mTLS by\napplying a `PeerAuthentication` policy. Cloud Service Mesh gives you the flexibility\nto apply the policy to the entire service mesh, to a namespace, or to an\nindividual workload. When you specify a policy for a specific workload, that\npolicy 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\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 outbound\nmTLS traffic to a service. You need to configure a destination rule to manage\nthat 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.19/docs/by-example/mtls)\n- [Learn about security in Cloud Service Mesh](/service-mesh/v1.19/docs/security/security-overview)"]]