第 2 章 将流量进入网格
使用 Istio API,您可以配置使用网关注入安装的网关代理,以接受来自网格外部的流量,并将该流量路由到网格中的服务。
您可以使用 LoadBalancer 类型 Service 或 OpenShift Routes 将网关代理公开给集群外的流量。
2.1. 关于配置使用网关注入安装的网关,以接受入口流量 复制链接链接已复制到粘贴板!
当使用网关注入安装网关时,您可以将其配置为使用 Istio 网关和 VirtualService 资源接收入口流量。Istio 网关资源 描述了在网格边缘运行的负载均衡器,接收传入或传出的 HTTP/TCP 连接。Gateway 规格描述了应公开的一组端口、要使用的协议类型以及负载均衡器的 Server Name Indication (SNI)配置。VirtualServices 定义应用到 Istio 网关的 路由规则,类似于您可以使用 VirtualServices 为内部网格流量定义路由规则。
在以下示例中,Istio 网关资源 将网关代理配置为充当外部流量的入口点。此配置会为主机公开端口 443 (HTTPS )。示例配置适用于带有 istio: ingressgateway 标签的 pod。tls 模式配置为 SIMPLE,它使用示例提供的证书和私钥来终止传入的 HTTPS 流量。
配置示例
apiVersion: networking.istio.io/v1
kind: Gateway
metadata:
name: info-gateway
namespace: info
spec:
selector:
istio: ingressgateway
servers:
- port:
number: 443
name: https-443
protocol: HTTPS
hosts:
- info.com
tls:
mode: SIMPLE
serverCertificate: /etc/certs/servercert.pem
privateKey: /etc/certs/privatekey.pem
以下 VirtualService 绑定到上例配置中显示的 Istio 网关资源。该规范定义将带有 /reviews/ 路径前缀的流量路由到 info 命名空间中的 reviews 服务的规则。VirtualService 明确引用前面显示的 网关资源。这样可确保路由规则仅应用于通过指定网关进入的流量。
配置示例
kind: VirtualService
metadata:
name: info-rule
namespace: info
spec:
hosts:
- info.com
gateways:
- info/bookinfo-gateway
http:
- match:
- uri:
prefix: /reviews/
route:
- destination:
port:
number: 9080
host: reviews.info.svc.cluster.local
2.1.1. 使用 Istio 网关和 VirtualService 资源公开服务 复制链接链接已复制到粘贴板!
此流程使用 Istio 网关和 VirtualService 资源来配置使用网关注入部署的网关。资源配置网关,将网格中的服务公开给网格外的流量。然后,您可以通过将网关设置为类型为 LoadBalancer 的 Service,将网关公开给集群外的流量。
先决条件
- 已使用网关注入安装了 Istio 网关。
流程
运行以下命令,创建名为
httpbin的命名空间:$ oc create namespace httpbin在命名空间中启用 sidecar 注入。如果您使用
InPlace升级策略,请运行以下命令:$ oc label namespace httpbin istio-injection=enabled注意如果使用
基于 Revision 的升级策略,请运行以下命令:要查找您的 &
lt;revision-name>,请运行以下命令:$ oc get istiorevisions.sailoperator.io输出示例:
NAME TYPE READY STATUS IN USE VERSION AGE default Local True Healthy True v1.24.3 3m33s使用修订名称标记命名空间,以启用 sidecar 注入:
$ oc label namespace httpbin istio.io/rev=default
运行以下命令,部署名为
httpbin的示例服务:$ oc apply -n httpbin -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/httpbin/httpbin.yaml创建名为
httpbin-gw.yaml的 YAML 文件,该文件定义了 Istio网关资源。此资源将网关代理配置为为主机公开端口 80 (HTTP),即httpbin.example.com。apiVersion: networking.istio.io/v1 kind: Gateway metadata: name: httpbin-gateway namespace: httpbin spec: selector: istio: <gateway_name>1 servers: - port: number: 80 name: http protocol: HTTP hosts: - httpbin.example.com2 运行以下命令来应用 YAML 文件:
$ oc apply -f httpbin-gw.yaml为
VirtualService创建名为httpbin-vs.yaml的 YAML 文件。VirtualService定义将网关代理的流量路由到httpbin服务的规则。apiVersion: networking.istio.io/v1 kind: VirtualService metadata: name: httpbin namespace: httpbin spec: hosts: - httpbin.example.com1 gateways: - httpbin-gateway2 http: - match: - uri: prefix: /status - uri: prefix: /headers route: - destination:3 port: number: 8000 host: httpbin运行以下命令来应用 YAML 文件:
$ oc apply -f httpbin-vs.yaml为了进行验证,请运行以下命令为
curl客户端创建一个命名空间:$ oc create namespace curl运行以下命令来部署
curl客户端:$ oc apply -n curl -f https://raw.githubusercontent.com/openshift-service-mesh/istio/refs/heads/master/samples/curl/curl.yaml运行以下命令,使用
curlpod 的名称设置CURL_POD变量:$ CURL_POD=$(oc get pods -n curl -l app=curl -o jsonpath='{.items[*].metadata.name}')使用
curl客户端,通过入口网关服务资源向httpbin应用程序的/headers端点发送请求。将请求的Host标头设置为httpbin.example.com,以匹配 Istio网关和VirtualService资源指定的主机。运行以下curl命令来发送请求:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>.<gateway_namespace>.svc.cluster.local/headers响应应具有
200 OK HTTP状态,表示请求成功。输出示例
HTTP/1.1 200 OK server: istio-envoy ...运行以下命令,将 curl 请求发送到在
httpbinVirtualService中定义的对应 URI 前缀匹配的端点:$ oc exec $CURL_POD -n curl -- \ curl -s -I \ -H Host:httpbin.example.com \ <gateway_name>.<gateway_namespace>.svc.cluster.local/get响应应返回
404 Not Found状态。这是正常的,因为/get端点在httpbinVirtualService资源中没有匹配的 URI 前缀。输出示例
HTTP/1.1 404 Not Found server: istio-envoy ...通过将
Service类型设置为LoadBalancer,将网关代理公开给集群外的流量:$ oc patch service <gateway_name> -n <gateway_namespace> -p '{"spec": {"type": "LoadBalancer"}}'注意网关也可以使用 OpenShift 路由公开给集群外部的流量。如需更多信息,请参阅"使用 OpenShift 路由向集群外的流量扩展网关"。
使用网关服务资源的外部主机名或 IP 地址,验证可以从集群外部访问
httpbin服务。确保为集群在其中运行的环境正确设置了INGRESS_HOST变量。如果集群在 AWS 上运行,请运行以下命令设置
INGRESS_HOST变量:$ INGRESS_HOST=$(oc get service <gateway_name> -n <gateway_namespace> -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')如果集群在 GCP 或 Azure 上运行,请运行以下命令设置
INGRESS_HOST变量:$ INGRESS_HOST=$(oc get service <gateway_name> -n <gateway_namespace> -o jsonpath='{.status.loadBalancer.ingress[0].ip}')运行以下命令,使用网关的主机向
httpbin服务发送curl请求:$ curl -s -I -H Host:httpbin.example.com http://$INGRESS_HOST/headers
-
验证响应是否具有
HTTP/1.1 200 OK状态,这表示请求是否成功。