Files
Kubernetes/网关/Istio/官方Istio使用示例.md
offends b44594def6
All checks were successful
continuous-integration/drone Build is passing
修改和新增
2025-12-23 01:53:01 +08:00

146 lines
4.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

> 本文作者:丁辉
# 官方Istio使用示例
[Bookinfo 应用](https://istio.io/latest/zh/docs/examples/bookinfo/)
## 前提条件
如果您还没有开始,请遵循 [Istio安装和使用](https://gitee.com/offends/Kubernetes/blob/main/%E7%BD%91%E5%85%B3/Istio/Istio%E5%AE%89%E8%A3%85%E5%92%8C%E4%BD%BF%E7%94%A8.md) 完成 Istio 的部署工作。
本文使用 Istio 官方推荐的 Gateway API 部署举例。
## 部署 Bookinfo
<img src="https://istio.io/latest/zh/docs/examples/bookinfo/withistio.svg" style="zoom:100%;" />
1. 进入示例目录
```bash
cd istio-1.28.1
```
2. 部署应用
```bash
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
```
3. 要确认 Bookinfo 应用正在运行,请从某个 Pod 中(例如从 `ratings` 中)用 `curl` 命令对此应用发送一条请求
```bash
kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}')" -c ratings -- curl -sS productpage:9080/productpage | grep -o "<title>.*</title>"
```
结果输出 `<title>Simple Bookstore App</title>` 即代表运行成功。
4. 对外开放应用
```bash
kubectl apply -f samples/bookinfo/gateway-api/bookinfo-gateway.yaml
```
5. 通过注解网关将服务类型更改为 `NodePort`
```bash
kubectl annotate gateway bookinfo-gateway networking.istio.io/service-type=NodePort --namespace=default
```
6. 检查网关的状态
```bash
kubectl get gateway
```
7. 查看 NodePort 对外端口
```bash
kubectl get svc bookinfo-gateway-istio
```
8. 浏览器访问
```bash
http://$ip:30000/productpage
```
刷新页面,应该会看到书评和评分发生变化, 因为请求分布在 `reviews` 服务的不同版本上
## 使用 Kiali 仪表板
1. 安装 Kiali 仪表板
```bash
kubectl apply -f samples/addons/prometheus.yaml
kubectl apply -f samples/addons/kiali.yaml
```
2. 修改对外访问模式为 NodePort
```bash
kubectl patch svc kiali -n istio-system -p '{"spec":{"type":"NodePort"}}'
```
3. 查看 NodePort 对外端口
```bash
kubectl get svc kiali -n istio-system
```
4. 浏览器访问
```bash
http://$ip:30001/kiali
```
5. 刷新页面并查看拓扑图
## 使用 Istio 实现灰度发布
[官方文档](https://istio.io/latest/zh/docs/tasks/traffic-management/traffic-shifting/#apply-weight-based-routing)
1. 定义服务可用版本
```bash
kubectl apply -f samples/bookinfo/platform/kube/bookinfo-versions.yaml
```
2. 首先,运行此命令将所有流量路由到各个微服务的 `v1` 版本
```bash
kubectl apply -f samples/bookinfo/gateway-api/route-reviews-v1.yaml
```
3. 在浏览器中打开 Bookinfo 站点。网址为 `http://$GATEWAY_URL/productpage` 其中 `$GATEWAY_URL` 是 Ingress 的外部 IP 地址,其描述参见 [Bookinfo](https://istio.io/latest/zh/docs/examples/bookinfo/#determine-the-ingress-IP-and-port) 文档。
请注意,不管刷新多少次,页面的评论部分都不会显示评价星级的内容。 这是因为 Istio 被配置为将星级评价的服务的所有流量都路由到了 `reviews:v1` 版本,而该版本的服务不访问带评价星级的服务。
4. 使用下面的命令把 50% 的流量从 `reviews:v1` 转移到 `reviews:v3`
```bash
kubectl apply -f samples/bookinfo/gateway-api/route-reviews-50-v3.yaml
```
5. 等待几秒钟,等待新的规则传播到代理中生效,确认规则已被替换。刷新浏览器中的 `/productpage` 页面,大约有 50% 的几率会看到页面中带**红色**星级的评价内容。 这是因为 `reviews` 的 `v3` 版本可以访问带星级评价,但 `v1` 版本不能
6. 如果您认为 `reviews:v3` 微服务已经稳定,您可以通过应用 Virtual Service 规则将 100% 的流量路由 `reviews:v3`
```bash
kubectl apply -f samples/bookinfo/gateway-api/route-reviews-v3.yaml
```
7. 现在,当您刷新 `/productpage` 时,您将始终看到带有**红色**星级评分的书评
8. 清理路由规则
```bash
kubectl delete httproute reviews
```
## 清理示例环境
```bash
samples/bookinfo/platform/kube/cleanup.sh
```