Istio学习
学习参考链接
https://www.bilibili.com/video/BV1zU411U7y1?spm_id_from=333.788.videopod.sections&vd_source=0372d3f32c3f19a6a2676a7529d6698a
https://istio.io/latest/docs/setup/getting-started/
https://github.com/istio/istio
安装
Helm方式安装
helm repo add istio https://istio-release.storage.googleapis.com/charts helm repo update # 在istio-system命名空间安装(自动创建) helm install istio-base istio/base -n istio-system helm install istiod istio/istiod -n istio-system
istioctl方式安装
这里直接从GitHub里下载release包安装
wget https://github.com/istio/istio/releases/download/1.25.2/istio-1.25.2-linux-amd64.tar.gz
tar -zxvf istio-1.25.2-linux-amd64.tar.gz
cd istio-1.25.2
# copy istioctl to an environment path
cp bin/istioctl /usr/local/bin/istioctl
# demo方式安装
istioctl install --set profile=demo -y
istioctl install --help
Examples: # Apply a default Istio installation istioctl install # Enable Tracing istioctl install --set meshConfig.enableTracing=true # Generate the demo profile and don't wait for confirmation istioctl install --set profile=demo --skip-confirmation # To override a setting that includes dots, escape them with a backslash (\). Your shell may require enclosing quotes. istioctl install --set "values.sidecarInjectorWebhook.injectedAnnotations.container\.apparmor\.security\.beta\.kubernetes\.io/istio-proxy=runtime/default"
安装samples 文件夹下 addons 里面的各种插件
![]()
kubectl apply -f samples/addons
注入方式
其核心功能之一是通过注入 Sidecar 代理(如 Envoy)来实现服务间通信的管理和监控
通过kubectl label为特定namespace指定注入sidecar
kubectl label namespace <namespace-name> istio-injection=enabled
通过istioctl kube-inject 命令注入
istioctl kube-inject -f original.yaml > injected.yaml
kubectl apply -f injected.yaml
或
kubectl apply -f <(istioctl kube-inject -f original.yaml)
主要组件
Gateway, VirtualService 和 DestinationRule
Gateway: 功能类似ingress controller, 接收外部流量并转发到内部服务.
VirtualService: 定义路由规则,流量镜像,等
DestinationRule: 定义版本信息,配置熔断,等
sample project bookinfo (https://github.com/woodhead66/istio/tree/master/samples/bookinfo)
Gateway yaml file from sample project bookinfo
kind: Gateway metadata: name: bookinfo-gateway spec: # The selector matches the ingress gateway pod labels. # If you installed Istio using Helm following the standard documentation, this would be "istio=ingress" selector: istio: ingressgateway # use istio default controller servers: - port: number: 8080 name: http protocol: HTTP hosts: - "*" - port: number: 443 name: https protocol: HTTPS hosts: - "bookinfo.example.com" tls: mode: SIMPLE serverCertificate: /etc/istio/credentials/cert.pem privateKey: /etc/istio/credentials/key.pem
VirtualService yaml file from sample project bookinfo
apiVersion: networking.istio.io/v1 kind: VirtualService metadata: name: reviews spec: hosts: - reviews http: - match: - headers: end-user: exact: jason route: - destination: host: reviews subset: v2 - route: - destination: host: reviews subset: v3
DestinationRule yaml file from sample project bookinfo
apiVersion: networking.istio.io/v1 kind: DestinationRule metadata: name: reviews spec: host: reviews trafficPolicy: loadBalancer: simple: RANDOM subsets: - name: v1 labels: version: v1 - name: v2 labels: version: v2 - name: v3 labels: version: v3
接入外部服务组件
ServiceEntry:用于在istio中接入访问管理外部第三方服务
sample project external(https://github.com/woodhead66/istio/tree/master/samples/external)
# This ServiceEntry exposes the hosts needed for installing packages with apt-get. # After applying this file, Istio-enabled pods (configured apt-get) be able to execute # `apt-get upgrade` and `apt-get install`. If this is not installed you may get # "404 Not Found" apiVersion: networking.istio.io/v1 kind: ServiceEntry metadata: name: make-aptget-work spec: hosts: - deb.debian.org - cdn-fastly.deb.debian.org - security.debian.org - archive.ubuntu.com - security.ubuntu.com ports: - number: 80 name: http protocol: HTTP
基于istio的 金丝雀(canary)发布的几种策略
1. 百分比
核心原理:通过配置 VirtualService 中的 weight 字段,按比例将流量分发到不同版本,再逐步放量
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: my-service spec: hosts: - my-service.default.svc.cluster.local http: - route: - destination: host: my-service.default.svc.cluster.local subset: v1 weight: 90 # 90% 流量到稳定版本 v1 - destination: host: my-service.default.svc.cluster.local subset: v2 weight: 10 # 10% 流量到新版本 v2
2. 特征
核心原理:根据请求的 Header、Cookie、URI 等特征,将特定用户(内部用户,区域用户等)或流量导向新版本,再逐步放量
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: my-service spec: hosts: - my-service.default.svc.cluster.local http: - match: # 匹配特定用户到新版本 v2 - headers: user-agent: regex: ".*Chrome.*" # Chrome 用户 - headers: cookie: regex: ".*user=test.*" # 特定 Cookie route: - destination: host: my-service.default.svc.cluster.local subset: v2 - route: # 其他用户到稳定版本 v1 - destination: host: my-service.default.svc.cluster.local subset: v1
3. 流量镜像
核心原理:现将流量镜像到新版本,同时监控新版本上的情况,再分发一定比例将流量到新版本,再逐步放量。
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: my-service spec: hosts: - my-service.default.svc.cluster.local http: - route: - destination: host: my-service.default.svc.cluster.local subset: v1 weight: 90 # 90% 流量到稳定版 v1
- destination:
host: my-service.default.svc.cluster.local
subset: v2
weight: 10 # 10% 实际流量到 v2
mirror: # 镜像到新版 v2
host: my-service.default.svc.cluster.local
subset: v2
mirrorPercentage:
value: 100 # 100% 镜像到 V2(可调整为 10%、50% 等)
istio的可视化

Kiali
kubectl get svc -n istio-system kiali

jaeger 追踪
kubectl get svc -n istio-system -l app=jaeger


问题一:在有istio 的k8s集群中,假设有20个微服务,其中5个是对外网的服务,另外15个是内部服务,这种情况下,我需要怎么配置 Gateway,virtualService,destinationRule? 分别配置多少个?
回答一:
一、核心配置组件与数量规划
二、具体配置方案
1. Gateway 配置(边缘入口)
2. VirtualService 配置(路由规则)
外部服务(绑定 Gateway)
内部服务(仅网格内访问)
3. DestinationRule 配置(流量策略)
三、配置数量总结
四、最佳实践
五、示例:完整配置模板

浙公网安备 33010602011771号