在这篇文章中,我想介绍一下基础知识,并展示如何从头开始构建支持Istio的“HelloWorld”应用程序。 要记住的一点是,Istio只管理你应用的流量。 在这种情况下,应用程序生命周期由底层平台Kubernetes管理。 因此,你需要了解容器和Kubernetes基础知识,并且需要了解Istio Routing原语,例如Gateway,VirtualService,DestinationRule。 我假设大多数人都知道容器和Kubernetes基础知识。 我将在本文中专注于Istio Routing。
基础步骤
创建一个Kubernetes集群并安装带有sidecare自动注入的Istio。
使用你选择的语言创建Hello World应用程序,创建Docker镜像并将其推送到公共镜像仓库。
为你的容器创建Kubernetes Deployment和Service。
创建Gateway以启用到群集的HTTP(S)流量。
创建VirtualService[2],通过Gateway公开Kubernetes服务。
(可选)如果要创建多个版本应用程序,请创建DestinationRule[3]以定义可从VirtualService引用的subsets。
(可选)如果要在服务网格外部调用其他外部服务,请创建ServiceEntry[4]。
我不会在本文中介绍步骤1和2,因为它们不是特定于Istio的。 如果您需要有关这些步骤的帮助,可以查看我在本文末尾提到的文章。 第3步也不是Istio特定的,但它是其他一切的先决条件,所以让我们从那开始。
Deployment和Service
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
创建Deployment和Service:
$ kubectl apply -f aspnetcore.yaml
service "aspnetcore-service" created
deployment.extensions "aspnetcore-v1" created
到目前为止没有任何特定的针对Istio的内容。
Gateway
让我们创建一个 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
让我们创建一个 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
$ 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应用程序:
![640](https://i-blog.csdnimg.cn/blog_migrate/387db06f4a2adb96a57dc7fdfe6ad92d.jpeg)
DestinationRule
首先,更新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版本交替出现:
![640](https://i-blog.csdnimg.cn/blog_migrate/387db06f4a2adb96a57dc7fdfe6ad92d.jpeg)
![640](https://i-blog.csdnimg.cn/blog_migrate/d676f8d7d705ede9ebd83b3ee368c8b9.jpeg)
这是符合预期的,因为两个版本都暴露在相同的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
希望这篇文章对你有用!如果您想了解更多信息,可以使用codelab系列以下两部分,其中所有这些概念和更多内容将在逐步的详细教程中进行说明:
https://codelabs.developers.google.com/codelabs/cloud-istio-aspnetcore-part1
https://codelabs.developers.google.com/codelabs/cloud-istio-aspnetcore-part2
https://github.com/istio/istio/tree/master/samples
https://istio.io/docs/reference/config/istio.networking.v1alpha3/#VirtualService
https://istio.io/docs/reference/config/istio.networking.v1alpha3/#DestinationRule
https://istio.io/docs/reference/config/istio.networking.v1alpha3/#ServiceEntry