本文将模拟一个运行了backend-v1的Pod出问题,并在发给backend-v1微服务的DestinationRule中设置断路器Circuit Breaker。当断路器发现访问微服务出问题后会将出问题的Pod暂时从转发目标中移除,15分钟后再尝试运行在该Pod的微服务是否能正常访问。
本文可在完成《OpenShift 4 之Service Mesh教程(3)- 用Kiali监控微服务运行》后进行操作。在开始正式操作前需要运行以下命令将运行应用的my-istio-app项目里的内容清空即可。
$ scripts/teardown.sh
$ oc apply -f ocp/frontend-v1-deployment.yml -n my-istio-app
$ oc apply -f ocp/frontend-service.yml -n my-istio-app
$ oc apply -f ocp/frontend-route.yml -n my-istio-app
$ oc apply -f ocp/backend-v1-deployment.yml -n my-istio-app
$ oc apply -f ocp/backend-service.yml -n my-istio-app
$ oc scale deployment backend-v1 --replicas=3
$ oc get pod
NAME READY STATUS RESTARTS AGE
backend-v1-6ddf9c7dcf-2qlls 2/2 Running 0 77s
backend-v1-6ddf9c7dcf-pk57w 2/2 Running 0 3m15s
backend-v1-6ddf9c7dcf-qj64x 2/2 Running 0 77s
frontend-v1-655f4478c-97xbg 2/2 Running 0 3m40s
$ scripts/run-50.sh
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:0.777830 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-2qlls, Elapsed Time:0.778008 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.796773 sec
...
$ oc rsh pod/backend-v1-6ddf9c7dcf-2qlls
Defaulting container name to backend.
$ curl http://localhost:8080
Backend version:v1,Response:200,Host:backend-v1-6ddf9c7dcf-2qlls, Message: Hello World!!
$ curl http://localhost:8080/stop
Backend version:v1,Response:200,Message: backend-v1-6ddf9c7dcf-2qlls is stopped
$ exit
$ cat istio-files/destination-rule-backend-circuit-breaker-with-pool-ejection.yml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: backend-destination-rule
spec:
host: backend
trafficPolicy:
connectionPool:
http: {}
tcp: {}
loadBalancer:
simple: ROUND_ROBIN
outlierDetection:
outlierDetection: #Detect error with condition
consecutiveErrors: 1 #If found 1 consecutive error (consecutiveErrors)
baseEjectionTime: 15m #then eject that pod from pool for 15 minutes (baseEjectionTime)
interval: 15m #check again within 15 minutes (interval)
maxEjectionPercent: 100 #All of pods can be ejected (maxEjectionPercent)
$ oc apply -f istio-files/destination-rule-backend-circuit-breaker-with-pool-ejection.yml
$ scripts/run-50.sh
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:1.176571 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:1.071071 sec
Backend:v1, Response Code: 504, Host:, Elapsed Time:0.011490 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.816486 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:0.805425 sec
Backend:v1, Response Code: 504, Host:, Elapsed Time:0.007417 sec
...
$ oc apply -f istio-files/destination-rule-backend-circuit-breaker-with-pool-ejection.yml
$ scripts/run-50.sh
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:0.797786 sec
Backend:v1, Response Code: 504, Host:, Elapsed Time:0.009550 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.819576 sec
$ oc rsh pod/backend-v1-6ddf9c7dcf-2qlls
Defaulting container name to backend.
$ curl http://localhost:8080/start
Backend version:v1,Response:200,Message: backend-v1-6ddf9c7dcf-2qlls is started
$ curl http://localhost:8080
Backend version:v1,Response:200,Host:backend-v1-6ddf9c7dcf-2qlls, Message: Hello World!!
$ exit
$ scripts/run-50.sh
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:1.133394 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:1.096140 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.788913 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:0.815003 sec
$ oc edit destinationrule backend-destination-rule
$ scripts/run-50.sh
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-2qlls, Elapsed Time:0.801652 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-2qlls, Elapsed Time:0.877466 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.812104 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-pk57w, Elapsed Time:0.821273 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-2qlls, Elapsed Time:0.776068 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.796318 sec
Backend:v1, Response Code: 200, Host:backend-v1-6ddf9c7dcf-qj64x, Elapsed Time:0.807531 sec