Kubernetes Ingress资源在具有简单的HTTP流量的各种场景下相对易于使用,但是在复杂的场景中存在其缺点,主要是因为其围绕路由规则的功能非常有限。使用Istio进行入口时,最明显的优势是获得了与Istio提供的路由流量相同级别的配置选项。通过自定义资源以及TLS终止、监视、跟踪和其他一些功能,可以轻松地重写各种匹配规则、重定向路由等。

在Kubernetes Ingress中,入口控制器负责监视入口资源并配置入口代理。在Istio中,控制器(istiod)是控制层面的东西,它监视上述Kubernetes定制资源,并相应地配置istio入口代理。当然,处理所有传入流量的istio入口代理就是Envoy,它在单独的部署中运行。

示例

我们先部署一个httpbin服务,包括ServiceAccount、Service、Deployment资源

apiVersion: v1
kind: ServiceAccount
metadata:
name: httpbin
---
apiVersion: v1
kind: Service
metadata:
name: httpbin
labels:
app: httpbin
spec:
ports:
- name: http
port: 8000
targetPort: 80
selector:
app: httpbin
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpbin
spec:
replicas: 1
selector:
matchLabels:
app: httpbin
version: v1
template:
metadata:
labels:
app: httpbin
version: v1
spec:
serviceAccountName: httpbin
containers:
- image: docker.io/kennethreitz/httpbin
imagePullPolicy: IfNotPresent
name: httpbin
ports:
- containerPort: 80

应用Gateway和VirtualService资源

apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
name: httpbin-gateway
spec:
selector:
istio: ingressgateway # use Istio default gateway implementation
servers:
- port:
number: 80
name: http
protocol: HTTP
hosts:
- "*"
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
name: httpbin
spec:
hosts:
- "*"
gateways:
- httpbin-gateway
http:
- match:
- uri:
prefix: /headers
route:
- destination:
port:
number: 8000
host: httpbin

首先gateway监听80端口,将匹配规则下的流量都路由到目标地址

获取NodePort端口

kubectl get svc -n istio-system

通过任意node地址加上端口访问,例如我的是http://192.168.1.110:31514/headers