7.5.9 创建虚拟服务

同样,接下来以域名myexample.com为例,创建链接到istio-myexample-customingressgateway的VirtualService,代码如下所示:


apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: istio-myexample-customvirtualservice
spec:
  hosts:
  - "www.myexample.com"
  gateways:
  - istio-myexample-customingressgateway
  http:
  - route:
    - destination:
        host: myexampleapp
        port:
          number: 80

然后,通过网关访问服务。以域名myexample.com为例,要获取对应的自定义网关服务的公网IP地址,可执行以下命令:


kubectl get svc -l istio=istio-myexample-customingressgateway
NAME        TYPE        CLUSTER-IP        EXTERNAL-IP       PORT(S)      AGE
istio-myexample-customingressgateway   LoadBalancer   172.19.10.11   114.55.193.155
   80:31562/TCP,443:30081/TCP   38m

设置INGRESS_HOST以及SECURE_INGRESS_PORT这两个环境变量,确定它们的正确取值,即替换成实际环境的地址值:


INGRESS_HOST=114.55.193.155 
SECURE_INGRESS_PORT=443

检查istio-ingressgateway pod是否正确地加载了证书和私钥:


kubectl exec -it -n default $(kubectl -n default get pods -l istio=istio-myexample-
customingressgateway -o jsonpath='{.items[0].metadata.name}') -- ls -al /etc/istio/
ingressgateway-certs

tls.crt和tls.key都应该保存在这个目录中。

检查Ingress Gateway证书中Subject字段的正确性:


kubectl exec -i -n default $(kubectl get pod -l istio=istio-myexample-customin-
gressgateway -n default -o jsonpath='{.items[0].metadata.name}')  -- cat /etc/istio/
ingressgateway-certs/tls.crt | openssl x509 -text -noout | grep 'Subject:'
        Subject: O=cert-manager, CN=myexample.com

检查Ingress Gateway的代理是否能够正确访问证书:


kubectl exec -ti $(kubectl get po -l istio=istio-myexample-customingressgateway 
-n default -o jsonpath={.items[0]..metadata.name}) -n default -- curl  127.0.0.1:
15000/certs
{
  "ca_cert": "",
  "cert_chain": "Certificate Path: /etc/istio/ingressgateway-certs/tls.crt, Serial 
Number: 5febf02dbc69641f4f2d5dbe9927bc20, Days until Expiration: 364"
}

至此,使用cert-manager部署自定义入口网关的所有步骤已完成。可通过HTTPS协议访问myexample.com服务,即curl发送https请求到istio-myexample-customingressgateway:


curl -k -HHost:www.myexample.com --resolve www.myexample.com:443:114.55.193.155
  https:// www.myexample.com
Welcome to myexample.com! This is one custom Istio Ingress Gateway powered by cert-manager!

回顾一下,获得证书后cert-manager将继续检查其有效性,并在接近到期时尝试更新。当证书上的“Not After”字段小于当前时间之后30天时,cert-manager认为证书即将到期。对于基于CA的颁发者,cert-manager将颁发证书,其中“Not After”字段设置为当前时间加上365天。