文章主要介紹如何把一個簡單的HelloWebApp裝在Istio+K8S環境下
下面是基本步驟:
- 創建一個 Kubernetes 集群並安裝帶有 sidecare 自動注入的 Istio。
- 使用您選擇的語言創建 Hellohttp 應用程序,創建 Docker 鏡像並將其推送到公共鏡像倉庫。
- 為你的容器創建 Kubernetes deployment 和 service。
- 為相應的Deployment inject sidecar
- 創建 Gateway 以啟用到群集的 HTTP(S)流量。
- 創建 Gataway 的VirtualService,通過 Gateway 公開 Kubernetes 服務。
- 如果要創建多個版本應用程序,請創建 DestinationRule 以定義可從 VirtualService 引用的 subsets。
前三點不在本文介紹,從第四點介紹
4.為相應的Deployment inject sidecar
在k8s,可以在整個Namespace enable sidecar后, 在Deployment YAML加上下面的annotation, 當Create POD時會自動加上Sidecar container. 我的測試環境中,整個Namespace沒有enable istio, 所以我用了命令行的方式,下載istio-1.8.3-win,在Bin目錄下有個istioctl.exe, 配置到環境變量path中,運行:
kubectl get deployment hello -n ns -o yaml | istioctl kube-inject -f - | kubectl apply -f -
執行完之后kubectl get pods 查看該POD有兩個Container,表明Sidecar enalbe 成功,如果命令執行失敗,要核實K8S上的Istio與Istioctl Command的版本號,要保持一致。
Istioctl
template: metadata: annotations: sidecar.istio.io/inject: "true"
5.創建 Gateway 以啟用到群集的 HTTP(S)流量。Gateway 描述了在ServiceMesh邊緣運行的負載均衡,用於接收傳入或傳出的 HTTP/TCP 連接。
前提: 在istio-system name空間有相應的istio-ingressgateway的pod與Service,注意istio-ingressgateway service是有External-IP的,這樣可以把ServiceMesh中的服務暴露到外面。
5.1 在自己的命名空間建立External Service, 指向istio-system namespace 的istio-ingressgate服務
kind: Service apiVersion: v1 metadata: name: istio-ingressgateway-delegate namespace: mynamespace spec: type: ExternalName sessionAffinity: None externalName: istio-ingressgateway.istio-system.svc.cluster.local ports: - name: http port: 80 targetPort: 80
5.2 在自己的命名空間建立Gataway, 指定域名及端口。
test.vt.xx.net : 這個域名是暴露到外面的域名
apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: gateway-mynamespace namespace: mynamespace spec: selector: istio: ingressgateway servers: - port: name: http number: 80 protocol: HTTP hosts: - "test.vt.xx.net"
6 創建 Gataway 的VirtualService,通過 Gateway 公開 Kubernetes 服務。
注意:Host的配置與2.2一致,gateways 的配置與2.2 中name一致。這兩個字段標識了VirtualService幫定的網關, destination host 指向了k8s Service的集群內地址。
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: mynamespace-vs-external namespace: sqo spec: hosts: - test.vt.xx.net gateways: - gateway-my-namespace http: - match: - uri: prefix: "/web/hello" route: - destination: host: hello.mynamespace.svc.cluster.local port: number: 9080 subset: stable
7. 創建Hello Service的VituralService 和 DestinationRule, 在DestinationRule里可以分版本,用subset 來區分版本。
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: hello-vs namespace: mynamespace spec: hosts: - hello.mynamespace.svc.cluster.local http: - route: - destination: host: hello.mynamespace.svc.cluster.local port: number: 9080 subset: stable apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: hello-dr namespace: mynamespace spec: host: hello.mynamespace.svc.cluster.local trafficPolicy: loadBalancer: simple: ROUND_ROBIN subsets: - name: stable labels: version: new-app-version-replace