1. Ingress的基本概念

服务的访问入口,接收外部请求并转发到后端服务。在Istio里Ingress也是一个网关,是通过gateway这个方式去实现的。

2. 实验:创建Ingress网关

引入一个新服务httpbin,同时把服务通过Ingress暴露给外部使其可以访问

1. 部署httpbin服务

官方提供了配置清单,在安装目录下 samples/httpbin/httpbin.yaml

  1. ##################################################################################################
  2. # httpbin service
  3. ##################################################################################################
  4. apiVersion: v1
  5. kind: ServiceAccount
  6. metadata:
  7. name: httpbin
  8. ---
  9. apiVersion: v1
  10. kind: Service
  11. metadata:
  12. name: httpbin
  13. labels:
  14. app: httpbin
  15. spec:
  16. ports:
  17. - name: http
  18. port: 8000
  19. targetPort: 80
  20. selector:
  21. app: httpbin
  22. ---
  23. apiVersion: apps/v1
  24. kind: Deployment
  25. metadata:
  26. name: httpbin
  27. spec:
  28. replicas: 1
  29. selector:
  30. matchLabels:
  31. app: httpbin
  32. version: v1
  33. template:
  34. metadata:
  35. labels:
  36. app: httpbin
  37. version: v1
  38. spec:
  39. serviceAccountName: httpbin
  40. containers:
  41. - image: docker.io/kennethreitz/httpbin
  42. imagePullPolicy: IfNotPresent
  43. name: httpbin
  44. ports:
  45. - containerPort: 80

kubectl apply -f samples/httpbin/httpbin.yaml
image.png
查看所有的pod
kubectl get pod
image.png

2. 定义Ingress gateway

gateway2.yaml清单如下
可以看到,依然使用的是gateway来部署Ingress,选择Istio默认的Ingressgateway作为selector,同时暴露80端口作为他的访问点。这里设置了一个host,希望请求是以这个方式进行访问

  1. apiVersion: networking.istio.io/v1alpha3
  2. kind: Gateway
  3. metadata:
  4. name: httpbin-gateway
  5. spec:
  6. selector:
  7. istio: ingressgateway
  8. servers:
  9. - port:
  10. number: 80
  11. name: http
  12. protocol: HTTP
  13. hosts:
  14. - "httpbin.example.com"

kubectl apply -f linshi/gateway2.yaml
image.png

3. 定义对应的虚拟服务

还需要给gateway创建一个对应的虚拟服务,让他来对这个服务做一个简单的路由
虚拟服务的配置清单如下,vs.yaml
首先需要和gateway对应起来,

  1. apiVersion: networking.istio.io/v1alpha3
  2. kind: VirtualService
  3. metadata:
  4. name: httpbin
  5. spec:
  6. hosts:
  7. - "httpbin.example.com"
  8. gateways:
  9. - httpbin-gateway # 和gateway对应起来(绑定)
  10. http:
  11. - match:
  12. - uri:
  13. prefix: /status
  14. - uri:
  15. prefix: /delay
  16. route:
  17. - destination:
  18. port:
  19. number: 8000
  20. host: httpbin

kubectl apply -f linshi/vs.yaml
image.png
查看目前虚拟服务信息
kubectl get vs
image.png

4. 测试

curl -I -HHost:httpbin.example.com http://localhost/status/200