一、Istio基础学习
因语雀和CSDN MarkDown格式有区别,导入到CSDN时可能会有格式显示问题,请查看原文链接:
https://www.yuque.com/dycloud/pss8ys
一、Service Mesh 介绍
1.1 Service Mesh 介绍
ServiceMesh 是将服务治理能力下沉到基础设施中,并将他们部署为服务消费者和服务提供者的独立流程。
- 每个服务都是用一个专用的代理
<font style="color:#DF2A3F;">Sidecar</font>
来完成高级网格功能 - 每个服务之间仅通过
<font style="color:#DF2A3F;">Sidecar</font>
代理容器互相通信。 - 每个代理之间形成了一个网状网络,并称之为
<font style="color:#DF2A3F;">ServiceMesh</font>
。
ServiceMesh 的核心理念:
非侵入式<font style="color:#DF2A3F;">Sidecar</font>
注入技术,将数据面组件注入到应用所在的容器,通过劫持应用流量来进行功能实现,应用无感知
- 北向API基于K8s CRD实现,完全声明式,标准化
- 数据面与控制面通过xDS gRPC标准化协议通信,支持订阅模式
ServiceMesh 核心特性:
- 服务&流量治理:熔断,故障注入,丰富的负载均衡算法,限流,健康检查,灰度发布,蓝绿部署等
- 流量与访问可视化:提供应用级别的监控,分布式调用链,访问日志等
- 安全连接:通过mTLS、认证、鉴权等安全措施帮助企业在零信任的网络中运行应用
1.2 新一代 ServiceMesh
为 ServiceMesh 中每个独立工作的代理提供集中式的"控制平面":
- 实际的服务流量任然直接在每个代理容器之间完成,但是控制平面会控制每个代理实例。
- 控制平面是代理能够实现如访问控制和指标收集之类的事情。
1.3 Kubernetes 和 ServiceMesh
- 除强大的服务编排能力之外,Kubernetes还基于Service提供了基础的服务互访能力,而轻量化运行的Pod,再加上服务注册、服务发现、负载均衡以及健康状态检测,天然就适合编排支持微服务
- 但Kubernetes提供的这种能力大多停留在四层的互访上,无法实现****微服务依赖的熔断、限流、动态路由或调用链埋点等应用层的能力,因而仍然不得不借助于微服务框架来完成
- 但微服务框架存在侵入性强和语言锁定等方面的问题,而且会架空Kubernetes的Service机制,而Service Mesh就是解决这些侵入性问题的最好方式
- 在Kubernetes平台上,最好的方式就是基于Kubernetes的服务注册和服务发现机制提供一个Kubernetes平台原生的
<font style="color:#DF2A3F;">ServiceMesh</font>
解决方案 - 复用了Kubernetes的编排能力,同时提供微服务治理所需的可观测性、 服务韧性、灵活分流、弹性和安全等能力
<font style="color:#DF2A3F;">Istio</font>
就是Kubernetes平台原生的ServiceMesh的实现之一 。
- 在Kubernetes平台上,最好的方式就是基于Kubernetes的服务注册和服务发现机制提供一个Kubernetes平台原生的
- 复用了Kubernetes Service的定义,并将其转换为Istio的服务模型
- 数据平面组件从
<font style="color:#DF2A3F;">kube-proxy</font>
转为各服务实例的Sidecar
二、Istio 基础学习
2.1 Istio 是什么
Istio 是一个开源服务网格,可透明地分层到现有的分布式应用程序上。Istio 的强大功能提供了一种统一且更有效的方式来保护、连接和监控服务。Istio 是实现负载平衡、服务到服务身份验证和监控的途径——几乎不需要或根本没有服务代码更改。它提供:
- 通过相互 TLS 加密、基于身份的强身份验证和授权,在集群中实现安全的服务到服务通信
- HTTP、gRPC、WebSocket 和 TCP 流量的自动负载均衡
- 通过丰富的路由规则、重试、故障转移和故障注入对流量行为进行细粒度控制
- 支持访问控制、速率限制和配额的可插拔策略层和配置 API
- 集群内所有流量(包括集群入口和出口)的自动指标、日志和跟踪
Istio 专为可扩展性而设计,可以处理各种部署需求。Istio 的控制平面在 Kubernetes 上运行,可以将部署在该集群中的应用程序添加到您的网格中,将网格扩展到其他集群,** 甚至连接在 Kubernetes 之外运行**的 VM 或其他端点。
2.2 Istio 主要功能
Istio 的绝大多数核心功能(如流量管理、路由分发、安全加密、监控慢点等数据平面的能力)其实都是基于 Envoy 实现的,所以前面才会详细的学习 Envoy 功能。
2.2.1 流量管理(Traffic Management)
- 协议支持(L7 层)
- 支持 HTTP、gRPC 等第七层协议,可以基于应用层协议精细控制流量,而不仅仅是四层的 IP 和端口。
- 动态路由(Dynamic Routing)
- 条件路由(Conditional Routing):根据HTTP头、请求路径、用户、版本等进行流量分流,把指定用户流量导向特定服务版本。
- 权重分流:按比例(如 70% 到v1、30% 到v2)进行灰度、金丝雀发布等流量分配。
- 流量镜像(Mirroring):实时复制生产流量到新版本服务测试但不影响真实业务,仅用于观测新版本行为。
- 弹性特征(Resiliency)
- 超时(Timeouts):自动设定请求超时,防止请求悬挂。
- 重试(Retries):请求失败后自动重试,提升瞬时故障应对能力。
- 熔断(Circuit Breakers):保护后端服务健康,拒绝过多错误请求,防止雪崩。
- 策略管理(Policy)
- 访问控制:如白名单、黑名单等,指定谁能访问哪些服务。
- 限流(Rate Limits):对请求量进行限制,防止滥用。
- 配额(Quotas):为用户、团队、API 设置请求配额。
- 测试功能
- 故障注入(Fault Injection):可人为插入延迟、错误等,用于模拟异常场景,验证系统稳定性和容错机制。
2.2.2 安全性(Security)
1. 加密通信(mTLS)
- 自动为服务间通信加密(mTLS),无需开发人员更改代码。
- 自动生成、分发、轮换证书,支持对接外部 CA。
2. 强身份(Strong Identity)
- 基于标准(如 SPIFFE)为每个服务颁发唯一身份证书,精确到工作负载级别,实现零信任体系。
3. 认证和鉴权(AuthN/AuthZ)
- 支持多种认证方式(JWT、OIDC 等),对接企业SSO。
- 细粒度授权规则(谁可以访问什么资源)。
2.2.3 可观测性(Observability)
1. 指标收集(Metrics)
- 自动收集“黄金指标”(如请求数、延时、错误率、流量等),对接 Prometheus、Grafana 可视化。
2. 分布式追踪(Tracing)
- 自动注入 trace 标识,跨服务追踪一次请求的全路径,便于定位性能瓶颈与故障点。
3. 流量观测
- 监控网内所有流量,包括集群内、入站(Ingress)、出站(Egress)等详细流向。
2.2.4 服务网格(Mesh)
1. 多运行环境支持
- 不光支持 Kubernetes,还可管理部署在 虚拟机(VM)上的服务。
- 支持异构环境,混合云部署。
2. 多集群管理
- 支持多集群服务网格,通过“多集群网关”、“联邦(Federation)”等特性,实现跨集群流量路由和安全通信。
2.3 Istio 系统架构演进
Istio 的体系架构可以分为两大部分:
- 控制平面(Control Plane)
- 数据平面(Data Plane)
2.3.1 Istio 初始架构(0.x-1.0 版本)
- Sidecar模式运行的Envoy根据控制平面组件
<font style="color:#DF2A3F;">Pilot</font>
定义配置略来决定如何路由流量 ,负责服务发现、路由配置 - 遥测功能都由
<font style="color:#DF2A3F;">Mixer</font>
来完成- 响应来自数据平面的查询,包括授权、访问控制和配额等
- 收集流量指标、日志和追踪数据等
- 根据启用的适配器,将收集的数据存储至相应适配器的后端
<font style="color:#DF2A3F;">Citadel</font>
- 帮助用户基于服务身份(而非网络控制机制)构建零 信任的安全网络环境
- 密钥管理
- Mixer 性能瓶颈:每次请求需两次远程调用(前置检查+后置遥测)
- 高延迟:增加 5-10ms 延迟(P99)
- 运维复杂:需部署多个独立组件
- 资源消耗大:Mixer 需要大量 CPU 和内存
2.3.2 过渡架构(1.1-1.4 版本)
<font style="color:#DF2A3F;">Mixer</font>
- 将插件模型替换为使用进程外的
<font style="color:#DF2A3F;">Adapter</font>
进行扩展- 性能表现更差
- 完成了Mixer与扩展间的解耦
- 将插件模型替换为使用进程外的
<font style="color:#DF2A3F;">Galley</font>
<font style="color:#DF2A3F;">Pilot</font>
中适配底层平台的功能独立成的组件- 是Istio的配置验证、摄取、处理和分发组件
- 负责将其余的
<font style="color:#DF2A3F;">Istio</font>
组件与从底层平台(例如Kubernetes)获取用户配置的细节隔离开来,从而将<font style="color:#DF2A3F;">Pilot</font>
与底层平台进行解耦
2.3.3 Istio v1.5+版本
- 回归单体
- 抛弃影响性的Mixer,遥测功能交由Envoy自行完成
- 将
<font style="color:rgb(0,0,0);">Pilot</font>
、<font style="color:rgb(0,0,0);">Citadel</font>
、<font style="color:rgb(0,0,0);">Galley</font>
和<font style="color:rgb(0,0,0);">Sidecar Injector</font>
整合为一个单体应用<font style="color:#DF2A3F;">istiod</font>
**<font style="color:#DF2A3F;">Istiod</font>**
<font style="color:#DF2A3F;">Istiod</font>
充当控制平面,将配置分发到所有Sidecar代理和网关- 它能够为支持网格的应用实现智能化的负载均衡机制,且相关流量绕过了
**<font style="color:rgb(0,0,0);">kube-proxy</font>**
;
2.4 Isito 主要组件
2.4.1 控制平面
主要负责配置管理、策略下发、安全管理、监控信息收集与分发,不处理业务数据流量。
<font style="color:#DF2A3F;">Istiod</font>
是 Istio v1.5 之后的统一控制面组件,集成了旧版的一系列控制组件(<font style="color:#DF2A3F;">Pilot</font>
、<font style="color:#DF2A3F;">Citadel</font>
、<font style="color:#DF2A3F;">Galley</font>
、<font style="color:#DF2A3F;">Mixer</font>
),主要功能如下:
- 配置管理:监听 Kubernetes 配置(如
<font style="color:#DF2A3F;">VirtualService</font>
、<font style="color:#DF2A3F;">DestinationRule</font>
、<font style="color:#DF2A3F;">PeerAuthentication</font>
、<font style="color:#DF2A3F;">AuthorizationPolicy</font>
等),解析用户写的网络和安全规则。 - 服务发现:感知集群内部部署的服务变化,生成路由表。
- 向 Envoy 下发配置:通过 xDS 协议,把路由策略、熔断、限流、安全等规则推送到
<font style="color:#DF2A3F;">Sidecar</font>
上。 - 证书管理与安全:负责为数据面(Envoy)自动签发、轮换、吊销 mTLS 证书,实现相互认证。
- 策略与遥测:协作处理策略决策和遥测上报(但自从 Mixer 被弃用后,主要通过 Envoy 直接与监控工具对接)。
<font style="color:#DF2A3F;">Polit</font>
:控制平面的核心组件。主要负责服务发现和配置管理相关功能。
<font style="color:#DF2A3F;">Citadel</font>
:安全组件,负责数字证书的生成、分发、吊销和轮换等生命周期管理机制。
<font style="color:#DF2A3F;">Galley</font>
:Istiod 与底层平台的中间适配层。
<font style="color:#DF2A3F;">Sidecar Injector</font>
:Sidecar 注入组件。
2.4.2 数据平面
实际处理所有业务流量,负责转发、流量分发、加密、监控埋点等。
**<font style="color:#DF2A3F;">Istio Proxy</font>**
- 基于 Envoy 实现的 Sidecar Proxy, 作为每个服务 Pod 的
<font style="color:#DF2A3F;">Sidecar</font>
容器并肩运行(也支持 VM 模式)。 - 核心能力:
- 实际进行流量拦截与转发(服务之间的网络流量全部经过 Envoy)
- 支持
<font style="color:rgb(51, 54, 57);">HTTP</font>
/<font style="color:rgb(51, 54, 57);">gRPC</font>
等多种协议的智能路由、条件分流、流量镜像、熔断限流、故障注入等功能 - mTLS 加密和认证。
- 自动采集指标(
<font style="color:#DF2A3F;">Metrics</font>
)、链路追踪(<font style="color:#DF2A3F;">Tracing</font>
)等。
**<font style="color:#DF2A3F;">Ingreess Gateway</font>**
:统一入口网关,流量从外部进入 Mesh 前先到 Gateway,由 Envoy 进行 L7/L4 流量治理。
**<font style="color:#DF2A3F;">Egress Gateway</font>**
:对出网流量统一管控,做访问策略或审计。
**<font style="color:#DF2A3F;">ProxyLess</font>**
:无 Sidecar Proxy 形式的数据面组件,需要侵入业务构建数据平面。
注意:目前 Istio 新版本支持两种数据平面模式:
**<font style="color:#DF2A3F;">sidecar</font>**
模式 ,它将 Envoy 代理与在集群中启动的每个 Pod 一起部署,或与 VM 上运行的服务一起运行。**<font style="color:#DF2A3F;">ambient</font>**
模式 ,该模式使用每节点的第 4 层代理,以及可选的每命名空间 Envoy 代理来实现第 7 层功能。他们详细的区别和使用场景可以查看**:**https://istio.io/latest/docs/overview/dataplane-modes/
这里主要学习 sidecar 模式,也是目前最主流的模式,如果想了解 ambient 模式,可以查看官网:
https://istio.io/latest/docs/ambient/
2.4.3 附加组件
- Kiali
- Prometheus
- EFK/PLG
- Tracing
- Jaeger/Zipkin/Skywalking/…
2.5 各组件功能详解
2.5.1 Pilot 功能介绍
Pilot 管理和配置部署在 <font style="color:#DF2A3F;">Istio</font>
服务网格中的所有 <font style="color:#DF2A3F;">Envoy</font>
代理实例,是 Istio 控制平面的核心组件。
它主要由<font style="color:#DF2A3F;">Platform Adapter</font>
、<font style="color:#DF2A3F;">Abstract Model</font>
、 <font style="color:#DF2A3F;">Envoy API (xDS)</font>
和<font style="color:#DF2A3F;">Rules API</font>
四个组件构成;
- 平台适配器:负责适配底层平台,并完成从平台特有的服务模型到
<font style="color:#DF2A3F;">Istio</font>
服务模型的转换; - 抽象聚合层:聚合底层不同平台的服务和配置规则并对上提供统一的接口,从而解耦EnvoyAPI和底层平台;
<font style="color:#DF2A3F;">Envoy API</font>
:Pilot通过<font style="color:#DF2A3F;">xDS</font>
服务器提供服务发现接口<font style="color:#DF2A3F;">xDS API</font>
,而xDS服务器接收并维护Envoy代理的连接,并基于Envoy订阅的资源名称进行配置分发;<font style="color:#DF2A3F;">Rules API</font>
:高级流量管理规则的API接口,用户通过该API配置流量管理规则并由其转换为低级配置,而后通过<font style="color:#DF2A3F;">discovery API</font>
分发到<font style="color:#DF2A3F;">Envoy</font>
实例;
Pilot 主要功能:
- 服务发现
- 配置Sidecar
- 流量治理
- A/B testing (AB 测试)
- Failover (故障转移)
- Fault Injection (故障注入)
- Canary rollout (金丝雀发布)
- Circuit breaker (熔断)
- Retries (重试)
- Timeouts (超时)
2.5.2 Citadel 功能介绍
<font style="color:#DF2A3F;">Citadel</font>
是<font style="color:#DF2A3F;">Istio</font>
控制平面的核心安全组件,负责服务的私钥和数字证书管理,用于提供自动生成、分发、 轮换及撤销私钥和数据证书的功能;- Kubernetes平台上,
<font style="color:#DF2A3F;">Citadel</font>
的传统工作方式是基于**<font style="color:#DF2A3F;">Secret</font>**
资源将证书及私钥注入到Sidecar容器中; - 非容器环境中,首先通过系统上运行的Node Agent生成CSR,而后向Citadel发起证书签署请求,并将接收到的证书和私钥存储于本地文件系统提供给Envoy使用;
- Istio 1.1版本起支持基于SDS API动态配置Secret给Envoy;
- Kubernetes平台上,
- 另外,除了
<font style="color:#DF2A3F;">Istio</font>
的<font style="color:#DF2A3F;">Citadel</font>
之外,还有Vault等其它证书和私钥管理系统可用; - Istio的安全模型需要多个组件协同工作
<font style="color:#DF2A3F;">Citadel</font>
管理私钥和数字证书<font style="color:#DF2A3F;">Sidecar</font>
和perimeter proxies执行服务间的安全通信<font style="color:#DF2A3F;">Pilot</font>
向代理分发认证策略和名称信息
2.5.3 Galley 功能介绍
<font style="color:#DF2A3F;">Galley</font>
负责向Istio控制平面的其它组件提供支撑功能,它核验进入网格的配置信息的格式和内容的正确性,并将这些配置信息提供给<font style="color:#DF2A3F;">Pilot</font>
和<font style="color:#DF2A3F;">Mixer</font>
;
<font style="color:#DF2A3F;">Galley</font>
从底层平台接收配置信息并完成分发,从而将其它组件同底层平台解耦- MCP(Mesh Configuration Protocol)是Istio网格中用于分发配置的传输协议
- 基于gRPC
- 与xDS协议类似,一次完整的MCP请求/响应流程包括请求、响应和ACK/NACK;
- MeshConfigRequest
- MeshConfigResponse
- 以MeshConfigRequest形式发送的ACK或NACK
- 目前尚不支持增量配置分发机制;
从实现上来说,<font style="color:rgb(0,0,0);">Galley</font>
通过Kubernetes的动态<font style="color:#DF2A3F;">Admission Controller(Admission Hook)</font>
完成组件配置的校验;
- 在
<font style="color:#DF2A3F;">Mutation</font>
阶段,实现对请求内容的修改,利用为服务注入<font style="color:rgb(0,0,0);">Sidecar</font>
; - 在Validation阶段实现对请求内容进行校验,Galley对配置的校验即发生在此阶段;
程序文件istio-galley
2.5.4 Ingress Gateway 和 Egress Gateway
<font style="color:#DF2A3F;">Istio Gateway</font>
用于将<font style="color:#DF2A3F;">Istio</font>
功能(例如,监控和路由规则)应用于进入服务网格的流量
- 通过将
<font style="color:#DF2A3F;">Envoy</font>
代理部署在服务之前,运维人员可以针对面向用户的服务进行A/B测试、金丝雀部署等 - Istio Gateway不同于Kubernetes Ingress
- 类似地,有必要时,也可以部署专用的
<font style="color:#DF2A3F;">Egress Gateway</font>
,运维人员可以为这些服务添加超时控制、重试、断路器等功能,同时还能从服务连接中获取各种细节指标
程序文件<font style="color:#DF2A3F;">istio-ingressgateway</font>
和<font style="color:#DF2A3F;">istio-egressgateway</font>
2.5.5 Sidecar 注入机制
非<font style="color:#DF2A3F;">Ambient</font>
模式下,Istio服务网格中的每个Pod都必须伴随一个Istio兼容的Sidecar一同运行
常用的将<font style="color:#DF2A3F;">Sidecar</font>
注入Pod中的方法有两种
- 手工注入:使用
<font style="color:#DF2A3F;">istioctl</font>
客户端工具进行注入 - 自动注入:使用
<font style="color:#DF2A3F;">Istio sidecar injector</font>
自动完成注入过程- 利用Kubernets webhook实现sidecar的自动注入
- 创建Pod时自动注入过程发生在Admission Controller的
<font style="color:#DF2A3F;">Mutation</font>
阶段,根据自动注入配置,kube-apiserver拦截到Pod创建请求时调用自动注入服务<font style="color:#DF2A3F;">istio-sidecar-injector</font>
生成Sidecar容器的描述并将其插入到Pod的配置清单中
三、Istio 安装
3.1 Istioctl 下载
https://github.com/istio/istio/releases/tag/1.26.3
[root@k8s-master01 ~]# curl -L https://istio.io/downloadIstio | sh -% Total % Received % Xferd Average Speed Time Time Time CurrentDload Upload Total Spent Left Speed
100 101 100 101 0 0 68 0 0:00:01 0:00:01 --:--:-- 68
100 5124 100 5124 0 0 2492 0 0:00:02 0:00:02 --:--:-- 17194Downloading istio-1.26.3 from https://github.com/istio/istio/releases/download/1.26.3/istio-1.26.3-linux-amd64.tar.gz ...Istio 1.26.3 download complete!The Istio release archive has been downloaded to the istio-1.26.3 directory.To configure the istioctl client tool for your workstation,
add the /root/istio-1.26.3/bin directory to your environment path variable with:export PATH="$PATH:/root/istio-1.26.3/bin"Begin the Istio pre-installation check by running:istioctl x precheck Try Istio in ambient modehttps://istio.io/latest/docs/ambient/getting-started/
Try Istio in sidecar modehttps://istio.io/latest/docs/setup/getting-started/
Install guides for ambient modehttps://istio.io/latest/docs/ambient/install/
Install guides for sidecar modehttps://istio.io/latest/docs/setup/install/Need more information? Visit https://istio.io/latest/docs/
[root@k8s-master01 ~]# mv istio-1.26.3/ /usr/local/istio
[root@k8s-master01 ~]# vim /etc/profile
export PATH=/usr/local/istio/bin:$PATH
[root@k8s-master01 ~]# source /etc/profile
Istio 目录结构:
[root@k8s-master01 /usr/local/istio]# ll
total 40
drwxr-x--- 2 root root 4096 Jul 25 19:38 bin # 二进制文件
-rw-r--r-- 1 root root 11357 Jul 25 19:38 LICENSE
drwxr-xr-x 4 root root 4096 Jul 25 19:38 manifests # 内置的配置档案
-rw-r----- 1 root root 983 Jul 25 19:38 manifest.yaml
-rw-r--r-- 1 root root 6927 Jul 25 19:38 README.md
drwxr-xr-x 27 root root 4096 Jul 25 19:38 samples # 部署各扩展组件的示例清单
drwxr-xr-x 3 root root 4096 Jul 25 19:38 tools
3.2 IstioOperator API 配置
IstioOperator API 主要包括以下几个配置段:
<font style="color:#DF2A3F;">components</font>
:组件配置参数<font style="color:#DF2A3F;">hub</font>
:获取各组件镜像的仓库,默认为"docker.io/istio"<font style="color:#DF2A3F;">meshConfig</font>
:网络(数据平面)相关配置参数<font style="color:#DF2A3F;">value</font>
:传递给 Chart 的各项参数值<font style="color:#DF2A3F;">addonComponents</font>
:附加组件(Kiali,prometheus 等)
IstioOperator API 中定义的组件(<font style="color:#DF2A3F;">Components</font>
)
- base
<font style="color:#DF2A3F;">pilot</font>
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
<font style="color:rgb(251, 71, 135);">enabled</font> | bool | <font style="color:rgb(251, 71, 135);">true</font> | 是否启用 Pilot |
<font style="color:rgb(251, 71, 135);">namespace</font> | string | <font style="color:rgb(251, 71, 135);">""</font> | 覆盖默认命名空间 |
<font style="color:rgb(251, 71, 135);">k8s</font> | object | <font style="color:rgb(251, 71, 135);">{}</font> | Kubernetes 资源定制 |
<font style="color:#DF2A3F;">ingressGateways</font>
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
<font style="color:rgb(251, 71, 135);">name</font> | string | <font style="color:rgb(251, 71, 135);">istio-ingressgateway</font> | 网关名称 |
<font style="color:rgb(251, 71, 135);">enabled</font> | bool | <font style="color:rgb(251, 71, 135);">true</font> | 是否启用 |
<font style="color:rgb(251, 71, 135);">namespace</font> | string | <font style="color:rgb(251, 71, 135);">""</font> | 覆盖命名空间 |
<font style="color:rgb(251, 71, 135);">label</font> | map | <font style="color:rgb(251, 71, 135);">{}</font> | 自定义 Pod 标签 |
<font style="color:rgb(251, 71, 135);">k8s</font> | object | <font style="color:rgb(251, 71, 135);">{}</font> | Kubernetes 资源定制 |
<font style="color:#DF2A3F;">egressGateways</font>
<font style="color:#DF2A3F;">cni</font>
参数 | 类型 | 默认值 | 说明 |
---|---|---|---|
<font style="color:rgb(251, 71, 135);">enabled</font> | bool | <font style="color:rgb(251, 71, 135);">false</font> | 是否启用 CNI 插件 |
<font style="color:rgb(251, 71, 135);">namespace</font> | string | <font style="color:rgb(251, 71, 135);">""</font> | 覆盖命名空间 |
<font style="color:rgb(251, 71, 135);">k8s</font> | object | <font style="color:rgb(251, 71, 135);">{}</font> | Kubernetes 资源定制 |
- istioRemote
上面的配置段各参数,都可以在 istioctl 命令行中使用 --set 选项进行独立配置,比如部署为 default 配置档案启用 debug 日志功能。可以使用如下命令
istioctl install --set profile=default --set values.global.logging.level=debug
3.2 部署 Istio
Istio 在安装时有很多配置文件,每个配置文件提供了不同的安装配置,如下:
**<font style="color:rgb(41, 54, 85);">default</font>**
:根据 IstioOperatorAPI 中。 建议将此配置文件用于生产部署和 主集群多集群网格。**<font style="color:rgb(41, 54, 85);">demo</font>**
:旨在展示资源需求适中的 Istio 功能的配置。它适合运行 Bookinfo 应用程序和相关任务。这是随快速入门说明一起安装的配置。**<font style="color:rgb(41, 54, 85);">minimal</font>**
:类似于default profile,但仅部署控制平台组件**<font style="color:rgb(0,0,0);">remote</font>**
:用于配置共享Control Plane的多集群环境**<font style="color:rgb(0,0,0);">empty</font>**
:不部署任何组件,通常帮助用户在自定义profile时生成基础配置信息**<font style="color:rgb(0,0,0);">preview</font>**
:包含预览性配置的profile,用于探索Istio的新功能,但不保证稳定性、安全性和性能**<font style="color:rgb(0,0,0);">ambient</font>**
:专用于部署ambient mesh环境的profile
[root@k8s-master01 /usr/local/istio/manifests/profiles]# ll
total 40
-rw-r--r-- 1 root root 246 Jul 25 19:38 ambient.yaml
-rw-r--r-- 1 root root 780 Jul 25 19:38 default.yaml
-rw-r--r-- 1 root root 177 Jul 25 19:38 demo.yaml
-rw-r--r-- 1 root root 317 Jul 25 19:38 empty.yaml
-rw-r--r-- 1 root root 216 Jul 25 19:38 minimal.yaml
-rw-r--r-- 1 root root 343 Jul 25 19:38 openshift-ambient.yaml
-rw-r--r-- 1 root root 184 Jul 25 19:38 openshift.yaml
-rw-r--r-- 1 root root 300 Jul 25 19:38 preview.yaml
-rw-r--r-- 1 root root 408 Jul 25 19:38 remote.yaml
-rw-r--r-- 1 root root 94 Jul 25 19:38 stable.yaml
每个内置的配置默认启动的组件都会有所不同
这里我部署一个 demo 配置的 istio,因为默认的镜像仓库地址为 docker.io,不可控元素导致无法拉取镜像,可以看这里https://www.yuque.com/dycloud/pss8ys/fimvpkdh870kxurc
[root@k8s-master01 ~]# istioctl install --set profile=demo --set hub=docker.1ms.run/istio --set tag=1.26.3|\ | \ | \ | \ /|| \ / || \ / || \ / || \ / || \ / || \
/______||__________\
____________________\__ _____/ \_____/ This will install the Istio 1.26.3 profile "demo" into the cluster. Proceed? (y/N) y
This installation will make default injection and validation pointing to the default revision, and originally it was pointing to the revisioned one.
✔ Istio core installed ⛵️
✔ Istiod installed 🧠
✔ Egress gateways installed 🛫
✔ Ingress gateways installed 🛬
✔ Installation complete # 安装成功后会默认会在istio-system中生成ingressgw、egressgw、istiod
[root@k8s-master01 ~]# kubectl get pods -n istio-system
NAME READY STATUS RESTARTS AGE
istio-egressgateway-65589498c8-g4zsx 1/1 Running 0 6s
istio-ingressgateway-8f8d6679b-nnc9l 1/1 Running 0 6s
istiod-846896755-6drrb 1/1 Running 0 10s
[root@k8s-master01 ~]# kubectl get svc -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-egressgateway ClusterIP 10.96.60.236 <none> 80/TCP,443/TCP 12s
istio-ingressgateway LoadBalancer 10.96.18.176 <pending> 15021:32386/TCP,80:31991/TCP,443:31793/TCP,31400:31897/TCP,15443:30420/TCP 12s
istiod ClusterIP 10.96.198.48 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 16s
TODO(这里我用的环境是公司内部 tke 环境,无法在家配置 CLB ,所以一直获取不到EXTERNAL-IP,访问有问题),我这边先用家里的虚拟机搭建 metallb 测试
搭建 metallb
# 安装MetalLB
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.13.7/config/manifests/metallb-native.yaml
cat <<EOF | kubectl apply -f -
apiVersion: metallb.io/v1beta1
kind: IPAddressPool
metadata:name: default-poolnamespace: metallb-system
spec:addresses:- 192.168.1.2-192.168.1.250 # 使用更小的范围进行测试
---
apiVersion: metallb.io/v1beta1
kind: L2Advertisement
metadata:name: defaultnamespace: metallb-system
spec:ipAddressPools:- default-pool
EOF
过几分钟就可以看到 ip 了
[root@k8s-master01 ~]# kubectl get svc -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
istio-egressgateway ClusterIP 10.96.60.236 <none> 80/TCP,443/TCP 56s
istio-ingressgateway LoadBalancer 10.96.18.176 192.168.1.2 15021:32386/TCP,80:31991/TCP,443:31793/TCP,31400:31897/TCP,15443:30420/TCP 56s
istiod ClusterIP 10.96.198.48 <none> 15010/TCP,15012/TCP,443/TCP,15014/TCP 60s
而如果是云平台如 tke 集群,会自动创建 clb 关联 <font style="color:#DF2A3F;">ingressgateway</font>
,如果是在云平台的 cvm 自建的 k8s 集群,需要手动创建 clb ,然后修改 ingressgateway 类型为 svc,再将 clb 指向该 svc 的端口即可,如下:
启动 Sidecar 自动注入功能
在网格应用运行的 namespace 上,为 namespace 添加标签,激活 Sidecar 自动注入
[root@k8s-master01 ~]# kubectl label namespace bookinfo istio-injection=enabled
[root@k8s-master01 ~]# istioctl analyze -n bookinfo -o json
[]
✔ No validation issues found when analyzing namespace: app.
此外还可以部署 istio 的额外组件,如 prometheus、jaeger、kiali、loki 等
[root@k8s-master01 /usr/local/istio/samples/addons]# ll
total 296
drwxr-xr-x 2 root root 4096 Jul 25 19:38 extras
-rw-r--r-- 1 root root 236295 Aug 2 11:06 grafana.yaml
-rw-r--r-- 1 root root 2646 Aug 2 11:07 jaeger.yaml
-rw-r--r-- 1 root root 10315 Jul 25 19:38 kiali.yaml
-rw-r--r-- 1 root root 208 Aug 2 11:04 lokipvc.yaml
-rw-r--r-- 1 root root 9835 Aug 2 11:04 loki.yaml
-rw-r--r-- 1 root root 16740 Aug 2 11:06 prometheus.yaml
-rw-r--r-- 1 root root 5441 Jul 25 19:38 README.md
[root@k8s-master01 /usr/local/istio/samples/addons]# pwd
/usr/local/istio/samples/addons[root@k8s-master01 addons]# kubectl apply -f . -n istio-system
serviceaccount/grafana created
configmap/grafana created
service/grafana created
deployment.apps/grafana created
configmap/istio-grafana-dashboards created
configmap/istio-services-grafana-dashboards created
deployment.apps/jaeger created
service/tracing created
service/zipkin created
service/jaeger-collector created
serviceaccount/kiali created
configmap/kiali created
clusterrole.rbac.authorization.k8s.io/kiali unchanged
clusterrolebinding.rbac.authorization.k8s.io/kiali unchanged
service/kiali created
deployment.apps/kiali created
serviceaccount/loki created
configmap/loki created
configmap/loki-runtime created
clusterrole.rbac.authorization.k8s.io/loki-clusterrole unchanged
clusterrolebinding.rbac.authorization.k8s.io/loki-clusterrolebinding unchanged
service/loki-memberlist created
service/loki-headless created
service/loki created
statefulset.apps/loki created
serviceaccount/prometheus created
configmap/prometheus created
clusterrole.rbac.authorization.k8s.io/prometheus unchanged
clusterrolebinding.rbac.authorization.k8s.io/prometheus unchanged
service/prometheus created
deployment.apps/prometheus created
[root@k8s-master01 /usr/local/istio/samples/addons]# kubectl get pods -n istio-system
NAME READY STATUS RESTARTS AGE
grafana-548947586b-jnjts 1/1 Running 0 2m58s
istio-egressgateway-65589498c8-db8pk 1/1 Running 0 15m
istio-ingressgateway-8f8d6679b-6rnw7 1/1 Running 0 15m
istiod-846896755-9s7g6 1/1 Running 0 15m
jaeger-b8465f948-dgp6s 1/1 Running 0 101s
kiali-7cbdf5689-vxl6b 1/1 Running 0 8m32s
prometheus-66bf456d8-lnk89 2/2 Running 0 2m16s
3.3 Istioctl 命令参数详解
3.3.1 安装与管理命令
<font style="color:#DF2A3F;">istioctl install</font>
:安装或重新配置 istio
-f, --filename string # 指定配置文件路径(YAML)
--set stringArray # 覆盖配置值(如 --set profile=demo)
--revision string # 指定安装的版本标签(用于多版本部署)
--skip-confirmation # 跳过确认提示(-y 等效)
--dry-run # 模拟执行,不实际安装
--verify # 安装后验证
示例:
istioctl install -y --set profile=demo --revision=1-18-0
<font style="color:#DF2A3F;">istioctl uninstall</font>
:卸载 istio
--revision string # 卸载指定版本
--skip-confirmation # 跳过确认提示
示例:
istioctl uninstall --revision=1-18-0 -y
<font style="color:#DF2A3F;">istioctl upgrade</font>
:升级 Istio 控制平面
-f, --filename string # 指定新配置文件
--set stringArray # 覆盖配置值
--skip-confirmation # 跳过确认提示
3.3.2 配置验证与分析命令
<font style="color:#DF2A3F;">istioctl analyze</font>
:分析配置并诊断问题
-n, --namespace string # 指定命名空间
-f, --filename string # 分析本地文件
-o, --output string # 输出格式(log/json)
--failure-threshold Level # 设置错误级别(info/warn/error)
示例:
istioctl analyze -n default -o json
<font style="color:#DF2A3F;">istioctl validate</font>
:验证资源配置文件
-f, --filename string # 验证本地文件
示例
istioctl validate -f virtual-service.yaml
3.3.3 代理管理命令
<font style="color:#DF2A3F;">istioctl proxy-config</font>
:查看 envoy 代理配置
子命令:
clusters # 查看集群配置
listeners # 查看监听器配置
routes # 查看路由配置
endpoints # 查看端点配置
log # 动态更新日志级别
bootstrap # 查看启动配置
参数
-n, --namespace string # 指定命名空间
-o, --output string # 输出格式(json/yaml)
--level string # 设置日志级别(如 debug、info)
示例
istioctl proxy-config clusters product-pod-123 -n default -o json
<font style="color:#DF2A3F;">istioctl proxy-status</font>
:检查配置同步状态
--revision string # 指定 Istiod 版本
-o, --output string # 输出格式(short/json)
输出说明:
<font style="color:rgb(251, 71, 135);">SYNCED</font>
:配置已同步<font style="color:rgb(251, 71, 135);">STALE</font>
:配置不同步<font style="color:rgb(251, 71, 135);">NOT SENT</font>
:未收到配置
示例:
[root@k8s-master01 /usr/local/istio/samples/addons]# istioctl proxy-status
NAME CLUSTER CDS LDS EDS RDS ECDS ISTIOD VERSION
istio-egressgateway-65589498c8-db8pk.istio-system Kubernetes SYNCED (13m) SYNCED (13m) SYNCED (0s) IGNORED IGNORED istiod-846896755-9s7g6 1.26.3
istio-ingressgateway-8f8d6679b-6rnw7.istio-system Kubernetes SYNCED (13m) SYNCED (13m) SYNCED (0s) IGNORED IGNORED istiod-846896755-9s7g6 1.26.3
3.3.4 诊断与调试命令
<font style="color:#DF2A3F;">istioctl dashboard</font>
:访问 webui 控制台
支持的面板:
controlz # Istiod 控制台
prometheus # Prometheus 监控
grafana # Grafana 仪表盘
kiali # Kiali 服务网格视图
jaeger # Jaeger 分布式追踪
关键参数:
--address string # 绑定地址(默认 localhost)
-p, --port int # 本地端口(默认随机)
示例:
istioctl dashboard kiali --address 0.0.0.0 -p 8080
<font style="color:#DF2A3F;">istioctl bug-report</font>
:收集集群诊断信息
--include stringArray # 包含组件(istiod/envoy)
--exclude stringArray # 排除组件
-o, --output string # 输出目录
示例:
istioctl bug-report --include=istiod,envoy
3.3.5 安全相关命令
istioctl create-remote-secret:创建多集群链接密钥
--name string # 集群名称
--context string # Kubeconfig 上下文
示例:
istioctl create-remote-secret --name=cluster-1 > secret.yaml
3.3.6 辅助命令
<font style="color:#DF2A3F;">istioctl kube-inject</font>
:手动注入 sidecar
-f, --filename string # 输入文件
-o, --output string # 输出文件
--inject=false # 禁用注入
示例
istioctl kube-inject -f deployment.yaml -o injected.yaml
<font style="color:#DF2A3F;">istioctl version</font>
:显示版本信息
--remote=true # 显示服务端版本
-o, --output # 输出格式(short/json)
示例:
istioctl version --remote=true
四、Istio CRD 详解
istio 安装成功之后,会在集群内创建多个 crd 资源,下面来学习下这些 crd 资源的作用
[root@k8s-master01 ~]# kubectl get crd |grep istio
authorizationpolicies.security.istio.io 2025-08-02T01:33:03Z
destinationrules.networking.istio.io 2025-08-02T01:33:03Z
envoyfilters.networking.istio.io 2025-07-21T06:56:13Z
gateways.networking.istio.io 2025-08-02T01:33:03Z
peerauthentications.security.istio.io 2025-08-02T01:33:03Z
proxyconfigs.networking.istio.io 2025-08-02T01:33:03Z
requestauthentications.security.istio.io 2025-08-02T01:33:03Z
serviceentries.networking.istio.io 2025-08-02T01:33:03Z
sidecars.networking.istio.io 2025-08-02T01:33:03Z
telemetries.telemetry.istio.io 2025-08-02T01:33:03Z
virtualservices.networking.istio.io 2025-08-02T01:33:03Z
wasmplugins.extensions.istio.io 2025-08-02T01:33:03Z
workloadentries.networking.istio.io 2025-08-02T01:33:03Z
workloadgroups.networking.istio.io 2025-08-02T01:33:03Z
4.1 核心流量管理 CRD
4.1.1 <font style="color:#DF2A3F;">virtualservices.networking.istio.io</font>
这是 istio 最核心的 crd,负责定义流量路由规则,可以实现:
- 基于 HTTP/HTTPS 请求属性(路径、header 头)路由流量
- 实现金丝雀发布、A/B 测试、蓝绿部署等
- 设置超时、重试、故障注入
例如:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: reviews-route
spec:hosts:- reviews.prod.svc.cluster.localhttp:- match:- headers:end-user:exact: jasonroute:- destination:host: reviews.prod.svc.cluster.localsubset: v2- route:- destination:host: reviews.prod.svc.cluster.localsubset: v1
4.1.2 <font style="color:#DF2A3F;">destinationrules.networking.istio.io</font>
定义流量策略,如:
- 配置负载均衡策略(轮询、随机等)
- 定义连接池配置(最大连接数、超时等)
- 配置 TLS 安全策略
- 创建服务子集(subsets)
例如:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:name: reviews-destination
spec:host: reviews.prod.svc.cluster.localtrafficPolicy:loadBalancer:simple: RANDOMsubsets:- name: v1labels:version: v1- name: v2labels:version: v2
4.1.3 <font style="color:#DF2A3F;">gateways.networking.istio.io</font>
定义入口/出口网关,配置网格边缘的负载均衡器,管理入站、出站的流量,与 virtualService 配合使用。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:name: ext-host-gwy
spec:selector:istio: ingressgatewayservers:- port:number: 443name: httpsprotocol: HTTPShosts:- myhost.comtls:mode: SIMPLEcredentialName: ssl-cert
4.2 服务发现与扩展
4.2.1 <font style="color:#DF2A3F;">serviceentries.networking.istio.io </font>
将外部服务注册到服务网格,可以让网格内的服务访问到外部服务(如 API、数据库),可以通过外部服务的 DNS 或静态 IP 访问
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:name: external-svc-https
spec:hosts:- api.example.comlocation: MESH_EXTERNALports:- number: 443name: httpsprotocol: TLSresolution: DNS
4.2.2 <font style="color:#DF2A3F;">sidecars.networking.istio.io</font>
配置 Sidecar 代理行为,可以实现:
- 限制 Sidecar 可以访问的服务范围
- 减少配置下发量,提高性能
- 定义出站/入站流量规则
apiVersion: networking.istio.io/v1alpha3
kind: Sidecar
metadata:name: default-sidecarnamespace: prod
spec:egress:- hosts:- "./*"- "istio-system/*"
4.2.3 <font style="color:#DF2A3F;">workloadentries.networking.istio.io</font>
将 vm 或裸机工作负载加入到服务网格中,可以扩展服务网格到非 k8s 环境中,与 serviceEntry 配合使用
apiVersion: networking.istio.io/v1alpha3
kind: WorkloadEntry
metadata:name: vm1
spec:address: 192.168.1.1labels:app: myappversion: v1
4.2.4 <font style="color:#DF2A3F;">workloadgroups.networking.istio.io</font>
定义工作负载组模板,可以为 workloadEntry 提供模板,简化批量工作负载的管理
apiVersion: networking.istio.io/v1alpha3
kind: WorkloadGroup
metadata:name: myapp-group
spec:metadata:labels:app: myapptemplate:serviceAccount: myapp-sanetwork: corp-network
4.3 安全相关 CRD
4.3.1 <font style="color:#DF2A3F;">authorizationpolicies.security.istio.io</font>
定义访问控制策略,可以实现基于角色的访问控制(RBAC)、可以控制服务间通信的权限。
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:name: require-jwt
spec:selector:matchLabels:app: myapprules:- from:- source:requestPrincipals: ["*"]
4.3.2 <font style="color:#DF2A3F;">peerauthentications.security.istio.io</font>
配置服务间认证策略,可以控制 mTLS 模式(STRICT,PERMISSIVE,DISABLE),还可以设置命名空间或工作负载级别的认证
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:name: default
spec:mtls:mode: STRICT
4.3.3 <font style="color:#DF2A3F;">requestauthentications.security.istio.io</font>
配置请求级认证,可以验证 JWT 令牌,定义令牌签发者和验证规则
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:name: jwt-auth
spec:selector:matchLabels:app: myappjwtRules:- issuer: "https://accounts.google.com"jwksUri: "https://www.googleapis.com/oauth2/v3/certs"
4.4 可观测与扩展
4.4.1 <font style="color:#DF2A3F;">telemetries.telemetry.istio.io</font>
配置遥测收集,可以自定义指标、日志收集、配置跟踪采样率、修改遥测数据格式等。
apiVersion: telemetry.istio.io/v1alpha1
kind: Telemetry
metadata:name: mesh-default
spec:accessLogging:- providers:- name: envoy"
4.4.2 <font style="color:#DF2A3F;">envoyfilters.networking.istio.io </font>
直接修改 Envoy 配置,可以添加自定义 Envoy 过滤器,实现高级流量操作
apiVersion: networking.istio.io/v1alpha3
kind: EnvoyFilter
metadata:name: custom-filter
spec:configPatches:- applyTo: HTTP_FILTERmatch:context: SIDECAR_INBOUNDpatch:operation: INSERT_BEFOREvalue:name: envoy.luaconfig:inlineCode: |function envoy_on_request(request_handle)request_handle:logInfo("Hello World!")end
4.4.3 <font style="color:#DF2A3F;">wasmplugins.extensions.istio.io </font>
管理 WebAssembly 插件,可以部署 Wasm 扩展到 Envoy 代理,实现动态扩展代理功能。
apiVersion: extensions.istio.io/v1alpha1
kind: WasmPlugin
metadata:name: my-wasm-plugin
spec:selector:matchLabels:app: myappurl: oci://registry.example.com/wasm-plugins/my-plugin:v1.0
4.4.4 <font style="color:#DF2A3F;">proxyconfigs.networking.istio.io </font>
配置代理行为,可以调整代理的并发设置、配置代理日志级别、负载默认代理行为等
apiVersion: networking.istio.io/v1beta1
kind: ProxyConfig
metadata:name: custom-config
spec:concurrency: 4environmentVariables:ENVOY_LOG_LEVEL: "debug"
4.5 CRD 协作架构
这个关系图展示了 Istio 各 CRD 如何协同工作:
- 流量入口:
<font style="color:#DF2A3F;">Gateway</font>
接收外部流量 - 路由决策:
<font style="color:#DF2A3F;">VirtualService</font>
处理复杂路由 - 策略应用:
<font style="color:#DF2A3F;">DestinationRule</font>
定义服务策略 - 安全控制:各种
<font style="color:#DF2A3F;">Policy</font>
实施安全措施 - 服务发现:
<font style="color:#DF2A3F;">ServiceEntry</font>
扩展服务范围 - 工作负载:
<font style="color:#DF2A3F;">WorkloadEntry</font>
集成非容器负载 - 监控扩展:
<font style="color:#DF2A3F;">Telemetry</font>
和代理扩展提供高级功能
五、部署 bookinfo 示例
下面来部署下官方提供的 bookinfo 示例,上面已经创建了bookinfo 命名空间,并且给命名空间添加了自动注入 sidecar 功能
5.1 部署 K8S API 网关 CRD
默认情况下,大多数 Kubernetes 集群上不会安装 Kubernetes 网关 API CRD,因此请确保在使用网关 API 之前安装它们。
kubectl get crd gateways.gateway.networking.k8s.io &> /dev/null || \
{ kubectl kustomize "github.com/kubernetes-sigs/gateway-api/config/crd?ref=v1.3.0" | kubectl apply -f -; }
5.2 部署 Bookinfo 示例
可以看到每个 Pod 都是 2 个容器,一个是应用自己的容器,另一个是 istio proxy 的代理容器
[root@k8s-master01 /usr/local/src/istio]# kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml -n bookinfo
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created[root@k8s-master01 /usr/local/src/istio]# kubectl get pods -n bookinfo
NAME READY STATUS RESTARTS AGE
details-v1-7b88fb8889-vv6c6 2/2 Running 0 4h8m
productpage-v1-79d79d6-tn546 2/2 Running 0 4h8m
ratings-v1-5b89cf4bbf-2gts5 2/2 Running 0 4h8m
reviews-v1-ffd57b847-p2mlz 2/2 Running 0 4h8m
reviews-v2-8cc6fd8dc-ljkzw 2/2 Running 0 4h8m
reviews-v3-cf48455-4zd6b 2/2 Running 0 4h8m
[root@k8s-master01 /usr/local/src/istio]#
[root@k8s-master01 /usr/local/src/istio]# kubectl get svc -n bookinfo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
details ClusterIP 10.96.173.237 <none> 9080/TCP 4h8m
productpage ClusterIP 10.96.185.70 <none> 9080/TCP 4h8m
ratings ClusterIP 10.96.245.50 <none> 9080/TCP 4h8m
reviews ClusterIP 10.96.119.54 <none> 9080/TCP 4h8m
可以进入 pod 中查看自动注入的容器,可以看到每个 pod 中自动注入的 sidecar 容器会启动两个进程,一个是 pilot-agent、一个是 envoy
[root@k8s-master01 ~]# kubectl exec -it -n bookinfo details-v1-7b88fb8889-vv6c6 -c istio-proxy bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
istio-proxy@details-v1-7b88fb8889-vv6c6:/$ ps -ef
UID PID PPID C STIME TTY TIME CMD
istio-p+ 1 0 0 04:46 ? 00:00:09 /usr/local/bin/pilot-agent proxy sidecar --domain bookinfo.svc.cluster.local --proxyLogLevel=warning --proxyComponentLogLevel=misc:error --log_outp
istio-p+ 13 1 0 04:46 ? 00:00:48 /usr/local/bin/envoy -c etc/istio/proxy/envoy-rev.json --drain-time-s 45 --drain-strategy immediate --local-address-ip-version v4 --file-flush-inte
istio-p+ 28 0 0 13:46 pts/0 00:00:00 bash
istio-p+ 35 28 0 13:46 pts/0 00:00:00 ps -ef
istio-proxy@details-v1-7b88fb8889-vv6c6:/$ exit
还可以使用 istioctl proxy-status 查看代理状态
[root@k8s-master01 ~]# istioctl proxy-status
NAME CLUSTER CDS LDS EDS RDS ECDS ISTIOD VERSION
details-v1-7b88fb8889-vv6c6.bookinfo Kubernetes SYNCED (5m13s) SYNCED (5m13s) SYNCED (14s) SYNCED (5m13s) IGNORED istiod-846896755-9s7g6 1.26.3
istio-egressgateway-65589498c8-db8pk.istio-system Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) IGNORED IGNORED istiod-846896755-9s7g6 1.26.3
istio-ingressgateway-8f8d6679b-6rnw7.istio-system Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) IGNORED IGNORED istiod-846896755-9s7g6 1.26.3
productpage-v1-79d79d6-tn546.bookinfo Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) SYNCED (12m) IGNORED istiod-846896755-9s7g6 1.26.3
ratings-v1-5b89cf4bbf-2gts5.bookinfo Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) SYNCED (12m) IGNORED istiod-846896755-9s7g6 1.26.3
reviews-v1-ffd57b847-p2mlz.bookinfo Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) SYNCED (12m) IGNORED istiod-846896755-9s7g6 1.26.3
reviews-v2-8cc6fd8dc-ljkzw.bookinfo Kubernetes SYNCED (10m) SYNCED (10m) SYNCED (14s) SYNCED (10m) IGNORED istiod-846896755-9s7g6 1.26.3
reviews-v3-cf48455-4zd6b.bookinfo Kubernetes SYNCED (12m) SYNCED (12m) SYNCED (14s) SYNCED (12m) IGNORED istiod-846896755-9s7g6 1.26.3
通过检查响应中的页面标题,验证应用是否在群集中运行:
[root@k8s-master01 /usr/local/src/istio]# kubectl exec -n bookinfo ratings-v1-5b89cf4bbf-2gts5 -- curl -sS productpage:9080/productpage |grep -o "<title>.*</title>"
<title>Simple Bookstore App</title>
5.2 配置 Bookinfo 外部访问
5.2.1 创建 Ingress Gateway
上面已经成功部署了 Bookinfo 官方示例,但是无法从外部访问,要让他可以对外访问,需要创建一个 ingress gateway,该网关将路径映射到 sidecar 容器。
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:name: bookinfo-gateway
spec:gatewayClassName: istiolisteners:- name: httpport: 80protocol: HTTPallowedRoutes:namespaces:from: Same
---
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:name: bookinfo
spec:parentRefs:- name: bookinfo-gatewayrules:- matches:- path:type: Exactvalue: /productpage- path:type: PathPrefixvalue: /static- path:type: Exactvalue: /login- path:type: Exactvalue: /logout- path:type: PathPrefixvalue: /api/v1/productsbackendRefs:- name: productpageport: 9080
创建
[root@k8s-master01 /usr/local/istio]# kubectl apply -f samples/bookinfo/gateway-api/bookinfo-gateway.yaml -n bookinfo
gateway.gateway.networking.k8s.io/bookinfo-gateway created
httproute.gateway.networking.k8s.io/bookinfo created
默认情况下,Istio 为网关创建 **<font style="color:#DF2A3F;">LoadBalancer</font>**
服务,只需要将域名解析到 该网关的地址即可。
[root@k8s-master01 kiali]# kubectl get svc -n bookinfo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
bookinfo-gateway-istio LoadBalancer 10.96.123.33 192.168.1.2 15021:30913/TCP,80:31210/TCP 43m
details ClusterIP 10.96.68.205 <none> 9080/TCP 61m
productpage ClusterIP 10.96.5.249 <none> 9080/TCP 61m
ratings ClusterIP 10.96.205.37 <none> 9080/TCP 61m
reviews ClusterIP 10.96.200.135 <none> 9080/TCP 61m
5.2.3 检查网关
[root@k8s-master01 /usr/local/istio]# kubectl get gateway -n bookinfo
NAME CLASS ADDRESS PROGRAMMED AGE
bookinfo-gateway istio bookinfo-gateway-istio.bookinfo.svc.cluster.local True 3m1s
可以通过 istioctl-config 命令查看该网关信息
[root@k8s-master01 kiali]# istioctl proxy-config listener bookinfo-gateway-istio-58cdc67897-drf2w -n bookinfo
ADDRESSES PORT MATCH DESTINATION
0.0.0.0 80 ALL Route: http.80
0.0.0.0 15021 ALL Inline Route: /healthz/ready*
0.0.0.0 15090 ALL Inline Route: /stats/prometheus*