官网文档:api
https://istio.io/docs/reference/config/networking/#VirtualService浏览器
在学习像Istio这样的新技术时,看一下示例应用程序老是一个好主意。 Istio repo有一些示例应用程序,但它们彷佛有各类不足。 文档中的BookInfo是一个很好的示例。 可是,对于我而言,它太冗长,服务太多,并且文档彷佛专一于管理BookInfo应用程序,而不是从头开始构建。 有一个较小的helloworld例子,但它更多的是关于自动伸缩而不是其余。 在这篇文章中,我想介绍一下基础知识,并展现如何从头开始构建支持Istio的“HelloWorld”应用程序。 要记住的一点是,Istio只管理你应用的流量。 在这种状况下,应用程序生命周期由底层平台Kubernetes管理。 所以,你须要了解容器和Kubernetes基础知识,而且须要了解Istio Routing原语,例如Gateway,VirtualService,DestinationRule。 我假设大多数人都知道容器和Kubernetes基础知识。 我将在本文中专一于Istio Routing。 # 基础步骤 如下这些大体就是你须要遵循的,以得到Istio的“HelloWorld”应用程序的步骤:
建立一个Kubernetes集群并安装带有sidecare自动注入的Istio。
使用你选择的语言建立Hello World应用程序,建立Docker镜像并将其推送到公共镜像仓库。
为你的容器建立Kubernetes Deployment和Service。
建立Gateway以启用到群集的HTTP(S)流量。
建立VirtualService,经过Gateway公开Kubernetes服务。
(可选)若是要建立多个版本应用程序,请建立DestinationRule以定义可从VirtualService引用的subsets。
(可选)若是要在服务网格外部调用其余外部服务,请建立ServiceEntry。
我不会在本文中介绍步骤1和2,由于它们不是特定于Istio的。 若是您须要有关这些步骤的帮助,能够查看我在本文末尾提到的文章。 第3步也不是Istio特定的,但它是其余一切的先决条件,因此让咱们从那开始。 # Deployment和Service 正如我所提到的,应用程序生命周期由Kubernetes管理。 所以,您须要从建立Kubernetes Deployment和Service开始。 个人状况以下,我有一个容器化的ASP.NET核心应用程序,其镜像我已经推送到谷歌镜像仓库。 让咱们从建立一个aspnetcore.yaml
文件开始:
apiVersion: v1
kind: Service
metadata:
name: aspnetcore-service
labels:
app: aspnetcore
spec:
ports:app
- port: 8080
name: http
selector:
app: aspnetcore
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: aspnetcore-v1
spec:
replicas: 1
template:
metadata:
labels:
app: aspnetcore
version: v1
spec:
containers:
- name: aspnetcore
image: gcr.io/istio-project-212517/hello-dotnet:v1
imagePullPolicy: Always #IfNotPresent
ports:
- containerPort: 8080
建立Deployment和Service:
$ kubectl apply -f aspnetcore.yaml
service "aspnetcore-service" created
deployment.extensions "aspnetcore-v1" created
到目前为止没有任何特定的针对Istio的内容。 # Gateway 咱们如今能够开始研究Istio Routing。 首先,咱们须要为服务网格启用HTTP/HTTPS流量。 为此,咱们须要建立一个网关。 Gateway描述了在边缘运行的负载均衡,用于接收传入或传出的HTTP/TCP链接。 让咱们建立一个aspnetcore-gateway.yaml
文件:
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: aspnetcore-gateway
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
建立Gateway:
$ kubectl apply -f aspnetcore-gateway.yaml
gateway.networking.istio.io "aspnetcore-gateway" created
此时,咱们为集群启用了HTTP流量。 咱们须要将以前建立的Kubernetes服务映射到Gateway。 咱们将使用VirtualService执行此操做。 # VirtualService VirtualService实际上将Kubernetes服务链接到Istio网关。 它还能够执行更多操做,例如定义一组流量路由规则,以便在主机被寻址时应用,但咱们不会深刻了解这些细节。 让咱们建立一个aspnetcore-virtualservice.yaml
文件:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: aspnetcore-virtualservice
spec:
hosts:
- "*"
gateways:
- aspnetcore-gateway
http:
- route:
destination:
host: aspnetcore-service
请注意,VirtualService与特定网关绑定,并定义引用Kubernetes服务的主机。 建立VirtualService:
$ kubectl apply -f aspnetcore-virtualservice.yaml
virtualservice.networking.istio.io "aspnetcore-virtualservice" created负载均衡
测试V1版本APP 咱们准备测试咱们的应用程序了。 咱们须要获取Istio Ingress Gateway的IP地址:
$ kubectl get svc istio-ingressgateway -n istio-system
NAME TYPE CLUSTER-IP EXTERNAL-IP
istio-ingressgateway LoadBalancer 10.31.247.41 35.240.XX.XXX
当咱们在浏览器中打开EXTERNAL-IP
时,咱们应该看到HelloWorld ASP.NET Core应用程序:
1.pngide
DestinationRule 在某些时候,你但愿将应用更新为新版本。 也许你想分割两个版本之间的流量。你须要建立一个DestinationRule来定义是哪些版本,在Istio中称为subset。 首先,更新aspnetcore.yaml文件以使用v2版本的容器定义v2的deployment:
apiVersion: v1
kind: Service
metadata:
name: aspnetcore-service
labels:
app: aspnetcore
spec:
ports:
- port: 8080
name: http
selector:
app: aspnetcore
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: aspnetcore-v1
spec:
replicas: 1
template:
metadata:
labels:
app: aspnetcore
version: v1
spec:
containers:
- name: aspnetcore
image: gcr.io/istio-project-212517/hello-dotnet:v1
imagePullPolicy: Always #IfNotPresent
ports:
- containerPort: 8080
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: aspnetcore-v2
spec:
replicas: 1
template:
metadata:
labels:
app: aspnetcore
version: v2
spec:
containers:
- name: aspnetcore
image: gcr.io/istio-project-212517/hello-dotnet:v2
imagePullPolicy: Always #IfNotPresent
ports:
- containerPort: 8080
建立新的Deployment:
$ kubectl apply -f aspnetcore.yaml
service "aspnetcore-service" unchanged
deployment.extensions "aspnetcore-v1" unchanged
deployment.extensions "aspnetcore-v2" created
若是使用EXTERNAL-IP刷新浏览器,您将看到应用程序的v1和v2版本交替出现:
2.png
3.png
这是符合预期的,由于两个版本都暴露在相同的Kubernetes服务以后:aspnetcore-service。 若是您想将服务仅指向v2,该怎么办? 这能够经过在VirtualService中指定subset来完成,但咱们须要首先在DestinationRules中定义这些subset。 DestinationRule本质上是将标签映射到Istio的subset。 建立一个aspnetcore-destinationrule.yaml
文件:
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: aspnetcore-destinationrule
spec:
host: aspnetcore-service
trafficPolicy:
tls:
mode: ISTIO_MUTUAL
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
建立DestinationRule:
$ kubectl apply -f aspnetcore-destinationrule.yaml
destinationrule.networking.istio.io "aspnetcore-destinationrule" created
如今你能够从VirtualService来引用v2 subset:
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: aspnetcore-virtualservice
spec:
hosts:
- "*"
gateways:
- aspnetcore-gateway
http:
- route:
- destination: host: aspnetcore-service subset: v2 更新VirtualService: $ kubectl apply -f aspnetcore-virtualservice.yaml virtualservice.networking.istio.io "aspnetcore-virtualservice" configured 若是你如今继续浏览EXTERNAL-IP,您如今应该只能看到应用程序的v2版本。 # ServiceEntry 我想在Istio Routing中提到的最后一件事是ServiceEntry。默认状况下,Istio中的全部外部流量都被阻止。若是要启用外部流量,则须要建立ServiceEntry以列出为外部流量启用的协议和主机。我不会在这篇文章中展现一个例子,但你能够在这里阅读更多相关内容。 但愿这篇文章对你有用!若是您想了解更多信息,可使用codelab系列如下两部分,其中全部这些概念和更多内容将在逐步的详细教程中进行说明: 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 Routing Basics(翻译:kelvinji2009)