sync traffic-management/request-routing (#12793)

Co-authored-by: zhuzhenghao <zhenghao.zhu@daocloud.io>
This commit is contained in:
Istio Automation 2023-02-26 19:02:14 -08:00 committed by GitHub
parent 59f025d990
commit b80f7f49ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 245 additions and 98 deletions

View File

@ -11,6 +11,8 @@ test: yes
此任务将展示如何将请求动态路由到微服务的多个版本。
{{< boilerplate gateway-api-gamma-support >}}
## 开始之前{#before-you-begin}
* 按照[安装指南](/zh/docs/setup/)中的说明安装 Istio。
@ -21,91 +23,183 @@ test: yes
## 关于这个任务{#about-this-task}
Istio [Bookinfo](/zh/docs/examples/bookinfo/) 示例包含四个独立的微服务,每个微服务都有多个版本。其中一个微服务 `reviews` 的三个不同版本已经部署并同时运行。为了说明这导致的问题,在浏览器中访问 Bookinfo 应用程序的 `/productpage` 并刷新几次。您会注意到有时书评的输出包含星级评分有时则不包含。这是因为没有明确的默认服务版本可路由Istio 将以循环方式将请求路由到所有可用版本。
Istio [Bookinfo](/zh/docs/examples/bookinfo/) 示例包含四个独立的微服务,每个微服务都有多个版本。其中一个微服务 `reviews` 的三个不同版本已经部署并同时运行。为了说明这导致的问题,在浏览器中访问 Bookinfo 应用程序的 `/productpage` 并刷新几次。
URL 是 `http://$GATEWAY_URL/productpage`
`$GATEWAY_URL` 是 Ingress 的外部访问 IP 地址,正如在 [Bookinfo](/zh/docs/examples/bookinfo/#determine-the-ingress-ip-and-port) 文档中所解释的那样。
您会注意到有时书评的输出包含星级评分有时则不包含。这是因为没有明确的默认服务版本可路由Istio 将以循环方式将请求路由到所有可用版本。
此任务的最初目标是应用将所有流量路由到微服务的 `v1` (版本 1的规则。稍后您将应用规则根据 HTTP 请求 header 的值路由流量。
## 应用 Virtual Service{#apply-a-virtual-service}
## 路由到版本 1{#route-to-version-1}
要仅路由到一个版本,请应用为微服务设置默认版本的 Virtual Service。在这种情况下Virtual Service 将所有流量路由到每个微服务的 `v1` 版本。
要仅路由到一个版本,请应用为微服务设置默认版本的 Virtual Service。
{{< warning >}}
如果您还没有应用 Destination Rule请先[应用默认目标规则](/zh/docs/examples/bookinfo/#apply-default-destination-rules)
如果尚未定义服务版本, 请按照[定义服务版本](/zh/docs/examples/bookinfo/#define-the-service-versions)中的说明进行操作
{{< /warning >}}
1. 运行以下命令以应用 Virtual Service
1. 运行以下命令以创建路由规则:
{{< tabset category-name="config-api" >}}
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/networking/virtual-service-all-v1.yaml@
{{< /text >}}
{{< tab name="Istio classic" category-value="istio-classic" >}}
由于配置传播是最终一致的,因此请等待几秒钟以使 Virtual Service 生效。
Istio 使用 Virtual Service 来定义路由规则。
运行以下命令以应用 Virtual Service
在这种情况下Virtual Service 将所有流量路由到每个微服务的 `v1` 版本。
1. 使用以下命令显示已定义的路由:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/networking/virtual-service-all-v1.yaml@
{{< /text >}}
{{< text bash yaml >}}
$ kubectl get virtualservices -o yaml
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
{{< /text >}}
由于配置传播是最终一致的,因此请等待几秒钟以使 Virtual Service 生效。
1. 您还可以使用以下命令显示相应的 `subset` 定义:
{{< /tab >}}
{{< text bash >}}
$ kubectl get destinationrules -o yaml
{{< /text >}}
{{< tab name="Gateway API" category-value="gateway-api" >}}
{{< text bash >}}
$ kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
name: reviews
spec:
parentRefs:
- kind: Service
name: reviews
port: 9080
rules:
- backendRefs:
- name: reviews-v1
port: 9080
EOF
{{< /text >}}
{{< /tab >}}
{{< /tabset >}}
2) 使用以下命令显示已定义的路由:
{{< tabset category-name="config-api" >}}
{{< tab name="Istio classic" category-value="istio-classic" >}}
{{< text bash yaml >}}
$ kubectl get virtualservices -o yaml
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- details
http:
- route:
- destination:
host: details
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- productpage
http:
- route:
- destination:
host: productpage
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- ratings
http:
- route:
- destination:
host: ratings
subset: v1
- apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- reviews
http:
- route:
- destination:
host: reviews
subset: v1
{{< /text >}}
您还可以使用以下命令显示相应的 `subset` 定义:
{{< text bash >}}
$ kubectl get destinationrules -o yaml
{{< /text >}}
{{< /tab >}}
{{< tab name="Gateway API" category-value="gateway-api" >}}
{{< text bash >}}
$ kubectl get httproute reviews -o yaml
...
spec:
parentRefs:
- group: gateway.networking.k8s.io
kind: Service
name: reviews
port: 9080
rules:
- backendRefs:
- group: ""
kind: Service
name: reviews-v1
port: 9080
weight: 1
matches:
- path:
type: PathPrefix
value: /
status:
parents:
- conditions:
- lastTransitionTime: "2022-11-08T19:56:19Z"
message: Route was valid
observedGeneration: 8
reason: Accepted
status: "True"
type: Accepted
- lastTransitionTime: "2022-11-08T19:56:19Z"
message: All references resolved
observedGeneration: 8
reason: ResolvedRefs
status: "True"
type: ResolvedRefs
controllerName: istio.io/gateway-controller
parentRef:
group: gateway.networking.k8s.io
kind: Service
name: reviews
port: 9080
{{< /text >}}
在资源状态中,确保 `reviews` 父级的 `Accepted` 条件为 `True`
{{< /tab >}}
{{< /tabset >}}
您已将 Istio 配置为路由到 Bookinfo 微服务的 `v1` 版本,最重要的是 `reviews` 服务的版本 1。
## 测试新的路由配置{#test-the-new-routing-configuration}
您可以通过再次刷新 Bookinfo 应用程序的 `/productpage` 轻松测试新配置。
1. 在浏览器中打开 Bookinfo 站点。网址为 `http://$GATEWAY_URL/productpage`,其中 `$GATEWAY_URL` 是外部的入口 IP 地址,如 [Bookinfo](/zh/docs/examples/bookinfo/#determine-the-ingress-IP-and-port) 文档中所述。
请注意,无论您刷新多少次,页面的评论部分都不会显示评级星标。这是因为您将 Istio 配置为将评论服务的所有流量路由到版本 `reviews:v1`,而此版本的服务不访问星级评分服务。
在浏览器中打开 Bookinfo 站点。网址为 `http://$GATEWAY_URL/productpage`,其中 `$GATEWAY_URL` 是外部的入口 IP 地址,如 [Bookinfo](/zh/docs/examples/bookinfo/#determine-the-ingress-IP-and-port) 文档中所述。
请注意,无论您刷新多少次,页面的评论部分都不会显示评级星标。这是因为您将 Istio 配置为将评论服务的所有流量路由到版本 `reviews:v1`,而此版本的服务不访问星级评分服务。
您已成功完成此任务的第一部分:将流量路由到服务的某一个版本。
@ -121,40 +215,77 @@ Istio 还支持在入口网关上基于强认证 JWT 的路由,参考 [JWT 基
1. 运行以下命令以启用基于用户的路由:
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml@
{{< /text >}}
{{< tabset category-name="config-api" >}}
1. 确认规则已创建:
{{< tab name="Istio classic" category-value="istio-classic" >}}
{{< text bash yaml >}}
$ kubectl get virtualservice reviews -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
{{< /text >}}
{{< text bash >}}
$ kubectl apply -f @samples/bookinfo/networking/virtual-service-reviews-test-v2.yaml@
{{< /text >}}
1. 在 Bookinfo 应用程序的 `/productpage` 上,以用户 `jason` 身份登录。
您可以使用以下命令确认规则已创建:
{{< text bash yaml >}}
$ kubectl get virtualservice reviews -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
...
spec:
hosts:
- reviews
http:
- match:
- headers:
end-user:
exact: jason
route:
- destination:
host: reviews
subset: v2
- route:
- destination:
host: reviews
subset: v1
{{< /text >}}
{{< /tab >}}
{{< tab name="Gateway API" category-value="gateway-api" >}}
{{< text bash >}}
$ kubectl apply -f - <<EOF
apiVersion: gateway.networking.k8s.io/v1beta1
kind: HTTPRoute
metadata:
name: reviews
spec:
parentRefs:
- kind: Service
name: reviews
port: 9080
rules:
- matches:
- headers:
- name: end-user
value: jason
backendRefs:
- name: reviews-v2
port: 9080
- backendRefs:
- name: reviews-v1
port: 9080
EOF
{{< /text >}}
{{< /tab >}}
{{< /tabset >}}
2) 在 Bookinfo 应用程序的 `/productpage` 上,以用户 `jason` 身份登录。
刷新浏览器。您看到了什么?星级评分显示在每个评论旁边。
1. 以其他用户身份登录(选择您想要的任何名称)。
3) 以其他用户身份登录(选择您想要的任何名称)。
刷新浏览器。现在星星消失了。这是因为除了 Jason 之外,所有用户的流量都被路由到 `reviews:v1`
@ -170,10 +301,26 @@ Istio 还支持在入口网关上基于强认证 JWT 的路由,参考 [JWT 基
## 清除{#cleanup}
1. 删除应用程序的 Virtual Service
1. 删除应用程序的路由规则
{{< text bash >}}
$ kubectl delete -f @samples/bookinfo/networking/virtual-service-all-v1.yaml@
{{< /text >}}
{{< tabset category-name="config-api" >}}
1. 如果您不打算探索任何后续任务,请参阅 [Bookinfo 清理](/zh/docs/examples/bookinfo/#cleanup)的说明关闭应用程序。
{{< tab name="Istio classic" category-value="istio-classic" >}}
{{< text bash >}}
$ kubectl delete -f @samples/bookinfo/networking/virtual-service-all-v1.yaml@
{{< /text >}}
{{< /tab >}}
{{< tab name="Gateway API" category-value="gateway-api" >}}
{{< text bash >}}
$ kubectl delete httproute reviews
{{< /text >}}
{{< /tab >}}
{{< /tabset >}}
2) 如果您不打算探索任何后续任务,请参阅 [Bookinfo 清理](/zh/docs/examples/bookinfo/#cleanup)的说明关闭应用程序。