总目录索引:istio从入门到放弃系列

1、网络弹性介绍

    网络弹性也称为运维弹性,是指网络在遇到灾难事件时快速恢复和继续运行的能力。灾难事件的范畴很广泛,比如长时间停电、网络设备故障、恶意***等

2、Istio 超时实例

    需要结合 Istio 故障注入模拟被调用方响应请求慢的场景,该实例的架构图如下:

网络弹性之超时(timeout)_第1张图片

   架构说明如下,本实例就是模拟客户端调用 nginx,nginx 将请求转发给 tomcat 的常见功能。tomcat  响应请求设置为 10s(通过故障注入实现,相当于 sleep 10s 逻辑),nginx 设置 client 的请求超时时间为 2s。因为  nginx 需要在 2s 内返回给 client,而 nginx 请求 tomcat 却需要 10s,因此模拟 client 调用 nginx  超时的情景。

2.1 部署客户端

apiVersion: apps/v1
kind: Deployment
metadata:
  name: client
spec:
  replicas: 1
  selector:
    matchLabels:
      app: client
  template:
    metadata:
      labels:
        app: client
    spec:
      containers:
      - name: busybox
        image: busybox
        imagePullPolicy: IfNotPresent
        command: [ "/bin/sh", "-c", "sleep 3600" ]

2.2 部署nginx和tomcat实例

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  selector:
    server: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    server: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      server: nginx
  template:
    metadata:
      labels:
        server: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14-alpine
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: tomcat-svc
spec:
  selector:
    server: tomcat
  ports:
  - name: http
    port: 8080
    targetPort: 8080
    protocol: TCP
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  labels:
    server: tomcat
spec:
  replicas: 1
  selector:
    matchLabels:
      server: tomcat
  template:
    metadata:
      labels:
        server: tomcat
    spec:
      containers:
      - name: tomcat
        image: docker.io/kubeguide/tomcat-app:v1
        imagePullPolicy: IfNotPresent

2.3  Istio 虚拟服务资源

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-vs
spec:
  hosts:
  - nginx-svc
  http:
  - route:
    - destination:
        host: nginx-svc
    timeout: 2s
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: tomcat-vs
spec:
  hosts:
  - tomcat-svc
  http:
  - fault:
      delay:
        percentage:
          value: 100
        fixedDelay: 10s
    route:
    - destination:
        host: tomcat-svc

2.4 部署以上示例,并进行istio注入

istioctl kube-inject -f test-client.yaml | kubectl apply -f -

istioctl kube-inject -f test-deploy.yaml | kubectl apply -f -

image-20200529155003268.png

3、配置nginx转发tomcat

kubectl exec -it nginx-579d7f7ff-9pspn /bin/sh

修改完后让配置生效

nginx  -t

nginx  -s  reload

修改内容如下

网络弹性之超时(timeout)_第2张图片

4、客户端验证

kubectl exec -it client-8496866cdf-vkmcw /bin/sh

wget -q -O - http://nginx-svc

wget -q -O - http://tomcat-svc:8080

网络弹性之超时(timeout)_第3张图片

网络弹性之超时(timeout)_第4张图片

为了实验效果,设置长了时间,和设置的nginx   2s和tomcat  10s是吻合的,除去操作时间

参考文章:https://blog.51cto.com/14625168/2498209