istio入门
# Istio 是什么?
# 一、istio是什么
Istio 是一种开源服务网格,可透明地分层到现有的分布式应用程序上。 Istio 的强大功能提供了一种统一且更高效的方式来保护、连接和监控服务。 Istio 是实现负载均衡、服务到服务身份验证和监控的途径 - 几乎无需更改服务代码。它为您提供:
- 使用双向 TLS 加密、强大的基于身份的身份验证和鉴权在集群中保护服务到服务通信
- HTTP、gRPC、WebSocket 和 TCP 流量的自动负载均衡
- 使用丰富的路由规则、重试、故障转移和故障注入对流量行为进行细粒度控制
- 支持访问控制、限流和配额的可插入策略层和配置 API
- 集群内所有流量(包括集群入口和出口)的自动指标、日志和链路追踪
# 下载 Istio CLI
Istio 使用名为 istioctl 的命令行工具进行配置。下载该工具以及 Istio 示例应用程序:
$ curl -L https://istio.io/downloadIstio | sh -
$ cd istio-1.29.2
$ export PATH=$PWD/bin:$PATH
2
3
通过打印版本的命令来检查您是否能够运行 istioctl。 此时,Istio 尚未安装在您的集群中,因此您将看到没有 Pod 就绪。
$ istioctl version
Istio is not present in the cluster: no running Istio pods in namespace "istio-system"
client version: 1.29.2
2
3
# 将 Istio 安装到你的集群上
istioctl 支持多种配置文件 (opens new window), 其中包含不同的默认选项,并可根据您的生产需求进行自定义。 ambient 配置文件中包含对 Ambient 模式的支持。使用以下命令安装 Istio:
$ istioctl install --set profile=ambient --skip-confirmation
安装完成后,您将看到以下输出,表明所有组件已成功安装。
✔ Istio core installed
✔ Istiod installed
✔ CNI installed
✔ Ztunnel installed
✔ Installation complete
2
3
4
5
# 安装 Kubernetes Gateway API CRD
您将使用 Kubernetes Gateway API 来配置流量路由。
请注意,Kubernetes Gateway API CRD 不会默认安装在大多数 Kubernetes 集群上, 因此请确保在使用 Gateway API 之前已安装好这些 CRD:
$ kubectl get crd gateways.gateway.networking.k8s.io &> /dev/null || \
kubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.4.0/standard-install.yaml
2
# 二、部署应用示例
# 部署 Bookinfo 应用程序
首先部署应用程序:
$ kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml
$ kubectl apply -f samples/bookinfo/platform/kube/bookinfo-versions.yaml
2
要验证应用程序是否正在运行,请检查 Pod 的状态:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
details-v1-cf74bb974-nw94k 1/1 Running 0 42s
productpage-v1-87d54dd59-wl7qf 1/1 Running 0 42s
ratings-v1-7c4bbf97db-rwkw5 1/1 Running 0 42s
reviews-v1-5fd6d4f8f8-66j45 1/1 Running 0 42s
reviews-v2-6f9b55c5db-6ts96 1/1 Running 0 42s
reviews-v3-7d99fd7978-dm6mx 1/1 Running 0 42s
2
3
4
5
6
7
8
要从集群外部访问 productpage 服务,需要配置入口网关。
# 部署并配置入口网关
您将使用 Kubernetes Gateway API 部署一个名为 bookinfo-gateway 的网关:
$ kubectl apply -f samples/bookinfo/gateway-api/bookinfo-gateway.yaml
在默认情况下,Istio 会为网关创建一个 LoadBalancer 服务。 由于您将通过隧道访问此网关,因此不需要负载均衡器。 通过注解将网关的服务类型更改为 ClusterIP:
$ kubectl annotate gateway bookinfo-gateway networking.istio.io/service-type=ClusterIP --namespace=default
要检查网关的状态,请运行:
$ kubectl get gateway
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.default.svc.cluster.local True 42s
2
3
等待网关按照程序显示后再继续。
# 访问应用程序
您将通过刚刚配置的网关连接到 Bookinfo productpage 服务。 要访问网关,您需要使用 kubectl port-forward 命令:
$ kubectl port-forward svc/bookinfo-gateway-istio 8080:80
打开浏览器并导航到 http://localhost:8080/productpage 以查看 Bookinfo 应用程序。
# 三、概念理解
Kubernetes Gateway API 是 Ingress 的进化版,旨在提供更强的表达能力、跨角色协作能力以及对 L4(TCP/UDP)等协议的原生支持。
要使用 Gateway API,你通常需要安装一个支持它的控制器(如 Istio, NGINX Gateway Fabric, Traefik, 或 Envoy Gateway)。
以下是一个完整的入口流量配置范例,分为三个核心步骤:
# 1. 定义基础设施 (GatewayClass)
通常由平台管理员配置。它定义了具体由哪个控制器(Controller)来处理流量。
YAML
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: my-gateway-class
spec:
controllerName: example.com/gateway-controller # 对应你安装的控制器名称
2
3
4
5
6
# 2. 定义入口点 (Gateway)
这相当于部署了一个“负载均衡器”实例,监听特定的端口和协议。
YAML
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: prod-gateway
namespace: infrastructure
spec:
gatewayClassName: my-gateway-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: All # 允许任何命名空间的 Route 挂载到此网关
2
3
4
5
6
7
8
9
10
11
12
13
14
# 3. 定义路由规则 (HTTPRoute)
由应用开发人员配置,将具体的域名和路径指向后端服务。
YAML
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: app-route
namespace: app-namespace
spec:
parentRefs:
- name: prod-gateway
namespace: infrastructure
hostnames:
- "example.com"
rules:
- matches:
- path:
type: PathPrefix
value: /api
backendRefs:
- name: api-service
port: 8080
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 💡 进阶:Gateway API 的核心优势
# 1. 流量拆分(灰度发布)
不再需要复杂的 Annotation,原生支持权重配置:
YAML
rules:
- backendRefs:
- name: svc-v1
port: 80
weight: 90
- name: svc-v2
port: 80
weight: 10
2
3
4
5
6
7
8
# 2. 基于请求头的路由
可以轻松实现 A/B 测试:
YAML
matches:
- headers:
- name: x-user-type
value: premium
2
3
4
# 3. 跨角色解耦
这是 Gateway API 设计的最巧妙之处。通过 parentRefs,运维人员管理的 Gateway 和开发人员管理的 HTTPRoute 可以分开存放。即便网关配置在 infrastructure 命名空间,应用代码依然可以安全地把自己“挂载”上去。
快速检查:
在尝试部署上述 YAML 之前,请确保你的集群已经安装了 Gateway API CRDs:
Bash
kubectl get crd | grep gateway.networking.k8s.io
如果没有结果,你需要先根据你所使用的控制器文档安装对应的 CRD 包。
# 四、示例
假设你的集群已经有一个名为 internet-gw 的网关在运行了。
第一步:部署你的新应用 (Deployment & Service)
YAML
# 常规的服务部署
apiVersion: v1
kind: Service
metadata:
name: my-new-app
spec:
selector:
app: my-new-app
ports:
- port: 80
2
3
4
5
6
7
8
9
10
第二步:只需要写一个 Route (HTTPRoute) 你不需要管 IP 是多少,也不管证书怎么配,直接关联已有的网关:
YAML
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: my-new-app-route
spec:
parentRefs:
- name: internet-gw # 引用已经存在的网关
namespace: infrastructure
rules:
- matches:
- path: { type: PathPrefix, value: /my-app }
backendRefs:
- name: my-new-app
port: 80
2
3
4
5
6
7
8
9
10
11
12
13
14
# 总结
你不需要重复定义基础设施。
- Gateway 是“路”。
- HTTPRoute 是“路标”。 你每开一家新店(新系统),只需要在现有的路上挂一个新的路标(HTTPRoute)指引流量即可。
|