Istio入坑指南(二) Istio的安装与简单的使用

Service Mesh

一、Istio安装

1、环境要求:

  • docker —> Docker Desktop for win 安装教程

  • kubernetes —> 具体版本根据Istio版本选择image-20211028101925605

    使用Docker Desktop提供的Kubernetes环境。

    image-20211029093945179

    • 设置中修改Docket Desktop 中Docker Engine配置,添加国内镜像地址
    {
      "registry-mirrors": [
        "https://docker.mirrors.ustc.edu.cn",
        "https://registry.docker-cn.com"
      ],
      "debug": false,
      "experimental": false,
      "features": {
        "buildkit": true
      },
      "builder": {
        "gc": {
          "enabled": true,
          "defaultKeepStorage": "20GB"
        }
      }
    }
    
    • 若直接开启kubernetes启动会比较慢,可以先拉取kubernetes所需要的镜像。

      前往Git仓库并Clone:https://github.com/AliyunContainerService/k8s-for-docker-desktop

      切换分支至目标版本

      image-20211029094228820

      切换了分支后,然后在仓库中运行Powershell命令,执行./load_images.ps1

      image-20211029094258362

      运行脚本,下载镜像

      image-20211029094321423

      等待镜像下载完成,就可以启动kubernetes,稍微等待一会,当左下角Kuberesnetes图标变为绿色说明kubernetes成功启动。

      image-20211029094506577

      image-20211029094547224

      可以通过命令行检查kubernetes是否安装成功并查看kubernetes版本

      $ kubectl version
      

2、下载Istio

2.1 下载
  • 命令行下载:
## 下载最新版本
$ curl -L https://istio.io/downloadIstio | sh -

## 选择版本与系统类型下载
$ curl -L https://istio.io/downloadIstio | ISTIO_VERSION=1.6.8 TARGET_ARCH=x86_64 sh -
  • samples/: 示例应用程序
  • bin/istioctl: 目录中的客户端二进制 文件。

istioctl客户端添加到您的环境变量路径

export PATH=$PWD/bin:$PATH

3、Istio安装

Istio提供了多种安装配置文件进行Istio的安装image-20211028105936162

安装命令:

## 选择demo配置文件
$ istioctl install --set profile=demo -y

4、验证

成功安装后,Istio所有的组件将会被部署在 istio-system 命名空间下

$ kubectl get po -n istio-system
NAME                                    READY   STATUS    RESTARTS   AGE
istio-egressgateway-6b4774576-fxqjk     1/1     Running   3          44d
istio-ingressgateway-5b6f9b6995-wm97w   1/1     Running   3          44d
istiod-7845cf4755-b45t7                 1/1     Running   3          44d

当所有pod运行起来后,Istio就启动成功了。

二、Istio中的服务部署与注入

1、为命名空间开启自动注入

创建一个用于测试的命名空间

$ kubectl create namespace bookinfo

为命名空间添加标签,可以为该命名空间开启自动注入,在后续部署在该命名空间下的应用程序将会自动注入Envoy Sidecar代理

$ kubectl label namespace bookinfo istio-injection=enabled
namespace/default labeled

2、BookInfo示例程序部署

2.1 BookInfo 简介

Bookinfo 应用中的几个微服务是由不同的语言编写的。 这些服务对 Istio 并无依赖,但是构成了一个有代表性的服务网格的例子:它由多个服务、多个语言构成,并且 reviews 服务具有多个版本。

该应用由四个单独的微服务构成。 这个应用模仿在线书店的一个分类,显示一本书的信息。 页面上会显示一本书的描述,书籍的细节(ISBN、页数等),以及关于这本书的一些评论。

Bookinfo 应用分为四个单独的微服务:

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

reviews 微服务有 3 个版本:

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

image-20211028110621281

2.2 部署程序
$ kubectl apply -f 1bookinfo.yaml -n bookinfo
service/details created
serviceaccount/bookinfo-details created
deployment.apps/details-v1 created
service/ratings created
serviceaccount/bookinfo-ratings created
deployment.apps/ratings-v1 created
service/reviews created
serviceaccount/bookinfo-reviews created
deployment.apps/reviews-v1 created
deployment.apps/reviews-v2 created
deployment.apps/reviews-v3 created
service/productpage created
serviceaccount/bookinfo-productpage created
deployment.apps/productpage-v1 created

当运行配置文件,会创建bookinfo应用程序所有服务的Service、Deployment以及对应的ServiceAccount。

应用程序将启动。当每个 pod 准备就绪时,Istio sidecar 将随之部署。

$ kubectl get pods
NAME                              READY   STATUS    RESTARTS   AGE
details-v1-558b8b4b76-2llld       2/2     Running   0          2m41s
productpage-v1-6987489c74-lpkgl   2/2     Running   0          2m40s
ratings-v1-7dc98c7588-vzftc       2/2     Running   0          2m41s
reviews-v1-7f99cc4496-gdxfn       2/2     Running   0          2m41s
reviews-v2-7d79d5bd5d-8zzqd       2/2     Running   0          2m41s
reviews-v3-7dbcdcbc56-m8dph       2/2     Running   0          2m41s
$ kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
details       ClusterIP   10.0.0.212      <none>        9080/TCP   29s
kubernetes    ClusterIP   10.0.0.1        <none>        443/TCP    25m
productpage   ClusterIP   10.0.0.57       <none>        9080/TCP   28s
ratings       ClusterIP   10.0.0.33       <none>        9080/TCP   29s
reviews       ClusterIP   10.0.0.28       <none>        9080/TCP   29s

此时bookinfo已经成功部署,但此时外部任然无法访问应用,只接受kubernetes集群内部的流量访问。

3、创建Istio Gateway,对外部流量

3.1 创建Istio Ingress Gateway,并与应用程序关联
$ kubectl apply -f 2bookinfo-gateway.yaml -n bookinfo
gateway.networking.istio.io/bookinfo-gateway created
virtualservice.networking.istio.io/bookinfo created

bookinfo-gateway:

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: bookinfo-gateway
spec:
  selector:
    istio: ingressgateway # use istio default controller
  servers:
  - port:
      number: 80
      name: http
      protocol: HTTP
    hosts:
    - "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: bookinfo
spec:
  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

  • 创建Istio gateway ,接收来自80端口的http流量
  • 为productpage服务创建VirtualService(虚拟服务),并绑定了istio gateway,且配置了允许流量流经的路径。
3.2 确定入口Ip和端口
  • 确定Ip:执行以下命令以确定您的 Kubernetes 集群是否在支持外部负载均衡器的环境中运行。
$ kubectl get svc istio-ingressgateway -n istio-system
NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                                                                      AGE
istio-ingressgateway   LoadBalancer   10.104.148.183   localhost     15021:32627/TCP,80:30139/TCP,443:31284/TCP,31400:30708/TCP,15443:30549/TCP,30000:31311/TCP   44d

如果EXTERNAL-IP设置了该值,则您的环境具有可用于入口网关的外部负载均衡器。如果该EXTERNAL-IP值为<none>(或永久<pending>),则您的环境不会为入口网关提供外部负载均衡器。在这种情况下,您可以使用服务的节点端口访问网关。

  • 确定端口:
$ kubectl get svc -n istio-system istio-ingressgateway -oyaml
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2021-09-14T02:18:50Z"
  labels:
    app: istio-ingressgateway
    install.operator.istio.io/owning-resource: unknown
    install.operator.istio.io/owning-resource-namespace: istio-system
    istio: ingressgateway
    istio.io/rev: default
    operator.istio.io/component: IngressGateways
    operator.istio.io/managed: Reconcile
    operator.istio.io/version: 1.8.6
    release: istio
  name: istio-ingressgateway
  namespace: istio-system
  resourceVersion: "888774"
  uid: 67f546d4-6a69-41d4-8ed1-f65d7365807f
spec:
  clusterIP: 10.104.148.183
  clusterIPs:
  - 10.104.148.183
  externalTrafficPolicy: Cluster
  ipFamilies:
  - IPv4
  ipFamilyPolicy: SingleStack
  ports:
  - name: status-port
    nodePort: 32627
    port: 15021
    protocol: TCP
    targetPort: 15021
  - name: http2
    nodePort: 30139
    port: 80
    protocol: TCP
    targetPort: 8080
  - name: https
    nodePort: 31284
    port: 443
    protocol: TCP
    targetPort: 8443
  - name: tcp
    nodePort: 30708
    port: 31400
    protocol: TCP
    targetPort: 31400
  - name: tls
    nodePort: 30549
    port: 15443
    protocol: TCP
    targetPort: 15443
  - name: http-30000
    nodePort: 31311
    port: 30000
    protocol: TCP
    targetPort: 30000
  selector:
    app: istio-ingressgateway
    istio: ingressgateway
  sessionAffinity: None
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - hostname: localhost

查找http命名开头的端口,确定了Ip与端口后就可以通过浏览器访问bookinfo应用了。

image-20211028135359147

三、Istio中的服务治理

1、流量路由管理

1.1 为所有服务创建DestinationRule(目的地规则)

为bookinfo所有的服务创建DestinationRule,该CRD定义了服务所有的版本子集

$ kubectl apply -f 3destination-rule-all.yaml -n bookinfo
destinationrule.networking.istio.io/productpage created
destinationrule.networking.istio.io/reviews created
destinationrule.networking.istio.io/ratings created
destinationrule.networking.istio.io/details created
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: productpage
spec:
  host: productpage
  subsets:
  - name: v1
    labels:
      version: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: reviews
spec:
  host: reviews
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v3
    labels:
      version: v3
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: ratings
spec:
  host: ratings
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
  - name: v2-mysql
    labels:
      version: v2-mysql
  - name: v2-mysql-vm
    labels:
      version: v2-mysql-vm
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: details
spec:
  host: details
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
---
1.2 创建VirtualService

通过创建VirtualService并设置路由规则可以配置流量的流向。

这里配置一个将所有流量路由到所有服务的v1 版本。

$ kubectl apply -f 4virtual-service-all-v1.yaml -n bookinfo
virtualservice.networking.istio.io/productpage created
virtualservice.networking.istio.io/reviews created
virtualservice.networking.istio.io/ratings created
virtualservice.networking.istio.io/details created
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: productpage
spec:
  hosts:
  - productpage
  http:
  - route:
    - destination:
        host: productpage
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - route:
    - destination:
        host: ratings
        subset: v1
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: details
spec:
  hosts:
  - details
  http:
  - route:
    - destination:
        host: details
        subset: v1
---

image-20211028141710152

此时无论如何刷新页面,只可以看见没有星星图案版本的Reviews服务,也就是v1版本的Reviews服务。

1.3 基于流量比例的路由

通过Istio我们可以设定流向每个版本流量的比例

$ kubectl apply -f 5virtual-service-reviews-80-20.yaml -n bookinfo
virtualservice.networking.istio.io/reviews configured
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v1
      weight: 80
    - destination:
        host: reviews
        subset: v2
      weight: 20

配置了20%流量流向v2版本,80%的流量流向v1版本

刷新页面可以看见黑色星标服务与没有星标的服务交替出现,并且出现的比例大致在1:4左右。

我们可以修改权重,配置可以实时生效,达到灰度发布的目的。

1.4 基于身份的路由配置

接下来,将更改路由配置,以将来自特定用户的所有流量路由到特定服务版本。在这种情况下,来自名为 Jason 的用户的所有流量都将路由到该服务reviews:v2

此示例是由于该productpage服务会为所有出站 HTTP 请求添加自定义标头。

$ kubectl apply -f 6virtual-service-reviews-test-v2.yaml -n bookinfo
virtualservice.networking.istio.io/reviews configured
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
    - reviews
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    route:
    - destination:
        host: reviews
        subset: v2
  - route:
    - destination:
        host: reviews
        subset: v1

当我们未登录时将无法看见带有星星标记的服务

image-20211028142522500

当我们以jason用户登录时,将会看见带有星星标记的服务

image-20211028142616408

2、故障注入

主动地为服务与服务调用时注入一些故障,来测试服务的弹性

2.1 延时注入

这里在userjason 下的reviews:v2ratings微服务的服务调用之间注入 7 秒延迟。此测试将发现一个有意引入 Bookinfo 应用程序的错误。

请注意,该reviews:v2服务有一个 10 秒的硬编码连接超时。

$ kubectl apply -f 7virtual-service-ratings-test-delay.yaml -n bookinfo
virtualservice.networking.istio.io/ratings created
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    fault:
      delay:
        percentage:
          value: 100.0
        fixedDelay: 7s
    route:
    - destination:
        host: ratings
        subset: v1
  - route:
    - destination:
        host: ratings
        subset: v1

当我们以未登录的状态访问bookinfo应用时,我们不会看见星星标记的服务。

当我们用jason登录时,页面会有一个7秒左右的延迟,并且最后由于超时的原因页面会有错误提示。

image-20211028150938480

2.2 Http故障注入

也可以为服务注入一个Http的故障,如500等

$ kubectl apply -f 8virtual-service-ratings-test-abort.yaml -n bookinfo
virtualservice.networking.istio.io/ratings configured
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - match:
    - headers:
        end-user:
          exact: jason
    fault:
      abort:
        percentage:
          value: 100.0
        httpStatus: 500
    route:
    - destination:
        host: ratings
        subset: v1
  - route:
    - destination:
        host: ratings
        subset: v1

当我们已jason用户登录时会发现ratings服务不可用

image-20211028153100371

3、设置超时时间

我们将前一步的延时缩短一点,缩短为2s,让服务不会因为延时而超时,当我们登录时会有2s左右的延时,但是页面可以正常访问,reviews服务正常提供服务。

我们可以通过配置VirtualService设定服务的超时时间。

$ kubectl apply -f 9virtual-service-rewiews-test-v2-timeout.yaml -n bookinfo
virtualservice.networking.istio.io/reviews configured
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: reviews
spec:
  hosts:
  - reviews
  http:
  - route:
    - destination:
        host: reviews
        subset: v2
    timeout: 0.5s

我们设置服务超时的时间为0.5s。

当我们再已jason用户登录时,2s的延迟后服务将会超时报错。

4、配置熔断

前提

命名空间下开启自动注入

4.1 启动httpbin实例
$ kubectl apply -f httpbin.yaml -n bookinfo
4.2 配置熔断器
$ kubectl apply -f 15dr-httpbin.yaml -n bookinfo
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: httpbin
spec:
  host: httpbin
  trafficPolicy:
    connectionPool:
      tcp:
        maxConnections: 1
      http:
        http1MaxPendingRequests: 1
        maxRequestsPerConnection: 1
    outlierDetection:
      consecutive5xxErrors: 1
      interval: 1s
      baseEjectionTime: 3m
      maxEjectionPercent: 100

配置文件设置了httpbin服务的tcp流量的最大连接数和http流量的最大请求数为1。

4.4 启动客户端,连接httpbin服务
$ kubectl apply -f fortio-deploy.yaml -n bookinfo

foritio作为客户端对httpbin服务发起请求。

查询foritio 服务pod的名称,进入foritio服务容器,对httpbin服务发起请求。

$ kubectl get po -n bookinfo
NAME                              READY   STATUS    RESTARTS   AGE
details-v1-79c697d759-mb44c       2/2     Running   2          19h
fortio-deploy-576dbdfbc4-z4tvn    2/2     Running   0          7m13s
httpbin-74fb669cc6-lvg6j          2/2     Running   2          23m
productpage-v1-65576bb7bf-ds89h   2/2     Running   2          19h
ratings-v1-7d99676f7f-989lt       2/2     Running   2          19h
reviews-v1-987d495c-7hvwx         2/2     Running   2          19h
reviews-v2-6c5bf657cf-qd52g       2/2     Running   2          19h
reviews-v3-5f7b9f4f77-jcsr5       2/2     Running   2          19h
$ kubectl exec -n bookinfo fortio-deploy-576dbdfbc4-z4tvn -c fortio -- /usr/bin/fortio curl -quiet http://httpbin:8000/get
HTTP/1.1 200 OK
server: envoy
date: Fri, 29 Oct 2021 02:28:40 GMT
content-type: application/json
content-length: 624
access-control-allow-origin: *
access-control-allow-credentials: true
x-envoy-upstream-service-time: 49

{
  "args": {},
  "headers": {
    "Content-Length": "0",
    "Host": "httpbin:8000",
    "User-Agent": "fortio.org/fortio-1.17.1",
    "X-B3-Parentspanid": "48baab2235a4b076",
    "X-B3-Sampled": "1",
    "X-B3-Spanid": "0fae4a52a7163ff1",
    "X-B3-Traceid": "054b3471c277887c48baab2235a4b076",
    "X-Envoy-Attempt-Count": "1",
    "X-Forwarded-Client-Cert": "By=spiffe://cluster.local/ns/bookinfo/sa/httpbin;Hash=48eda2548deaccb52fa524859b02323c1ad5aedcfdd27372b9af2435125e9edd;Subject=\"\";URI=spiffe://cluster.local/ns/bookinfo/sa/default"
  },
  "origin": "127.0.0.1",
  "url": "http://httpbin:8000/get"
}

可以看见服务请求成功,那么我们就可以开始提高并发数,测试熔断。

使用两个并发连接 ( -c 2)调用服务并发送 20 个请求 ( -n 20):

$ kubectl exec -n bookinfo fortio-deploy-576dbdfbc4-z4tvn -c fortio -- /usr/bin/fortio load -c 2 -qps 0 -n 20 -loglevel Warning http://httpbin:8000/get
02:35:44 I logger.go:127> Log level is now 3 Warning (was 2 Info)
Fortio 1.17.1 running at 0 queries per second, 8->8 procs, for 20 calls: http://httpbin:8000/get
Starting at max qps with 2 thread(s) [gomax 8] for exactly 20 calls (10 per thread + 0)
Ended after 45.6804ms : 20 calls. qps=437.82
Aggregated Function Time : count 20 avg 0.00447278 +/- 0.002527 min 0.0030049 max 0.0126282 sum 0.0894556
# range, mid point, percentile, count
>= 0.0030049 <= 0.004 , 0.00350245 , 70.00, 14
> 0.004 <= 0.005 , 0.0045 , 90.00, 4
> 0.011 <= 0.012 , 0.0115 , 95.00, 1
> 0.012 <= 0.0126282 , 0.0123141 , 100.00, 1
# target 50% 0.00369382
# target 75% 0.00425
# target 90% 0.005
# target 99% 0.0125026
# target 99.9% 0.0126156
Sockets used: 2 (for perfect keepalive, would be 2)
Jitter: false
Code 200 : 20 (100.0 %)
Response Header Sizes : count 20 avg 230 +/- 0 min 230 max 230 sum 4600
Response Body/Total Sizes : count 20 avg 854 +/- 0 min 854 max 854 sum 17080
All done 20 calls (plus 0 warmup) 4.473 ms avg, 437.8 qps

可以发现所有的请求都成功了,说明istio-proxy为我们留了一些回旋余地,我们继续提高并发数。

将并发连接数提高到 3。

$ kubectl exec -n bookinfo fortio-deploy-576dbdfbc4-z4tvn -c fortio -- /usr/bin/fortio load -c 3 -qps 0 -n 30 -loglevel Warning http://httpbin:8000/get
02:38:21 I logger.go:127> Log level is now 3 Warning (was 2 Info)
Fortio 1.17.1 running at 0 queries per second, 8->8 procs, for 30 calls: http://httpbin:8000/get
Starting at max qps with 3 thread(s) [gomax 8] for exactly 30 calls (10 per thread + 0)
02:38:21 W http_client.go:806> [0] Non ok http code 503 (HTTP/1.1 503)
02:38:21 W http_client.go:806> [2] Non ok http code 503 (HTTP/1.1 503)
02:38:21 W http_client.go:806> [0] Non ok http code 503 (HTTP/1.1 503)
02:38:21 W http_client.go:806> [0] Non ok http code 503 (HTTP/1.1 503)
02:38:21 W http_client.go:806> [2] Non ok http code 503 (HTTP/1.1 503)
02:38:21 W http_client.go:806> [0] Non ok http code 503 (HTTP/1.1 503)
Ended after 104.9574ms : 30 calls. qps=285.83
Aggregated Function Time : count 30 avg 0.00839021 +/- 0.01082 min 0.0007072 max 0.0459461 sum 0.2517063
# range, mid point, percentile, count
>= 0.0007072 <= 0.001 , 0.0008536 , 6.67, 2
> 0.001 <= 0.002 , 0.0015 , 10.00, 1
> 0.002 <= 0.003 , 0.0025 , 13.33, 1
> 0.003 <= 0.004 , 0.0035 , 56.67, 13
> 0.004 <= 0.005 , 0.0045 , 73.33, 5
> 0.007 <= 0.008 , 0.0075 , 76.67, 1
> 0.01 <= 0.011 , 0.0105 , 80.00, 1
> 0.014 <= 0.016 , 0.015 , 83.33, 1
> 0.018 <= 0.02 , 0.019 , 90.00, 2
> 0.02 <= 0.025 , 0.0225 , 93.33, 1
> 0.04 <= 0.045 , 0.0425 , 96.67, 1
> 0.045 <= 0.0459461 , 0.0454731 , 100.00, 1
# target 50% 0.00384615
# target 75% 0.0075
# target 90% 0.02
# target 99% 0.0456623
# target 99.9% 0.0459177
Sockets used: 9 (for perfect keepalive, would be 3)
Jitter: false
Code 200 : 24 (80.0 %)
Code 503 : 6 (20.0 %)
Response Header Sizes : count 30 avg 184.1 +/- 92.05 min 0 max 231 sum 5523
Response Body/Total Sizes : count 30 avg 731.5 +/- 245.3 min 241 max 855 sum 21945
All done 30 calls (plus 0 warmup) 8.390 ms avg, 285.8 qps

这里有20%的服务被熔断了。

四、Istio中的安全策略

Istio中可以直接配置服务间访问时的安全策略,并实时生效

1、Http流量的授权策略

为Http服务设置授权策略可以设定该服务接受哪些服务的调用,这里我们演示的思路是设定一个全局的访问拒绝策略,通过修改配置,一步一步开放每个服务,使其出现在浏览器上。

1.1 创建一个全局的访问拒绝策略
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: allow-nothing
  namespace: bookinfo
spec:
  {}

该策略将会拒绝所有对bookinfo命名空间下工作负载的访问,我们访问页面,会发现访问被拒绝了。

image-20211028172429408

接下来通过修改配置文件,一步一步开发被限制访问的服务。

1.2 开放ProductPage服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "productpage-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: productpage
  action: ALLOW
  rules:
  - to:
    - operation:
        methods: ["GET"]

运行该配置文件,会为productpage服务设置授权策略,该策略为设置from字段,则运行所有用户对productpage工作负载进行访问,并且只开放了GET方法的访问。

等待几秒钟,Istiod将规则下发,刷新页面我们会看到productpage页面可以正常访问,但是details服务和reviews服务任然无法正常访问。

image-20211028173205111

1.3 开放Details服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "details-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: details
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/bookinfo/sa/bookinfo-productpage"]
    to:
    - operation:
        methods: ["GET"]

同样配置授权策略为Details服务开放访问,此时配置了from字段,并且绑定了productpage服务的serviceAccount,说明Details服务只接受来自与bookinfo-productpage这个serviceAccount绑定的工作负载的Get请求访问。

运行该配置,等待几秒钟后刷新页面将会看到Details服务可以正常显示了,而reviews服务任然处于拒绝访问状态。

image-20211028173653605

而我们修改配置文件,将from字段中的bookinfo-productpage修改为其他服务,再去访问页面,details服务间又不能正常显示。

1.4 开放Reviews服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "reviews-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: reviews
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/bookinfo/sa/bookinfo-productpage"]
    to:
    - operation:
        methods: ["GET"]

同样我们可以开放reviews服务

image-20211028174117615

1.5 开放ratings服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "ratings-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: ratings
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/bookinfo/sa/bookinfo-reviews"]
    to:
    - operation:
        methods: ["GET"]

image-20211028174438096

JbkmwF-1635500907014)]

而我们修改配置文件,将from字段中的bookinfo-productpage修改为其他服务,再去访问页面,details服务间又不能正常显示。

1.4 开放Reviews服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "reviews-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: reviews
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/bookinfo/sa/bookinfo-productpage"]
    to:
    - operation:
        methods: ["GET"]

同样我们可以开放reviews服务

[外链图片转存中…(img-NPZngFnZ-1635500907015)]

1.5 开放ratings服务
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: "ratings-viewer"
  namespace: bookinfo
spec:
  selector:
    matchLabels:
      app: ratings
  action: ALLOW
  rules:
  - from:
    - source:
        principals: ["cluster.local/ns/bookinfo/sa/bookinfo-reviews"]
    to:
    - operation:
        methods: ["GET"]

[外链图片转存中…(img-HS9vAqfY-1635500907016)]

自此,bookinfo页面恢复正常。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值