Rancher中使用Istio流量分配
2020-09-18 本文已影响0人
OPS_Joy
- 什么是istio
Istio 是一个由谷歌、IBM 与 Lyft 共同开发的开源项目,旨在提供一种统一化的微服务连接、安全保障、管理与监控方式,
Istio 项目能够为微服务架构提供流量管理机制,同时亦为其它增值功能(包括安全性、监控、路由、连接管理与策略等)
更详细的资料参考istio中文网站https://istio.io/latest/zh/docs/ -
通过rancher平台安装istio
图片.png
等待几分钟,安装完毕,查看组件信息
图片.png
图片.png -
测试流量分发准备
新建一个命名空间joy-ns,开启istio自动注入,一个busybox做客户端,两个deployment,两个service,一个通用service,一个istio虚拟service
简易流程图
图片.png
Deployment文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-v1
namespace: joy-ns
spec:
replicas: 1
selector:
matchLabels:
app: myapp
version: v1
template:
metadata:
labels:
app: myapp
version: v1
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
-----
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-v2
namespace: joy-ns
spec:
replicas: 1
selector:
matchLabels:
app: myapp
version: v2
template:
metadata:
labels:
app: myapp
version: v2
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v2
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80
service文件
apiVersion: v1
kind: Service
metadata:
name: myappv1-svc
namespace: joy-ns
labels:
app: myapp
version: v1
spec:
selector:
app: myapp
version: v1
type: ClusterIP
ports:
- port: 80
targetPort: 80
-----
apiVersion: v1
kind: Service
metadata:
name: myappv2-svc
namespace: joy-ns
labels:
app: myapp
version: v2
spec:
selector:
app: myapp
version: v2
type: ClusterIP
ports:
- port: 80
targetPort: 80
通用service
apiVersion: v1
kind: Service
metadata:
name: myapp-svc
namespace: joy-ns
labels:
app: myapp
spec:
selector:
app: myapp
type: ClusterIP
ports:
- port: 80
targetPort: 80
创建所有yaml
kubectl apply -f .
[root@harbor myapp]# kubectl get pods -n joy-ns
NAME READY STATUS RESTARTS AGE
busybox-65fd4885b7-hjfx9 2/2 Running 0 17h
myapp-v1-6f6788f845-r9cpm 2/2 Running 0 19h
myapp-v2-5c98456b77-r9z7r 2/2 Running 0 19h
[root@harbor myapp]# kubectl get svc -n joy-ns
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
busybox ClusterIP None <none> 42/TCP 17h
myapp-svc ClusterIP 10.43.111.247 <none> 80/TCP 17h
myappv1-svc ClusterIP 10.43.175.13 <none> 80/TCP 17h
myappv2-svc ClusterIP 10.43.21.36 <none> 80/TCP 17h
进入客户端访问
使用wget -q -O - http://myapp-svc不停刷新
可以看到默认情况下,应该是轮询状态50%分配。
现在通过启用istio虚拟服务,配置权重比例的路由
虚拟service文件
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: myapp-svc-vt
namespace: joy-ns
spec:
hosts:
- myapp-svc
http:
- route:
- destination:
host: myappv1-svc
weight: 90
- destination:
host: myappv2-svc
weight: 10
创建yaml,查看
图片.png
图片.png