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號