Istio 1.0學習筆記(六):初識Istio Gateway
在Kubernetes中,可以使用Ingress資源將叢集內部的Service暴露到叢集外部,可參見之前整理的 ofollow,noindex" target="_blank">《Kubernetes Ingress實戰》 。 而Istio這個Service Mesh則推薦使用另一個更好的配置模型,即Istio Gateway。Istio Gateway可以允許我們將Istio的功能(如:監控和路由規則)應用到進入叢集的流量。
本篇的實現環境如下:Kubernetes 1.12.0、Istio 1.0.3
kubectl get node -o wide NAMESTATUSROLESAGEVERSIONINTERNAL-IPEXTERNAL-IPOS-IMAGEKERNEL-VERSIONCONTAINER-RUNTIME node1Readyedge,master2d20hv1.12.0192.168.61.11<none>CentOS Linux 7 (Core)3.10.0-693.el7.x86_64docker://18.6.1 node2Readyedge2d20hv1.12.0192.168.61.12<none>CentOS Linux 7 (Core)3.10.0-693.el7.x86_64docker://18.6.1
注意當Istio升級到1.0.3後,發現映象倉庫地址已經由gcr.io修改到docker hub上,省去了過去科學上網把映象搞到的步驟
1.確認Istio Gateway的入口IP和埠
檢視一下Istio Gateway的Service:
kubectl get svc istio-ingressgateway -n istio-system NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE istio-ingressgatewayLoadBalancer10.105.171.39<pending>80:31380/TCP,443:31390/TCP,31400:31400/TCP,15011:31171/TCP,8060:31505/TCP,853:30056/TCP,15030:30693/TCP,15031:32334/TCP20h
當前 EXTERNAL-IP
處於 pending
狀態,我們目前的環境並沒有可用於Istio Ingress Gateway外部的負載均衡器,為了使得可以從外部訪問,通過修改 istio-ingressgateway
這個Service的externalIps,以為當前Kubernetes叢集的kube-proxy啟用了ipvs,所以這個指定一個VIP 192.168.61.9作為externalIp。
kubectl edit svc istio-ingressgateway -n istio-system ...... spec: externalIPs: - 192.168.61.9 ......
kubectl get svc istio-ingressgateway -n istio-system NAMETYPECLUSTER-IPEXTERNAL-IPPORT(S)AGE istio-ingressgatewayLoadBalancer10.105.171.39192.168.61.980:31380/TCP,443:31390/TCP,31400:31400/TCP,15011:31171/TCP,8060:31505/TCP,853:30056/TCP,15030:30693/TCP,15031:32334/TCP20h
此時 EXTERNAL-IP
已經設定為 192.168.61.9
這個VIP了,http相關的埠為80和443
2.使用Istio Gateway接入叢集外部流量
在使用Istio Gateway之前,我們檢視一下它的Pod,通過 istio=ingressgateway
這個Label可以找到它:
kubectl get pod -n istio-system -l istio=ingressgateway -o wide NAMEREADYSTATUSRESTARTSAGEIPNODENOMINATED NODE istio-ingressgateway-7958d776b5-ddpbc1/1Running020h10.244.1.67node2<none>
前面我們在 《Istio 1.0學習筆記(一):在Kubernetes安裝Istio》 中將Istio中自帶的grafana和jaeger暴露到叢集外部時使用的Kubernetes的Ingress,本節我們嘗試切換到Istio Gateway。
之前我們使用Ingress暴露grafana時建立的Ingress如下:
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: grafana namespace: istio-system annotations: nginx.ingress.kubernetes.io/ssl-redirect: "true" nginx.ingress.kubernetes.io/secure-backends: "false" spec: rules: - host: istio-grafana.frognew.com http: paths: - path: / backend: serviceName: grafana servicePort: 3000 tls: - hosts: - istio-grafana.frognew.com secretName: "frognew-com-tls-secret"
這次我們換成建立grafana的Gateway資源:
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: grafana-gateway namespace: istio-system spec: selector: istio: ingressgateway # use Istio default gateway implementation servers: - port: number: 80 name: http protocol: HTTP hosts: - istio-grafana.frognew.com
在前面的學習過程中,使用Istio對服務進行流量管理時需要用到VirtualService,這裡在使用Gateway時也要和VirtualService搭配使用,下面建立grafana的ViertualService:
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: grafana namespace: istio-system spec: hosts: - istio-grafana.frognew.com gateways: - grafana-gateway http: - match: - uri: prefix: / route: - destination: port: number: 3000 host: grafana
我們為grafana建立了一個VirtualService,並建立了到grafana Service的路由規則。
此時在瀏覽器中開啟:http://istio-grafana.frognew.com即可。
下面為Gateway開啟https,先將站點的SSL證書存放到istio-system名稱空間中,要求名稱必須是 istio-ingressgateway-certs
:
kubectl create secret tls istio-ingressgateway-certs --cert=fullchain.pem --key=privkey.pem -n istio-system
修改Gateway:
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: grafana-gateway namespace: istio-system spec: selector: istio: ingressgateway servers: - port: number: 443 name: https protocol: HTTPS tls: mode: SIMPLE serverCertificate: /etc/istio/ingressgateway-certs/tls.crt privateKey: /etc/istio/ingressgateway-certs/tls.key hosts: - istio-grafana.frognew.com
注意證書的位置必須是 /etc/istio/ingressgateway-certs
和建立Secret時的名稱一致。此時,在瀏覽器中可以開啟:https://istio-grafana.frognew.com/