共用方式為


Kubernetes 託管

Kubernetes 是裝載 Orleans 應用程式的熱門選擇。 Orleans 會在 Kubernetes 中執行,但不需要特定設定,它也可以利用裝載平臺可以提供的額外知識。

Microsoft.Orleans.Hosting.Kubernetes 套件會新增整合,以在 Kubernetes 叢集中裝載 Orleans 應用程式。 封裝提供擴充方法 UseKubernetesHosting,其會執行下列動作:

請注意,Kubernetes 裝載套件不會使用 Kubernetes 進行叢集。 針對叢集,仍然需要個別的叢集提供者。 如需設定叢集的詳細資訊,請參閱 伺服器組態 檔。

這項功能會對服務的部署方式施加一些需求:

  • Silo 名稱必須符合 Pod 名稱。
  • Pod 必須具有 orleans/serviceIdorleans/clusterId 標籤,這些標籤對應於筒倉的 ServiceIdClusterId。 上述方法會將環境變數中的這些標籤傳遞到 Orleans 的對應選項。
  • Pod 必須設定下列環境變數:POD_NAMEPOD_NAMESPACEPOD_IPORLEANS_SERVICE_IDORLEANS_CLUSTER_ID

下列範例示範如何正確設定這些標籤和環境變數:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dictionary-app
  labels:
    orleans/serviceId: dictionary-app
spec:
  selector:
    matchLabels:
      orleans/serviceId: dictionary-app
  replicas: 3
  template:
    metadata:
      labels:
        # This label is used to identify the service to Orleans
        orleans/serviceId: dictionary-app

        # This label is used to identify an instance of a cluster to Orleans.
        # Typically, this will be the same value as the previous label, or any
        # fixed value.
        # In cases where you are not using rolling deployments (for example,
        # blue/green deployments),
        # this value can allow for distinct clusters which do not communicate
        # directly with each others,
        # but which still share the same storage and other resources.
        orleans/clusterId: dictionary-app
    spec:
      containers:
        - name: main
          image: my-registry.azurecr.io/my-image
          imagePullPolicy: Always
          ports:
          # Define the ports which Orleans uses
          - containerPort: 11111
          - containerPort: 30000
          env:
          # The Azure Storage connection string for clustering is injected as an
          # environment variable
          # It must be created separately using a command such as:
          # > kubectl create secret generic az-storage-acct `
          #     --from-file=key=./az-storage-acct.txt
          - name: STORAGE_CONNECTION_STRING
            valueFrom:
              secretKeyRef:
                name: az-storage-acct
                key: key
          # Configure settings to let Orleans know which cluster it belongs to
          # and which pod it is running in
          - name: ORLEANS_SERVICE_ID
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['orleans/serviceId']
          - name: ORLEANS_CLUSTER_ID
            valueFrom:
              fieldRef:
                fieldPath: metadata.labels['orleans/clusterId']
          - name: POD_NAMESPACE
            valueFrom:
              fieldRef:
                fieldPath: metadata.namespace
          - name: POD_NAME
            valueFrom:
              fieldRef:
                fieldPath: metadata.name
          - name: POD_IP
            valueFrom:
              fieldRef:
                fieldPath: status.podIP
          - name: DOTNET_SHUTDOWNTIMEOUTSECONDS
            value: "120"
          request:
            # Set resource requests
      terminationGracePeriodSeconds: 180
      imagePullSecrets:
        - name: my-image-pull-secret
  minReadySeconds: 60
  strategy:
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1

針對已啟用 RBAC 的叢集,Pod 的 Kubernetes 服務帳戶可能也需要被賦予所需的存取權限:

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: orleans-hosting
rules:
- apiGroups: [ "" ]
  resources: ["pods"]
  verbs: ["get", "watch", "list", "delete", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: orleans-hosting-binding
subjects:
- kind: ServiceAccount
  name: default
  apiGroup: ''
roleRef:
  kind: Role
  name: orleans-hosting
  apiGroup: ''

存活性、準備狀態和啟動檢查

Kubernetes 可以探查 Pod,以判定服務的健康狀況。 如需詳細資訊,請參閱 Kubernetes 文件中的 設定 liveness、readiness 和 startup 探針

Orleans 使用叢集成員協議,迅速偵測並恢復程序或網路故障。 每個節點都會監視其他節點的子集,並傳送定期探測。 如果節點無法回應來自多個其他節點的多個後續探查,則會強制從叢集移除。 一旦失敗的節點得知它已被移除,就會立即終止。 Kubernetes 會重新啟動終止的進程,並嘗試重新加入叢集。

Kubernetes 的探查可協助判斷 Pod 中的進程是否正在執行,而且不會卡在殭屍狀態中。 探針不會驗證 Pod 間的連線能力或回應性,也不會進行任何應用層級的功能檢查。 如果 Pod 無法回應活躍度探查,Kubernetes 最終可能會終止該 Pod 並重新排程。 因此,Kubernetes 的探查和 Orleans『探查是互補的。

建議的方法是在 Kubernetes 中設定存活探測器(Liveness Probes),以執行基礎的本地檢查,以確認應用程式是否如預期般運作。 當出現完全凍結的情況時,例如運行時錯誤或其他不太可能的事件,這些探測器用於終止過程。

資源配額

Kubernetes 可與作業系統搭配運作,以實作 資源配額。 這可強制執行 CPU 和記憶體保留和/或限制。 對於提供互動式負載的主要應用程式,除非必要,否則不建議實作限制性限制。 請務必注意,要求和限制的意義和實作方式大不相同。 設定要求或限制之前,請花一點時間深入了解其實作和強制執行方式。 例如,Kubernetes、Linux 核心和監視系統之間可能不會統一測量記憶體。 CPU 配額可能無法以您預期的方式強制執行。

故障排除

Pod 墜毀,抱怨 KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined

完整例外狀況訊息:

Unhandled exception. k8s.Exceptions.KubeConfigException: unable to load in-cluster configuration, KUBERNETES_SERVICE_HOST and KUBERNETES_SERVICE_PORT must be defined
at k8s.KubernetesClientConfiguration.InClusterConfig()
  • 檢查 Pod 內是否已設定 KUBERNETES_SERVICE_HOSTKUBERNETES_SERVICE_PORT 環境變數。 您可以執行下列命令來檢查 kubectl exec -it <pod_name> /bin/bash -c env
  • 請確定在 Kubernetes deployment.yaml上將 automountServiceAccountToken 設為 true。 如需詳細資訊,請參閱 設定 Pod 的服務帳戶