新消息:在TwitterMastodon取得專案更新

Pomerium Ingress

本教學涵蓋安裝 Pomerium Ingress 控制器並使用 cert-manager 保護它。Pomerium 是一個身分感知代理,也可以為您的 Kubernetes 服務提供自訂的 Ingress 控制器。

先決條件

  1. 安裝 Kubectl 並將內容設定為您將使用的叢集。

  2. Pomerium 連接至身分提供者 (IdP) 以驗證使用者身分。請參閱他們的指南之一,了解如何設定您選擇的 IdP 以提供 oauth2 驗證。

  3. 本教學假設您已為此叢集保留網域名稱空間 (例如 *.example.com)。您將需要存取此網域的 DNS,以根據需要指派 A 和 CNAME 記錄。

安裝 Pomerium Ingress 控制器

  1. 將 Pomerium 安裝至您的叢集

    kubectl apply -f https://raw.githubusercontent.com/pomerium/ingress-controller/main/deployment.yaml

    使用您的 IdP 設定定義一個密鑰。請參閱 Pomerium 的身分提供者頁面,以取得針對您的 IdP 的更多資訊

    apiVersion: v1
    kind: Secret
    metadata:
    name: idp
    namespace: pomerium
    type: Opaque
    stringData:
    client_id: ${IDP_PROVIDED_CLIENT_ID}
    client_secret: ${IDP_PROVIDED_CLIENT_SECRET}

    使用 kubectl apply -f 將密鑰新增至叢集。

  2. 定義 Pomerium 的全域設定

    apiVersion: ingress.pomerium.io/v1
    kind: Pomerium
    metadata:
    name: global
    namespace: pomerium
    spec:
    secrets: pomerium/bootstrap
    authenticate:
    url: https://authenticate.example.com
    identityProvider:
    provider: ${YOUR_IdP}
    secret: pomerium/idp
    # certificates:
    # - pomerium/pomerium-proxy-tls

    ${YOUR_IdP} 取代為您的身分提供者。使用 kubectl -f 進行套用。

    請注意,最後兩行已註解。它們參考我們將在後續過程中建立的 TLS 憑證。

安裝 cert-manager

使用 cert-manager 文件安裝章節中記載的任何方法安裝 cert-manager。最簡單的方法是下載並套用提供的資訊清單

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.16.1/cert-manager.yaml

設定 Let's Encrypt 發行者

為了 Ingresses 和網際網路之間的通訊,我們需要使用受信任的憑證授權機構 (例如 Let's Encrypt) 簽署的憑證。此範例會建立兩個 Let's Encrypt 發行者,一個用於暫存,一個用於生產。

Let's Encrypt 生產發行者有嚴格的速率限制。在您的設定完成之前,您可能必須多次重新建立服務,從而達到這些限制。在建立堆疊時,很容易將速率限制與組態或操作中的錯誤混淆。

因此,我們將從 Let's Encrypt 暫存發行者開始。一旦您的組態幾乎完成,我們將切換到生產發行者。這兩個發行者都設定為使用 HTTP01 挑戰提供者。

  1. 下列 YAML 定義了暫存憑證發行者。您必須將電子郵件地址更新為您自己的。Let's Encrypt 需要 email 欄位,並使用該欄位來通知您憑證到期和更新。

    apiVersion: cert-manager.io/v1
    kind: Issuer
    metadata:
    name: letsencrypt-staging
    namespace: pomerium
    spec:
    acme:
    # The ACME server URL
    server: https://acme-staging-v02.api.letsencrypt.org/directory
    # Email address used for ACME registration
    email: user@example.com
    # Name of a secret used to store the ACME account private key
    privateKeySecretRef:
    name: letsencrypt-staging
    # Enable the HTTP-01 challenge provider
    solvers:
    - http01:
    ingress:
    ingressClassName: pomerium

    您可以下載並編輯範例,然後使用 kubectl apply -f 套用,或編輯,然後在一個命令中套用自訂資源

    kubectl create --edit -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/pomerium-staging-issuer.yaml
  2. 建立生產發行者並部署它。與暫存發行者一樣,使用您自己的電子郵件地址更新此範例

    apiVersion: cert-manager.io/v1
    kind: Issuer
    metadata:
    name: letsencrypt-prod
    namespace: pomerium
    spec:
    acme:
    # The ACME server URL
    server: https://acme-v02.api.letsencrypt.org/directory
    # Email address used for ACME registration
    email: user@example.com
    # Name of a secret used to store the ACME account private key
    privateKeySecretRef:
    name: letsencrypt-prod
    # Enable the HTTP-01 challenge provider
    solvers:
    - http01:
    ingress:
    ingressClassName: pomerium
    kubectl create --edit -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/pomerium-production-issuer.yaml
  3. 您可以在建立發行者後確認其狀態

    kubectl describe issuer -n pomerium letsencrypt-staging
    kubectl describe issuer -n pomerium letsencrypt-prod

    您應該會看到列出的發行者具有已註冊的帳戶。

  4. 定義 Pomerium Proxy 服務的憑證。這應該是您需要手動定義的唯一憑證

    apiVersion: cert-manager.io/v1
    kind: Certificate
    metadata:
    name: pomerium-proxy-tls
    namespace: pomerium
    spec:
    dnsNames:
    - 'authenticate.example.com'
    issuerRef:
    kind: Issuer
    name: letsencrypt-staging
    secretName: pomerium-proxy-tls

    調整 dnsNames 值以符合您的網域名稱空間。子網域(範例中為 authenticate)必須符合您 IdP 設定中用於回呼 URL 的網域。使用 kubectl -f 新增憑證。

  5. 取消註解 Pomerium 全域組態中參考您新建立的憑證的最後兩行,然後重新套用至叢集。

Pomerium 現在應該已安裝並在您的叢集中執行。您可以前往瀏覽器中的 https://authenticate.example.com 來驗證。使用 kubectl describe pomerium 來檢視 Pomerium 部署的狀態並查看最近的事件。

定義測試服務

為了測試我們新的 Ingress 控制器,我們將 kuard 應用程式新增至我們的叢集,並為其定義一個 Ingress。

  1. 定義 kuard 部署和相關聯的服務

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: kuard
    spec:
    selector:
    matchLabels:
    app: kuard
    replicas: 1
    template:
    metadata:
    labels:
    app: kuard
    spec:
    containers:
    - image: gcr.io/kuar-demo/kuard-amd64:1
    imagePullPolicy: Always
    name: kuard
    ports:
    - containerPort: 8080
    apiVersion: v1
    kind: Service
    metadata:
    name: kuard
    spec:
    ports:
    - port: 80
    targetPort: 8080
    protocol: TCP
    selector:
    app: kuard

    您可以在本機下載並參考這些檔案,也可以從本文件 GitHub 來源存放庫中參考它們。

    若要從 GitHub 直接從教學檔案安裝範例服務

    kubectl apply -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/deployment.yaml
    kubectl apply -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/service.yaml
  2. 為我們的測試服務建立一個新的 Ingress 資訊清單 (example-ingress.yaml)

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: kuard
    annotations:
    cert-manager.io/issuer: letsencrypt-staging
    ingress.pomerium.io/policy: '[{"allow":{"and":[{"domain":{"is":"example.com"}}]}}]'
    spec:
    ingressClassName: pomerium
    rules:
    - host: kuard.example.com
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: kuard
    port:
    number: 80
    tls:
    - hosts:
    - kuard.example.com
    secretName: kuard.example.com-tls

    再次將 example.com 的參考變更為符合您的網域名稱空間。

  3. 將 Ingress 資訊清單套用至叢集

    kubectl apply -f example-ingress.yaml

Pomerium Ingress 控制器將使用 cert-manager 自動從 letsencrypt-staging 發行者為 kuard.example.com 的路由佈建憑證。

在您的叢集中正確設定所有應用程式服務後,請調整 Ingresses (包括驗證服務) 的發行者,以使用 letsencrypt-prod