2.6. 在 ambient 模式中使用 Kubernetes Gateway API 来公开服务
您可以使用 Kubernetes 网关 API 创建 网关和 HTTPRoute 资源,并在 ambient 模式中部署网关。资源配置网关,将网格中的服务公开给网格外的流量。
先决条件
-
以具有
cluster-admin角色的用户身份登录到 OpenShift Container Platform Web 控制台。 - 已安装 Red Hat OpenShift Service Mesh Operator。
-
您已部署了
Istio资源。 - 您可以使用 Kubernetes 原生网关 API 资源。
- 您是使用 Istio 辅助模式,或计划迁移到 ambient 模式。
当使用 ambient 模式(istio.io/dataplane-mode=ambient)时,建议使用 Kubernetes 网关 API 进行入口配置,因为 Istio 网关和 VirtualService 资源与 ambient 模式不完全兼容。
流程
运行以下命令,创建一个名为
httpbin的命名空间:oc create namespace httpbin
$ oc create namespace httpbinCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,为 ambient 模式应用标签:
oc label namespace httpbin istio.io/dataplane-mode=ambient
$ oc label namespace httpbin istio.io/dataplane-mode=ambientCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,部署名为
httpbin的示例服务:oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yaml
$ oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 通过创建名为
httpbin-waypoint.yaml的 YAML 文件来部署 waypoint 代理,如下例所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令来应用 YAML 文件:
oc apply -f httpbin-waypoint.yaml
$ oc apply -f httpbin-waypoint.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,在
httpbin服务中启用 ingress waypoint 路由:oc label service httpbin -n httpbin istio.io/ingress-use-waypoint=true
$ oc label service httpbin -n httpbin istio.io/ingress-use-waypoint=trueCopy to Clipboard Copied! Toggle word wrap Toggle overflow 该标签确保 ingress 网关路由的流量通过 waypoint 代理和第 7 层(L7)策略在 waypoint 代理上配置到入口流量,然后再到达
httpbin服务。运行以下命令,将 waypoint 标签应用到命名空间,以便命名空间路由中的所有服务都通过 waypoint 进行路由:
oc label ns httpbin istio.io/use-waypoint=httpbin-waypoint
$ oc label ns httpbin istio.io/use-waypoint=httpbin-waypointCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建名为
httpbin-gw.yaml的 YAML 文件,用于定义 Kubernetes 网关资源。此资源将网关代理配置为为主机公开端口 80 (HTTP),即httpbin.example.com。Copy to Clipboard Copied! Toggle word wrap Toggle overflow "httpbin.example.com"- 指定客户端在尝试访问关联端口上网格服务时使用的虚拟主机名。
运行以下命令来应用 YAML 文件:
oc apply -f httpbin-gw.yaml
$ oc apply -f httpbin-gw.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建名为
httpbin-ingress-hr.yaml的 YAML 文件,该文件为 ingress 网关定义HTTPRoute资源,如下例所示:Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
spec.parentRefs将HTTPROUTE资源绑定到上一步中创建的 Kubernetes 网关资源。 -
spec.rules.通过定义包括backendRefshttpbin服务的 name 和 port 来将匹配的流量路由到httpbin服务。
HTTPRoute资源指定将网关代理的流量路由到httpbin服务的规则。-
运行以下命令来应用 YAML 文件:
oc apply -f httpbin-ingress-hr.yaml
$ oc apply -f httpbin-ingress-hr.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 创建名为
httpbin-waypoint-hr.yaml的 YAML 文件,该文件为 waypoint 代理定义HTTPRoute资源。Copy to Clipboard Copied! Toggle word wrap Toggle overflow -
spec.parentRefs将HTTPRoute资源绑定到httpbin服务。当与服务上的istio.io/ingress-use-waypoint=true标签结合使用时,HTTPRoute配置 L7 路由规则,其中point 代理针对目标到该服务的流量强制执行。 -
spec.rules.通过定义包括backendRefshttpbin服务的 name 和 port 来将匹配的流量路由到httpbin服务。
-
运行以下命令来应用 YAML 文件:
oc apply -f httpbin-waypoint-hr.yaml
$ oc apply -f httpbin-waypoint-hr.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 注意在本例中,因为
istio.io/ingress-use-waypoint=true标签,来自 ingress 网关的流量通过 waypoint 代理流。HTTPRoute资源随后在流量到达 httpbin 服务前应用基于路径的路由策略。运行以下命令,确保 waypoint 代理已就绪:
oc wait --for=condition=programmed gtw httpbin-waypoint -n httpbin
$ oc wait --for=condition=programmed gtw httpbin-waypoint -n httpbinCopy to Clipboard Copied! Toggle word wrap Toggle overflow
验证
运行以下命令,为
curl客户端创建命名空间:oc create namespace curl
$ oc create namespace curlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令来部署
curl客户端:oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yaml
$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,将 ambient 模式的标签应用到
curl命名空间:oc label namespace curl istio.io/dataplane-mode=ambient
$ oc label namespace curl istio.io/dataplane-mode=ambientCopy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,使用
curlpod 的名称设置CURL_POD变量:CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')$ CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow 使用
curl客户端,通过入口网关服务资源向httpbin应用程序的/headers端点发送请求。将请求的 Host 标头设置为httpbin.example.com,以匹配 Kubernetes 网关和HTTPROUTE资源指定的主机。运行以下命令来发送curl请求:oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ httpbin-gateway-istio.httpbin.svc.cluster.local/headers$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ httpbin-gateway-istio.httpbin.svc.cluster.local/headersCopy to Clipboard Copied! Toggle word wrap Toggle overflow 响应应返回
200 OKHTTP 状态,这表示请求成功,如下例所示:HTTP/1.1 200 OK server: istio-envoy ...
HTTP/1.1 200 OK server: istio-envoy ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令,向没有相应 Uniform Resource Identifier (URI)前缀匹配
的端点发送curl请求:oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ httpbin-gateway-istio.httpbin.svc.cluster.local/get$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ httpbin-gateway-istio.httpbin.svc.cluster.local/getCopy to Clipboard Copied! Toggle word wrap Toggle overflow 响应返回
404 Not Found状态,因为/get端点在httpbinHTTPROUTE资源中没有匹配的 URI 前缀,如下例所示:HTTP/1.1 404 Not Found server: istio-envoy ...
HTTP/1.1 404 Not Found server: istio-envoy ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow 通过将
Service类型设置为LoadBalancer,将网关代理公开给集群外的流量。运行以下命令:oc patch service httpbin-gateway-istio -n httpbin -p '{"spec": {"type": "LoadBalancer"}}'$ oc patch service httpbin-gateway-istio -n httpbin -p '{"spec": {"type": "LoadBalancer"}}'Copy to Clipboard Copied! Toggle word wrap Toggle overflow 注意网关也可以使用 OpenShift 路由公开给集群外部的流量。如需更多信息,请参阅"使用 OpenShift 路由向集群外的流量扩展网关"。
使用网关服务资源的外部主机名或 IP 地址,验证可以从集群外部访问
httpbin服务。确保为运行集群的环境正确设置了INGRESS_HOST变量。运行以下命令设置
INGRESS_HOST变量:export INGRESS_HOST=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.status.addresses[0].value}')$ export INGRESS_HOST=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.status.addresses[0].value}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow 运行以下命令设置
INGRESS_PORT变量:INGRESS_PORT=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.spec.listeners[?(@.name=="http")].port}')$ INGRESS_PORT=$(oc get gtw httpbin-gateway -n httpbin -o jsonpath='{.spec.listeners[?(@.name=="http")].port}')Copy to Clipboard Copied! Toggle word wrap Toggle overflow 使用网关主机,运行以下命令来向
httpbin服务发送curl请求:curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headers
$ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headersCopy to Clipboard Copied! Toggle word wrap Toggle overflow
-
验证响应是否具有
HTTP/1.1 200 OK状态,这表示请求是否成功。