大学网站群建设方案,网站建设实战教程,外贸网站模板免费,网站怎样做公众号故障注入 Istio流量治理有故障注入的功能#xff0c;在接收到用户请求程序的流量时#xff0c;注入故障现象#xff0c;例如注入HTTP请求错误#xff0c;当有流量进入Sidecar时#xff0c;直接返回一个500的错误请求代码。
通过故障注入可以用来测试整个应用程序的故障恢…故障注入 Istio流量治理有故障注入的功能在接收到用户请求程序的流量时注入故障现象例如注入HTTP请求错误当有流量进入Sidecar时直接返回一个500的错误请求代码。
通过故障注入可以用来测试整个应用程序的故障恢复能力注入各种故障现象针对不同的故障现象做出应对措施。
故障注入有两种类型 延迟Delay注入延迟故障可以模拟出高负载时模拟出系统响应很慢的一种现象。中止abort注入中止故障可以模拟出系统崩溃直接返回HTTP错误代码或者TCP连接失败的现象 使用故障注入时不可用启用超时和重试的配置故障注入时在VirtualService资源中进行配置的。 两种故障注入的配置清单
故障注入-延迟配置清单
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:name: tomcat-vsnamespace: istio-traffic
spec:http:- fault: #定义故障注入delay: #定义故障注入的延迟配置percentage: #配置要将流量注入故障的比例这里为100%也可以针对50%的流量进行故障注入剩下50%的流量做一些其他路由匹配 value: 100fixedDelay: 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-vsnamespace: istio-traffic
spec:http:- fault: #定义故障注入abort: #定义故障注入的中止配置percentage: #配置要将流量注入故障的比例这里为100%也可以针对50%的流量进行故障注入剩下50%的流量做一些其他路由匹配 value: 100httpStatus: 503#注入的故障为返回一个503的HTTP状态码route: #将满足的流量路由到tomcat的service资源上- destination:host: tomcat-svc 重试 重试是非常好理解的如果请求超时那么就会进行重试。
在Istio服务网格中Envoy代理在请求失败或者超时后并不会尝试重新连接服务不会去重试需要我们来配置Istio的重试机制。重试和超时策略可以同时使用。 VirtualService资源中重试配置清单
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: ratings
spec:hosts:- ratingshttp:- route:- destination:host: ratingssubset: v1retries: #定义重试策略attempts: 3#重试的次数为3次perTryTimeout: 2s #重试的间隔为2秒
在Istio服务网格中Envoy代理在请求失败或者超时后并不会尝试重新连接服务不会去重试需要我们来配置Istio的重试机制。
重试和超时策略可以同时使用。
VirtualService资源中重试配置清单
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: ratings
spec:hosts:- ratingshttp:- route:- destination:host: ratingssubset: v1retries: #定义重试策略attempts: 3 #重试的次数为3次perTryTimeout: 2s #重试的间隔为2秒 配置Istio中应用程序的故障注入以及重试 案例描述 案例Nginx反向代理Tomcat服务Nginx服务的VirtualService开始重试机制Tomcat服务的VirtualService配置故障注入永不响应Nginx服务的请求观察是否会触发重试机制。 大致实现思路 在Istio中部署一个Nginx服务和一个Tomcat服务并配置Nginx反向代理Tomcat服务。 通过VirtualService为Nginx服务设置重试次数当请求失败后进行重试。 Tomcat服务通过VirtualService中的故障注入配置返回503错误代码任何请求都将失败。 此时Nginx再去反向代理Tomcat时Tomcat只会返回503错误代码观察是否有重试记录。 创建Namespace并开启Sidecar自动注入。
[rootk8s-master istio-traffic]# kubectl label ns istio-traffic istio-injectionenabled
namespace/istio-traffic labeled[rootk8s-master istio-traffic]# kubectl create ns istio-traffic
namespace/istio-traffic created 在Istio中部署Nginx服务
1编写资源编排文件
[rootk8s-master istio-traffic]# vim nginx-k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-proxynamespace: istio-trafficlabels:server: nginxapp: web
spec:replicas:
1selector:matchLabels:server: nginxapp: webtemplate:metadata:name: nginxlabels: server: nginxapp: webspec:containers:- name: nginximage: jiangxlrepo/know-system:v1 #knowsystem镜像中包含了NginximagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:name: nginx-svcnamespace: istio-traffic
spec:selector:server: nginxports:- name: httpport: 80targetPort: 80protocol: TCP 2创建资源并查看资源的状态
1.创建资源
[rootk8s-master istio-traffic]# kubectl apply -f nginx-k8s.yaml
deployment.apps/nginx-proxy created
service/nginx-svc created2.查看资源的状态
[rootk8s-master istio-traffic]# kubectl get all -n istio-traffic
NAME READY STATUS RESTARTS AGE
pod/nginx-proxy-7c487c794d-wt6nq 2/2 Running 0 53sNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc ClusterIP 10.105.3.226 none 80/TCP 53sNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-proxy 1/1 1 1 54sNAME DESIRED CURRENT READY AGE
replicaset.apps/nginx-proxy-7c487c794d 1 1 1 54s
2.3.在Istio中部署Tomcat服务
1编写资源编排文件
[rootk8s-master istio-traffic]# vim tomcat-k8s.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: tomcatnamespace: istio-trafficlabels:server: tomcatapp: web
spec:replicas:
1selector:matchLabels:server: tomcatapp: webtemplate:metadata:name: tomcatlabels: server: tomcatapp: webspec:containers:- name: tomcatimage: docker.io/kubeguide/tomcat-app:v1 imagePullPolicy: IfNotPresent
---
apiVersion: v1
kind: Service
metadata:name: tomcat-svcnamespace: istio-traffic
spec:selector:server: tomcatports:- name: httpport: 8080targetPort: 8080protocol: TCP
2创建资源并查看资源的状态
1.创建资源
[rootk8s-master istio-traffic]# kubectl apply -f tomcat-k8s.yaml
deployment.apps/tomcat created
service/tomcat-svc created2.查看资源的状态
[rootk8s-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 16sNAME 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 16sNAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx-proxy 1/1 1 1 6m1s
deployment.apps/tomcat 1/1 1 1 16sNAME 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反向代理
[rootnginx-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配置
[rootnginx-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
[rootnginx-proxy-76ccd8b9fc-8hqbq /]# /data/nginx/sbin/nginx -s reload
2.5.配置Nginx服务VirtualService资源中的重试
配置Nginx服务的VirtualService资源设置超时时间。
1.编写资源编排文件
[rootk8s-master istio-traffic]# vim nginx-vs-att.yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:name: nginx-vsnamespace: 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.创建资源编排文件
[rootk8s-master istio-traffic]# kubectl apply -f nginx-vs-att.yaml
virtualservice.networking.istio.io/nginx-vs created3.查看资源
[rootk8s-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.编写资源编排文件
[rootk8s-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: 100httpStatus: 503 #注入的故障为返回一个503的HTTP状态码route: #将满足的流量路由到tomcat的service资源上- destination:host: tomcat-svc2.创建资源编排文件
[rootk8s-master istio-traffic]# kubectl apply -f tomcat-vs-att.yaml
virtualservice.networking.istio.io/tomcat-vs created3.查看资源
[rootk8s-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转发请求到TomcatTomcat不会响应Nginx的请求此时就会触发配置的重试机制每隔2两秒进行一次重试重试三次后退出。
[rootk8s-master istio-traffic]# kubectl run -it busybox --image busybox:1.28 --restartNever --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次重试就表示重试配置已经生效。
[rootk8s-master ~]# kubectl logs -f nginx-proxy-76ccd8b9fc-8hqbq -c istio-proxy -n istio-traffic