- 离线安装
下载连接:
https://github.com/istio/istio/releases/download/1.25.2/istio-1.25.2-linux-amd64.tar.gz
tar -zxf istio-1.25.2-linux-amd64.tar.gz && cd istio-1.25.2
cp bin/istioctl /usr/local/bin/
# 生成 Istio 的安装清单文件
istioctl manifest generate --set profile=minimal \
--set values.meshConfig.enableAutoMtls=false \
--set components.pilot.k8s.env[0].name="PILOT_HTTP10" \
--set components.pilot.k8s.env[0].value="支持http1.0" \
> istio-minimal.yaml
sed -i 's/支持http1.0/"1"/g' istio-minimal.yaml
# 手动下载镜像推送到本私有镜像仓库
grep "image: " istio-minimal.yaml
# 修改镜像仓库地址
sed -i "s/docker.io/wellregistry:5000/g" istio-minimal.yaml
# 启动istiod
kubectl create ns istio-system
kubectl apply -f istio-minimal.yaml
# 给命名空间添加标签,指示 Istio 在部署应用的时候,自动注入 Envoy Sidecar 代理
kubectl label namespace default istio-injection=enabled
# 在yaml文件的spec.template.metadata.lables下添加标签,开启或关闭注入Sidecar
sidecar.istio.io/inject: "true"
# istio默认配置大概支持1000个svc、2000个sidecar、70000个request请求
- 卸载
istioctl x uninstall --purge
- 流量控制
通过VirtualService与DestinationRule根据权重控制流量比例。请求到达服务网格,VirtualService 根据 hosts 和路由规则(如路径、Header)匹配请求将流量指向 DestinationRule 中定义的子集(subset),DestinationRule 根据子集的标签(如 version: product)选择具体的 Pod DestinationRule中的spec.subets.labels对应Deployment中的metadata.labels
apiVersion: v1
kind: Service
metadata:
name: tmc
namespace: cloud2
labels:
name: tmc
spec:
type: NodePort
ports:
- name: tcp-tmc # 名称包含tcp才能与vs的路由类型匹配
protocol: TCP
port: 8080
nodePort: 32004
selector:
app: tmc
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
namespace: cloud2
name: tmc
spec:
hosts:
- tmc # 目标服务名称
tcp: # 路由类型
- match:
- port: 8080
route: # 定义 tcp 路由
- destination:
host: tmc # 目标服务名称
subset: prod # 在DestinationRule中定义的子集
weight: 50
- destination:
host: tmc # 目标服务名称
subset: gray # 在DestinationRule中定义的子集
weight: 50
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
namespace: cloud2
name: tmc
spec:
host: tmc # 目标服务名称
subsets:
- name: prod # 子集名称
labels:
version: prod # 按标签选择Pod
- name: gray # 子集名称
labels:
version: gray # 按标签选择Pod
deployment示例:
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: cloud2
name: tmc
labels:
app: tmc
version: prod
spec:
replicas: 1
selector:
matchLabels:
app: tmc
version: prod
template:
metadata:
labels:
app: tmc
version: prod
sidecar.istio.io/inject: "true"
spec:
containers:
...
