阐明

博客文章地址:https://blog.taoluyuan.com/posts/istio-getting-started/
本次要是内容:

  1. 应用 istioctl 装置 istio
  2. 采纳 istio 官网提供 的 利用bookinfo,实现多版本的服务利用部署
  3. istio 网关 gateway,vs,dr 的根本应用
  4. 利用监测工具 prometheus,grafana,jaeger 查看 istio 的监控数据

文章提到的yaml,也是istio官网提供的,整顿后独自放到github github k8s-istio-practice
根目录 makefile 集成了相干命令,你们能够间接通过 makefile 装置 service,gateway,vs,dr,监控,以实现跟文章一样的成果

istio

官网文档:https://istio.io/latest/zh/docs/

装置

参考官网装置文档:官网demo配置组合装置文档

  1. 采纳 demo 配置组合,它蕴含了一组专为测试筹备的性能汇合
     curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.17.2 sh - && \     cd istio-1.17.2 && \     export PATH=$PWD/bin:$PATH && \     istioctl install --set profile=demo
  1. 给命名空间增加标签,批示 在 default命名空间 部署利用的时候,主动注入 Envoy
kubectl label namespace default istio-injection=enabled
  1. 装置的istio相干资源在 istio-system 命名空间下,能够通过以下查看装置的资源

    k get all -n istio-system

    bookinfo 应用程序

bookinfo 架构及介绍

bookinfo 由四个独自的微服务形成 ,以下是官网对bookinfo的介绍,也能够间接看官网文档 istio-bookinfo

这个利用模拟在线书店的一个分类,显示一本书的信息。 页面上会显示一本书的形容,书籍的细节(ISBN、页数等),以及对于这本书的一些评论。

Bookinfo 利用分为四个独自的微服务:

  • productpage. 这个微服务会调用 details 和 reviews 两个微服务,用来生成页面。
  • details. 这个微服务中蕴含了书籍的信息。
  • reviews. 这个微服务中蕴含了书籍相干的评论。它还会调用 ratings 微服务。
  • ratings. 这个微服务中蕴含了由书籍评估组成的评级信息。

reviews 微服务有 3 个版本:

  • v1 版本不会调用 ratings 服务
  • v2 版本会调用 ratings 服务,并应用 1 到 5 个彩色星形图标来显示评分信息。
  • v3 版本会调用 ratings 服务,并应用 1 到 5 个红色星形图标来显示评分信息。

装置bookinfo

官网提醒装置 bookinfo的命令

kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

bookinfo.yaml 的istio 源码地址在 bookinfo

我将同名文件 bookinfo.yaml 下载后, 放到了本仓库的根目录 能够在 本地
执行

kubectl apply -f ./bookinfo.yaml 

或者 make

make install-bookinfo

文章前面对于 yaml 的文件,我都会将官网的yaml下载到本地,演示也是用本地的yaml,不便大家查看

验证装置的服务

  1. 下面的命令会启动全副的四个服务,其中也包含了 reviews 服务的三个版本(v1、v2 以及 v3)
  2. 确认 service和pod 都启动胜利

    kubectl get services
kubectl get pods
  1. 确认bookinfo 接口服务 是否失常

    kubectl exec -it $(kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}') -c ratings -- curl productpage:9080/productpage | grep -o "<title>.*</title>"

    下面命令步骤为:

  2. 通过 kubectl get pod -l app=ratings -o jsonpath='{.items[0].metadata.name}' 获取到 ratings 服务的pod名称
  3. 通过 kubectl exec -it $(...) -c ratings 进入到 ratings 容器中
  4. 通过 curl productpage:9080/productpage 来拜访 productpage 服务,并且通过 grep -o "<title>.*</title>" 来查看返回的页面的title,如果返回有值,阐明服务失常
  5. bookinfo.yam 的 service port 外面定义了 9080,所以能够通过 productpage:9080/productpage 来拜访 productpage 服务

留神:

源码,bookinfo外面调用是用的短名称,建议您在生产环境中指定齐全限定的主机名,比方 productpage.default.svc.cluster.local

对外开放bookinfo 服务

Service 的默认类型是 ClusterIP,目前 bookinfo 只是集群外部的服务,内部无法访问
须要创立一个 Gateway 和 VirtualService 来对外开放服务

装置 Gateway 和 VirtualService

kubectl apply -f ./bookinfo-gateway.yaml

或者 make

make install-bookinfo-gateway
apiVersion: networking.istio.io/v1alpha3kind: Gatewaymetadata:  name: bookinfo-gatewayspec:  selector:    istio: ingressgateway # use istio default controller  servers:  - port:      number: 80      name: http      protocol: HTTP    hosts:    - "*"---apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata:  name: bookinfospec:  hosts:  - "*"  gateways:  - bookinfo-gateway  http:  - match:    - uri:        exact: /productpage    - uri:        prefix: /static    - uri:        exact: /login    - uri:        exact: /logout    - uri:        prefix: /api/v1/products    route:    - destination:        host: productpage        port:          number: 9080

通过以下命令查看 Gateway 和 VirtualService 是否创立胜利

kubectl get gateway
kubectl get virtualservice

能够看到 有一个 bookinfo-gateway 和 bookinfo 的 VirtualService

批改 istio-ingressgateway 为 NodePort

通过以下命令获取到 ingressgateway 的 ip

kubectl get svc istio-ingressgateway -n istio-system

istio-ingressgateway默认的服务类型是LoadBalancer,我是本地机器装置的k8s,批改 istio-ingressgateway type LoadBalancer 为 NodePort,port: 80 映射为 port: 30080
能够通过 nodeip:30080/productpage 来拜访 bookinfo 服务

kubectl edit svc istio-ingressgateway -n istio-system

下面命令 会关上一个vim 编辑器,你须要批改 type 为 NodePort,并且将 批改 port: 80 映射为 port: 30080
查看批改后果

kubectl get svc istio-ingressgateway -n istio-system

显示相似为

NAME                   TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)                                                                      AGEistio-ingressgateway   NodePort   10.43.249.207   <none>        15021:30177/TCP,80:30080/TCP,443:30068/TCP,31400:30163/TCP,15443:30184/TCP   43d

拜访 bookinfo 服务

下面曾经将 istio-ingressgateway 的type 设置为 NodePort,并且将 port: 80 映射为 port: 30080
能够间接通过 k8s 节点的 ip 和 port 来拜访 bookinfo 服务了
我的集群ip是 192.168.31.180,所以能够通过 http://192.168.31.180:30080/productpage 拜访bookinfo 服务,须要将ip,prot 替换成本人的

如官网所说,多刷新几次利用的页面,就会 看到 productpage 页面中会随机展现 reviews 服务的不同版本的成果(红色、彩色的星形或者没有显示)。reviews 服务呈现这种状况是因为还没有应用 Istio 来管制版本的路由。

可视化网格监控

装置 istio Telemetry Addons

istion 和几个遥测利用都做了集成:kiali,jaeger,prometheus,grafana ,能够通过这些工具来监控 istio 的网格

istio Telemetry Addons相干源码地位在 istio Telemetry Addons

我将 istio Telemetry Addons 其中的 jaeger,prometheus,grafana,kiali 相干的yaml文件放在 仓库 addons 目录下,能够通过以下命令装置

kubectl apply -f ./addons

或者

make install-telemetry-addons

期待装置实现后,通过以下命令查看kiali 是否部署实现

kubectl rollout status deployment/kiali -n istio-system

istio 官网文档说,采样率默认是 1%,所以要想查看追踪数据在第一个跟踪可见之前,您须要发送至多 100 个申请。 应用以下命令向 productpage 服务发送 100 个申请,须要留神将ip,prot 替换成本人的

for i in `seq 1 100`; do curl -s -o /dev/null http://192.168.31.180:30080/productpage; done

或者 make,须要将makefile 中的GATEWAY_URL 替换成本人的

make send-100-request

应用 kiali 监控网格

应用以下命令关上 kiali,会主动关上浏览器

istioctl dashboard kiali

应用 jaeger 监控网格

应用以下命令关上 jaeger,会主动关上浏览器

istioctl dashboard jaeger

应用 grafana 监控网格

应用以下命令关上 prometheus,会主动关上浏览器

istioctl dashboard grafana

总结

istio 部署多版本利用 和 可视化网格监控 已实现
文字有点多,然而操作起来很简略,我将所有的 yaml 文件都放在了仓库中github k8s-istio-practice,将所有的命令都写在了 makefile 中,只须要执行以下:

  1. 装置 istio

    make install-istio
  2. 部署 bookinfo 利用

    make install-bookinfo
  3. 部署网关和路由

    make install-bookinfo-gateway
  4. 批改 istio-ingressgateway 为 NodePort
    执行以下命令 批改 istio-ingressgateway 为 NodePort,并且将 port: 80 映射为 port: 30080

    kubectl edit svc istio-ingressgateway -n istio-system
  5. 拜访 bookinfo 服务
    通过 k8s 节点的 ip 和 port 来拜访 bookinfo 服务了,须要将ip,prot 替换成本人的

      http://192.168.31.180:30080/productpage
  6. 通过 监测工具监控网格

    • 装置 istio Telemetry Addons

      make install-telemetry-addons
    • 关上 kiali

      istioctl dashboard kiali