Pomerium Ingress
本教程介绍如何安装 Pomerium Ingress 控制器 并使用 cert-manager 来保护它。 Pomerium 是一种身份感知代理,也可以为您的 Kubernetes 服务提供自定义 Ingress 控制器。
先决条件
-
安装 Kubectl 并将上下文设置为您将要使用的集群。
-
Pomerium 连接到身份提供者 (**IdP**) 以验证用户。请参阅他们的 指南 中的其中一个,了解如何设置您选择的 IdP 以提供 oauth2 验证。
-
本教程假设您已为该集群保留了域名空间(例如
*.example.com
)。您将需要访问该域名的 DNS,以根据需要分配 A 和 CNAME 记录。
安装 Pomerium Ingress 控制器
-
将 Pomerium 安装到您的集群
kubectl apply -f https://raw.githubusercontent.com/pomerium/ingress-controller/main/deployment.yaml使用您的 IdP 配置定义一个 Secret。有关特定于您的 IdP 的更多信息,请参阅 Pomerium 的 身份提供者 页面
apiVersion: v1kind: Secretmetadata:name: idpnamespace: pomeriumtype: OpaquestringData:client_id: ${IDP_PROVIDED_CLIENT_ID}client_secret: ${IDP_PROVIDED_CLIENT_SECRET}使用
kubectl apply -f
将 Secret 添加到集群。 -
为 Pomerium 定义全局设置
apiVersion: ingress.pomerium.io/v1kind: Pomeriummetadata:name: globalnamespace: pomeriumspec:secrets: pomerium/bootstrapauthenticate:url: https://authenticate.example.comidentityProvider: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 发行商
对于 Ingress 和互联网之间的通信,我们希望使用由受信任的证书颁发机构(如 Let's Encrypt)签名的证书。此示例创建了两个 Let's Encrypt 发行商,一个用于暂存,另一个用于生产。
Let's Encrypt 生产发行商具有 严格的速率限制。在您的配置最终确定之前,您可能需要多次重新创建服务,从而达到这些限制。在构建您的堆栈时,很容易将速率限制与配置或操作错误混淆。
因此,我们将从 Let's Encrypt 暂存发行商开始。配置完成后,我们将切换到生产发行商。这两个发行商都配置为使用 HTTP01
挑战提供程序。
-
以下 YAML 定义了一个暂存证书发行商。您必须将电子邮件地址更新为您的电子邮件地址。
email
字段是 Let's Encrypt 要求的,用于通知您证书到期和更新。apiVersion: cert-manager.io/v1kind: Issuermetadata:name: letsencrypt-stagingnamespace: pomeriumspec:acme:# The ACME server URLserver: https://acme-staging-v02.api.letsencrypt.org/directory# Email address used for ACME registrationemail: user@example.com# Name of a secret used to store the ACME account private keyprivateKeySecretRef:name: letsencrypt-staging# Enable the HTTP-01 challenge providersolvers:- 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 -
创建一个生产发行商并部署它。与暂存发行商一样,使用您自己的电子邮件地址更新此示例
apiVersion: cert-manager.io/v1kind: Issuermetadata:name: letsencrypt-prodnamespace: pomeriumspec:acme:# The ACME server URLserver: https://acme-v02.api.letsencrypt.org/directory# Email address used for ACME registrationemail: user@example.com# Name of a secret used to store the ACME account private keyprivateKeySecretRef:name: letsencrypt-prod# Enable the HTTP-01 challenge providersolvers:- http01:ingress:ingressClassName: pomeriumkubectl create --edit -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/pomerium-production-issuer.yaml -
您可以在创建发行商后确认其状态
kubectl describe issuer -n pomerium letsencrypt-stagingkubectl describe issuer -n pomerium letsencrypt-prod您应该看到发行商列表,其中包含已注册的帐户。
-
为 Pomerium 代理服务定义证书。这应该是您需要手动定义的唯一证书
apiVersion: cert-manager.io/v1kind: Certificatemetadata:name: pomerium-proxy-tlsnamespace: pomeriumspec:dnsNames:- 'authenticate.example.com'issuerRef:kind: Issuername: letsencrypt-stagingsecretName: pomerium-proxy-tls调整
dnsNames
值以匹配您的域名空间。子域名(在我们示例中为authenticate
)必须与 IdP 配置中用于回调 URL 的域名匹配。使用kubectl -f
添加证书。 -
取消注释 Pomerium 全局配置中引用您新创建证书的最后两行,然后重新应用到集群。
Pomerium 现在应该已安装并在您的集群中运行。您可以通过在浏览器中访问 https://authenticate.example.com
来验证。使用 kubectl describe pomerium
查看 Pomerium 部署的状态并查看最近的事件。
定义一个测试服务
为了测试我们的新 Ingress 控制器,我们将 kuard 应用添加到我们的集群并为其定义 Ingress。
-
定义 kuard 部署和相关服务
apiVersion: apps/v1kind: Deploymentmetadata:name: kuardspec:selector:matchLabels:app: kuardreplicas: 1template:metadata:labels:app: kuardspec:containers:- image: gcr.io/kuar-demo/kuard-amd64:1imagePullPolicy: Alwaysname: kuardports:- containerPort: 8080apiVersion: v1kind: Servicemetadata:name: kuardspec:ports:- port: 80targetPort: 8080protocol: TCPselector:app: kuard您可以下载并本地引用这些文件,也可以从本文档的 GitHub 源代码库中引用它们。
要从 GitHub 的教程文件中直接安装示例服务,请执行以下操作
kubectl apply -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/deployment.yamlkubectl apply -f https://raw.githubusercontent.com/cert-manager/website/master/content/docs/tutorials/acme/example/service.yaml -
为我们的测试服务创建一个新的 Ingress 清单 (
example-ingress.yaml
)apiVersion: networking.k8s.io/v1kind: Ingressmetadata:name: kuardannotations:cert-manager.io/issuer: letsencrypt-stagingingress.pomerium.io/policy: '[{"allow":{"and":[{"domain":{"is":"example.com"}}]}}]'spec:ingressClassName: pomeriumrules:- host: kuard.example.comhttp:paths:- path: /pathType: Prefixbackend:service:name: kuardport:number: 80tls:- hosts:- kuard.example.comsecretName: kuard.example.com-tls同样,将对
example.com
的引用更改为匹配您的域名空间。 -
将 Ingress 清单应用到集群
kubectl apply -f example-ingress.yaml
Pomerium Ingress 控制器将使用 cert-manager 从 letsencrypt-staging
发行商为 kuard.example.com
的路由自动配置证书。
在您在集群中正确配置所有应用程序服务后,将 Ingress(包括身份验证服务)的发行商调整为使用 letsencrypt-prod
。