Istio 服务网格安装教程:从零开始部署

Istio 是目前最流行的服务网格(Service Mesh)解决方案,为微服务架构提供流量管理、安全性和可观测性。本教程将带你从零开始在 Kubernetes 集群上安装 Istio。

环境准备

在开始之前,确保你已经具备以下条件:

  • 一个运行中的 Kubernetes 集群(v1.26 或更高版本)
  • kubectl 命令行工具已配置好集群访问
  • 集群至少有 4GB 可用内存

第一步:下载 Istio

首先下载最新版本的 Istio。我们使用官方脚本自动下载:

# 下载最新版 Istio
curl -L https://istio.io/downloadIstio | sh -

# 或指定版本下载
curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.24.0 sh -

下载完成后,进入 Istio 目录并将 istioctl 添加到 PATH:

cd istio-1.24.0
export PATH=$PWD/bin:$PATH

第二步:安装 Istio

Istio 提供多种配置 profile,适用于不同场景:

  • demo:包含所有组件,适合学习和演示
  • default:生产环境推荐配置
  • minimal:最小化安装,仅包含核心组件

对于初学者,推荐使用 demo profile:

# 使用 demo profile 安装
istioctl install --set profile=demo -y

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

等待安装完成,验证 Istio 组件是否正常运行:

kubectl get pods -n istio-system

你应该看到类似以下输出(所有 Pod 状态为 Running):

NAME                                    READY   STATUS    RESTARTS   AGE
istio-egressgateway-xxx                 1/1     Running   0          2m
istio-ingressgateway-xxx                1/1     Running   0          2m
istiod-xxx                              1/1     Running   0          2m

第三步:启用自动 Sidecar 注入

Istio 通过 Envoy sidecar 代理来管理服务间的流量。为命名空间添加标签以启用自动注入:

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

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

第四步:部署示例应用(可选)

Istio 自带一个 Bookinfo 示例应用,可以用来验证安装:

# 部署 Bookinfo 示例
kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

# 等待 Pod 启动
kubectl get pods

# 验证应用是否正常
kubectl exec "$(kubectl get pod -l app=ratings -o jsonpath={.items[0].metadata.name})" -c ratings -- curl -s productpage:9080/productpage | grep -o ".*"

第五步:安装可观测性组件

Istio 提供了丰富的可观测性工具,包括 Kiali、Prometheus、Grafana 和 Jaeger:

# 安装 Kiali(服务网格可视化仪表板)
kubectl apply -f samples/addons/kiali.yaml

# 安装 Prometheus(指标收集)
kubectl apply -f samples/addons/prometheus.yaml

# 安装 Grafana(指标可视化)
kubectl apply -f samples/addons/grafana.yaml

# 安装 Jaeger(分布式追踪)
kubectl apply -f samples/addons/jaeger.yaml

访问 Kiali 仪表板:

istioctl dashboard kiali

常见问题排查

Pod 无法启动

检查 Pod 事件和日志:

kubectl describe pod  -n istio-system
kubectl logs  -n istio-system

Sidecar 注入失败

确认命名空间标签正确,并检查 istiod 日志:

kubectl logs -l app=istiod -n istio-system

卸载 Istio

如需卸载 Istio:

# 卸载 Istio
istioctl uninstall --purge

# 删除命名空间
kubectl delete namespace istio-system

总结

通过以上步骤,你已经成功在 Kubernetes 集群上安装了 Istio 服务网格。接下来可以探索 Istio 的流量管理、安全策略和可观测性功能。

推荐阅读官方文档了解更多高级功能:https://istio.io/latest/docs/

发表回复

后才能评论