注意:Cloud Service Mesh 1.6.8 以上版本會自動支援 Canonical 服務。
標準服務是一組導入相同服務和 API 的工作負載。對於支援的工作負載類型,Cloud Service Mesh 會根據 Kubernetes API 伺服器的現有資訊,自動建立 Canonical Service 資源。本頁面說明自動定義標準服務的標籤,以及如何手動調整服務的範圍。目前支援的工作負載執行個體類型如下:
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Defining a Canonical Service\n============================\n\n\n**Note:** Canonical Services are supported automatically in Cloud Service Mesh version 1.6.8 and higher.\n\n[Canonical Services](/service-mesh/v1.24/docs/canonical-service) is a group of workloads\nthat implement the same service(s) and API(s). For supported workload types,\nCloud Service Mesh automatically creates Canonical Service resources based on existing\ninformation from the Kubernetes API Server. This page explains what labels\nautomatically define Canonical Services and how you can manually adjust the\nboundaries of your services.\nThe currently supported workload instance types are:\n\n- Kubernetes Pods (including via Kubernetes Deployments, Kube Run Services, etc.)\n- Virtual Machine instances\n- Mesh-external services (specifically, ServiceEntry resources with a location of `MESH_EXTERNAL`)\n\nWhat defines Canonical Services\n-------------------------------\n\nCloud Service Mesh determines the Canonical Service membership by reading the\n`service.istio.io/canonical-name`\n[label](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/)\non the Kubernetes configuration resource associated with each workload instance:\n\n- For Pods, the label is in the Kubernetes Pod resource\n- For VMs, the label in the Istio [WorkloadEntry](https://istio.io/v1.24/docs/reference/config/networking/workload-entry/) resource\n- For external services, the label is in the Istio [ServiceEntry](https://istio.io/v1.24/docs/reference/config/networking/service-entry/) resource\n\nCanonical Services have the same\n[Kubernetes namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/)\nas their associated workload instances and cannot span namespaces.\n\nAutomatic labeling rules\n------------------------\n\nCloud Service Mesh automatically groups your Pod- and VM-based workloads into\nCanonical Services with no action on your part.\nYou only need to take action to:\n\n- Adjust labels for user/reader clarity\n- Override the default behavior.\n\n### Automatic labeling in Kubernetes Pods\n\nCanonical Services focus around the Kubernetes `app.kubernetes.io/name` and\n`app` labels. Note that the former label takes precedence.\nIf you use either of these two labels on your workloads, no further work is\nrequired.\n\n### Automatic labeling in Virtual Machines\n\nTo build Canonical Services on your VMs, you must add your VMs to a\nservice mesh by configuring a\n[WorkloadEntry resource](https://istio.io/latest/docs/reference/config/networking/workload-entry/)\nin your Kubernetes API server.\n\nManually labeling\n-----------------\n\nTo manually apply or override a Canonical Service label apply the\n`service.istio.io/canonical-name` label to supported workload resource\nconfigurations.\nIn order for an external service to be recognized as a Canonical Service, you\nmust manually label the applicable ServiceEntry.\n\n### Manual labeling in Kubernetes Pods\n\nTo deploy many Pods at once using a\n[Deployment](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/),\nset the `service.istio.io/canonical-name` label on the PodTemplateSpec: \n\n apiVersion: apps/v1\n kind: Deployment\n metadata:\n name: my-deployment\n namespace: my-namespace\n spec:\n replicas: 3\n template:\n metadata:\n labels:\n service.istio.io/canonical-name: my-service\n spec:\n containers:\n ...\n\nTo label the Canonical Service of a single Pod, add the\n`service.istio.io/canonical-name` label to the `labels` section of your Pod\nconfiguration: \n\n apiVersion: v1\n kind: Pod\n metadata:\n name: my-test-pod\n namespace: my-namespace\n labels:\n service.istio.io/canonical-name: my-service\n spec:\n ...\n\n### Label virtual machines manually\n\nTo label the Canonical Service of a single VM/WorkloadEntry, add the\n`service.istio.io/canonical-name` label to the \"labels\" section of your\nWorkloadEntry configuration: \n\n apiVersion: networking.istio.io/v1alpha3\n kind: WorkloadEntry\n metadata:\n name: my-vm-123\n namespace: my-namespace\n labels:\n service.istio.io/canonical-name: my-service\n spec:\n ...\n\n### Label external services manually\n\nTo label the Canonical Service of a single external service/ServiceEntry, add the\n`service.istio.io/canonical-name` label to the \"labels\" section of your\nServiceEntry configuration: \n\n apiVersion: networking.istio.io/v1alpha3\n kind: ServiceEntry\n metadata:\n name: example-com\n namespace: my-namespace\n labels:\n service.istio.io/canonical-name: an-external-service\n spec:\n location: MESH_EXTERNAL\n ...\n\nWhat's next\n-----------\n\n- [Learn about Canonical Services](/service-mesh/v1.24/docs/canonical-service)\n- [Learn about best practices in Canonical Services](/service-mesh/v1.24/docs/canonical-service-best-practices)"]]