istio实现对外暴露服务

时间:2023-03-10 00:39:28
istio实现对外暴露服务

1、确认istio-ingressgateway是否有对外的IP

kubectl get  service istio-ingressgateway -n istio-system

istio实现对外暴露服务

如果 EXTERNAL-IP 有值(IP 地址或主机名),则说明您的环境具有可用于 Ingress 网关的外部负载均衡器。如果 EXTERNAL-IP 值是 <none>(或一直是 <pending> ),则说明可能您的环境并没有为 Ingress 网关提供外部负载均衡器的功能。

可以通过以下方法添加外部IP

kubectl edit  service istio-ingressgateway -n istio-system

istio实现对外暴露服务

kubectl get  service istio-ingressgateway -n istio-system

istio实现对外暴露服务

二、建立deployment、service、Gateway、VirtualService

1)新建nginx-istio-test.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-app
spec:
replicas:
template:
metadata:
annotations:
sidecar.istio.io/inject: "true"
labels:
app: nginx-app
spec:
containers:
- name: nginx-app
image: nginx
imagePullPolicy: IfNotPresent
ports:
- containerPort:
---
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
labels:
svcname: nginx-svc
spec:
ports:
- port:
protocol: TCP
targetPort:
selector:
app: nginx-app ---
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: nginx-gateway
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number:
name: nginx-http
protocol: HTTP
hosts:
- istio-nginx.boshen.com ---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: nginx-vs
spec:
hosts:
- istio-nginx.boshen.com
gateways:
- nginx-gateway
http:
- match:
- uri:
prefix: /
route:
- destination:
port:
number:
host: nginx-svc

在Gateway中

istio实现对外暴露服务

VirtualService 映射的就是 Envoy 中的 Http Route Table,大家可以注意到上面的 VirtualService 配置文件中有一个 gateways 字段,如果有这个字段,就表示这个 Http Route Table 是绑在 ingressgateway 的 Listener 中的;如果没有这个字段,就表示这个 Http Route Table 是绑在 Istio 所管理的所有微服务应用的 Pod 上的。

2)部署yaml

kubectl apply -f nginx-istio-test.yaml

3)在k8s的master节点和node节点的/etc/hosts里面加上以下内容

istio实现对外暴露服务

4)在windows机器上的C:\Windows\System32\drivers\etc\hosts里面加上

istio实现对外暴露服务

5)在浏览器访问:http://istio-nginx.boshen.com/

istio实现对外暴露服务