查看原文
其他

Istio路由基础教程

Mete Atamel 几米宋 2022-09-06

作者:Mete Atamel 译者:Jianjun 原文:https://medium.com/google-cloud/istio-routing-basics-14feab3c040e

当学习像Istio这样的新技术时,我推荐看一看项目自带的示例。Istio包含了一些示例程序,但都有各种各样的不足。比如说BookInfo就是很好的一个应用。但是对我来说,它太冗长,服务太多,而且文档似乎专注于管理BookInfo应用程序,而不是从头构建。另外还有一个小一点的示例-helleworld,但是它仅关注于自动伸缩。

在这篇文章中,我想从基础讲起,并向您展示如何从头开始构建支持Istio的“HelloWorld”应用程序。要记住的一点是,Istio只管理您应用的流量,应用程序生命周期由底层平台Kubernetes管理。因此,您需要了解容器和Kubernetes基础知识,并且需要了解Istio 路由原语,例如Gateway,VirtualService,DestinationRule。我假设大多数人都知道容器和Kubernetes基础知识。我将在本文中专注于介绍Istio 路由。

基本步骤

这些大致是创建Istio“HelloWorld”应用程序的步骤:

  1. 创建一个Kubernetes集群并安装带有自动sidecar注入的Istio。

  2. 使用您选择的语言创建一个HelloWorld应用程序,基于这个程序创建一个Docker镜像并将其推送到公共图像存储库。

  3. 为容器创建Kubernetes Deployment和Service。

  4. 创建Gateway以启用到群集的HTTP(S)流量。

  5. 创建VirtualService以通过Gateway公开Kubernetes服务。

  6. (可选)如果要创建应用程序的多个版本,请创建DestinationRule以定义可从VirtualService引用的子集。

  7. (可选)如果要从服务网格调用外部服务,请创建ServiceEntry。

我不会在本文中介绍步骤1和2,因为它们不是Istio特有的。 如果您需要有关这些步骤的帮助,可以查看我在本文末尾提到的codelabs。 第3步也不是Istio特定的,但它是其他一切的先决条件,所以我们从那开始。

Deployment和Service

正如我所提到的,应用程序生命周期由Kubernetes管理。 因此,您需要从创建Kubernetes Deployment和Service开始。我有一个容器化的ASP.NET核心应用程序,容器镜像我已经推送到谷歌容器注册表。 让我们从创建一个aspnetcore.yaml文件开始:

  1. apiVersion: v1

  2. kind: Service

  3. metadata:

  4.  name: aspnetcore-service

  5.  labels:

  6.    app: aspnetcore

  7. spec:

  8.  ports:

  9.  - port: 8080

  10.    name: http

  11.  selector:

  12.    app: aspnetcore

  13. ---

  14. apiVersion: extensions/v1beta1

  15. kind: Deployment

  16. metadata:

  17.  name: aspnetcore-v1

  18. spec:

  19.  replicas: 1

  20.  template:

  21.    metadata:

  22.      labels:

  23.        app: aspnetcore

  24.        version: v1

  25.    spec:

  26.      containers:

  27.      - name: aspnetcore

  28.        image: gcr.io/istio-project-212517/hello-dotnet:v1

  29.        imagePullPolicy: Always #IfNotPresent

  30.        ports:

  31.        - containerPort: 8080

创建deployment和service:

  1. $ kubectl apply -f aspnetcore.yaml

  2. service "aspnetcore-service" created

  3. deployment.extensions "aspnetcore-v1" created

到现在为止还没有专门讲到Istio。

Gateway

我们现在可以回到Istio路由了。首先,我们需要为服务网格启用HTTP / HTTPS流量。 为此,我们需要创建一个Gateway。 Gateway描述了在网络边缘运行的负载均衡器,用于接收传入或传出的HTTP / TCP连接。

让我们创建一个aspnetcore-gateway.yaml文件:

  1. apiVersion: networking.istio.io/v1alpha3

  2. kind: Gateway

  3. metadata:

  4.  name: aspnetcore-gateway

  5. spec:

  6.  selector:

  7.    istio: ingressgateway # use istio default controller

  8.  servers:

  9.  - port:

  10.      number: 80

  11.      name: http

  12.      protocol: HTTP

  13.    hosts:

  14.    - "*"

创建Gateway:

  1. $ kubectl apply -f aspnetcore-gateway.yaml

  2. gateway.networking.istio.io "aspnetcore-gateway" created

我们已经为集群启用了HTTP流量。 我们需要将之前创建的Kubernetes服务映射到Gateway。 我们将使用VirtualService执行此操作。

VirtualService

VirtualService实际上将Kubernetes服务连接到Istio网关。 它还可以执行更多操作,例如定义一组流量路由规则,以便在主机被寻址时应用,但我们不会深入介绍这些细节。

让我们创建一个aspnetcore-virtualservice.yaml文件:

  1. apiVersion: networking.istio.io/v1alpha3

  2. kind: VirtualService

  3. metadata:

  4.  name: aspnetcore-virtualservice

  5. spec:

  6.  hosts:

  7.  - "*"

  8.  gateways:

  9.  - aspnetcore-gateway

  10.  http:

  11.  - route:

  12.    - destination:

  13.        host: aspnetcore-service

请注意,VirtualService与特定网关绑定,并定义引用Kubernetes服务的主机。

创建VirtualService:

  1. $ kubectl apply -f aspnetcore-virtualservice.yaml

  2. virtualservice.networking.istio.io "aspnetcore-virtualservice" created

测试app v1版本

现在可以开始测试我们的应用了,首先需要拿到Istio Ingress Gateway的外部IP地址。

  1. $ kubectl get svc istio-ingressgateway -n istio-system

  2. NAME                   TYPE           CLUSTER-IP     EXTERNAL-IP                                                                                                        

  3. istio-ingressgateway   LoadBalancer   10.31.247.41   35.240.XX.XXX

当我们用浏览器访问外部地址时,我们应该看到HelloWorld ASP.NET Core程序。

DestinationRule

在某些时候,您希望将应用更新为新版本。 也许你想分割两个版本之间的流量。 您需要创建一个DestinationRule来定义那些版本,在Istio中称为子集。

首先,更新aspnetcore.yaml文件,用v2版本的容器来定义v2的部署(Deployment):

  1. apiVersion: v1

  2. kind: Service

  3. metadata:

  4.  name: aspnetcore-service

  5.  labels:

  6.    app: aspnetcore

  7. spec:

  8.  ports:

  9.  - port: 8080

  10.    name: http

  11.  selector:

  12.    app: aspnetcore

  13. ---

  14. apiVersion: extensions/v1beta1

  15. kind: Deployment

  16. metadata:

  17.  name: aspnetcore-v1

  18. spec:

  19.  replicas: 1

  20.  template:

  21.    metadata:

  22.      labels:

  23.        app: aspnetcore

  24.        version: v1

  25.    spec:

  26.      containers:

  27.      - name: aspnetcore

  28.        image: gcr.io/istio-project-212517/hello-dotnet:v1

  29.        imagePullPolicy: Always #IfNotPresent

  30.        ports:

  31.        - containerPort: 8080

  32. ---

  33. apiVersion: extensions/v1beta1

  34. kind: Deployment

  35. metadata:

  36.  name: aspnetcore-v2

  37. spec:

  38.  replicas: 1

  39.  template:

  40.    metadata:

  41.      labels:

  42.        app: aspnetcore

  43.        version: v2

  44.    spec:

  45.      containers:

  46.      - name: aspnetcore

  47.        image: gcr.io/istio-project-212517/hello-dotnet:v2

  48.        imagePullPolicy: Always #IfNotPresent

  49.        ports:

  50.        - containerPort: 8080

创建一个新的部署(Deployment):

  1. $ kubectl apply -f aspnetcore.yaml

  2. service "aspnetcore-service" unchanged

  3. deployment.extensions "aspnetcore-v1" unchanged

  4. deployment.extensions "aspnetcore-v2" created

如果刷新浏览器,你可以看到VirtualService 在v1 和v2 版本之间切换:

这个结果是预料之中的,因为这两个版本都暴露在相同的Kubernetes服务之后:aspnetcore-service。

如果您想将服务仅限于v2该怎么办? 可以通过在VirtualService中指定子集来完成,但我们需要首先在DestinationRules中定义这些子集。 DestinationRule本质上将标签映射到Istio子集。

创建一个aspnetcore-destinationrule.yaml文件:

  1. apiVersion: networking.istio.io/v1alpha3

  2. kind: DestinationRule

  3. metadata:

  4.  name: aspnetcore-destinationrule

  5. spec:

  6.  host: aspnetcore-service

  7.  trafficPolicy:

  8.    tls:

  9.      mode: ISTIO_MUTUAL

  10.  subsets:

  11.  - name: v1

  12.    labels:

  13.      version: v1

  14.  - name: v2

  15.    labels:

  16.      version: v2

创建DestinnationRule:

  1. $ kubectl apply -f aspnetcore-destinationrule.yaml

  2. destinationrule.networking.istio.io "aspnetcore-destinationrule" created

现在,你可以在VirtualService指向v2子集:

  1. apiVersion: networking.istio.io/v1alpha3

  2. kind: VirtualService

  3. metadata:

  4.  name: aspnetcore-virtualservice

  5. spec:

  6.  hosts:

  7.  - "*"

  8.  gateways:

  9.  - aspnetcore-gateway

  10.  http:

  11.  - route:

  12.    - destination:

  13.        host: aspnetcore-service

  14.         subset: v2

更新VirtualService:

  1. $ kubectl apply -f aspnetcore-virtualservice.yaml

  2. virtualservice.networking.istio.io "aspnetcore-virtualservice" configured

现在再刷新浏览器,你应该只会看到v2版本的内容了。

ServiceEntry

最后我大概提一下ServiceEntry. 所有外部流量在Istio中都是默认被阻断了的,如果你需要启用外部流量就需要创建一个ServiceEntry来列出所有的已经启用外部流量的协议和主机。你可以从这里了解更多信息,我在这篇文章中就不多做阐述了。

希望这篇文章对你有所助益!如果你还想了解更多,这里有一个系列更详细的阐述了这篇文章提到的所有概念和解释:

  • Deploy ASP.NET Core app to Google Kubernetes Engine with Istio (Part 1)

  • Deploy ASP.NET Core app to Google Kubernetes Engine with Istio (Part 2)

相关阅读推荐

教程|使用Istio实现一个Service Mesh以简化微服务间的通信模式

推荐|目前最完整的Istio Service Mesh示例教程汇总

VMWare 开源团队讲述服务网格的未来Part 2:Istio 1.0之后何去何从?

云端设计平台Coohom在生产环境中使用istio的经验与实践

Istio微服务平台集成实践

Istio免费直播课程推荐

本课程来自 IBM 微课程,通过视频直播的方式帮助您快速了解 Istio,每周一期。

  • 11月1日 Istio初探

  • 11月8日 Istio上手

  • 11月15日 Istio的安全管理

  • 11月22日 Envoy

  • 11月29日 使用Istio来监控和可视化微服务

  • 12月6日 Istio mixer - 基本概念,策略、遥测与扩展

  • 12月13日 Istio跨云管理方案解析

  • 12月20日 Istio使用案例:Serverless 平台knative

  • 详情请参考:IBM微讲堂之年度大戏《Istio系列》

点击【阅读原文】跳转到ServiceMesher网站上浏览可以查看文中的链接。

  • SOFAMesh(https://github.com/alipay/sofa-mesh)基于Istio的大规模服务网格解决方案

  • SOFAMosn(https://github.com/alipay/sofa-mosn)使用Go语言开发的高性能Sidecar代理

合作社区

参与社区

以下是参与ServiceMesher社区的方式,最简单的方式是联系我!

  • 加入微信交流群:关注本微信公众号后访问主页右下角有获取联系方式按钮,添加好友时请注明姓名-公司

  • 社区网址:http://www.servicemesher.com

  • Slack:https://servicemesher.slack.com (需要邀请才能加入)

  • GitHub:https://github.com/servicemesher

  • Istio中文文档进度追踪:https://github.com/servicemesher/istio-official-translation

  • Twitter: https://twitter.com/servicemesher

  • 提供文章线索与投稿:https://github.com/servicemesher/trans


您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存