下面,我们针对frontend到advertisement的请求,来实现具体的规则
redirect(重定向)¶
1、配置规则
[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.8
[root@master01 11.8]# kubectl apply -f redirect.yaml -n weather
2、查看规则
[root@master01 11.8]# kubectl get vs advertisement-route -n weather -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"advertisement-route","namespace":"weather"},"spec":{"hosts":["advertisement"],"http":[{"match":[{"uri":{"prefix":"/ad"}}],"redirect":{"authority":"advertisement.weather.svc.cluster.local","uri":"/maintenanced"}}]}}
creationTimestamp: "2023-11-09T01:59:22Z"
generation: 4
name: advertisement-route
namespace: weather
resourceVersion: "1199062"
uid: c9228253-7c58-4d29-b739-f3a90155d3a4
spec:
hosts:
- advertisement
http:
- match:
- uri:
prefix: /ad
redirect:
authority: advertisement.weather.svc.cluster.local
uri: /maintenanced
说明:将/ad 重定向到 /mantenanced
3、测试
[root@master01 11.8]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl -I http://advertisement:3003/ad'
HTTP/1.1 301 Moved Permanently
location: http://advertisement.weather.svc.cluster.local/maintenanced
date: Sat, 11 Nov 2023 03:02:36 GMT
server: envoy
transfer-encoding: chunked
rewrite(重写)¶
1、配置规则
[root@master01 ~]# cd /root/cloud-native-istio/11_traffic-management/11.9
[root@master01 11.9]# kubectl apply -f rewrite.yaml -n weather
2、查看规则
[root@master01 11.9]# kubectl get vs advertisement-route -n weather -o yaml
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
annotations:
kubectl.kubernetes.io/last-applied-configuration: |
{"apiVersion":"networking.istio.io/v1alpha3","kind":"VirtualService","metadata":{"annotations":{},"name":"advertisement-route","namespace":"weather"},"spec":{"hosts":["advertisement"],"http":[{"match":[{"uri":{"prefix":"/demo/"}}],"rewrite":{"uri":"/"},"route":[{"destination":{"host":"advertisement","subset":"v1"}}]}]}}
creationTimestamp: "2023-11-09T01:59:22Z"
generation: 5
name: advertisement-route
namespace: weather
resourceVersion: "1199403"
uid: c9228253-7c58-4d29-b739-f3a90155d3a4
spec:
hosts:
- advertisement
http:
- match:
- uri:
prefix: /demo/
rewrite:
uri: /
route:
- destination:
host: advertisement
subset: v1
3、测试
[root@master01 11.9]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl http://advertisement:3003/ad --silent -w "Status: %{http_code}\n"'
Status: 404
[root@master01 11.9]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl http://advertisement:3003/demo/ad --silent -w "Status: %{http_code}\n"'
{"adImgName":"airCleanImg"}
Status: 200
4、对比效果,把advertisement-route这条规则删除,再来测试
[root@master01 11.9]# kubectl delete vs advertisement-route -n weather
virtualservice.networking.istio.io "advertisement-route" deleted
[root@master01 11.9]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl http://advertisement:3003/demo/ad --silent -w "Status: %{http_code}\n"'
404 page not foundStatus: 404
[root@master01 11.9]# kubectl -n weather exec -it `kubectl get po -n weather|grep frontend-v1|awk '{print $1}'` -- bash -c 'curl http://advertisement:3003/ad --silent -w "Status: %{http_code}\n"'
{"adImgName":"tshirtImg"}
Status: 200