0
点赞
收藏
分享

微信扫一扫

Istio服务网格进阶⑦:Istio流量治理之故障注入与重试

文章目录

1.Istio故障注入与重试的概念

1.1.故障注入

Istio流量治理有故障注入的功能,在接收到用户请求程序的流量时,注入故障现象,例如注入HTTP请求错误,当有流量进入Sidecar时,直接返回一个500的错误请求代码。

通过故障注入可以用来测试整个应用程序的故障恢复能力,注入各种故障现象,针对不同的故障现象做出应对措施。

故障注入有两种类型:

使用故障注入时,不可用启用超时和重试的配置,故障注入时在VirtualService资源中进行配置的

两种故障注入的配置清单:

  • 故障注入-延迟配置清单
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: tomcat-vs
  namespace: istio-traffic 
spec:
  http:
  - fault:							#定义故障注入
      delay:						#定义故障注入的延迟配置
        percentage:						#配置要将流量注入故障的比例,这里为100%,也可以针对50%的流量进行故障注入,剩下50%的流量做一些其他路由匹配				
          value: 100
        fixedDelay: 10s					#故障注入延迟响应时间10秒
    route:						#将满足的流量路由到tomcat的service资源上
    - destination:
        host: tomcat-svc

注意:
	1.falut和route是同级参数,当满足故障注入条件的流量会被route进行路由分发。
	2.重试和超时的配置都是在route中设置的,当我们配置了故障注入后就无法再配置超时和重试了。
  • 故障注入-中止配置清单
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: tomcat-vs
  namespace: istio-traffic 
spec:
  http:
  - fault:							#定义故障注入
      abort:						#定义故障注入的中止配置
        percentage:						#配置要将流量注入故障的比例,这里为100%,也可以针对50%的流量进行故障注入,剩下50%的流量做一些其他路由匹配				
          value: 100
        httpStatus: 503 					#注入的故障为返回一个503的HTTP状态码
    route:						#将满足的流量路由到tomcat的service资源上
    - destination:
        host: tomcat-svc

1.2.重试

重试是非常好理解的,如果请求超时那么就会进行重试。

在Istio服务网格中,Envoy代理在请求失败或者超时后,并不会尝试重新连接服务,不会去重试,需要我们来配置Istio的重试机制。

重试和超时策略可以同时使用。

VirtualService资源中重试配置清单:

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: ratings
spec:
  hosts:
  - ratings
  http:
  - route:
    - destination:
        host: ratings
        subset: v1
    retries:						#定义重试策略
      attempts: 3					#重试的次数为3次
      perTryTimeout: 2s					#重试的间隔为2秒

2.配置Istio中应用程序的故障注入以及重试

2.1.案例描述

创建Namespace并开启Sidecar自动注入。

[root@k8s-master istio-traffic]# kubectl label ns istio-traffic istio-injection=enabled
namespace/istio-traffic labeled
[root@k8s-master istio-traffic]# kubectl create ns istio-traffic
namespace/istio-traffic created

2.2.在Istio中部署Nginx服务

1)编写资源编排文件

[root@k8s-master istio-traffic]# vim nginx-k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-proxy
  namespace: istio-traffic
  labels:
    server: nginx
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      server: nginx
      app: web
  template:
    metadata:
      name: nginx
      labels: 
        server: nginx
        app: web
    spec:
      containers:
      - name: nginx
        image: jiangxlrepo/know-system:v1		#knowsystem镜像中包含了Nginx
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: istio-traffic
spec:
  selector:
    server: nginx
  ports:
  - name: http
    port: 80
    targetPort: 80
    protocol: TCP

2)创建资源并查看资源的状态

1.创建资源
[root@k8s-master istio-traffic]# kubectl apply -f nginx-k8s.yaml
deployment.apps/nginx-proxy created
service/nginx-svc created

2.查看资源的状态
[root@k8s-master istio-traffic]# kubectl get all -n istio-traffic
NAME READY STATUS RESTARTS AGE
pod/nginx-proxy-7c487c794d-wt6nq 2/2 Running 0 53s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc ClusterIP 10.105.3.226 <none> 80/TCP 53s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-proxy 1/1 1 1 54s

NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-proxy-7c487c794d 1 1 1 54s

2.3.在Istio中部署Tomcat服务

1)编写资源编排文件

[root@k8s-master istio-traffic]# vim tomcat-k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat
  namespace: istio-traffic
  labels:
    server: tomcat
    app: web
spec:
  replicas: 1
  selector:
    matchLabels:
      server: tomcat
      app: web
  template:
    metadata:
      name: tomcat
      labels: 
        server: tomcat
        app: web
    spec:
      containers:
      - name: tomcat
        image: docker.io/kubeguide/tomcat-app:v1 
        imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:
  name: tomcat-svc
  namespace: istio-traffic
spec:
  selector:
    server: tomcat
  ports:
  - name: http
    port: 8080
    targetPort: 8080
    protocol: TCP

2)创建资源并查看资源的状态

1.创建资源
[root@k8s-master istio-traffic]# kubectl apply -f tomcat-k8s.yaml
deployment.apps/tomcat created
service/tomcat-svc created

2.查看资源的状态
[root@k8s-master istio-traffic]# kubectl get all -n istio-traffic
NAME READY STATUS RESTARTS AGE
pod/nginx-proxy-7c487c794d-wt6nq 2/2 Running 0 6m1s
pod/tomcat-86ddb8f5c9-7n2xj 2/2 Running 0 16s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc ClusterIP 10.105.3.226 <none> 80/TCP 6m1s
service/tomcat-svc ClusterIP 10.100.46.31 <none> 8080/TCP 16s

NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-proxy 1/1 1 1 6m1s
deployment.apps/tomcat 1/1 1 1 16s

NAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-proxy-7c487c794d 1 1 1 6m1s
replicaset.apps/tomcat-86ddb8f5c9 1 1 1 16s

2.4.配置Nginx反向代理Tomcat

1.配置Nginx反向代理
[root@nginx-proxy-76ccd8b9fc-8hqbq /]# vim /data/nginx/conf/conf.d/istio-tomcat.conf
server {
listen 80;
server_name _;

location / {
proxy_pass http://tomcat-svc:8080;
proxy_http_version 1.1;
}
}

2.重载Nginx配置
[root@nginx-proxy-76ccd8b9fc-8hqbq /]# /data/nginx/sbin/nginx -t
nginx: the configuration file /data/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /data/nginx/conf/nginx.conf test is successful
[root@nginx-proxy-76ccd8b9fc-8hqbq /]# /data/nginx/sbin/nginx -s reload

2.5.配置Nginx服务VirtualService资源中的重试

配置Nginx服务的VirtualService资源,设置超时时间。

1.编写资源编排文件
[root@k8s-master istio-traffic]# vim nginx-vs-att.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: nginx-vs
  namespace: istio-traffic 
spec:
  hosts:
  - nginx-svc					#虚拟主机列表中指定了Nginx的Service资源名称,用于内部调用,接收来自nginx-svc service资源的流量
  http:
  - route:						#将流量转发到nginx-svc Service资源上
    - destination: 
        host: nginx-svc				
    retries:					#定义重试策略
      attempts: 3					#重试次数为3次
      perTryTimeout: 2s			#重试的间隔为2秒

 
2.创建资源编排文件
[root@k8s-master istio-traffic]# kubectl apply -f nginx-vs-att.yaml
virtualservice.networking.istio.io/nginx-vs created

3.查看资源
[root@k8s-master istio-traffic]# kubectl get vs -n istio-traffic 
NAME       GATEWAYS   HOSTS           AGE
nginx-vs              ["nginx-svc"]   58s

2.6.配置Tomcat服务VirtualService资源中的故障注入

配置Nginx服务的VirtualService资源,设置请求响应的延时时间,将延时时间设置的稍微长一些,经过这段时间后,再返回响应,就可以实现超时配置的效果了。

1.编写资源编排文件
[root@k8s-master istio-traffic]# vim tomcat-vs-att.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: tomcat-vs
spec:
  hosts:
  - tomcat-svc				#虚拟主机列表中指定了Tomcat的Service资源名称,用于内部调用,接收来自tomcat-svc service资源的流量
  http:
  - fault:							#定义故障注入策略
      abort:						#定义中止类型的故障注入
        percentage:						#配置要将流量注入故障的比例,这里为100%,也可以针对50%的流量进行故障注入,剩下50%的流量做一些其他路由匹配				
          value: 100
        httpStatus: 503				#注入的故障为返回一个503的HTTP状态码
    route:						#将满足的流量路由到tomcat的service资源上
    - destination:
        host: tomcat-svc
 
2.创建资源编排文件
[root@k8s-master istio-traffic]# kubectl apply -f tomcat-vs-att.yaml
virtualservice.networking.istio.io/tomcat-vs created

3.查看资源
[root@k8s-master istio-traffic]# kubectl get vs -n istio-traffic
NAME        GATEWAYS   HOSTS            AGE
nginx-vs               ["nginx-svc"]    6m42s
tomcat-vs              ["tomcat-svc"]   4s

3.验证Istio配置的流量重试效果

Tomcat服务配置了故障注入,所有流量请求都会返回503的错误代码,Nginx转发请求到Tomcat,Tomcat不会响应Nginx的请求,此时就会触发配置的重试机制,每隔2两秒进行一次重试,重试三次后退出。

[root@k8s-master istio-traffic]# kubectl run -it  busybox --image busybox:1.28 --restart=Never --rm busybox -n istio-traffic -- sh
/ # wget -q -O - http://nginx-svc
wget: server returned error: HTTP/1.1 503 Service Unavailable
/ # wget -q -O - http://nginx-svc
wget: server returned error: HTTP/1.1 503 Service Unavailable
/ # wget -q -O - http://nginx-svc
wget: server returned error: HTTP/1.1 503 Service Unavailable

我们可以在busybox容器中请求Nginx服务,由Nginx将请求转发到Tomcat中,由于Tomcat无法响应,Nginx发送一次请求后会接着重试3次,当我们执行一次wget的请求时,就会产生4条请求记录,而这4条请求记录会产生8条Envoy代理程序的日志,分别是出入流量的日志,我们可以在日志中清晰的看到每次请求或者重试都会有2条日志记录。

当我们看到日志中一次请求连带了3次重试,就表示重试配置已经生效。

[root@k8s-master ~]# kubectl logs -f nginx-proxy-76ccd8b9fc-8hqbq -c istio-proxy -n istio-traffic 

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SPDbCU2h-1664092586083)(.\Istio进阶-图片笔记存放\image-20220304194246331.png)]

举报

相关推荐

0 条评论