我们将两个版本归类到一个版本的pod上,去进行适配到一个pod上去,通过标签关联来做区分
对于多个版本,在同一个host,通过标签来标注不同的版本信息来进行管理,而后在vs中进行调用
- 子集
需要在DestinationRule集群上面进行配置
DestinationRule在cluster配置的,通过routes进行调度
基于子集,在本案例中根据version标签来备注,类似如下:
selector:
app: linuxea_app
version: v0.2
service
首先仍然照样创建一个service关联到标签
apiVersion: v1
kind: Service
metadata:
name: dpment
namespace: java-demo
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
selector:
app: linuxea_app
type: ClusterIP
定义DestinationRule集群
而后创建DestinationRule,在一个Host里面使用subsets通过标签版本关联两个service,两个service分别关联不同的pod,版本也不同
- host 与service保持一致
- 使用subsets
-
- 定义v11,并根据标签筛选v1.1到v11子集
- 定义v10,并根据标签筛选v1.0到v10子集
我们修改下标签
pod yaml
dpment-b 如下
--- apiVersion: v1 kind: Service metadata: name: dpment-b namespace: java-demo spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: linuxea_app version: v0.2 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: dpment-linuxea-b namespace: java-demo spec: replicas: 2 selector: matchLabels: app: linuxea_app version: v1.1 template: metadata: labels: app: linuxea_app version: v1.1 spec: containers: - name: nginx-b # imagePullPolicy: Always image: registry.cn-hangzhou.aliyuncs.com/marksugar/nginx:v2.0 ports: - name: http containerPort: 80
dpment-a如下
--- apiVersion: v1 kind: Service metadata: name: dpment-a namespace: java-demo spec: ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: linuxea_app version: v1.0 type: ClusterIP --- apiVersion: apps/v1 kind: Deployment metadata: name: dpment-linuxea-a namespace: java-demo spec: replicas: selector: matchLabels: app: linuxea_app version: v1.0 template: metadata: labels: app: linuxea_app version: v1.0 spec: containers: - name: nginx-a # imagePullPolicy: Always image: registry.cn-hangzhou.aliyuncs.com/marksugar/nginx:v1.0 ports: - name: http containerPort: 80
创建完成
(base) [root@master1 2]# kubectl -n java-demo get svc,pod NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/dpment ClusterIP 10.96.155.138 <none> 80/TCP 22h service/dpment-a ClusterIP 10.99.74.80 <none> 80/TCP 12s service/dpment-b ClusterIP 10.101.155.240 <none> 80/TCP 33s NAME READY STATUS RESTARTS AGE pod/cli 2/2 Running 0 22h pod/dpment-linuxea-a-777847fd74-fsnsv 2/2 Running 0 12s pod/dpment-linuxea-b-55694cb7f5-576qs 2/2 Running 0 32s pod/dpment-linuxea-b-55694cb7f5-lhkrb 2/2 Running 0 32s
DestinationRule
如果有多个版本,此时的subsets的逻辑组内就可以有很多个版本标签来匹配相对应的每个不同版本的服务
dr如下
---
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: dpment
namespace: java-demo
spec:
host: dpment # 与service保持一致
subsets: # 逻辑组
- name: v11 # 定义v11并根据标签,筛选v1.1到v11子集
labels:
version: v1.1
- name: v10 # 定义v10并根据标签,筛选v1.0到v10子集
labels:
version: v1.0
---
dr一旦创建完成在cluster中就能看到相关信息
IMP=$(kubectl -n java-demo get pod -l app=linuxea_app -o jsonpath={.items[0].metadata.name})
使用istioctl proxy-config cluster $IMP.java-demo
查看定义好的cluster
(base) [root@master1 2]# istioctl proxy-config cluster $IMP.java-demo
...
dpment-a.java-demo.svc.cluster.local 80 - outbound EDS
dpment-b.java-demo.svc.cluster.local 80 - outbound EDS
dpment.java-demo.svc.cluster.local 80 - outbound EDS dpment.java-demo
dpment.java-demo.svc.cluster.local 80 v10 outbound EDS dpment.java-demo
dpment.java-demo.svc.cluster.local 80 v11 outbound EDS dpment.java-demo
...
可以看到,在cluster中,每个service都是一个集群,这些并且可以被访问
bash-4.4# curl dpment
linuxea-dpment-linuxea-a-68dc49d5d-h6v6v.com-127.0.0.1/8 130.130.0.3/24 version number 1.0
bash-4.4# curl dpment-a
linuxea-dpment-linuxea-a-68dc49d5d-c9pcb.com-127.0.0.1/8 130.130.0.4/24 version number 1.0
bash-4.4# curl dpment-b
linuxea-dpment-linuxea-b-59b448f49c-nfkfh.com-127.0.0.1/8 130.130.0.13/24 version number 2.0
因此,我们删除多余的dpment-a和dpment-b
- dpment已经是我们现在的子集的service,dr和vs都是使用的dpment,删掉不会影响到dpment
- 一旦删除dpment-a和dpment-b,listnrners和cluster,routes都会被删除
(base) [root@master1 2]# kubectl -n java-demo delete svc dpment-a dpment-b
service "dpment-a" deleted
service "dpment-b" deleted
VirtualService
而后仍然需要配置一个VirtualService用于url路径路由,路由规则不变,但是路由的host就不变,都是dpment。只是subset不同
spec:
hosts:
- dpment # service
http:
- name: version
match:
- uri:
prefix: /version/
rewrite:
uri: /
route:
- destination:
host: dpment # service
subset: v10
- name: default
route:
- destination:
host: dpment # service
subset: v11
- 如果访问的是/version/的url就重写为/并且路由到dpment-b,否则就路由到dpment-a
yaml如下
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
name: dpment
namespace: java-demo
spec:
hosts:
- dpment
http:
- name: version
match:
- uri:
prefix: /version/
rewrite:
uri: /
route:
- destination:
host: dpment
subset: v10
- name: default
route:
- destination:
host: dpment
subset: v11
创建完成,在kiali中可以查看已经配置好的配置
services
在vs和dr中,均已配置完成
通过命令测试
bash-4.4# while true;do curl dpment; curl dpment/version/;sleep 0.$RANDOM;done
linuxea-dpment-linuxea-a-68dc49d5d-c9pcb.com-127.0.0.1/8 130.130.0.4/24 version number 1.0
linuxea-dpment-linuxea-b-59b448f49c-j7gk9.com-127.0.0.1/8 130.130.1.121/24 version number 2.0
linuxea-dpment-linuxea-a-68dc49d5d-h6v6v.com-127.0.0.1/8 130.130.0.3/24 version number 1.0
linuxea-dpment-linuxea-b-59b448f49c-j7gk9.com-127.0.0.1/8 130.130.1.121/24 version number 2.0
linuxea-dpment-linuxea-a-68dc49d5d-svl52.com-127.0.0.1/8 130.130.1.119/24 version number 1.0
linuxea-dpment-linuxea-b-59b448f49c-j7gk9.com-127.0.0.1/8 130.130.1.121/24 version number 2.0
linuxea-dpment-linuxea-a-68dc49d5d-c9pcb.com-127.0.0.1/8 130.130.0.4/24 version number 1.0
linuxea-dpment-linuxea-b-59b448f49c-nfkfh.com-127.0.0.1/8 130.130.0.13/24 version number 2.0
linuxea-dpment-linuxea-a-68dc49d5d-c9pcb.com-127.0.0.1/8 130.130.0.4/24 version number 1.0
linuxea-dpment-linuxea-b-59b448f49c-nfkfh.com-127.0.0.1/8 130.130.0.13/24 version number 2.0
仍然能看到一样的效果