云计算-Kubernetes+Istio 实现金丝雀发布:流量管理、熔断、流量镜像、ingreess、污点及pv案例实战
介绍
在微服务架构中,如何安全、高效地实现服务发布与流量管理是保障业务稳定性的核心挑战。金丝雀发布(Canary Release)、灰度发布等策略通过精细化的流量控制,可有效降低新版本上线风险, Istio 作为主流的服务网格(Service Mesh)工具。
此次Istio 在 Kubernetes 集群中的实战应用展开,从基础环境准备(如系统内核升级以适配 IPVS 模块)到 Istio 的完整部署流程,再到核心功能的实践 —— 包括通过虚拟服务(VirtualService)和目标规则(DestinationRule)实现流量分配(如 10% 流量导向金丝雀版本)、配置熔断机制保护服务稳定性、利用流量镜像实现无感知测试、通过 Ingress Gateway 暴露服务并控制外部访问路径
1.金丝雀发布-安装 Istio
由于 Kubernetes 使用的 IPVS 模块需要系统内核版本支持,试使用提供的软件包(Canary_v1.0.tar.gz 在 http 服务下)将系统内核进行升级,在 Kubernetes 集群上完成 Istio 的安装,并将 default Namespace 设置自动注入。
升级内核
设置 GRUB_DEFAULT=0,通过上面查询显示的编号为 0 的内核作为默认内核 [root@master ~]# vi /etc/default/grub #GRUB_DEFAULT=0必须在GRUB_DEFAULT=saved下面,否则升级失败(可能),放在首部内核升级失败 [root@master ~]# grub2-mkconfig -o /boot/grub2/grub.cfg [root@masater ~]#tar -zxvf Canary_v1.0.tar.gz [root@master]# cd kernel/ [root@master kernel]# yum install -y kernel-lt-5.4.119-1.el7.elrepo.x86_64.rpm linux-firmware-20200421-80.git78c0348.el7_9.noarch.rpm #重启 reboot #验证 [root@master ~]# uname -r 5.4.119-1.el7.elrepo.x86_64
安装Istio
#导入镜像 [root@masater ~]# cd images/ #确保harbor启动完成 [root@masater images]# ./image_push.sh 导入比较慢,多等等 #istio环境配置 [root@master ~]# mv istio-1.9.5/ /usr/local/bin/ [root@master ~]# cat /etc/profile export istio=/usr/local/bin/istio-1.9.5 export PATH=$PATH:$istio/bin [root@master ~]# source /etc/profile ------------------------- 检查运行环境: [root@master istio-1.9.5]# istioctl x precheck #没有报错即可 安装istio的核心组件: #注意修改IP(采用demo的安装方式) [root@master istio-1.9.5]# istioctl install -y --set profile=demo --set hub=192.168.30.10/library 查看安装的资源: [root@master istio-1.9.5]# kubectl -n istio-system get pods NAME READY STATUS RESTARTS AGE istio-egressgateway-6f9f4ddc9c-rgtcl 1/1 Running 0 6m46s istio-ingressgateway-78b47bc88b-52fd2 1/1 Running 0 6m46s istiod-67dbfcd4dd-56n66 1/1 Running 0 9m10s #将default Namespace设置自动注入(为资源设置标签) [root@master istio-1.9.5]# kubectl label namespace default istio-injection=enabled namespace/default labeled #查看目前开启自动注入的 Namespace: [root@master istio-1.9.5]# kubectl get ns --show-labels=true
2.金丝雀发布-流量控制
使用文件(istio-1.9.5/samples/helloworld/helloworld.yaml)在 default 命名空间下完成 hellworld 服务的部署,然后设置路由规则来控制流量分配,创建一个虚拟服务helloworld;再创建一个目标规则 helloworld,将 10%的流量发送到金丝雀版本(v2)。
[root@master ~]# cd /usr/local/bin/istio-1.9.5/samples/ [root@master samples]# kubectl apply -f helloworld/helloworld.yaml [root@master helloworld]# cat helloworld-route.yaml #创建一个虚拟服务helloworld apiVersion: networking.istio.io/v1alpha3 kind: VirtualService #针对指定服务的流量路由规则 metadata: name: helloworld spec: hosts: #必要字段:流量的目标主机 - helloworld http: #HTTP 流量规则的有序列表