对于istio流量管理我们通过灰度发布将流量转移到不同的服务上面,你可以去任意的配置。比如10%流量到新版本,90%到旧版本,这些是都可以去配置的。
1、创建命名空间并开启自动注入
-
[root
@master ~]
# kubectl create ns bookinfo
-
namespace/bookinfo created
-
[root
@master ~]
# kubectl label namespace bookinfo istio-injection=enabled
-
namespace/bookinfo labeled
-
[root@master bookinfo]
# kubectl apply -f platform/kube/bookinfo.yaml -n bookinfo
-
service/details created
-
serviceaccount/bookinfo-details created
-
deployment.apps/details-v1 created
-
service/ratings created
-
serviceaccount/bookinfo-ratings created
-
deployment.apps/ratings-v1 created
-
service/reviews created
-
serviceaccount/bookinfo-reviews created
-
deployment.apps/reviews-v1 created
-
deployment.apps/reviews-v2 created
-
deployment.apps/reviews-v3 created
-
service/productpage created
-
serviceaccount/bookinfo-productpage created
-
deployment.apps/productpage-v1 created
-
-
[root@master bookinfo]
# pwd
-
/root/istio-1.8.2/samples/bookinfo
-
[root
@master bookinfo]
# kubectl apply -f networking/bookinfo-gateway.yaml -n bookinfo
-
gateway.networking.istio.io/bookinfo-gateway created
-
virtualservice.networking.istio.io/bookinfo created
-
-
[root
@master bookinfo]
# kubectl get gateway -n bookinfo
-
NAME AGE
-
bookinfo-gateway 5s
-
-
[root
@master bookinfo]
# kubectl describe gateway bookinfo-gateway -n bookinfo
-
Selector:
-
Istio: ingressgateway
-
Servers:
-
Hosts:
-
*
-
Port:
-
Name: http
-
Number:
80
-
Protocol: HTTP
-
Events: <none>
-
[root
@master
~]# kubectl
get virtualservice
-n bookinfo
-
NAME GATEWAYS HOSTS AGE
-
bookinfo [bookinfo
-gateway] [
*]
11h
-
-
[root
@master
~]# kubectl
describe virtualservice
-n bookinfo
-
Spec:
-
Gateways:
-
bookinfo
-gateway
-
Hosts:
-
*
-
Http:
-
Match:
-
Uri:
-
Exact:
/productpage
-
Uri:
-
Prefix:
/
static
-
Uri:
-
Exact:
/login
-
Uri:
-
Exact:
/logout
-
Uri:
-
Prefix:
/api
/v1
/products
-
Route:
-
Destination:
-
Host: productpage
-
Port:
-
-
[root
@master
~]# kubectl
get svc
-n bookinfo
-
NAME TYPE CLUSTER
-IP
EXTERNAL
-IP PORT(S) AGE
-
details ClusterIP
10.233
.55
.211
<
none
>
9080
/TCP
11h
-
productpage ClusterIP
10.233
.32
.73
<
none
>
9080
/TCP
11h
-
ratings ClusterIP
10.233
.9
.73
<
none
>
9080
/TCP
11h
-
reviews ClusterIP
10.233
.29
.140
<
none
>
9080
/TCP
11h
v1版本不带星
v2版本黑色星
v3版本
现在三个版本都提供服务。
灰度发布:基于权重的路由
v1版本升级到v2版本,准备给其10%的流量,没有问题在扩大百分比。
上面部署之后流量都是均匀分配的。
-
[root@master networking]# cat virtual-service-all-v1.yaml
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: productpage
-
spec:
-
hosts:
-
- productpage
-
http:
-
- route:
-
- destination:
-
host: productpage
-
subset: v1
-
---
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: reviews
-
spec:
-
hosts:
-
- reviews
-
http:
-
- route:
-
- destination:
-
host: reviews
-
subset: v1
-
---
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: ratings
-
spec:
-
hosts:
-
- ratings
-
http:
-
- route:
-
- destination:
-
host: ratings
-
subset: v1
-
---
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: details
-
spec:
-
hosts:
-
- details
-
http:
-
- route:
-
- destination:
-
host: details
-
subset: v1
-
---
-
[root@master networking]# cat destination-rule-all.yaml
-
apiVersion: networking.istio.io/v1alpha3
-
kind: DestinationRule
-
metadata:
-
name: productpage
-
spec:
-
host: productpage
-
subsets:
-
- name: v1
-
labels:
-
version: v1
-
---
-
apiVersion: networking.istio.io/v1alpha3
-
kind: DestinationRule
-
metadata:
-
name: reviews
-
spec:
-
host: reviews
-
subsets:
-
- name: v1
-
labels:
-
version: v1
-
- name: v2
-
labels:
-
version: v2
-
- name: v3
-
labels:
-
version: v3
-
[root@master networking]# cat virtual-service-reviews-
80-
20.yaml
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: reviews
-
spec:
-
hosts:
-
- reviews
-
http:
-
- route:
-
- destination:
-
host: reviews
-
subset: v1
-
weight:
80
-
- destination:
-
host: reviews
-
subset: v2
-
weight:
20
kubectl apply -f networking/virtual-service-reviews-90-10.yaml -n bookinfo
-
[root@master networking]# cat virtual-service-reviews-
90-
10.yaml
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: reviews
-
spec:
-
hosts:
-
- reviews
-
http:
-
- route:
-
- destination:
-
host: reviews
-
subset: v1
-
weight:
90
-
- destination:
-
host: reviews
-
subset: v2
-
weight:
10
-
[root@master networking]# cat virtual-service-reviews-v3.yaml
-
apiVersion: networking.istio.io/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: reviews
-
spec:
-
hosts:
-
- reviews
-
http:
-
- route:
-
- destination:
-
host: reviews
-
subset: v3
virtualservice:上面可以看到其实灰度发布一直调整的都是虚拟服务。根据权重转发到不同的子集上面,也就是destionationrule,subset hostname要匹配上。
destionationrule:主要将3个版本的reviews给匹配上,创建对应的子集subset,并且为其取名, 名字可以任意的去取名 v1 v2 v3 a b c,只要虚拟服务的subset和destionationrule 的subset对应上就行了,同时host也对应上。
在版本筛选上面它通过pod的标签去筛选,比如version。destionationrule就是通过版本号来创建subset子集。
- 将部署应用的deployment里面pod标签增加一个version: "v1",这也是istio实现灰度发布的重点,基于pod的标签,匹配到不同的版本,所以得对deployment部署的pod增加标签,标签可以自定义去设置,要规划好。
- 部署的deployment是要注入到istio里面
- 配置目标规则,destionationrule就可以根据服务版本去匹配了,去关联服务的版本。
- 创建虚拟服务,怎么分配流量,实现灰度发布。
灰度发布:基于请求内容的路由
类似于a/b测试,针对某些用户去采样,而不是随机的找一部分。根据http携带的信息去完成流量的分配。
-
[root
@master networking]# cat virtual
-service
-reviews
-jason
-v2
-v3.yaml
-
apiVersion: networking.istio.io
/v1alpha3
-
kind: VirtualService
-
metadata:
-
name: reviews
-
spec:
-
hosts:
-
- reviews
-
http:
-
-
match:
-
- headers:
-
end
-
user:
-
exact: jason
-
route:
-
- destination:
-
host: reviews
-
subset: v2
-
-
-
- route:
-
- destination:
-
host: reviews
-
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