共用方式為


在相同的 Kubernetes 命名空間中建立具有多個實例的解決方案

在本文中,您將使用 CLI 進行工作負載的協調,在相同的 Kubernetes 命名空間中建立一個包含多個實例的解決方案。

先決條件

建立目標

若要建立目標,請使用 solution-scope CLI 中的 自變數,其會對應至 Kubernetes 命名空間。 請確保所有目標都使用相同的solution-scope,以使其對應至相同的命名空間。 撰寫 Helm 圖表時,請使 deployment.yaml 中的部署名稱具有動態性。 這可確保部署名稱是唯一的,而且可以在運行時間指定。

例如,在檔案中 deployment.yaml ,您可以使用下列程式代碼讓部署名稱成為動態:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: fsad-{{ .Values.DeploymentName }}
spec:
  replicas: 1
  selector:
    matchLabels:
      name: fsad
  template:
    metadata:
      labels:
        name: fsad
      annotations:
        my-config-checksum: {{ include (print $.Template.BasePath "/configmap.yaml") . | sha256sum }}
    spec:
      nodeSelector:
        kubernetes.io/os: "linux"
      containers:
        - name: fsad
          image: contosocm.azurecr.io/sharedsyncadapter:latest
          volumeMounts:
            - mountPath: /app/appsettings.yaml
              subPath: appsettings.yaml
              name: config
              readOnly: true
          ports:
            - containerPort: 80
      volumes:
         - name: config
           configMap:
            name: fsad-config-{{ .Values.DeploymentName }}

在這裡,部署名稱取自 GitHub 存放庫壓縮資料夾中的 values.yaml

同樣地,如果在應用程式中使用configmap,那麼configmap的名稱也是動態的。

apiVersion: v1
kind: ConfigMap
metadata:
  name: fsad-config-{{ .Values.DeploymentName }}

定義 DeploymentName 在組態範本和架構中為可設定的,以便在部署期間加以設定。 組態範本和架構分別定義在 app-config-template.yamlapp-config-schema.yaml 檔案中。 組態範本檔案包含方案的組態架構和範本。

以下是 DeploymentName 檔案中的 組態範本

schema:
  name: <schema-name>
  version: 1.0.0  
configs:
  DeploymentName: ${{$val(DeploymentName)}}

以下是 DeploymentName 檔案中的 組態架構

rules:
  configs:
    DeploymentName:
      type: string
      required: true
      editableAt:
          - line
      editableBy:
          - ot
      pattern: "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$"  #K8s object naming convention        

備註

請查看配置範本配置架構,以獲取用來定義配置範本和架構的規則清單,及在該架構中撰寫條件或巢狀表達式以進行自定義驗證的詳細資訊。

fsad-specs.json 檔案中,將元件名稱參考為 DeploymentName

{
    "components": [
        {
            "name": "${{$property(DeploymentName)}}",
            "type": "helm.v3",
            "properties": {
                "chart": {
                    "repo": "<repo uri>",
                    "version": "<version>",
                    "wait": true,
                    "timeout": "5m"
                }
            }
        }
    ]
}

根據階層,使用config set命令設定DeploymentName,如建立具有多個共用配接器相依性的解決方案中所述,或透過工作負載協調流程入口網站中的「設定」索引標籤進行設定。