istio 服务跟踪和流量管理

创建目标规则和默认路由

使用Istio来管理这两个服务的流量

定义一个名称为nginx-web的DestinationRule 目标规则,

利用Pod标签把nginx-web服务分成两个subset, 分别命名为v1和v2

# nginx-destinationRule.yaml 
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
  name: nginx-web
spec:
  host: nginx-web
  subsets:
  - name: v1
    labels:
      version: v1
  - name: v2
    labels:
      version: v2
      
      
#### 部署到集群上

 kubectl apply -f nginx-destinationRule.yaml          
创建默认的路由规则VirtualService 不论是否进行进一步的流量控制,都建议为网格中的服务创建默认的路由规则

定义一个VirtualService对象,它负责接管对 “nginx-web”这一主机名的访问,
将流量都转发到DestinationRule定义的v2 subset上

# cat nginx-virtual-service-reviews-v2.yaml 
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-web-v2
spec:
  hosts:
    - nginx-web
  http:
  - route: 
    - destination: 
        host: nginx-web
        subset: v2

#### 部署到集群上
kubectl apply -f nginx-virtual-service-reviews-v2.yaml 

再次进入客户端Pod, 看看新定义的流量管理规则是否生效

### 结果可以看出,访问只返回 v2 版本
# kubectl exec -it sleep-7995b95fdb-8vzmx sh
Defaulting container name to sleep.
Use 'kubectl describe pod/sleep-7995b95fdb-8vzmx -n default' to see all of the containers in this pod.
/ # while true; do curl http://nginx-web:80 ; sleep 1; done
<h1>Welcome to V2 !</h1>
<h1>Welcome to V2 !</h1>
<h1>Welcome to V2 !</h1>
<h1>Welcome to V2 !</h1>

金丝雀部署

金丝雀(Canary)部署指的是让少量用户使用应用新版本的一个过程,
借助这一过程能够验证新版本是否存在问题,然后能够确保以更高的质量发布给更多的受众
将 20% 的用户发送至带有缺陷的 v2 版本(这就是金丝雀发布),
并将 80% 的用户发送至正常的服务 v1 版本
这可以通过如下的 VirtualService 来实现

# cat nginx-virtual-service-reviews-80-20.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-web
spec:
  hosts:
    - nginx-web
  http:
  - route: 
    - destination: 
        host: nginx-web
        subset: v1
      weight: 80
    - destination: 
        host: nginx-web
        subset: v2
      weight: 20

#### 部署到集群上
kubectl apply -f nginx-virtual-service-reviews-80-20.yaml

再次进入客户端Pod, 看看新定义的流量管理规则是否生效

### 结果可以看出,大部分结果返回 v1 版本
# kubectl exec -it sleep-7995b95fdb-8vzmx sh
Defaulting container name to sleep.
Use 'kubectl describe pod/sleep-7995b95fdb-8vzmx -n default' to see all of the containers in this pod.
/ # while true; do curl http://nginx-web ; sleep 1; done
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V2 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V1 !</h1>
<h1>Welcome to V2 !</h1>

带有缺陷的服务版本会有三分之一的概率在生成响应时耗费过长的时间,
三分之一的概率遇到服务器内部错误,其余的请求均能正常完成。

为了降低这些缺陷的影响并给用户带来更好的用户体验,我们会采取如下的措施:

  • 如果服务耗时超过了 8 秒钟,将会超时;
  • 对于失败的请求进行重试。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: nginx-web
spec:
  hosts:
    - nginx-web
  http:
  - route: 
    - destination: 
        host: nginx-web
        subset: v1
      weight: 80
    - destination: 
        host: nginx-web
        subset: v2
      weight: 20
    timeout: 8s
    retries:
      attempts: 3
      perTryTimeout: 3s

猜你喜欢

转载自blog.csdn.net/u011327801/article/details/91047188