该任务描述了如何使用Ingress资源配置Istio,来实暴露一个服务到服务网格集群的外部。
[warning] 使用
Istio Gateway,而不是使用Ingress,主要是因为可以使用Istio提供的所有功能。比如丰富的流量管理和安全的功能。
1. 准备工作
- 部署服务
$ kubectl apply -f samples/httpbin/httpbin.yaml
- 确定Ingress的IP和端口号
$ kubectl get svc istio-ingressgateway -n istio-system$ export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')$ export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')$ export SECURE_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="https")].port}')$ export TCP_INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="tcp")].port}')
2. 使用Ingress 资源配置ingress
Kubernetes Ingress资源暴露HTTP和HTTPS的资源给集群的外部,来让外部访问。
- 创建Istio
Gateway
$ kubectl apply -f - <<EOFapiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata:annotations:kubernetes.io/ingress.class: istioname: ingressspec:rules:- host: httpbin.example.comhttp:paths:- path: /status/*backend:serviceName: httpbinservicePort: 8000EOF
这个kubernetes.io/ingress.class注释是必须的,它用来告诉Istio gateway控制器,它应该处理这个Ingress,否则它将会被忽略。
- 使用curl来访问
httpbin服务。
$ curl -I -HHost:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/status/200
- 访问其它没有暴露的URL,你应该能看到404错误。
$ curl -I -HHost:httpbin.example.com http://$INGRESS_HOST:$INGRESS_PORT/headers
2. 下一步
2.1 TLS
Ingress支持指定的TLS设置。这是通过Istio支持的。但是引用的secret必须存在于istio-ingressgateway的部署名称空间中。 cert-manager可以用来产生这些证书。
2.2 指定path type
默认的,Istio对待路径是精确匹配,除非他们以/*或.*, 在这种情况下,他们将会以前辍匹配。其它的正则表达式是不支持的。
在Kubernetes 1.18中,添加一个新的字段pathType。 这允许明确的宣告一个路径是作为Exact或者Prefix
2.3 指定IngressClass
在kubernetes 1.18中,一个新的资源IngressClass被添加,取代了在Ingress资源上的kubernetes.io/ingress.class的注释。
假如你使用这个资源的话,那么需要设置controller字段为istio.io/ingress-controller ,如下案例所示
apiVersion: networking.k8s.io/v1beta1kind: IngressClassmetadata:name: istiospec:controller: istio.io/ingress-controller---apiVersion: networking.k8s.io/v1beta1kind: Ingressmetadata:name: ingressspec:ingressClassName: istio...
3. 清除本小节实验
删除Ingress配置且关闭httpbin服务
$ kubectl delete ingress ingress$ kubectl delete --ignore-not-found=true -f samples/httpbin/httpbin.yaml
