K8s+istio之Gataway,VirtualService,DestinationRule-飞外

文章主要介绍如何把一个简单的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后, 在DeploymentYAML加上下面的annotation,当CreatePOD时会自动加上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: ServiceapiVersion: v1metadata: name: istio-ingressgateway-delegate namespace: mynamespacespec: 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/v1alpha3kind: Gatewaymetadata: name: gateway-mynamespace namespace: mynamespacespec: 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指向了k8sService的集群内地址。

apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: mynamespace-vs-external namespace: sqospec: 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. 创建HelloService的VituralService和DestinationRule,在DestinationRule里可以分版本,用subset来区分版本。
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: hello-vs namespace: mynamespacespec: hosts: - hello.mynamespace.svc.cluster.local http: - route: - destination: host: hello.mynamespace.svc.cluster.local port: number: 9080 subset: stableapiVersion: networking.istio.io/v1alpha3kind: DestinationRulemetadata: name: hello-dr namespace: mynamespacespec: host: hello.mynamespace.svc.cluster.local trafficPolicy: loadBalancer: simple: ROUND_ROBIN subsets: - name: stable labels: version: new-app-version-replace