Gateway: istio的一種資源類型,Istio Gateway告訴k8s的istio-ingressgateway pods可以打開哪些主機和端口(如下的80是 ingressgateway pod容器中的端口)
gateway是定義了哪些的hosts可以到達ingress pod。
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: galaxygateway spec: selector: istio: ingressgateway servers: - port: number: 80 name: http protocol: HTTP hosts: - "*"
(A) selector:
istio: ingressgateway , 這個label是在ingressgateway的pod中定義的。 用如下命令可以查到
(B) 如下的port 80, 不是指主機的端口,而是ingressgateway 的pod中容器的80端口。
servers:
- port: number: 80 name: http protocol: HTTP
(C) hosts:
"*"
這就在Istio的ingress網關上開辟了一個端口,但是只是接受訪問和流量輸入,當流量到達這個網關時,它還不知道發送到哪里去。
現在我們的網關已准備好接收流量,我們必須告知它將收到的流量發往何處,Istio使用名為“VirtualService”類型配置流量發往何處。
將一個網關列表配置給VirtualService,然后Istio使用VirtualService配置中定義的路由再配置那些網關.
URI路徑前綴匹配/env的將發往指定目標.(注意: 如果有多個virtualservice文件,后面的會覆蓋前面的,所以要把所有的路由信息都配置到一個virtualservice)
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: flaskapp spec: hosts: - "*" gateways: - galaxygateway http: - match: - uri: prefix: /env route: - destination: host: flaskapp.default.svc.cluster.local # 服務的全限量名稱 格式是 服務名.namespace 名稱.svc.cluster.local port: number: 80 # 服務對外暴露的端口。
(A)gateways: - galaxygateway
注意: 這里Virtualservice配置的規則針對galaxygateway其作用,而其屬於網格外部的流量,對於網格內部流量不起作用,如果也想對網格內部的流量也其作用,這里需要把
mesh也加上。
(B) “*”
如果:A:external-IP有值,說明環境中配置了負載均衡器,可以供ingress gateway使用。
B: external-IP的值是none 或者pending,說明環境匯中沒有負載均衡器,這種情況如果想直接訪問gateway,就需要使用服務的node port
Kubernetes的三種外部訪問方式:NodePort、LoadBalancer和Ingress
$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].nodePort}')
$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].nodePort}')
NodePort 是31380,這個值可以改,但是范圍只能是30000 - 32767。
If routing to your application is required to run on 443/80, your Kubernetes cluster must have an external load balancer deployed.
If one is not present, the traffic will be routed to the ingress node port.
如果你的引用需要在443/80端口上跑,那么k8s集群必須有外部的負載均衡器,或者要使用node port。
如何從外部訪問呢:
http://XXXXXXXXXXXXX.com:31380/env/version ()
curl 9.42.18.65:31380/env/version 用IP地址:port的方式訪問,從這個來看,相當於L4的負載均衡器。
curl -I -H Host:<域名>.com http://9.42.18.65:31380/env/version # 在header里加上Host 偽裝成gateway需要的HOSTS
http://9.42.18.65:31380/env/version # 必須指定ingress service 對外暴露的端口 31380
總結:
我上面的例子中,gateway 可以把指定的URL 告訴 ingress pod處理。 virtualservice對指定URL進行 service 調用
1. Gateway: Istio Gateway是負責打開k8s上相關Istio的pods(pod!pod!pod!)上的端口並接收主機的流量,是接收流量與路由之間的關鍵鏈接。
2. VirtualService: Istio VirtualService是“附加”到Gateway上的,並負責定義Gateway應實現的路由。可以將多個VirtualServices連接到Gateway,但不適用於同一個域。