金丝雀发布(Canary):也是一种发布策略,和国内常说的灰度发布是同一类策略。蓝绿部署是准备两套系统,在两套系统之间进行切换,金丝雀策略是只有一套系统,逐渐替换这套系统。
Istio 提供一种简单的方式来为已部署的服务建立网络,该网络具有负载均衡、服务间认证、监控等功能,只需要对服务的代码进行一点或不需要做任何改动。想要让服务支持 Istio,只需要在您的环境中部署一个特殊的 sidecar 代理,使用 Istio 控制平面功能配置和管理代理,拦截微服务之间的所有网络通信:
HTTP、gRPC、WebSocket 和 TCP 流量的自动负载均衡。
通过丰富的路由规则、重试、故障转移和故障注入,可以对流量行为进行细粒度控制。
可插入的策略层和配置 API,支持访问控制、速率限制和配额。
对出入集群入口和出口中所有流量的自动度量指标、日志记录和追踪。
通过强大的基于身份的验证和授权,在集群中实现安全的服务间通信。
Istio 旨在实现可扩展性,满足各种部署需求。
ISTIO 金丝雀部署
1.定义k8s的service demo4
apiVersion: v1
kind: Service
metadata:name: demo4namespace: test1labels:app: demo4
spec:ports:- port: 80targetPort: httpprotocol: TCPname: httpselector:app: demo4
2. 定义两个版本的 deploy 文件
两个版本都包含服务选择标签 app:demo4
apiVersion: apps/v1beta1
kind: Deployment
metadata:name: demo4-deployment-v1namespace: test1
spec:replicas: 1template:metadata:annotations:# 允许注入 sidecarsidecar.istio.io/inject: "true"labels:app: demo4version: v1spec:containers:- name: demo4-v1image: mritd/demolivenessProbe:httpGet:path: /port: 80scheme: HTTPinitialDelaySeconds: 30timeoutSeconds: 5periodSeconds: 10successThreshold: 1failureThreshold: 5readinessProbe:httpGet:path: /port: 80scheme: HTTPinitialDelaySeconds: 30timeoutSeconds: 5periodSeconds: 10successThreshold: 1failureThreshold: 5ports:- name: httpcontainerPort: 80protocol: TCP---
apiVersion: apps/v1beta1
kind: Deployment
metadata:name: demo4-deployment-v2namespace: test1
spec:replicas: 1template:metadata:labels:app: demo4version: v2annotations:sidecar.istio.io/inject: "true"spec:containers:- name: demo4-v2image: mritd/demolivenessProbe:httpGet:path: /port: 80scheme: HTTPinitialDelaySeconds: 30timeoutSeconds: 5periodSeconds: 10successThreshold: 1failureThreshold: 5readinessProbe:httpGet:path: /port: 80scheme: HTTPinitialDelaySeconds: 30timeoutSeconds: 5periodSeconds: 10successThreshold: 1failureThreshold: 5ports:- name: httpcontainerPort: 80protocol: TCP
上面定义和普通k8s定义蓝绿部署是一样的
3. 设置路由规则来控制流量分配
如将 10% 的流量发送到金丝雀版本(v2), 后面可以渐渐的把所有流量都切到金丝雀版本(v2),只需要修改weight: 10参数,注意v1和v2版本和一定要等于100.
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: demo4-vsnamespace: test1
spec:hosts:- demo4.a.comgateways:- demo4-gatewayhttp:- route:- destination:host: demo4.test1.svc.cluster.localsubset: v1weight: 90- destination:host: demo4.test1.svc.cluster.localsubset: v2weight: 10
---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:name: demo4namespace: test1
spec:host: demo4.test1.svc.cluster.localsubsets:- name: v1labels:version: v1- name: v2labels:version: v2
高层次的金丝雀部署
只允许特定网站上50%的用户流量路由到金丝雀(v2)版本,而其他用户则不受影响.
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:name: demo4-vsnamespace: test1
spec:hosts:- demo4.a.comgateways:- demo4-gatewayhttp:- match:- headers:cookie:regex: "^(.*?;)?(email=[^;]*@some-company-name.com)(;.*)?$"route:- destination:host: demo4.test1.svc.cluster.localsubset: v1weight: 50- destination:host: demo4.test1.svc.cluster.localsubset: v2weight: 50- route:- destination:host: demo4.test1.svc.cluster.localsubset: v1
说明:
Istio Virtual Service,用于控制当前deployment和金丝雀deployment流量分配的权重
Istio Destination Rule,包含当前deployment和金丝雀deployment的子集(subset)
Istio Gateway(可选),如果服务需要从容器集群外被访问则需要搭建gateway
Istio 服务网格提供了管理流量分配所需的基础控制,并完全独立于部署缩放。这允许简单而强大的方式来进行金丝雀测试和上线。
下一篇:MySQL数据库基本操作