共用方式為


使用 AKS 叢集 應用程式閘道 上的 [讓我們加密憑證]

您可以將 Azure Kubernetes Service (AKS) 實例設定為使用 Let's Encrypt ,並自動取得網域的 TLS/SSL 憑證。 憑證會安裝在 Azure 應用程式閘道 上,這會為您的 AKS 叢集執行 TLS/SSL 終止。

本文描述的設定會使用 cert-manager Kubernetes 附加元件,其會自動建立和管理憑證。

提示

請考慮針對 Kubernetes 輸入解決方案的容器 應用程式閘道。

安裝附加元件

使用下列步驟在 現有的 AKS 叢集上安裝 cert-manager

  1. 執行下列腳本以安裝 cert-manager Helm 圖表。 此指令碼會執行下列動作:

    • 在您的 AKS 叢集上建立新的 cert-manager 命名空間
    • 建立下列自訂資源定義 (CRD):Certificate、、ChallengeClusterIssuerIssuerOrder
    • 安裝 cert-manager 圖表(來自 cert-manager 網站
    #!/bin/bash
    
    # Install the CustomResourceDefinition resources separately
    kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.1/cert-manager.crds.yaml
    
    # Create the namespace for cert-manager
    kubectl create namespace cert-manager
    
    # Label the cert-manager namespace to disable resource validation
    kubectl label namespace cert-manager cert-manager.io/disable-validation=true
    
    # Add the Jetstack Helm repository
    helm repo add jetstack https://charts.jetstack.io
    
    # Update your local Helm chart repository cache
    helm repo update
    
    # Install the cert-manager Helm chart
    # Helm v3+
    helm install \
      cert-manager jetstack/cert-manager \
      --namespace cert-manager \
      --version v1.16.1 \
      # --set installCRDs=true
    
    # To automatically install and manage the CRDs as part of your Helm release,
    # you must add the --set installCRDs=true flag to your Helm installation command.
    
  2. 建立 ClusterIssuer 資源。 Cert-manager 需要此資源來代表發出已簽署憑證的 Let's Encrypt 證書頒發機構單位。

    Cert-manager 使用非命名空間的 ClusterIssuer 資源時,以發行可從多個命名空間取用的憑證。 讓我們加密會使用 ACME 通訊協定來確認您控制特定功能變數名稱併發出憑證。 您可以在 cert-manager 檔中取得設定屬性的詳細資訊ClusterIssuer

    ClusterIssuer 指示 cert-manager 使用用於測試的 Let's Encrypt 預備環境來發行憑證。 (根證書不存在於瀏覽器/用戶端信任存放區中。

    下列 YAML 中的預設挑戰類型為 http01。 您可以在 Let's Encrypt 檔中找到其他挑戰類型。

    在下列 YAML 中,請務必將 取代 <YOUR.EMAIL@ADDRESS> 為您的資訊。

    #!/bin/bash
    kubectl apply -f - <<EOF
    apiVersion: cert-manager.io/v1
    kind: ClusterIssuer
    metadata:
      name: letsencrypt-staging
    spec:
      acme:
        # You must replace this email address with your own.
        # Let's Encrypt uses this to contact you about expiring
        # certificates, and issues related to your account.
        email: <YOUR.EMAIL@ADDRESS>
        # ACME server URL for Let's Encrypt's staging environment.
        # The staging environment won't issue trusted certificates but is
        # used to ensure that the verification process is working properly
        # before moving to production
        server: https://acme-staging-v02.api.letsencrypt.org/directory
        privateKeySecretRef:
          # Secret resource used to store the account's private key.
          name: example-issuer-account-key
        # Enable the HTTP-01 challenge provider
        # you prove ownership of a domain by ensuring that a particular
        # file is present at the domain
        solvers:
          - http01:
            ingress:
             #   class: azure/application-gateway
               ingressTemplate:
                 metadata:
                   annotations:
                     kubernetes.io/ingress.class: azure/application-gateway
    EOF
    
  3. 使用 應用程式閘道 部署搭配 Let's Encrypt 憑證,建立輸入資源以公開guestbook應用程式。

    請確定您的 應用程式閘道 部署具有具有 DNS 名稱的公用前端 IP 組態。 使用預設 azure.com 網域,或布建 Azure DNS 區域,然後指派您自己的自定義網域。 批註 certmanager.k8s.io/cluster-issuer: letsencrypt-staging 會指示 cert-manager 處理標記的輸入資源。

    在下列 YAML 中,請務必將 取代<PLACEHOLDERS.COM>為您自己的網域或 應用程式閘道 網域 (例如 , kh-aks-ingress.westeurope.cloudapp.azure.com)。

    kubectl apply -f - <<EOF
    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: guestbook-letsencrypt-staging
      annotations:
        kubernetes.io/ingress.class: azure/application-gateway
        cert-manager.io/cluster-issuer: letsencrypt-staging
    spec:
      tls:
      - hosts:
        - <PLACEHOLDERS.COM>
        secretName: guestbook-secret-name
      rules:
      - host: <PLACEHOLDERS.COM>
          http:
          paths:
          - backend:
              serviceName: frontend
              servicePort: 80
    EOF
    

    幾秒鐘之後,您可以使用自動發行的 Let's Encrypt 憑證進行預備,透過 應用程式閘道 HTTPS URL 存取guestbook服務。

    您的瀏覽器可能會警告您無效的證書頒發機構單位。 CN=Fake LE Intermediate X1原因是已發行預備憑證。 此警告表示系統如預期般運作,且您已準備好使用生產憑證。

  4. 成功設定預備憑證之後,您可以切換至生產 ACME 伺服器:

    1. 將輸入資源上的暫存批註取代為 cert-manager.io/cluster-issuer: letsencrypt-prod
    2. 刪除您稍早建立的現有預備 ClusterIssuer 資源。 將上 ClusterIssuer 一個 YAML 中的 ACME 伺服器取代為 https://acme-v02.api.letsencrypt.org/directory,以建立新的預備資源。

在 Let's Encrypt 憑證到期之前, cert-manager 請自動更新 Kubernetes 秘密存放區中的憑證。 此時,應用程式閘道 輸入控制器會套用其用來設定 應用程式閘道 之輸入資源中所參考的更新密碼。