本指南示範如何使用 cert-manager 自動發行 SSL/TLS 憑證,並將 SSL/TLS 憑證更新至容器部署 Azure 應用程式閘道 的一或多個前端。 我們使用閘道 API 來設定必要的資源。
基於此範例的目的,我們有憑證管理員設定從 Let's Encrypt 發行的憑證,以示範端對端部署,其中容器 應用程式閘道 提供 TLS 卸除。
若要讓 Let's Encrypt 簽發的憑證,授權單位需要挑戰來驗證網域擁有權。 此驗證會藉由允許憑證管理員建立 Pod 和 HTTPRoute 資源,以在憑證發行期間公開端點,證明您擁有功能變數名稱。
如需 cert-manager 和 Let's Encrypt with AKS 的詳細資訊,請參閱 這裡。
必要條件
如果遵循 BYO 部署策略,則請確定您已設定適用於容器的應用程式閘道資源和 ALB 控制器
如果遵循 ALB 受控部署策略,請確定您已透過 ApplicationLoadBalancer 自訂資源佈建了 ALB 控制器,以及適用於容器的應用程式閘道。
部署範例 HTTP 應用程式在您的叢集上套用下列 deployment.yaml 檔案,以建立範例 Web 應用程式來示範標頭重寫。
kubectl apply -f https://raw.githubusercontent.com/MicrosoftDocs/azure-docs/refs/heads/main/articles/application-gateway/for-containers/examples/traffic-split-scenario/deployment.yaml
此指令會在您的叢集上建立下列物件:
- 名為
test-infra
的命名空間 - 在
test-infra
命名空間中的兩個服務,分別名為backend-v1
和backend-v2
- 在
test-infra
命名空間中的兩個部署,分別名為backend-v1
和backend-v2
- 名為
建立閘道資源
建立新的 Gateway
資源,以在挑戰程序期間接聽來自 Let's Encrypt 的 HTTP 要求。
建立閘道:
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: gateway-01
namespace: test-infra
annotations:
alb.networking.azure.io/alb-namespace: alb-test-infra
alb.networking.azure.io/alb-name: alb-test
cert-manager.io/issuer: letsencrypt-cert
spec:
gatewayClassName: azure-alb-external
listeners:
- name: http-listener
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
EOF
注意
當 ALB 控制器在 ARM 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對前端資源使用下列命名慣例:fe-<8 個隨機產生的字元>
如果您要變更在 Azure 中建立的前端名稱,請考慮遵循自備部署策略。
建立閘道資源之後,請確定狀態有效,接聽程式的狀態為 [已程式化],並且已經將位址指派給閘道。
kubectl get gateway gateway-01 -n test-infra -o yaml
成功建立閘道後輸出內容的範例。
status:
addresses:
- type: IPAddress
value: xxxx.yyyy.alb.azure.com
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Valid Gateway
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
listeners:
- attachedRoutes: 0
conditions:
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Listener is accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T21:04:55Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
name: https-listener
supportedKinds:
- group: gateway.networking.k8s.io
kind: HTTPRoute
安裝 Cert-Manager
使用 Helm 安裝 cert-manager:
helm repo add jetstack https://charts.jetstack.io --force-update
helm install \
cert-manager jetstack/cert-manager \
--namespace cert-manager \
--create-namespace \
--version v1.17.1 \
--set config.enableGatewayAPI=true \
--set crds.enabled=true
Helm 安裝會在名為 cert-manager
的新命名空間中建立三個部署和一些服務和 Pod。 它也會安裝叢集範圍的支持資源,例如 RBAC 角色和自定義資源定義。
建立 ClusterIssuer
建立 ClusterIssuer 資源,以定義 cert-manager 如何與 Let's Encrypt 通訊。 在此範例中,會使用 HTTP 挑戰。 在挑戰期間,cert-manager 會 HTTPRoute
建立資源與對應的 Pod,並呈現驗證端點,以證明網域的擁有權。
提示
Let's Encrypt 支援的其他挑戰記載于 letsencrypt.org - 挑戰類型
kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: ClusterIssuer
metadata:
name: letsencrypt-prod
namespace: test-infra
spec:
acme:
server: https://acme-v02.api.letsencrypt.org/directory # production endpoint
#server: https://acme-staging-v02.api.letsencrypt.org/directory # staging endpoint
email: your-email@example.com
privateKeySecretRef:
name: letsencrypt-private-key
solvers:
- http01:
gatewayHTTPRoute:
parentRefs:
- name: gateway-01
namespace: test-infra
kind: Gateway
EOF
確認資源已建立
kubectl get ClusterIssuer -A -o yaml
狀態應該會顯示 True
,並輸入 Ready
。
status:
acme:
lastPrivateKeyHash: x+xxxxxxxxxxxxxxxxxxxxxxx+MY4PAEeotr9XH3V7I=
lastRegisteredEmail: your-email@example.com
uri: https://acme-staging-v02.api.letsencrypt.org/acme/acct/165888253
conditions:
- lastTransitionTime: "2024-10-04T21:22:40Z"
message: The ACME account was registered with the ACME server
observedGeneration: 1
reason: ACMEAccountRegistered
status: "True"
type: Ready
建立憑證
kubectl apply -f - <<EOF
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: letsencrypt-cert
namespace: test-infra
spec:
secretName: letsencrypt-secret # name published to secret store
issuerRef:
name: letsencrypt-prod # ClusterIssuer resource name
kind: ClusterIssuer
dnsNames:
- contoso.com # domain name to be used
EOF
執行下列命令來驗證憑證的發行。 如果憑證已發出,則資料行的值 READY
應該是 True
。
kubectl get certificate letsencrypt-cert -n test-infra
如果憑證未發出,您可以執行下列命令來驗證挑戰的狀態。
注意
如果已成功發行憑證,將不會再列出挑戰。
kubectl get challenges -n test-infra -o yaml
在閘道資源上啟用 HTTPS
修改閘道以新增第二個接聽程式,以終止已發行的 Let's Encrypt 憑證的 HTTPS 要求。
建立閘道:
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: gateway-01
namespace: test-infra
annotations:
alb.networking.azure.io/alb-namespace: alb-test-infra
alb.networking.azure.io/alb-name: alb-test
cert-manager.io/issuer: letsencrypt-cert
spec:
gatewayClassName: azure-alb-external
listeners:
- name: http-listener
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Same
- name: https-listener
port: 443
protocol: HTTPS
tls:
certificateRefs:
- name: letsencrypt-secret
allowedRoutes:
namespaces:
from: Same
EOF
注意
當 ALB 控制器在 ARM 中建立適用於容器的應用程式閘道資源時,ALB 控制器會針對前端資源使用下列命名慣例:fe-<8 個隨機產生的字元>
如果您要變更在 Azure 中建立的前端名稱,請考慮遵循自備部署策略。
建立接聽主機名的 HTTPRoute
建立 HTTPRoute 來處理接聽程式所 https-listener
接收的要求。
重要
請確定您將取代 contoso.com
為您預期要核發憑證的功能變數名稱。
kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: https-example
namespace: test-infra
spec:
parentRefs:
- name: gateway-01
hostnames:
- "contoso.com"
rules:
- backendRefs:
- name: backend-v1
port: 8080
EOF
建立 HTTPRoute 資源之後,請確定路由的狀態為「已接受」,且適用於容器的應用程式閘道資源的狀態為「已程式化」。
kubectl get httproute cert-manager-route -n test-infra -o yaml
確認已成功更新適用於容器的應用程式閘道資源的狀態。
status:
parents:
- conditions:
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: ""
observedGeneration: 1
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Route is Accepted
observedGeneration: 1
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2023-06-19T22:18:23Z"
message: Application Gateway For Containers resource has been successfully updated.
observedGeneration: 1
reason: Programmed
status: "True"
type: Programmed
controllerName: alb.networking.azure.io/alb-controller
parentRef:
group: gateway.networking.k8s.io
kind: Gateway
name: gateway-01
namespace: test-infra
測試應用程式的存取權
現在我們已準備好透過憑證所使用的主機名,將一些流量傳送至範例應用程式。
重要
請確定您將取代 contoso.com
為您預期要核發憑證的功能變數名稱。
curl https://contoso.com/ -v 2>&1 | grep issuer
結果時,您應該會看到
* issuer: C=US; O=Let's Encrypt; CN=R10
恭喜,您已安裝 ALB 控制器、部署後端應用程式、從 Let's Encrypt with cert-manager 發出憑證,以及透過容器 應用程式閘道 將流量路由傳送至應用程式。