在 Kubernetes 上使用 DH2i DxEnterprise 部署可用性群組

適用於:SQL Server - Linux

此教學課程說明如何針對部署到 Azure Kubernetes Service (AKS) Kubernetes 叢集中的 SQL Server Linux 型容器,使用 DH2i DxEnterprise 設定 SQL Server Always On 可用性群組 (AG)。 您可以選擇邊車設定 (慣用),或建置您自己的自定義容器映像。

注意

Microsoft 支援資料移動、可用性群組與 SQL Server 元件。 DH2i 負責支援 DxEnterprise 產品,其中包括叢集與仲裁管理。

使用本文所述的步驟,了解如何部署 StatefulSet,並使用 DH2i DxEnterprise 解決方案來建立和設定可用性群組 (AG)。 本教學課程包含下列步驟。

  • 建立無周邊服務設定
  • 在與邊車容器相同的 Pod 中使用 SQL Server 和 DxEnterprise 建立 StatefulSet 設定
  • 建立及設定 SQL Server AG,並新增次要複本
  • 在 AG 中建立資料庫,並測試容錯移轉

必要條件

本教學課程示範具有三個複本的 AG 範例。 您需要:

  • Azure Kubernetes Service (AKS) 或 Kubernetes 叢集。
  • 已啟用 AG 功能和通道的有效 DxEnterprise 授權。 如需詳細資訊,請參閱非生產環境的開發人員版本,或生產工作負載的 DxEnterprise 軟體

建立無周邊服務

  1. 在 Kubernetes 叢集中,無周邊服務可讓您的 Pod 使用主機名彼此連線。

    若要建立無周邊服務,請使用下列範例內容建立名為 headless_services.yaml 的 YAML 檔案。

    #Headless services for local connections/resolution
    apiVersion: v1
    kind: Service
    metadata:
    name: dxemssql-0
    spec:
    clusterIP: None
    selector:
        statefulset.kubernetes.io/pod-name: dxemssql-0
    ports:
    - name: dxl
        protocol: TCP
        port: 7979
    - name: dxc-tcp
        protocol: TCP
        port: 7980
    - name: dxc-udp
        protocol: UDP
        port: 7981
    - name: sql
        protocol: TCP
        port: 1433
    - name: listener
        protocol: TCP
        port: 14033
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: dxemssql-1
    spec:
    clusterIP: None
    selector:
        statefulset.kubernetes.io/pod-name: dxemssql-1
    ports:
    - name: dxl
        protocol: TCP
        port: 7979
    - name: dxc-tcp
        protocol: TCP
        port: 7980
    - name: dxc-udp
        protocol: UDP
        port: 7981
    - name: sql
        protocol: TCP
        port: 1433
    - name: listener
        protocol: TCP
        port: 14033
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: dxemssql-2
    spec:
    clusterIP: None
    selector:
        statefulset.kubernetes.io/pod-name: dxemssql-2
    ports:
    - name: dxl
        protocol: TCP
        port: 7979
    - name: dxc-tcp
        protocol: TCP
        port: 7980
    - name: dxc-udp
        protocol: UDP
        port: 7981
    - name: sql
        protocol: TCP
        port: 1433
    - name: listener
        protocol: TCP
        port: 14033
    
  2. 執行下列 命令以套用組態。

    kubectl apply -f headless_services.yaml
    

建立 StatefulSet

  1. 使用下列範例內容建立 StatefulSet YAML 檔案,並將它命名為 dxemssql.yaml

    此 StatefulSet 設定會建立三個 DxEMSSQL 複本,利用永續性磁碟區宣告來儲存其資料。 此 StatefulSet 中的每個 Pod 都包含兩個容器:SQL Server 容器和 DxEnterprise 容器。 這些容器會在「邊車」設定中彼此分開啟動,但 DxEnterprise 會管理 SQL Server 容器中的 AG 複本。

    #DxEnterprise + MSSQL StatefulSet
    apiVersion: apps/v1
    kind: StatefulSet
    metadata:
    name: dxemssql
    spec:
    serviceName: "dxemssql"
    replicas: 3
    selector:
        matchLabels:
        app: dxemssql
    template:
        metadata:
        labels:
            app: dxemssql
        spec:
        securityContext:
            fsGroup: 10001
        containers:
        - name: sql
            image: mcr.microsoft.com/mssql/server:2022-latest
            env:
            - name: ACCEPT_EULA
            value: "Y"
            - name: MSSQL_ENABLE_HADR
            value: "1"
            - name: MSSQL_SA_PASSWORD
            valueFrom:
                secretKeyRef:
                name: mssql
                key: MSSQL_SA_PASSWORD
            volumeMounts:
            - name: mssql
            mountPath: "/var/opt/mssql"
        - name: dxe
            image: dh2i/dxe
            env:
            - name: MSSQL_SA_PASSWORD
            valueFrom:
                secretKeyRef:
                name: mssql
                key: MSSQL_SA_PASSWORD
            volumeMounts:
            - name: dxe
            mountPath: "/etc/dh2i"
    volumeClaimTemplates:
    - metadata:
        name: dxe
        spec:
        accessModes:
        - ReadWriteOnce
        resources:
            requests:
            storage: 1Gi
    - metadata:
        name: mssql
        spec:
        accessModes:
        - ReadWriteOnce
        resources:
            requests:
            storage: 1Gi
    
  2. 建立 SQL Server 執行個體的認證。

    kubectl create secret generic mssql --from-literal=MSSQL_SA_PASSWORD="<password>"
    
  3. 套用 StatefulSet 設定。

    kubectl apply -f dxemssql.yaml
    
  4. 確認 Pod 的狀態,並在 Pod 的狀態變成 running 時繼續進行下一個步驟。

    kubectl get pods
    kubectl describe pods
    

建立可用性群組並測試容錯移轉

如需建立和設定 AG、新增複本和測試容錯移轉的詳細資訊,請參閱 Kubernetes 中的 SQL Server 可用性群組

設定可用性群組接聽程式的步驟 (選擇性)

也可以遵循下列步驟,設定可用性群組接聽程式。

  1. 確定您已使用 DxEnterprise 建立 AG 接聽程式,如 DH2i 文件結尾的選擇性步驟中所述。

  2. 在 Kubernetes 中,您可以選擇性地建立靜態 IP 位址。 建立靜態 IP 位址時,確保如果刪除並重新建立接聽程式服務,指派給接聽程式服務的外部 IP 位址不會變更。 按照步驟在 Azure Kubernetes Service (AKS) 中建立靜態 IP 位址

  3. 建立 IP 位址之後,請指派該 IP 位址並建立負載平衡器服務,如下列 YAML 範例所示。

    apiVersion: v1
    kind: Service
    metadata:
      name: agslistener
    spec:
      type: LoadBalancer
      loadBalancerIP: 52.140.117.62
      selector:
        app: mssql
      ports:
      - protocol: TCP
        port: 44444
        targetPort: 44444
    

設定讀取/寫入連線重新導向的步驟 (選擇性)

建立可用性群組之後,您可以遵循下列步驟,啟用從次要複本到主要複本的讀取/寫入連線重新導向。 如需詳細資訊,請參閱次要到主要複本讀取/寫入連線重新導向 (Always On 可用性群組)

USE [master];
GO

ALTER AVAILABILITY
GROUP [ag_name] MODIFY REPLICA
    ON N'<name of the primary replica>'
WITH (SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL));
GO

USE [master];
GO

ALTER AVAILABILITY
GROUP [AGS1] MODIFY REPLICA
    ON N'<name of the secondary-0 replica>'
WITH (SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL));
GO

USE [master];
GO

ALTER AVAILABILITY
GROUP [AGS1] MODIFY REPLICA
    ON N'<name of the secondary-1 replica>'
WITH (SECONDARY_ROLE(ALLOW_CONNECTIONS = ALL));
GO

USE [master];
GO

ALTER AVAILABILITY
GROUP AGS1 MODIFY REPLICA
    ON N'<name of the primary replica>'
WITH (PRIMARY_ROLE(READ_WRITE_ROUTING_URL = 'TCP://<External IP address of primary -0>:1433'));
GO

USE [master];
GO

ALTER AVAILABILITY
GROUP AGS1 MODIFY REPLICA
    ON N'<name of the secondary-0 replica>'
WITH (PRIMARY_ROLE(READ_WRITE_ROUTING_URL = 'TCP://<External IP address of secondary -0>:1433'));
GO

USE [master];
GO

ALTER AVAILABILITY
GROUP AGS1 MODIFY REPLICA
    ON N'<name of the secondary-1 replica>'
WITH (PRIMARY_ROLE(READ_WRITE_ROUTING_URL = 'TCP://<External IP address of secondary -1>:1433'));
GO