Istio 完整部署教程 - 从安装到配置实战

前言

Istio 是最流行的 Service Mesh 实现,本文详细介绍 Istio 在 Kubernetes 上的完整部署流程,包含安装、配置、验证和实际应用示例。

一、环境准备

1.1 系统要求

组件 版本要求 推荐配置
Kubernetes 1.25+ 1.28+
CPU 4 核 8 核+
内存 8GB 16GB+

1.2 安装 istioctl

# 下载并安装 istioctl
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.20.0 sh -

# 添加到 PATH
export PATH=$PWD/bin:$PATH

# 验证安装
istioctl version

二、安装 Istio

2.1 选择安装 Profile

# 查看可用 Profile
istioctl profile list

# 输出:
# - default     # 默认配置,适合生产
# - demo        # 演示配置,适合学习
# - minimal     # 最小化安装
# - remote      # 远程集群
# - empty       # 空配置,完全自定义
# - preview     # 预览新功能

2.2 安装 Istio

# 使用 demo profile 安装(学习/测试)
istioctl install --set profile=demo -y

# 使用 default profile 安装(生产环境)
istioctl install --set profile=default -y

# 自定义配置安装
istioctl install \
  --set profile=minimal \
  --set values.global.proxy.resources.requests.cpu=100m \
  --set values.global.proxy.resources.requests.memory=128Mi \
  --set values.global.proxy.resources.limits.cpu=500m \
  --set values.global.proxy.resources.limits.memory=256Mi \
  -y

2.3 验证安装

# 检查 Istio 组件状态
kubectl get pods -n istio-system

# 应该看到:
# istiod-xxxxx     1/1   Running
# istio-ingressgateway-xxxxx   1/1   Running
# istio-egressgateway-xxxxx    1/1   Running

# 验证 Istio 安装
istioctl verify-install

# 检查 CRD
kubectl get crds | grep istio.io

三、启用 Sidecar 注入

# 为命名空间启用自动注入
kubectl label namespace default istio-injection=enabled

# 验证标签
kubectl get namespace -L istio-injection

# 手动注入(可选)
istioctl kube-inject -f deployment.yaml -o deployment-injected.yaml

四、部署示例应用

4.1 部署 Bookinfo 应用

# 部署示例应用
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/bookinfo/platform/kube/bookinfo.yaml

# 检查 Pod 状态(每个 Pod 应该有 2 个容器:app + envoy)
kubectl get pods

# 查看 Sidecar 注入情况
kubectl get pods -o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[*].name}{"\n"}{end}'

4.2 配置网关

# 配置 Istio 网关
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/bookinfo/networking/bookinfo-gateway.yaml

# 获取访问地址
export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
export GATEWAY_URL=$INGRESS_HOST:$INGRESS_PORT

# 访问应用
curl -s -o /dev/null -w "%{http_code}" http://$GATEWAY_URL/productpage

五、流量管理配置

5.1 配置 VirtualService

# 配置路由规则
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
      weight: 90
    - destination:
        host: productpage
        subset: v2
      weight: 10

5.2 配置 DestinationRule

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  trafficPolicy:
    loadBalancer:
      simple: ROUND_ROBIN
    connectionPool:
      tcp:
        maxConnections: 100
      http:
        h2UpgradePolicy: UPGRADE
        http1MaxPendingRequests: 100
        http2MaxRequests: 1000
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2

六、安全配置

6.1 启用 mTLS

# 配置网格级别的 mTLS
apiVersion: security.istio.io/v1beta1
kind: PeerAuthentication
metadata:
  name: default
  namespace: istio-system
spec:
  mtls:
    mode: STRICT

6.2 配置授权策略

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: require-jwt
  namespace: default
spec:
  selector:
    matchLabels:
      app: productpage
  action: ALLOW
  rules:
  - from:
    - source:
        requestPrincipals: ["*"]
    to:
    - operation:
        methods: ["GET", "POST"]

七、可观测性配置

7.1 安装 Kiali(服务网格可视化)

# 安装 Kiali
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/addons/kiali.yaml

# 访问 Kiali
istioctl dashboard kiali

7.2 安装 Prometheus 和 Grafana

# 安装监控组件
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/addons/prometheus.yaml
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/addons/grafana.yaml

# 访问 Grafana
istioctl dashboard grafana

# 访问 Prometheus
istioctl dashboard prometheus

7.3 安装 Jaeger(分布式追踪)

# 安装 Jaeger
kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.20/samples/addons/jaeger.yaml

# 访问 Jaeger
istioctl dashboard jaeger

八、故障排查

8.1 常见问题

# 检查 Sidecar 注入
kubectl get pods -n default -o jsonpath='{range .items[*]}{.metadata.name}: {.spec.containers[*].name}{"\n"}{end}'

# 查看 Envoy 配置
istioctl proxy-config all .

# 查看 Envoy 日志
kubectl logs  -c istio-proxy

# 分析配置
istioctl analyze

# 检查连通性
istioctl proxy-status

总结

本文详细介绍了 Istio 的完整部署流程,从安装、配置到实际应用。通过 Istio,你可以轻松实现服务网格的流量管理、安全和可观测性功能。


Service Mesh 系列文章 2/10

发表回复

后才能评论