zoukankan      html  css  js  c++  java
  • K8s + istio 之 Gataway, VirtualService, DestinationRule

    文章主要介绍如何把一个简单的HelloWebApp装在Istio+K8S环境下

    下面是基本步骤:

    1. 创建一个 Kubernetes 集群并安装带有 sidecare 自动注入的 Istio。
    2. 使用您选择的语言创建 Hellohttp 应用程序,创建 Docker 镜像并将其推送到公共镜像仓库。
    3. 为你的容器创建 Kubernetes deployment 和 service。
    4. 为相应的Deployment inject sidecar
    5. 创建 Gateway 以启用到群集的 HTTP(S)流量。
    6. 创建 Gataway 的VirtualService,通过 Gateway 公开 Kubernetes 服务。
    7. 如果要创建多个版本应用程序,请创建 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
    

      

     

  • 相关阅读:
    虚拟设备 ide1:0 将开始断开
    虚拟机集群启动 某一台启动失败
    jeesite1,工具类,文件介绍
    line-clamp
    js中同名的函数的调用情况
    获取子页面iframe的点击事件及iframe跨域的交互
    Docker环境搭建入门
    软件工程课后作业:论我对百度搜索的看法
    第二阶段第十天12.10
    软件工程:用户场景描述
  • 原文地址:https://www.cnblogs.com/Ivyduan/p/14593656.html
Copyright © 2011-2022 走看看