官網文檔: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)