效果图
deploy
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-server-vs
spec:
hosts:
- "huwq.server.com"
gateways:
- gateway-all
http:
- route:
- destination:
host: my-service.default.svc.cluster.local
port:
number: 8080
[root@k8s-master1 rule]# cat Deployment.yaml
# my-service-v1 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service-v1
spec:
replicas: 1
selector:
matchLabels:
app: my-service
version: v1
template:
metadata:
labels:
app: my-service
version: v1
spec:
containers:
- name: my-service
image: hub.c.163.com/library/tomcat:7.0
ports:
- containerPort: 8080
---
# my-service-v2 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service-v2
spec:
replicas: 1
selector:
matchLabels:
app: my-service
version: v2
template:
metadata:
labels:
app: my-service
version: v2
spec:
containers:
- name: my-service
image: hub.c.163.com/library/tomcat:8.0
ports:
- containerPort: 8080
---
# my-service-v3 Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-service-v3
spec:
replicas: 1
selector:
matchLabels:
app: my-service
version: v3
template:
metadata:
labels:
app: my-service
version: v3
spec:
containers:
- name: my-service
image: hub.c.163.com/library/tomcat:9.0
ports:
- containerPort: 8080
svc
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-service
ports:
- port: 8080
targetPort: 8080
gw
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: gateway-all
spec:
# The selector matches the ingress gateway pod labels.
# If you installed Istio using Helm following the standard documentation, this would be "istio=ingress"
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
Destination
# my-service Destination Rule
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: my-service
spec:
host: my-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
vs
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-server-vs
spec:
hosts:
- "huwq.server.com"
gateways:
- gateway-all
http:
- route:
- destination:
host: my-service.default.svc.cluster.local
port:
number: 8080
权重分发
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-server-vs
spec:
hosts:
- "huwq.server.com"
gateways:
- gateway-all
http:
- route:
- destination:
host: my-service
subset: v1
weight: 100
- destination:
host: my-service
subset: v2
weight: 0
- destination:
host: my-service
subset: v3
weight: 0
header 分发
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: my-server-vs
spec:
hosts:
- "huwq.server.com"
gateways:
- gateway-all
http:
- match:
- headers:
version:
exact: "7.0" # 根据请求头 version 的值来路由
route:
- destination:
host: my-service
subset: v1
- match:
- headers:
version:
exact: "8.0"
route:
- destination:
host: my-service
subset: v2
- match:
- headers:
version:
exact: "9.0"
route:
- destination:
host: my-service
subset: v3
- route:
- destination:
host: my-service
subset: v1 # 默认路由