说明
ServiceEntry和kubernetes service的功能是一样的,不同的地方是ServiceEntry指向外部地址或者其他kubernetes service或者endpoint
话不多说,现在结合VirtualService给出个Demo
VirtualService yaml文件
kind: VirtualService
metadata:
name: ssdemo
namespace: demo
spec:
gateways:
- kubeflow/kubeflow-gateway # 网关
hosts:
- '*'
http:
- match:
- uri:
prefix: /tensorboard/qinzhao/ssdemo/
rewrite:
uri: /
route:
- destination:
host: mymongodb.somedomain ## ServiceEntry指定的域名
port:
number: 6006 # 外部端口
timeout: 300s
ServiceEntry yaml
apiVersion: networking.istio.io/v1alpha3
kind: ServiceEntry
metadata:
name: external-svc-mongocluster
namespace: qinzhao
spec:
hosts:
- mymongodb.somedomain # 服务网格内地址
ports:
- number: 6006 # 外部协议端口
name: http
protocol: http # 外部协议名称
location: MESH_EXTERNAL # 表明是服务网格之外的
resolution: STATIC
endpoints:
- address: 192.168.1.10 # 外部服务地址
需要注意的是 VirtualService和ServiceEntry中的 mymongodb.somedomain
需要保持一致
WorkloadEntry使用例子
WorkloadEntry就是可以通过ServiceEntry选择的实例,对应kubernetes节点上的Pod实例
kind: WorkloadEntry
metadata:
name: details-vm-2
namespace: qinzhao
spec:
address: 192.168.1.10 # 外部地址
ports:
number: 6006
labels:
app: details-legacy
instance-id: vm1
ServiceEntry
kind: ServiceEntry
metadata:
name: details-svc
namespace: qinzhao
spec:
hosts:
- details.bookinfo.com
location: MESH_INTERNAL
ports:
- number: 80
name: http
protocol: HTTP
targetPort: 6006
resolution: DNS
workloadSelector:
labels:
app: details-legacy
VirtualService
kind: VirtualService
metadata:
name: ssdemo
namespace: qinzhao
spec:
gateways:
- kubeflow/kubeflow-gateway
hosts:
- '*'
http:
- match:
- uri:
prefix: /tensorboard/qinzhao/ssdemo/
rewrite:
uri: /
route:
- destination:
host: details.bookinfo.com # 对应上 ServiceEntry hosts的字段
port:
number: 80
timeout: 300s