不使用gateway的情况
如果没有gw,那么只需要 DestinationRule 和 VirtualService 就可以了
在DestinationRule中,host 需要对应的是 Kubernetes中的service
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: nginx-desrule
namespace: lizhe
spec:
host: nginx-service
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
对应的VirtualService
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-default
namespace: version
spec:
hosts:
- nginx-service
http:
- route:
- destination:
host: nginx-service
subset: v2
此时使用以下3种内部dns都可以访问
curl nginx-service/helloworld.html
curl nginx-service.version/helloworld.html
curl nginx-service.version.svc/helloworld.html
使用Gateway的情况
创建一个 Gateway
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: nginx-gateway
namespace: version
spec:
selector:
istio: ingressgateway # use istio default controller
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*.lizhe.com"
再创建DestinationRule
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: nginx-desrule
namespace: version
spec:
host: nginx-service.version.svc.cluster.local
subsets:
- name: v1
labels:
version: v1
- name: v2
labels:
version: v2
- name: v3
labels:
version: v3
最后用一个VirtualService把它们两个粘在一起
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-default-v2
namespace: version
spec:
hosts:
- "hello.lizhe.com"
gateways:
- nginx-gateway
http:
- route:
- destination:
host: nginx-service.version.svc.cluster.local
subset: v2
对于service可以使用全量名
下面是一个错误例子
不过可以使用 通配符 *
root@istio1:~/yamls# cat dr.yaml
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
name: nginx-desrule
namespace: version
spec:
host: nginx-service.version.svc.cluster.local
subsets:
- name: v2
labels:
version: v2
root@istio1:~/yamls# cat vs.yaml
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-default
namespace: version
spec:
hosts:
- "*.version.svc.cluster.local"
http:
- route:
- destination:
host: nginx-service.version.svc.cluster.local
subset: v2
root@istio1:~/yamls# kubectl apply -f ./
destinationrule.networking.istio.io/nginx-desrule unchanged
virtualservice.networking.istio.io/nginx-default configured
root@istio1:~/yamls#
gw和vs匹配
No | gw | vs | result |
1 | *.lizhe.com | hello.lizhe.com | correct |
2 | *.lizhe.com | *.lizhe.com | error |
3 | *.com | hello.lizhe.com | correct |
4 | *.com | *.lizhe.com | error |
使用2个 VirtualService ,一个持有 Gateway,一个不持有Gateway