小言_互联网的博客

Istio灰度发布:部署Bookinfo微服务项目

361人阅读  评论(0)

对于istio流量管理我们通过灰度发布将流量转移到不同的服务上面,你可以去任意的配置。比如10%流量到新版本,90%到旧版本,这些是都可以去配置的。

1、创建命名空间并开启自动注入 


  
  1. [root @master ~] # kubectl create ns bookinfo
  2. namespace/bookinfo created
  3. [root @master ~] # kubectl label namespace bookinfo istio-injection=enabled
  4. namespace/bookinfo labeled
2、部署应用YAML

   
  1. [root@master bookinfo] # kubectl apply -f platform/kube/bookinfo.yaml -n bookinfo
  2. service/details created
  3. serviceaccount/bookinfo-details created
  4. deployment.apps/details-v1 created
  5. service/ratings created
  6. serviceaccount/bookinfo-ratings created
  7. deployment.apps/ratings-v1 created
  8. service/reviews created
  9. serviceaccount/bookinfo-reviews created
  10. deployment.apps/reviews-v1 created
  11. deployment.apps/reviews-v2 created
  12. deployment.apps/reviews-v3 created
  13. service/productpage created
  14. serviceaccount/bookinfo-productpage created
  15. deployment.apps/productpage-v1 created
  16. [root@master bookinfo] # pwd
  17. /root/istio-1.8.2/samples/bookinfo
reviews 微服务部署 3 个版本,用于测试灰度发布效果:
v1 版本不会调用 ratings 服务
v2 版本会调用 ratings 服务,并使用 5个黑色五角星来显示评分信息(版本升级,流量过去一部分)
v3 版本会调用 ratings 服务,并使用5个红色五角星 来显示评分信息
3、创建Ingress网关(在ingress gateway创建入口,然后配置虚拟服务路由到具体服务)

   
  1. [root @master bookinfo] # kubectl apply -f networking/bookinfo-gateway.yaml -n bookinfo
  2. gateway.networking.istio.io/bookinfo-gateway created
  3. virtualservice.networking.istio.io/bookinfo created
  4. [root @master bookinfo] # kubectl get gateway -n bookinfo
  5. NAME AGE
  6. bookinfo-gateway 5s
  7. [root @master bookinfo] # kubectl describe gateway bookinfo-gateway -n bookinfo
  8. Selector:
  9. Istio: ingressgateway
  10. Servers:
  11. Hosts:
  12. *
  13. Port:
  14. Name: http
  15. Number: 80
  16. Protocol: HTTP
  17. Events: <none>

  
  1. [root @master ~]# kubectl get virtualservice -n bookinfo
  2. NAME GATEWAYS HOSTS AGE
  3. bookinfo [bookinfo -gateway] [ *] 11h
  4. [root @master ~]# kubectl describe virtualservice -n bookinfo
  5. Spec:
  6. Gateways:
  7. bookinfo -gateway
  8. Hosts:
  9. *
  10. Http:
  11. Match:
  12. Uri:
  13. Exact: /productpage
  14. Uri:
  15. Prefix: / static
  16. Uri:
  17. Exact: /login
  18. Uri:
  19. Exact: /logout
  20. Uri:
  21. Prefix: /api /v1 /products
  22. Route:
  23. Destination:
  24. Host: productpage
  25. Port:
  26. [root @master ~]# kubectl get svc -n bookinfo
  27. NAME TYPE CLUSTER -IP EXTERNAL -IP PORT(S) AGE
  28. details ClusterIP 10.233 .55 .211 < none > 9080 /TCP 11h
  29. productpage ClusterIP 10.233 .32 .73 < none > 9080 /TCP 11h
  30. ratings ClusterIP 10.233 .9 .73 < none > 9080 /TCP 11h
  31. reviews ClusterIP 10.233 .29 .140 < none > 9080 /TCP 11h
4、确认网关和访问地址,访问应用页面
kubectl get pods -n istio-system      访问地址:http://192.168.31.62:31928/productpage 

v1版本不带星 

v2版本黑色星 

v3版本 

现在三个版本都提供服务。

灰度发布:基于权重的路由


v1版本升级到v2版本,准备给其10%的流量,没有问题在扩大百分比。

上面部署之后流量都是均匀分配的。 

任务:
1. 流量全部发送到reviews v1版本(不带五角星)
2. 将90%的流量发送到reviews v1版本,另外10%的流量发送到reviews v2版本(
5个黑色五角星), 最后完全切换到v2版本
3. 将50%的流量发送到v2版本,另外50%的流量发送到v3版本(
5个红色五角星)

kubectl apply -f networking/virtual-service-all-v1.yaml -n bookinfo

   
  1. [root@master networking]# cat virtual-service-all-v1.yaml
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: productpage
  6. spec:
  7. hosts:
  8. - productpage
  9. http:
  10. - route:
  11. - destination:
  12. host: productpage
  13. subset: v1
  14. ---
  15. apiVersion: networking.istio.io/v1alpha3
  16. kind: VirtualService
  17. metadata:
  18. name: reviews
  19. spec:
  20. hosts:
  21. - reviews
  22. http:
  23. - route:
  24. - destination:
  25. host: reviews
  26. subset: v1
  27. ---
  28. apiVersion: networking.istio.io/v1alpha3
  29. kind: VirtualService
  30. metadata:
  31. name: ratings
  32. spec:
  33. hosts:
  34. - ratings
  35. http:
  36. - route:
  37. - destination:
  38. host: ratings
  39. subset: v1
  40. ---
  41. apiVersion: networking.istio.io/v1alpha3
  42. kind: VirtualService
  43. metadata:
  44. name: details
  45. spec:
  46. hosts:
  47. - details
  48. http:
  49. - route:
  50. - destination:
  51. host: details
  52. subset: v1
  53. ---
只要在virtual里面看到的subset,它都会创建对应的 DestinationRule。
kubectl apply -f networking/destination-rule-all.yaml -n bookinfo
 
DestinationRule(目标规则):定义虚拟服务路由目标地址的真实地址,即子集(subset),支持多种负载均衡策略:
随机
权重
最小请求数

    
  1. [root@master networking]# cat destination-rule-all.yaml
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: DestinationRule
  4. metadata:
  5. name: productpage
  6. spec:
  7. host: productpage
  8. subsets:
  9. - name: v1
  10. labels:
  11. version: v1
  12. ---
  13. apiVersion: networking.istio.io/v1alpha3
  14. kind: DestinationRule
  15. metadata:
  16. name: reviews
  17. spec:
  18. host: reviews
  19. subsets:
  20. - name: v1
  21. labels:
  22. version: v1
  23. - name: v2
  24. labels:
  25. version: v2
  26. - name: v3
  27. labels:
  28. version: v3
kubectl apply -f  virtual-service-reviews-80-20.yaml -n bookinfo

     
  1. [root@master networking]# cat virtual-service-reviews- 80- 20.yaml
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: reviews
  6. spec:
  7. hosts:
  8. - reviews
  9. http:
  10. - route:
  11. - destination:
  12. host: reviews
  13. subset: v1
  14. weight: 80
  15. - destination:
  16. host: reviews
  17. subset: v2
  18. weight: 20

kubectl apply -f networking/virtual-service-reviews-90-10.yaml -n bookinfo


     
  1. [root@master networking]# cat virtual-service-reviews- 90- 10.yaml
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: reviews
  6. spec:
  7. hosts:
  8. - reviews
  9. http:
  10. - route:
  11. - destination:
  12. host: reviews
  13. subset: v1
  14. weight: 90
  15. - destination:
  16. host: reviews
  17. subset: v2
  18. weight: 10
 virtual-service-reviews-v3.yaml

      
  1. [root@master networking]# cat virtual-service-reviews-v3.yaml
  2. apiVersion: networking.istio.io/v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: reviews
  6. spec:
  7. hosts:
  8. - reviews
  9. http:
  10. - route:
  11. - destination:
  12. host: reviews
  13. subset: v3

virtualservice:上面可以看到其实灰度发布一直调整的都是虚拟服务。根据权重转发到不同的子集上面,也就是destionationrule,subset hostname要匹配上。

destionationrule:主要将3个版本的reviews给匹配上,创建对应的子集subset,并且为其取名, 名字可以任意的去取名 v1 v2 v3 a b c,只要虚拟服务的subset和destionationrule 的subset对应上就行了,同时host也对应上。

在版本筛选上面它通过pod的标签去筛选,比如version。destionationrule就是通过版本号来创建subset子集。

  1. 将部署应用的deployment里面pod标签增加一个version: "v1",这也是istio实现灰度发布的重点,基于pod的标签,匹配到不同的版本,所以得对deployment部署的pod增加标签,标签可以自定义去设置,要规划好。
  2. 部署的deployment是要注入到istio里面
  3. 配置目标规则,destionationrule就可以根据服务版本去匹配了,去关联服务的版本。
  4. 创建虚拟服务,怎么分配流量,实现灰度发布。

灰度发布:基于请求内容的路由


类似于a/b测试,针对某些用户去采样,而不是随机的找一部分。根据http携带的信息去完成流量的分配。

任务: 将特定用户的请求发送到reviews v2版本(5个黑色五角星),其他用户则不受影响(v3)
kubectl apply -f networking/virtual-service-reviews-jason-v2-v3.yaml -n bookinfo

        
  1. [root @master networking]# cat virtual -service -reviews -jason -v2 -v3.yaml
  2. apiVersion: networking.istio.io /v1alpha3
  3. kind: VirtualService
  4. metadata:
  5. name: reviews
  6. spec:
  7. hosts:
  8. - reviews
  9. http:
  10. - match:
  11. - headers:
  12. end - user:
  13. exact: jason
  14. route:
  15. - destination:
  16. host: reviews
  17. subset: v2
  18. - route:
  19. - destination:
  20. host: reviews
  21. subset: v3

这里有有层级关系,match下面包含了route,请求头里面包含了jason用户就转发到v2版本,下面route对应的是match,除了jason用户之外,其他的引流到v3版本。

流量镜像


流量镜像:将请求复制一份,并根据策略来处理这个请求,不会影响真实请求。(根据接受过来的流量,帮你路由到不同的目标地址里面)

应用场景:
  • 线上问题排查
  • 用真实的流量验证应用功能是否正常
  • 对镜像环境压力测试
  • 收集真实流量数据进行分析

 现在没有做任何的分流,现在两个都是提供服务的,现在保留一个服务工作。

这样百分之百的流量往v1版本。

接下来实现流量镜像的功能,也简单,只需要家mirror字段就行了,所有的请求都让v1版本去处理,同时复制一份请求发给v2版本。

只要有一个请求就复制一个请求。实际上被istio的envoy给代理了一次,所以下面多了一个ip。

 

 

 

 

 

 

 

 


转载:https://blog.csdn.net/qq_34556414/article/details/125796478
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场