先创建v1和v2服务器。
[root@localhost ~]# cat test-istio.yaml
apiVersion: v1
kind: Pod
metadata:
name: v1
labels:
app: ttt
version: a
spec:
containers:
- name: test-istio-v1
image: nginx:latest
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- echo "this is v1" > /usr/share/nginx/html/index.html && nginx -g "daemon off;" && tail -f
---
apiVersion: v1
kind: Pod
metadata:
name: v2
labels:
app: ttt
version: b
spec:
containers:
- name: test-istio-v2
image: nginx:latest
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- echo "this is v2" > /usr/share/nginx/html/index.html && nginx -g "daemon off;" && tail -f
然后创建service,使用v1和v2共有的标签,使其流量可以同时到达v1和v2。
[root@localhost ~]# cat service.yaml
apiVersion: v1
kind: Service
metadata:
name: ttt
spec:
ports:
- port: 222
targetPort: 80
name: http
selector:
app: ttt
先在集群内部访问测试。
流量会以50%的概率随机到达两个pod。
然后创建一个gateway资源,管理访问任意域名,80端口的http协议流量。
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: gateway-test
spec:
selector:
istio: ingressgateway #设置istio控制器
servers:
- port:
number: 80
protocol: HTTP #设置外部请求信息
name: http
hosts:
- "*"
然后创建virtualservice,使用先前创建的gateway资源,这里当满足条件时就将流量发往a,其他所有流量发往b。
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: virt
spec:
hosts:
- "*"
gateways:
- gateway-test
http:
- route: #此处设置两个路由,分别路由到两个版本。
- destination:
host: ttt #设置连接service
subset: a #这里设置a,用于给路由连接
match:
- uri:
exact: /index.html #这里b的路径,满足这个条件就往版本a
- route:
- destination:
host: ttt #设置连接service
subset: b
这里注意service不要设置错误,然后创建路由
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: dest-nginx
spec:
host: ttt #设置service名称
subsets:
- name: a
labels:
version: a #设置pod标签
- name: b
labels:
version: b #设置pod标签
这里当vs流量发往a的会匹配version: a标签然后发往v1
这里当vs流量发往b的会匹配version: b标签然后发往v2
接下来访问测试,先获取ingress控制器的端口,发现这里80端口映射的2862
[root@localhost ~]# kubectl get service -n istio-system | grep ingress
istio-ingressgateway LoadBalancer 10.96.131.242 <pending> 15021:62733/TCP,80:2862/TCP,443:14201/TCP,31400:10275/TCP,15443:16212/TCP 36m
先测试没有满足条件的,按照设置会将流量发往v2
测试满足条件
至此istio流量管理完成