使用连接链接配置和部署网关策略
在 OpenShift 中保护、保护和连接 API
摘要
前言
对红帽文档提供反馈
红帽感谢您对产品文档的反馈。
要进行改进,请创建一个 Jira 问题,并描述您推荐的更改。提供尽可能多的详细信息,以帮助文档团队快速定位请求。
前提条件
- 您有一个红帽客户门户网站帐户。此帐户允许您登录到 Red Hat Jira Software 实例。如果您没有帐户,系统会提示您创建一个帐户。
流程
- 单击以下链接: 创建问题。
- 在 Summary 文本框中,输入问题的简短描述。
在 Description 文本框中,提供以下信息:
- 找到此问题的页面的 URL。
- 有关此问题的详细描述。您可以将信息保留在其他字段中,使其默认值。
- 在 Reporter 字段中输入 JIRA 用户名。
- 点 Create 将 JIRA 问题提交到文档团队。
感谢您抽出时间提供反馈。
第 1 章 使用连接链接在 OpenShift 上保护、保护和连接 API
本指南指导您使用 OpenShift 上的连接链接来保护、保护和连接基于 Kubernetes 网关 API 的网关公开的 API。您可以将此流程用于在单个 OpenShift 集群上部署的网关,或使用共享 HTTPS 侦听器主机名在多个 OpenShift 集群中分发网关。本指南演示了平台工程师和应用程序开发人员用户角色如何能够各自使用连接链接来实现其目标。
1.1. 多集群环境中可以使用哪些连接链接
您可以在一个或多个 OpenShift 集群中利用连接链接的功能。以下功能设计为在多个集群以及单集群环境中工作:
-
multicluster ingress: 连接链接使用 DNS 提供多集群入口连接,以使用
DNSPolicy
中定义的策略将流量传递给您的网关。 -
全局速率限制: 当配置为使用共享 Redis 或 Dragonfly 存储的计数器时,链接可以启用全局速率限制用例,具体取决于
RateLimitPolicy
定义的限制。 -
全局身份验证 : 您可以配置连接链接
AuthPolicy
,以利用外部身份验证供应商来确保不同的集群公开相同的 API 身份验证和授权。 -
自动 TLS 证书生成 : 您可以使用与 cert-manager 和 ACME 供应商(如 Let 的 Encrypt)集成,将
TLSPolicy
配置为根据网关侦听器主机自动置备 TLS 证书。 - 与联合指标存储集成 : 连接链接具有示例仪表板和指标,用于视觉化您的网关,并观察在多个集群中到达这些网关的流量。
1.2. 用户角色工作流
- 平台工程师: 本指南介绍了如何部署提供安全通信的网关,并受到保护并可供应用程序开发团队用于部署 API 的使用。然后,它逐步在不同地理区域的集群中使用此网关,利用连接链接将特定流量带到您地理在一起的网关。这种方法减少了延迟和分发负载,同时仍然使用全局速率限制和 auth 保护。
应用程序开发人员 :本指南介绍了如何部署应用程序 API,并演示了如何覆盖您的网关级全局身份验证和速率限制策略,以配置应用程序级别的身份验证和速率限制要求。
注意有关在部署 OpenShift observability 堆栈和用户工作负载监控时用户角色如何观察和监控网关的详情,请参阅 Link Observability 指南。
1.3. 部署管理工具
虽然本指南使用 kubectl
命令来实现简单性,但使用多个集群非常复杂。最好使用 Argo CD 等工具来管理将资源部署到多个集群。
第 2 章 检查连接链接安装和权限
本指南要求您至少有一个 OpenShift 集群成功安装连接链接,并且具有正确的用户权限。
先决条件
- 在一个或多个集群中完成了连接链接安装步骤,如在 OpenShift 上安装连接链接 中所述。
-
已安装
kubectl
或oc
命令。 - 您对本指南中使用的 OpenShift 命名空间具有写入访问权限。
- 您有一个带有 Amazon Route 53 的 AWS 帐户,以及本指南中的示例的 DNS 区域。也支持 Google Cloud DNS 和 Microsoft Azure DNS。
可选:
- 对于多集群环境中的速率限制,您需要在多个集群中安装连接链接,并有一个共享的基于 Redis 的数据存储。如需了解更多详细信息,请参阅在 OpenShift 上安装连接链接。
- 对于 Observability,OpenShift 用户工作负载监控配置为对中央存储系统(如 Thanos )进行远程写入,如 连接 Link Observability 指南 中所述。
第 3 章 连接链接平台工程师工作流
本小节介绍了作为平台工程师,您如何部署提供安全通信的网关,并可供应用程序开发团队使用。它还演示了如何在不同的地理区域的多个集群中使用此网关。
先决条件
- 请参阅 第 2 章 检查连接链接安装和权限。
在多集群环境中,您必须单独在每个集群中执行以下步骤,除非特别排除。
3.1. 第 1 步 - 设置环境变量
流程
设置以下环境变量,在本指南中方便使用:
export zid=change-to-your-DNS-zone-ID export rootDomain=demo.example.com export gatewayNS=api-gateway export gatewayName=external export devNS=toystore export AWS_ACCESS_KEY_ID=xxxx export AWS_SECRET_ACCESS_KEY=xxxx export AWS_REGION=us-east-1 export clusterIssuerName=lets-encrypt export EMAIL=foo@example.com
export zid=change-to-your-DNS-zone-ID export rootDomain=demo.example.com export gatewayNS=api-gateway export gatewayName=external export devNS=toystore export AWS_ACCESS_KEY_ID=xxxx export AWS_SECRET_ACCESS_KEY=xxxx export AWS_REGION=us-east-1 export clusterIssuerName=lets-encrypt export EMAIL=foo@example.com
Copy to Clipboard Copied! 在本例中,
zid
是 AWS Route 53 控制台中显示的托管区 ID。rootDomain
是您要用于连接链接的顶级 AWS Route 53 域名。注意本指南仅将环境变量用于方便。如果您知道环境变量值,您可以设置所需的
.yaml
文件来满足您的需要。
3.2. 第 2 步 - 设置 DNS 供应商 secret
DNS 供应商提供一个凭证来访问连接链接用来设置 DNS 配置的 DNS 区域。您必须确保此凭证只能访问您要管理的区。
您必须将以下 Secret
资源应用到每个集群。如果要添加额外的集群,请将其添加到新集群中。
流程
如果您的网关命名空间不存在,请按如下所示创建:
kubectl create ns ${gatewayNS}
kubectl create ns ${gatewayNS}
Copy to Clipboard Copied! 如果在安装连接链接时尚未创建 DNS 供应商凭证的 secret,请在 Gateway 命名空间中创建此 secret,如下所示:
kubectl -n ${gatewayNS} create secret generic aws-credentials \ --type=kuadrant.io/aws \ --from-literal=AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \ --from-literal=AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
kubectl -n ${gatewayNS} create secret generic aws-credentials \ --type=kuadrant.io/aws \ --from-literal=AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \ --from-literal=AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
Copy to Clipboard Copied! 在添加 TLS 签发者前,还必须在
cert-manager
命名空间中创建凭证 secret,如下所示:kubectl -n cert-manager create secret generic aws-credentials \ --type=kuadrant.io/aws \ --from-literal=AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \ --from-literal=AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
kubectl -n cert-manager create secret generic aws-credentials \ --type=kuadrant.io/aws \ --from-literal=AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \ --from-literal=AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
Copy to Clipboard Copied!
3.3. 第 3 步 - 添加 TLS 签发者
要保护与网关的通信,您将为 TLS 证书定义 TLS 签发者。这个示例使用 Let 的 Encrypt,但您可以使用 cert-manager
支持的任何证书签发者。
流程
输入以下命令来定义 TLS 签发者。这个示例使用 Let 的 Encrypt,它还必须适用于所有集群:
kubectl apply -f - <<EOF apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: ${clusterIssuerName} spec: acme: email: ${EMAIL} privateKeySecretRef: name: le-secret server: https://acme-v02.api.letsencrypt.org/directory solvers: - dns01: route53: hostedZoneID: ${zid} region: ${AWS_REGION} accessKeyIDSecretRef: key: AWS_ACCESS_KEY_ID name: aws-credentials secretAccessKeySecretRef: key: AWS_SECRET_ACCESS_KEY name: aws-credentials EOF
kubectl apply -f - <<EOF apiVersion: cert-manager.io/v1 kind: ClusterIssuer metadata: name: ${clusterIssuerName} spec: acme: email: ${EMAIL} privateKeySecretRef: name: le-secret server: https://acme-v02.api.letsencrypt.org/directory solvers: - dns01: route53: hostedZoneID: ${zid} region: ${AWS_REGION} accessKeyIDSecretRef: key: AWS_ACCESS_KEY_ID name: aws-credentials secretAccessKeySecretRef: key: AWS_SECRET_ACCESS_KEY name: aws-credentials EOF
Copy to Clipboard Copied! 等待
ClusterIssuer
就绪,如下所示:kubectl wait clusterissuer/${clusterIssuerName} --for=condition=ready=true
kubectl wait clusterissuer/${clusterIssuerName} --for=condition=ready=true
Copy to Clipboard Copied!
3.4. 第 4 步 - 设置网关
要连接链接,以便在两个或多个集群中使用 DNS 来平衡流量,您必须使用共享主机定义网关。您将使用基于根域的通配符主机名的 HTTPS 侦听器来定义此定义。如前文所述,您必须将这些资源应用到所有集群。
现在,网关设置为只接受来自同一命名空间的 HTTPRoute
。这样,您可以限制谁可以使用网关,直到它准备好常规使用。
流程
输入以下命令来创建网关:
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: ${gatewayName} namespace: ${gatewayNS} labels: kuadrant.io/gateway: "true" spec: gatewayClassName: istio listeners: - allowedRoutes: namespaces: from: Same hostname: "*.${rootDomain}" name: api port: 443 protocol: HTTPS tls: certificateRefs: - group: "" kind: Secret name: api-${gatewayName}-tls mode: Terminate EOF
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: ${gatewayName} namespace: ${gatewayNS} labels: kuadrant.io/gateway: "true" spec: gatewayClassName: istio listeners: - allowedRoutes: namespaces: from: Same hostname: "*.${rootDomain}" name: api port: 443 protocol: HTTPS tls: certificateRefs: - group: "" kind: Secret name: api-${gatewayName}-tls mode: Terminate EOF
Copy to Clipboard Copied! 检查网关的状态,如下所示:
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}' kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Programmed")].message}'
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}' kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Programmed")].message}'
Copy to Clipboard Copied! 您的网关应被接受并编程,这意味着有效并分配一个外部地址。
但是,如果您检查 HTTPS 侦听器状态,您会看到它尚未编程,或者准备好因为错误的 TLS 配置接受流量:
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.listeners[0].conditions[?(@.type=="Programmed")].message}'
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.listeners[0].conditions[?(@.type=="Programmed")].message}'
Copy to Clipboard Copied! 连接链接可使用 TLSPolicy 来帮助解决这个问题,该策略在下一步中描述。
3.4.1. 可选:配置从网关实例中提取的指标
如果您在集群中设置了 Prometheus,您可以将 PodMonitor
配置为直接从 Gateway pod 中提取指标。指标需要此配置,如 istio_requests_total
。您必须在运行网关的命名空间中添加以下配置:
kubectl apply -f - <<EOF apiVersion: monitoring.coreos.com/v1 kind: PodMonitor metadata: name: istio-proxies-monitor namespace: ${gatewayNS} spec: selector: matchExpressions: - key: istio-prometheus-ignore operator: DoesNotExist podMetricsEndpoints: - path: /stats/prometheus interval: 30s relabelings: - action: keep sourceLabels: ["__meta_kubernetes_pod_container_name"] regex: "istio-proxy" - action: keep sourceLabels: ["__meta_kubernetes_pod_annotationpresent_prometheus_io_scrape"] - action: replace regex: (\d+);(([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4}) replacement: "[\$2]:\$1" sourceLabels: [ "__meta_kubernetes_pod_annotation_prometheus_io_port", "__meta_kubernetes_pod_ip", ] targetLabel: "__address__" - action: replace regex: (\d+);((([0-9]+?)(\.|$)){4}) replacement: "\$2:\$1" sourceLabels: [ "__meta_kubernetes_pod_annotation_prometheus_io_port", "__meta_kubernetes_pod_ip", ] targetLabel: "__address__" - action: labeldrop regex: "__meta_kubernetes_pod_label_(.+)" - sourceLabels: ["__meta_kubernetes_namespace"] action: replace targetLabel: namespace - sourceLabels: ["__meta_kubernetes_pod_name"] action: replace targetLabel: pod_name EOF
kubectl apply -f - <<EOF
apiVersion: monitoring.coreos.com/v1
kind: PodMonitor
metadata:
name: istio-proxies-monitor
namespace: ${gatewayNS}
spec:
selector:
matchExpressions:
- key: istio-prometheus-ignore
operator: DoesNotExist
podMetricsEndpoints:
- path: /stats/prometheus
interval: 30s
relabelings:
- action: keep
sourceLabels: ["__meta_kubernetes_pod_container_name"]
regex: "istio-proxy"
- action: keep
sourceLabels:
["__meta_kubernetes_pod_annotationpresent_prometheus_io_scrape"]
- action: replace
regex: (\d+);(([A-Fa-f0-9]{1,4}::?){1,7}[A-Fa-f0-9]{1,4})
replacement: "[\$2]:\$1"
sourceLabels:
[
"__meta_kubernetes_pod_annotation_prometheus_io_port",
"__meta_kubernetes_pod_ip",
]
targetLabel: "__address__"
- action: replace
regex: (\d+);((([0-9]+?)(\.|$)){4})
replacement: "\$2:\$1"
sourceLabels:
[
"__meta_kubernetes_pod_annotation_prometheus_io_port",
"__meta_kubernetes_pod_ip",
]
targetLabel: "__address__"
- action: labeldrop
regex: "__meta_kubernetes_pod_label_(.+)"
- sourceLabels: ["__meta_kubernetes_namespace"]
action: replace
targetLabel: namespace
- sourceLabels: ["__meta_kubernetes_pod_name"]
action: replace
targetLabel: pod_name
EOF
有关配置指标的更多信息,请参阅 连接链接 Observability 指南。
3.5. 第 5 步 - 配置您的网关策略和 HTTP 路由
虽然您的网关现已部署,但它没有公开的端点,且您的 HTTPS 侦听器没有被编程。接下来,您可以设置一个 TLSPolicy
,它利用您的 CertificateIssuer
设置 HTTPS 侦听器证书。
您将定义一个 AuthPolicy
,它将为任何未保护的端点设置默认的 HTTP 403
响应,以及一个 RateLimitPolicy
,它将设置默认的 artificially low 全局限制,以进一步保护此网关公开的任何端点。
您还将使用负载平衡策略定义 DNSPolicy
,以及用于网关的 HTTPRoute
与后端应用程序 API 通信。
3.5.1. 设置 TLS 策略
流程
为您的网关设置
TLSPolicy
,如下所示:kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: TLSPolicy metadata: name: ${gatewayName}-tls namespace: ${gatewayNS} spec: targetRef: name: ${gatewayName} group: gateway.networking.k8s.io kind: Gateway issuerRef: group: cert-manager.io kind: ClusterIssuer name: ${clusterIssuerName} EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: TLSPolicy metadata: name: ${gatewayName}-tls namespace: ${gatewayNS} spec: targetRef: name: ${gatewayName} group: gateway.networking.k8s.io kind: Gateway issuerRef: group: cert-manager.io kind: ClusterIssuer name: ${clusterIssuerName} EOF
Copy to Clipboard Copied! 检查控制器是否接受了您的 TLS 策略,如下所示:
kubectl get tlspolicy ${gatewayName}-tls -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
kubectl get tlspolicy ${gatewayName}-tls -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
Copy to Clipboard Copied!
3.5.2. 设置 Auth 策略
流程
为您的网关设置默认 deny-all
AuthPolicy
,如下所示:kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: AuthPolicy metadata: name: ${gatewayName}-auth namespace: ${gatewayNS} spec: targetRef: group: gateway.networking.k8s.io kind: Gateway name: ${gatewayName} defaults: rules: authorization: "deny": opa: rego: "allow = false" EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: AuthPolicy metadata: name: ${gatewayName}-auth namespace: ${gatewayNS} spec: targetRef: group: gateway.networking.k8s.io kind: Gateway name: ${gatewayName} defaults: rules: authorization: "deny": opa: rego: "allow = false" EOF
Copy to Clipboard Copied! 检查控制器是否接受您的 auth 策略,如下所示:
kubectl get authpolicy ${gatewayName}-auth -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
kubectl get authpolicy ${gatewayName}-auth -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
Copy to Clipboard Copied!
3.5.3. 设置速率限制策略
流程
为您的网关设置默认
RateLimitPolicy
,如下所示:kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: RateLimitPolicy metadata: name: ${gatewayName}-rlp namespace: ${gatewayNS} spec: targetRef: group: gateway.networking.k8s.io kind: Gateway name: ${gatewayName} defaults: limits: "low-limit": rates: - limit: 2 window: 10s EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: RateLimitPolicy metadata: name: ${gatewayName}-rlp namespace: ${gatewayNS} spec: targetRef: group: gateway.networking.k8s.io kind: Gateway name: ${gatewayName} defaults: limits: "low-limit": rates: - limit: 2 window: 10s EOF
Copy to Clipboard Copied! 注意根据您的集群应用
RateLimitPolicy
可能需要几分钟时间。本例中的限制非常低,可轻松地显示它。要检查您的速率限值已被接受,请输入以下命令:
kubectl get ratelimitpolicy ${gatewayName}-rlp -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
kubectl get ratelimitpolicy ${gatewayName}-rlp -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
Copy to Clipboard Copied!
3.5.4. 设置 DNS 策略
流程
为您的网关设置
DNSPolicy
,如下所示:kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: DNSPolicy metadata: name: ${gatewayName}-dnspolicy namespace: ${gatewayNS} spec: targetRef: name: ${gatewayName} group: gateway.networking.k8s.io kind: Gateway providerRefs: - name: aws-credentials loadBalancing: weight: 120 geo: EU defaultGeo: true EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: DNSPolicy metadata: name: ${gatewayName}-dnspolicy namespace: ${gatewayNS} spec: targetRef: name: ${gatewayName} group: gateway.networking.k8s.io kind: Gateway providerRefs: - name: aws-credentials loadBalancing: weight: 120 geo: EU defaultGeo: true EOF
Copy to Clipboard Copied! 注意DNSPolicy
将使用您之前定义的 DNS ProviderSecret
。本例中的
地理位置是EU
,但您可以更改它以符合您的要求。检查您的
DNSPolicy
是否已接受,如下所示:kubectl get dnspolicy ${gatewayName}-dnspolicy -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
kubectl get dnspolicy ${gatewayName}-dnspolicy -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Accepted")].message}'
Copy to Clipboard Copied!
3.5.5. 创建 HTTP 路由
出于测试目的,本节假定部署了 toystore 应用。如需更多信息,请参阅 第 4 章 连接链接应用程序开发人员工作流。
流程
创建一个
HTTPRoute
以测试您的网关,如下所示:kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: test namespace: ${gatewayNS} labels: service: toystore spec: parentRefs: - name: ${gatewayName} namespace: ${gatewayNS} hostnames: - "test.${rootDomain}" rules: - backendRefs: - name: toystore port: 80 EOF
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: test namespace: ${gatewayNS} labels: service: toystore spec: parentRefs: - name: ${gatewayName} namespace: ${gatewayNS} hostnames: - "test.${rootDomain}" rules: - backendRefs: - name: toystore port: 80 EOF
Copy to Clipboard Copied! 检查您的网关策略是否强制实施,如下所示:
kubectl get dnspolicy ${gatewayName}-dnspolicy -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}' kubectl get authpolicy ${gatewayName}-auth -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}' kubectl get ratelimitpolicy ${gatewayName}-rlp -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}'
kubectl get dnspolicy ${gatewayName}-dnspolicy -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}' kubectl get authpolicy ${gatewayName}-auth -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}' kubectl get ratelimitpolicy ${gatewayName}-rlp -n ${gatewayNS} -o=jsonpath='{.status.conditions[?(@.type=="Enforced")].message}'
Copy to Clipboard Copied! 检查您的 HTTPS 侦听器是否已就绪,如下所示:
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.listeners[0].conditions[?(@.type=="Programmed")].message}'
kubectl get gateway ${gatewayName} -n ${gatewayNS} -o=jsonpath='{.status.listeners[0].conditions[?(@.type=="Programmed")].message}'
Copy to Clipboard Copied!
3.6. 第 6 步 - 测试连接并拒绝所有 auth
您可以使用 curl
测试端点连接和 auth。
流程
输入以下命令:
curl -w "%{http_code}" https://$(kubectl get httproute test -n ${gatewayNS} -o=jsonpath='{.spec.hostnames[0]}')
curl -w "%{http_code}" https://$(kubectl get httproute test -n ${gatewayNS} -o=jsonpath='{.spec.hostnames[0]}')
Copy to Clipboard Copied! 您应看到 HTTP
403
响应。
3.7. 第 7 步 - 为其他命名空间打开网关
因为您已配置了网关,使用连接链接策略进行保护,并经过测试,您现在可以打开它以供其他命名空间中的其他团队使用。
流程
输入以下命令:
kubectl patch gateway ${gatewayName} -n ${gatewayNS} --type='json' -p='[{"op": "replace", "path": "/spec/listeners/0/allowedRoutes/namespaces/from", "value":"All"}]'
kubectl patch gateway ${gatewayName} -n ${gatewayNS} --type='json' -p='[{"op": "replace", "path": "/spec/listeners/0/allowedRoutes/namespaces/from", "value":"All"}]'
Copy to Clipboard Copied!
3.8. 第 8 步 - 将网关扩展到多个集群并配置基于地理的路由
流程
要在多个集群中分发此网关,请对每个集群重复此设置过程。
默认情况下,这将实施循环 DNS 策略,在不同集群中平均分配流量。将您的网关设置为根据其地理位置为客户端提供服务非常简单。
假设根据本指南在多个集群中部署了网关实例,下一步涉及使用可见网关的地理区域更新 DNS 控制器。
例如,如果您在北美有一个集群,在欧盟中有一个集群,您可以根据它们的位置将流量定向到这些网关。对于北美集群,您可以创建一个 DNSPolicy,并将
loadBalancing:geo
字段设置为US
。
第 4 章 连接链接应用程序开发人员工作流
此流程中的这一部分演示了作为应用程序开发人员,您可以覆盖现有的网关级策略来配置应用程序级别的路由、身份验证和速率限制要求。
先决条件
- 您的连接链接环境已设置,策略已配置,如 第 3 章 连接链接平台工程师工作流 所述。
4.1. 第 1 步 - 部署 toystore 应用程序
流程
如果不存在,为应用程序创建命名空间(如果不存在):
kubectl create ns ${devNS}
kubectl create ns ${devNS}
Copy to Clipboard Copied! 将
toystore
应用程序部署到开发人员命名空间中(如果还没有部署):kubectl apply -f https://raw.githubusercontent.com/Kuadrant/Kuadrant-operator/main/examples/toystore/toystore.yaml -n ${devNS}
kubectl apply -f https://raw.githubusercontent.com/Kuadrant/Kuadrant-operator/main/examples/toystore/toystore.yaml -n ${devNS}
Copy to Clipboard Copied!
4.2. 第 2 步 - 为您的 API 设置 HTTPRoute
流程
输入以下命令为您的 Toystore 应用程序 API 定义 HTTP 路由:
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: toystore labels: deployment: toystore service: toystore spec: parentRefs: - name: ${gatewayName} namespace: ${gatewayNS} hostnames: - "api.${rootDomain}" rules: - matches: - method: GET path: type: PathPrefix value: "/cars" - method: GET path: type: PathPrefix value: "/dolls" backendRefs: - name: toystore port: 80 - matches: - path: type: PathPrefix value: "/admin" backendRefs: - name: toystore port: 80 EOF
kubectl apply -f - <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: toystore labels: deployment: toystore service: toystore spec: parentRefs: - name: ${gatewayName} namespace: ${gatewayNS} hostnames: - "api.${rootDomain}" rules: - matches: - method: GET path: type: PathPrefix value: "/cars" - method: GET path: type: PathPrefix value: "/dolls" backendRefs: - name: toystore port: 80 - matches: - path: type: PathPrefix value: "/admin" backendRefs: - name: toystore port: 80 EOF
Copy to Clipboard Copied! 使用这个
HTTPRoute
时,部署的服务现在由网关公开。您可以通过 HTTPS 访问 API 端点,如下所示:
export INGRESS_HOST=$(kubectl get gtw ${gatewayName} -o jsonpath='{.status.addresses[0].value}' -n api-gateway) curl --resolve api.${rootDomain}:443:${INGRESS_HOST} "https://api.${rootDomain}/cars"
export INGRESS_HOST=$(kubectl get gtw ${gatewayName} -o jsonpath='{.status.addresses[0].value}' -n api-gateway) curl --resolve api.${rootDomain}:443:${INGRESS_HOST} "https://api.${rootDomain}/cars"
Copy to Clipboard Copied!
4.3. 第 3 步 - 覆盖网关的 deny-all AuthPolicy
接下来,您将允许对 Toystore API 进行身份验证的访问。您可以通过定义一个以上一步中创建的 HTTPRoute
资源为目标的 AuthPolicy
。
任何新的 HTTPRoutes 仍会受到现有网关级策略的影响。由于您想要用户现在访问此 API,所以您必须覆盖该网关策略。为了简单起见,您可以使用 API 密钥来验证请求,但也可使用 OpenID Connect 等其他选项。
流程
为 bob 和 alice 用户定义 API 密钥,如下所示:
kubectl apply -f - <<EOF apiVersion: v1 kind: Secret metadata: name: bob-key labels: authorino.kuadrant.io/managed-by: authorino app: toystore annotations: secret.kuadrant.io/user-id: bob stringData: api_key: IAMBOB type: Opaque --- apiVersion: v1 kind: Secret metadata: name: alice-key labels: authorino.kuadrant.io/managed-by: authorino app: toystore annotations: secret.kuadrant.io/user-id: alice stringData: api_key: IAMALICE type: Opaque EOF
kubectl apply -f - <<EOF apiVersion: v1 kind: Secret metadata: name: bob-key labels: authorino.kuadrant.io/managed-by: authorino app: toystore annotations: secret.kuadrant.io/user-id: bob stringData: api_key: IAMBOB type: Opaque --- apiVersion: v1 kind: Secret metadata: name: alice-key labels: authorino.kuadrant.io/managed-by: authorino app: toystore annotations: secret.kuadrant.io/user-id: alice stringData: api_key: IAMALICE type: Opaque EOF
Copy to Clipboard Copied! 覆盖
AuthPolicy
以开始接受 API 密钥,如下所示:kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: AuthPolicy metadata: name: toystore spec: targetRef: group: gateway.networking.k8s.io kind: HTTPRoute name: toystore rules: authentication: "api-key-users": apiKey: selector: matchLabels: app: toystore credentials: authorizationHeader: prefix: APIKEY response: success: filters: "identity": json: properties: "userid": selector: auth.identity.metadata.annotations.secret\.kuadrant\.io/user-id EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: AuthPolicy metadata: name: toystore spec: targetRef: group: gateway.networking.k8s.io kind: HTTPRoute name: toystore rules: authentication: "api-key-users": apiKey: selector: matchLabels: app: toystore credentials: authorizationHeader: prefix: APIKEY response: success: filters: "identity": json: properties: "userid": selector: auth.identity.metadata.annotations.secret\.kuadrant\.io/user-id EOF
Copy to Clipboard Copied!
4.4. 第 4 步 - 覆盖网关的 RateLimitPolicy
配置的网关限制为一般情况提供一组很好的限制。但是,作为 Toystore API 的开发人员,您可能只想允许特定用户的请求数量,以及所有其他用户的一般限制。
流程
输入以下命令为特定用户设置速率限制:
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: RateLimitPolicy metadata: name: toystore spec: targetRef: group: gateway.networking.k8s.io kind: HTTPRoute name: toystore limits: "general-user": rates: - limit: 1 duration: 3 unit: second counters: - metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid when: - selector: metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid operator: neq value: bob "bob-limit": rates: - limit: 2 duration: 3 unit: second when: - selector: metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid operator: eq value: bob EOF
kubectl apply -f - <<EOF apiVersion: kuadrant.io/v1 kind: RateLimitPolicy metadata: name: toystore spec: targetRef: group: gateway.networking.k8s.io kind: HTTPRoute name: toystore limits: "general-user": rates: - limit: 1 duration: 3 unit: second counters: - metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid when: - selector: metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid operator: neq value: bob "bob-limit": rates: - limit: 2 duration: 3 unit: second when: - selector: metadata.filter_metadata.envoy\.filters\.http\.ext_authz.identity.userid operator: eq value: bob EOF
Copy to Clipboard Copied! 注意应用
RateLimitPolicy
可能需要几分钟时间,具体取决于您的集群。另外,您可以向 bob 提供与所有其他用户相比要使用的多个请求两次。
要测试您的新设置,请按如下所示 发送请求 :
while :; do curl --resolve api.${rootDomain}:443:${INGRESS_HOST} --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
while :; do curl --resolve api.${rootDomain}:443:${INGRESS_HOST} --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
Copy to Clipboard Copied! 以 bob 身份发送请求,如下所示:
while :; do curl --resolve api.${rootDomain}:443:${INGRESS_HOST} --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMBOB' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
while :; do curl --resolve api.${rootDomain}:443:${INGRESS_HOST} --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMBOB' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
Copy to Clipboard Copied! 注意如果您设置 DNS 供应商并配置了
DNSPolicy
,如平台工程师工作流中所述,您可以省略--resolve api.${rootDomain}:443:${INGRESS_HOST}
标志。例如,对于 alice,如下所示:while :; do curl --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
while :; do curl --write-out '%{http_code}\n' --silent --output /dev/null -H 'Authorization: APIKEY IAMALICE' "https://api.${rootDomain}/cars" | grep -E --color "\b(429)\b|$"; sleep 1; done
Copy to Clipboard Copied! 注意如果您遵循多个集群的本指南,
HTTPRoute
主机名的 DNS 记录将有多个 IP 地址。这意味着,在集群间以轮循模式发出请求,因为您的 DNS 供应商向查找发送不同的响应。
附录 A. 使用您的红帽订阅
红帽连接链接通过软件订阅提供。要管理您的订阅,请通过红帽客户门户网站访问您的帐户。
管理您的订阅
- 转至 access.redhat.com。
- 如果您还没有帐户,请创建一个帐户。
- 登录到您的帐户。
- 在菜单栏中,单击 Subscriptions 以查看和管理您的订阅。
更新于 2025-06-12