前面我们已经将Istio 1.0.0安装到了Kubernetes集群中,本篇我们将会将示例应用Booinfo跑起来。

1.Bookinfo示例应用概述

Bookinfo这个示例应用由4个独立的微服务组成,Istio使用这个应用来演示Istio的Service Mesh的各种功能。 Bookinfo应用的端到端架构图如下:

BookInfo Application without Istio

  • productpage微服务: 调用details微服务和reviews微服务来生成页面
  • details微服务: 包含图书的详细信息
  • reviews微服务: 提供图书的评论功能,也可以调用rating微服务给图书评级
  • ratings微服务: 提供图书的评级功能

另外还要注意到reviews微服务目前有3个版本:

  • v1不会调用ratings微服务
  • v2调用ratings微服务,并将评级显示1~5个黑色星星
  • v3调用ratings微服务,并将每个评级显示为1~5个红色星星

2.部署Bookinfo应用

之前在学习istio 0.2.x的版本部署Bookinfo应用时采用的是手动注入sidecar的形式,这次我们体验自动注入sidecar。

注意到前面在安装istio 1.0的时候, Istio-sidecar-injector容器已经启动:

1kubectl get pod -n istio-system | grep  injector
2istio-sidecar-injector-854f6498d9-22lb8     1/1       Running     2          7h

istio-sidecar-injector可以自动将Envoy容器作为sidecar注入到我们的业务应用Pod中,只要业务应用Pod所在的namespace包含istio-injection=enabled的Label。

查看istio-1.0.0/samples/bookinfo/platform/kube中的yaml文件,默认是将bookinfo应用部署到Kubernetes的default namespace的,为了体验sidecar的自动注入,我们将default命名空间打上istio-injection=enabled的Label。

1kubectl label namespace default istio-injection=enabled

在default命名空间开启自动注入sidecar后,只需要使用kubect部署bookinfo应用即可:

1cd istio-1.0.0
2kubectl apply -f samples/bookinfo/platform/kube/bookinfo.yaml

部署完成后确保所有的Service和Pod处于正确的状态:

 1kubectl get pod
 2NAME                             READY     STATUS    RESTARTS   AGE
 3details-v1-6764bbc7f7-hg6ft      2/2       Running   0          18m
 4productpage-v1-54b8b9f55-fnk7h   2/2       Running   0          18m
 5ratings-v1-7bc85949-ldmv7        2/2       Running   0          18m
 6reviews-v1-fdbf674bb-zwbft       2/2       Running   0          18m
 7reviews-v2-5bdc5877d6-dfgl2      2/2       Running   0          18m
 8reviews-v3-dd846cc78-g9w9s       2/2       Running   0          20m
 9
10
11kubectl get svc
12NAME          TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
13details       ClusterIP   10.110.172.127   <none>        9080/TCP   18m
14kubernetes    ClusterIP   10.96.0.1        <none>        443/TCP    15d
15productpage   ClusterIP   10.106.144.141   <none>        9080/TCP   18m
16ratings       ClusterIP   10.105.190.232   <none>        9080/TCP   18m
17reviews       ClusterIP   10.101.41.120    <none>        9080/TCP   18m

3.从集群外部访问Bookinfo应用

为了从集群外部访问Bookinfo应用,需要将它的四个Service中的productpage暴露到集群外部,当然可以使用Ingress Nginx去暴露。 这里为了体验一下Istio Ingress Gateway,将使用Istio Ingress Gateway暴露。

Istio Ingress Gateway使用type为LoadBalancer的istio-ingressgateway Service接收流量:

1kubectl get svc -n istio-system -l app=istio-ingressgateway
2NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                                                     AGE
3istio-ingressgateway   LoadBalancer   10.109.195.178   <pending>     80:31380/TCP,443:31390/TCP,31400:31400/TCP,15011:31808/TCP,8060:31442/TCP,15030:32489/TCP,15031:31002/TCP   18h

注意当前这个istio-ingressgateway Service的EXTERNAL-IP为空,因为我们是使用本地测试环境部署Kubernetes,这里为测试简单,直接修改istio-ingressgateway Service的externalIPs,指定一个内网Kubernetes Node的IP,修改后如下:

1kubectl get svc -n istio-system -l app=istio-ingressgateway
2NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)                                                                                                     AGE
3istio-ingressgateway   LoadBalancer   10.109.195.178   192.168.61.11   80:31380/TCP,443:31390/TCP,31400:31400/TCP,15011:31808/TCP,8060:31442/TCP,15030:32489/TCP,15031:31002/TCP   18h

查看一下samples/bookinfo/networking/bookinfo-gateway.yaml:

 1apiVersion: networking.istio.io/v1alpha3
 2kind: Gateway
 3metadata:
 4  name: bookinfo-gateway
 5spec:
 6  selector:
 7    istio: ingressgateway # use istio default controller
 8  servers:
 9  - port:
10      number: 80
11      name: http
12      protocol: HTTP
13    hosts:
14    - "*"
15---
16apiVersion: networking.istio.io/v1alpha3
17kind: VirtualService
18metadata:
19  name: bookinfo
20spec:
21  hosts:
22  - "*"
23  gateways:
24  - bookinfo-gateway
25  http:
26  - match:
27    - uri:
28        exact: /productpage
29    - uri:
30        exact: /login
31    - uri:
32        exact: /logout
33    - uri:
34        prefix: /api/v1/products
35    route:
36    - destination:
37        host: productpage
38        port:
39          number: 9080

下面我们创建bookinfo的Gateway和VirtualService:

1kubectl apply -f samples/bookinfo/networking/bookinfo-gateway.yaml
2gateway.networking.istio.io/bookinfo-gateway created
3virtualservice.networking.istio.io/bookinfo created
4
5
6kubectl get gateway
7NAME               AGE
8bookinfo-gateway   1m

以http://${GATEWAY_URL}/productpage访问,这里是http://192.168.61.12/productpage。

4.总结

查看Bookinfo的各个微服务的Pod,每个Pod都是两个容器:

1kubectl get pod
2NAME                             READY     STATUS    RESTARTS   AGE
3details-v1-6764bbc7f7-hg6ft      2/2       Running   0          18m
4productpage-v1-54b8b9f55-fnk7h   2/2       Running   0          18m
5ratings-v1-7bc85949-ldmv7        2/2       Running   0          18m
6reviews-v1-fdbf674bb-zwbft       2/2       Running   0          18m
7reviews-v2-5bdc5877d6-dfgl2      2/2       Running   0          18m
8reviews-v3-dd846cc78-g9w9s       2/2       Running   0          20m

其中istio-proxy Image: gcr.io/istio-release/proxyv2:1.0.0被作为sidecar注入。

Bookinfo部署到k8s上的的结构图是下面这样的:

BookInfo Application with Istio

所有的微服务之间的调用都是通过Envoy sidecar,即图中小黑色长方形,微服务的sidecar组成了服务网格Service Mesh。

参考