Skip to content

4、Ingress 入口流量

Ingress 入口流量

image-20231118074447319

image-20231116112434001

我们知道在 Kubernetes 中我们会使用 Ingress来暴露 HTTP 流量的入口,在 Istio 中我们是通过 Gateway来暴露流量入口的,那么我们是否可以使用 Ingress对象来支持 Istio 的流量入口呢?答案是肯定的,但是我们还是建议使用 Gateway而不是 Ingress来使用 Istio 提供的完整功能集,例如丰富的流量管理和安全功能。

k8s gateway api。

目录

[toc]

本节实战

实战名称
🚩 实战:Kubernetes Ingress-2023.11.15(测试成功)
🚩 实战:Ingress Gateway-2023.11.15(测试成功)
🚩 实战:istio安全网关-2023.11.16(测试成功)
🚩 实战:TLS 透传-2023.11.16(测试成功)

Kubernetes Ingress

==🚩 实战:Kubernetes Ingress-2023.11.15(测试成功)==

实验环境:

bash
k8sv1.27.6(containerd:[root@master1 ~]#istioctl versionclientversion:1.19.3controlplaneversion:1.19.3dataplaneversion:1.19.3(8 proxies)

实验软件:

链接:https:A[实战步骤] -->B(1、 部署服务端测试应用)A[实战步骤] -->C(2、 创建ingress)A[实战步骤] -->D(3、 测试)

下面我们来使用 Ingress 资源配置 Istio 的入口网关,使用 Kubernetes Ingress 可以暴露从集群外到集群内服务的 HTTP 和 HTTPS 路由。

查看:

⚠️ ==特别注意:==

image-20231115202149163

可以看到这个 Ingress 对象和我们在 Kubernetes 中的使用方法没有什么区别,唯一不同的是我们这里使用了一个 kubernetes.io/ingress.class注解来告知 Istio Ingress Gateway 它应该处理该 Ingress,否则它将被忽略。

我们应用上面的资源清单:

image-20231115203434692

注意使用 -H标志将 Host的 HTTP 头设置为 httpbin.k8s.local, 因为 Ingress 中已经配置为处理访问该域名的请求,但是在测试环境中,该域名并没有相应的 DNS 绑定,当然我们也可以直接在 /etc/hosts中添加一个映射(当然是istio-ingressgateway-9c8b9b586-vj44lpod所在node节点了)。

image-20231115204409741

image-20231115204427150

上面这种方式更加简洁,而且可以避免使用注解的方式,但是需要注意的是,该资源对象只能在 Kubernetes 1.18 及以上版本中使用。

符合预期,测试结束。😘

此外 Ingress 还可以进行 TLS 设置,Istio 支持此功能,但是引用的 Secret 必须存在于 istio-ingressgateway部署的命名空间(通常是 istio-system)中。

通过 Kubernetes Ingress 我们可以将流量导入到 Istio 中,但是对于一些高级的流量管理功能,比如路由、熔断、限流等就很难实现了,所以我们还是建议使用 Istio 的 Gateway资源来暴露流量入口。

Ingress Gateway

img

前面我们都是通过 Istio 提供的 Gateway资源来暴露流量入口,与 Ingress 相比,Gateway 提供了更广泛的自定义和灵活性,并允许将 Istio 功能(例如监控和路由规则)应用于进入集群的流量。

Ingress Gateway

==🚩 实战:Ingress Gateway-2023.11.15(测试成功)==

实验环境:

bash
k8sv1.27.6(containerd:[root@master1 ~]#istioctl versionclientversion:1.19.3controlplaneversion:1.19.3dataplaneversion:1.19.3(8 proxies)

实验软件:

链接:https:A[实战步骤] -->B(1、 部署服务端测试应用)A[实战步骤] -->C(2、 创建ingress)A[实战步骤] -->D(3、 测试)

Ingress Gateway 描述在网格边界运作的负载均衡器,用于接收传入的 HTTP/TCP 连接。它会配置暴露的端口、协议等,但与 Kubernetes Ingress 资源不同,Gateway对象不会包括任何流量路由配置,和路由规则相关的配置需要使用 VirtualServiceDestinationRule资源来实现。

上面的资源清单我们就通过 Gateway对象暴露了 HTTP 流量的入口,然后通过 VirtualService对象来配置流量路由,一共配置了两个路由规则,允许流量流向路径 /status/delaygateways列表指定了哪些请求允许通 httpbin-gateway网关,所有其他外部请求均被拒绝并返回 404 响应。

测试结束。😘

当然除了这最基本的功能之外,Gateway还支持很多其他高级功能,比如 TLS、SNI等。

安全网关

我们已经了解了如何使用 Gateway对象来对外暴露 HTTP 服务,那么我们如何使用 TLS 或 mTLS 来暴露安全的 HTTPS 服务呢?

==🚩 实战:istio安全网关-2023.11.16(测试成功)==

实验环境:

bash
k8sv1.27.6(containerd:[root@master1 ~]#istioctl versionclientversion:1.19.3controlplaneversion:1.19.3dataplaneversion:1.19.3(8 proxies)

实验软件:

链接:https:opensslreq-x509-sha256-nodes-days365-newkeyrsa:2048-subj'/O=example Inc./CN=example.com'-keyoutexample_certs1/example.com.key-outexample_certs1/example.com.crt

image-20231116060013152

(1)

image-20231116060451784

image-20231116060524272

查看:

(2)

image-20231116061020563

(3)

1.单主机配置 TLS Ingress Gateway

接下来我们就可以在 Gateway对象中配置 TLS 了。

  • 首先我们需要创建一个 Secret对象,用来存储证书和密钥:
bash
$kubectlcreate-nistio-systemsecrettlshttpbin-credential\--key=example_certs1/httpbin.example.com.key\--cert=example_certs1/httpbin.example.com.crt

查看:

bash
[root@master1 ~]#kubectl get secret -nistio-systemNAMETYPEDATAAGEhttpbin-credentialkubernetes.io/tls211s……
  • 然后在 Gateway对象中需要定义一个 443端口的网关,并将 credentialName的值设置为 httpbin-credential,该值与 Secret的名称相同,TLS 模式的值应为 SIMPLE,内容如下所示:
yaml
#tls-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:Gatewaymetadata:name:mygatewayspec:selector:istio:ingressgateway# use istio default ingress gatewayservers:- port:number:443name:httpsprotocol:HTTPStls:mode:SIMPLEcredentialName:httpbin-credential# 必须和 secret 对象名称一致hosts:- httpbin.example.com
  • 接下来,通过定义相应的 VirtualService对象来配置网关的入口流量路由:
yaml
#tls-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:VirtualServicemetadata:name:httpbinspec:hosts:- "httpbin.example.com"gateways:- mygatewayhttp:- match:- uri:prefix:/status- uri:prefix:/delayroute:- destination:port:number:8000host:httpbin
  • 部署上面2个对象

完整清单:

yaml
#tls-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:Gatewaymetadata:name:mygatewayspec:selector:istio:ingressgateway# use istio default ingress gatewayservers:- port:number:443name:httpsprotocol:HTTPStls:mode:SIMPLEcredentialName:httpbin-credential# 必须和 secret 对象名称一致hosts:- httpbin.example.com---apiVersion:networking.istio.io/v1alpha3kind:VirtualServicemetadata:name:httpbinspec:hosts:- "httpbin.example.com"gateways:- mygatewayhttp:- match:- uri:prefix:/status- uri:prefix:/delayroute:- destination:port:number:8000host:httpbin

部署:

bash
[root@master1 istio-1.19.3]#kubectl apply -f tls-gateway.yaml gateway.networking.istio.io/mygatewaycreatedvirtualservice.networking.istio.io/httpbincreated

查看:

bash
[root@master1 istio-1.19.3]#kubectl get gatewayNAMEAGE……mygateway26s[root@master1 istio-1.19.3]#kubectl get vsNAMEGATEWAYSHOSTSAGE……httpbin["mygateway"] ["httpbin.example.com"] 26s……
  • 应用上面的资源对象后,我们就可以向 httpbin服务发送 HTTPS 请求了:

这里要用到的是istio-ingressgateway的443端口:

bash
[root@master1 istio-1.19.3]#kubectl get svc -nistio-systemNAMETYPECLUSTER-IPEXTERNAL-IPPORT(S) AGEistio-egressgatewayClusterIP10.109.85.119<none>80/TCP,443/TCP8distio-ingressgatewayLoadBalancer10.105.233.167<pending>15021:31410/TCP,80:31666/TCP,443:32213/TCP,31400:30291/TCP,15443:31787/TCP8distiodClusterIP10.109.185.251<none>15010/TCP,15012/TCP,443/TCP,15014/TCP8d
bash
# 获取 Ingress Gateway 的地址和 HTTPS 端口exportINGRESS_HOST=$(kubectlgetpod-listio=ingressgateway-nistio-system-ojsonpath='{.items[0].status.hostIP}')exportSECURE_INGRESS_PORT=$(kubectl-nistio-systemgetserviceistio-ingressgateway-ojsonpath='{.spec.ports[?(@.name=="https")].nodePort}')echo$INGRESS_HOSTecho$SECURE_INGRESS_PORT#[root@master1 istio-1.19.3]#echo $INGRESS_HOST#172.29.9.63#[root@master1 istio-1.19.3]#echo $SECURE_INGRESS_PORT#32213# 访问 httpbin 服务$curl-v-HHost:httpbin.example.com--resolve"httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST"\--cacertexample_certs1/example.com.crt"https:# ......*Initializing NSS with certpath:sql:/etc/pki/nssdb*CAfile:example_certs1/example.com.crtCApath:none*SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256*Server certificate:*subject:O=httpbinorganization,CN=httpbin.example.com*start date:Nov 13 06:58:40 2023 GMT*expire date:Nov 12 06:58:40 2024 GMT*common name:httpbin.example.com*issuer:CN=example.com,O=exampleInc.>GET /status/418 HTTP/1.1>User-Agent:curl/7.29.0>Accept:*/*>Host:httpbin.example.com><HTTP/1.1 418 Unknown<server:istio-envoy# ......-=[teapot]=-_...._.'_ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`

image-20231116063024423

  • 接着我们可以删除网关的 Secret 然后使用不同的证书和密钥重新创建它来更改网关的凭据:
bash
kubectl -n istio-system delete secret httpbin-credential

# 创建新的证书和密钥
kubectl create -n istio-system secret tls httpbin-credential \
  --key=example_certs2/httpbin.example.com.key \
  --cert=example_certs2/httpbin.example.com.crt
  • 使用新的证书链和 curl 来访问 httpbin 服务:
bash
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
  --cacert example_certs2/example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
...
HTTP/1.1 418 Unknown
...

    -=[ teapot ]=-

       _...._
     .'  _ _ `.|."` ^ `". _,\_;`"---"`||;/\_ _/`"""`

image-20231116063232207

  • 如果使用之前的证书链来访问 httpbin,则会失败:
bash
$curl-v-HHost:httpbin.example.com--resolve"httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST"\--cacertexample_certs1/example.com.crt"https:# ......*Initializing NSS with certpath:sql:/etc/pki/nssdb*CAfile:example_certs1/example.com.crtCApath:none*Server certificate:*subject:O=httpbinorganization,CN=httpbin.example.com*start date:Nov 13 06:59:17 2023 GMT*expire date:Nov 12 06:59:17 2024 GMT*common name:httpbin.example.com*issuer:CN=example.com,O=exampleInc.*NSS error -8182 (SEC_ERROR_BAD_SIGNATURE)*Peer's certificate has an invalid signature.*Closing connection 0curl:(60) Peer's certificate has an invalid signature.# ......

测试结束。😘

2.多个主机配置 TLS Ingress Gateway

上面是为单个主机配置 TLS 入口网关的方法。

此外我们还可以为多个主机(例如 httpbin.example.comhelloworld-v1.example.com)配置入口网关,入口网关配置有与每个主机相对应的唯一凭据。

  • 首先删除并使用原始证书和密钥重新创建 Secret 来恢复上一个示例中的 httpbin凭据:
bash
kubectl-nistio-systemdeletesecrethttpbin-credentialkubectlcreate-nistio-systemsecrettlshttpbin-credential\--key=example_certs1/httpbin.example.com.key\--cert=example_certs1/httpbin.example.com.crt
  • 然后我们启动 helloworld-v1示例:

还可以这样搞哦:

bash
# 创建 service=helloworld 的服务$kubectlapply-fsamples/helloworld/helloworld.yaml-lservice=helloworld# 创建 version=v1 的应用$kubectlapply-fsamples/helloworld/helloworld.yaml-lversion=v1
  • 然后创建 helloworld-credentialSecret:
bash
$kubectlcreate-nistio-systemsecrettlshelloworld-credential--key=example_certs1/helloworld.example.com.key--cert=example_certs1/helloworld.example.com.crt
  • 然后使用 httpbin.example.comhelloworld.example.com主机配置入口网关,创建如下所示的 Gateway对象:
yaml
#tls2-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:Gatewaymetadata:name:mygatewayspec:selector:istio:ingressgateway# use istio default ingress gatewayservers:- port:number:443name:https-httpbinprotocol:HTTPStls:mode:SIMPLEcredentialName:httpbin-credentialhosts:- httpbin.example.com- port:number:443name:https-helloworldprotocol:HTTPStls:mode:SIMPLEcredentialName:helloworld-credentialhosts:- helloworld.example.com

这里我们为 443端口定义一个具有两个 server配置的网关,将每个端口上的 credentialName值分别设置为 httpbin-credentialhelloworld-credential,将 TLS 模式设置为 SIMPLE

  • 然后定义相应的 VirtualService来配置网关的流量路由。
yaml
#tls2-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:VirtualServicemetadata:name:helloworldspec:hosts:- helloworld.example.comgateways:- mygatewayhttp:- match:- uri:exact:/helloroute:- destination:host:helloworldport:number:5000
  • 直接应用上面的资源对象即可

完整清单如下:

yaml
#tls2-gateway.yamlapiVersion:networking.istio.io/v1alpha3kind:Gatewaymetadata:name:mygatewayspec:selector:istio:ingressgateway# use istio default ingress gatewayservers:- port:number:443name:https-httpbinprotocol:HTTPStls:mode:SIMPLEcredentialName:httpbin-credentialhosts:- httpbin.example.com- port:number:443name:https-helloworldprotocol:HTTPStls:mode:SIMPLEcredentialName:helloworld-credentialhosts:- helloworld.example.com---apiVersion:networking.istio.io/v1alpha3kind:VirtualServicemetadata:name:helloworldspec:hosts:- helloworld.example.comgateways:- mygatewayhttp:- match:- uri:exact:/helloroute:- destination:host:helloworldport:number:5000

部署:

bash
kubectlapply-ftls2-gateway.yaml

查看:

bash
[root@master1 ~]#kubectl get gatewayNAMEAGEmygateway20m[root@master1 ~]#kubectl get vsNAMEGATEWAYSHOSTSAGEhelloworld["mygateway"] ["helloworld.example.com"] 19shttpbin["mygateway"] ["httpbin.example.com"] 20m
  • 然后向 helloworld.example.com发送 HTTPS 请求:
bash
$curl-v-HHost:helloworld.example.com--resolve"helloworld.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST"\--cacertexample_certs1/example.com.crt"https:# ......*Initializing NSS with certpath:sql:/etc/pki/nssdb*CAfile:example_certs1/example.com.crtCApath:none*SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256*Server certificate:*subject:O=helloworldorganization,CN=helloworld.example.com*start date:Nov 13 07:01:57 2023 GMT*expire date:Nov 12 07:01:57 2024 GMT*common name:helloworld.example.com*issuer:CN=example.com,O=exampleInc.# ......<HTTP/1.1 200 OK# ......Helloversion:v1,instance:helloworld-v1-b6c45f55-85l49

image-20231116064325345

  • 同样向 httpbin.example.com发送一个 HTTPS 请求,仍然返回一个茶壶:
bash
$curl-v-HHost:httpbin.example.com--resolve"httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST"\--cacertexample_certs1/example.com.crt"https:# ......-=[teapot]=-_...._.'_ _ `.
    | ."` ^ `". _,
    \_;`"---"`|//
      |       ;/
      \_     _/
        `"""`

测试结束。😘

3.双向 TLS Ingress Gateway

同样我们还可以扩展网关的定义以支持==双向 TLS==。双向 TLS 简称 mTLS,是一种相互身份验证的方法。mTLS 通过验证他们都拥有正确的私人密钥来确保网络连接两端的各方都是他们声称的身份。他们各自的 TLS 证书中的信息提供了额外的验证。

k8s里很多地方都是一个双向认证。

image-20231116064819341

通常在 TLS 中,服务器有一个 TLS 证书和一个公钥/私钥对,而客户端没有,典型的 TLS 流程是这样运作的:

  • 客户端连接到服务器
  • 服务器出示其 TLS 证书
  • 客户端验证服务器的证书
  • 客户端和服务器通过加密的 TLS 连接交换信息

img

然而,在 mTLS 中,客户端和服务器都有一个证书,并且双方都使用它们的公钥/私钥对进行身份验证。与常规 TLS 相比,mTLS 中有一些额外步骤来验证双方。

  • 客户端连接到服务器
  • 服务器出示其 TLS 证书
  • 客户端验证服务器的证书
  • 客户端出示其 TLS 证书
  • 服务器验证客户端的证书
  • 服务器授予访问权限
  • 客户端和服务器通过加密的 TLS 连接交换信息

img

接下来我们就来使用 mTLS 来配置 Ingress Gateway。

  • 首先还是删除其 Secret 并创建一个新的来更改入口网关的凭据,==需要注意的是服务器使用 CA 证书来验证其客户端,我们必须使用名称 cacert 来持有 CA 证书。==
bash
$ kubectl -n istio-system delete secret httpbin-credential

$ kubectl create -n istio-system secret generic httpbin-credential \
  --from-file=tls.key=example_certs1/httpbin.example.com.key \
  --from-file=tls.crt=example_certs1/httpbin.example.com.crt \
  --from-file=ca.crt=example_certs1/example.com.crt
  • 接下来更改 Gateway 的定义将 TLS 模式设置为 MUTUAL
yaml
#tls3-gateway.yaml
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: mygateway
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
    - port:
        number: 443
        name: https
        protocol: HTTPS
      tls:
        mode: MUTUAL # 设置为 MUTUAL,双向 TLS
        credentialName: httpbin-credential
      hosts:
        - httpbin.example.com

部署:

bash
kubectl apply -f tls3-gateway.yaml
  • 应用后我们来尝试使用之前的方法发送 HTTPS 请求:
bash
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
--cacert example_certs1/example.com.crt "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
# ......
*   CAfile: example_certs1/example.com.crt
  CApath: none
* NSS: client certificate not found (nickname not specified)
* NSS error -12227 (SSL_ERROR_HANDSHAKE_FAILURE_ALERT)
* SSL peer was unable to negotiate an acceptable set of security parameters.
* Closing connection 0
curl: (35) NSS: client certificate not found (nickname not specified)

image-20231116065925000

  • 从提示可以看出,客户端证书没有找到,由于我们这里配置的是双向 TLS 的方式,我们没有将客户端证书传递给 curl,我们可以通过 --cert--key 标志来进行传递:
bash
$ curl -v -HHost:httpbin.example.com --resolve "httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST" \
 --cacert example_certs1/example.com.crt --cert example_certs1/client.example.com.crt --key example_certs1/client.example.com.key \
 "https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418"
# ......
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: example_certs1/example.com.crt
  CApath: none
* NSS: client certificate from file
*       subject: O=client organization,CN=client.example.com
*       start date: Nov 13 07:02:22 2023 GMT
*       expire date: Nov 12 07:02:22 2024 GMT
*       common name: client.example.com
*       issuer: CN=example.com,O=example Inc.
* SSL connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate:
*       subject: O=httpbin organization,CN=httpbin.example.com
*       start date: Nov 13 06:58:40 2023 GMT
*       expire date: Nov 12 06:58:40 2024 GMT
*       common name: httpbin.example.com
*       issuer: CN=example.com,O=example Inc.
< HTTP/1.1 418 Unknown
# ......
<

    -=[ teapot ]=-

       _...._
     .'  _ _ `.|."` ^ `". _,\_;`"---"`||;/\_ _/`"""`

到这里我们就验证了通过 TLS 或 mTLS 将服务暴露到服务网格外。

  • 测试完成后记得清理下资源:(6)
bash
# 删除网关配置和路由:kubectldeletegatewaymygatewaykubectldeletevirtualservicehttpbinhelloworld# 删除 Secret、证书和密钥:kubectldelete-nistio-systemsecrethttpbin-credentialhelloworld-credentialrm-rf./example_certs1./example_certs2# 关闭 httpbin 和 helloworld 服务:kubectldelete-fsamples/httpbin/httpbin.yamlkubectldeletedeploymenthelloworld-v1kubectldeleteservicehelloworld

Ingress Gateway TLS 透传

前面我们了解了如何为 HTTP 服务配置 HTTPS 访问入口,那如果我们的后台服务本身就是 HTTPS 的,那么如何为 HTTPS 服务配置 HTTPS 访问入口即配置 Ingress Gateway 执行 SNI透传,而不是对传入请求进行 TLS 终止。

既然提到了 TLS 终止,那么我们可以先了解下什么是 ==TLS 终止==(TLS Termination)。

image-20231116074500594

1.TLS Termination

它的主要作用是,作为一个前置代理服务器接收外部到达的加密 TLS 流量,然后将其解密为 HTTP 明文,最后再将流量转发到内部的某个服务。

img

在实际应用中,内部的服务通常是以 HTTP 明文的方式通信,然后通过一个边界入口网关(ingress gateway)统一处理所有的 TLS 流量。这样 TLS 对所有的内部服务都是透明的,无需对每个服务去配置证书和私钥。通过一个统一的入口配置,我们还可以做很多事情,如日志,路由,路由策略等。

当然,对于一些安全级别较高的内部服务来说,未加密的流量可能是不可接受的,我们也可以配置来将加密的流量透传到该服务中,也就是这里我们需要的 SNI透传。

同样的如果反过来,就是 TLS Origination

2.TLS Origination

作为一个代理服务器,接收内部服务的 HTTP 明文流量,然后将其加密,最后转发到一个 HTTPS 服务上,该服务既可以是内部,也可以是外部的,但看起来就像是一个内部的服务,流程如下,

img

作为与边界入口网关对立的存在,出口网关也通常放置在网络的边界。所有的出口流量都被它接管,在这个节点上我们可以统一实施一些访问控制策略,或监控,或日志等,这和 Ingres Gateway 的功能其实是一样的,最大的不同在于将明文流量加密再转发。

3.TLS 透传

==🚩 实战:TLS 透传-2023.11.16(测试成功)==

实验环境:

bash
k8sv1.27.6(containerd:[root@master1 ~]#istioctl versionclientversion:1.19.3controlplaneversion:1.19.3dataplaneversion:1.19.3(8 proxies)

实验软件:

链接:https:opensslx509-req-sha256-days365-CAexample.com.crt-CAkeyexample.com.key-set_serial0-innginx.example.com.csr-outnginx.example.com.crt

查看:

image-20231116073143833

到这里我们的 HTTPS 服务就准备好了。

需要注意的是,这里的 VirtualService对象中我们配置的 tls字段了,描述了用于路由未终止的 TLS 流量(TLS/HTTPS)的匹配条件和动作。它通过 sniHosts指定了 SNI主机名,以便 Gateway可以将流量路由到正确的 VirtualService

完整清单如下:

部署:

bash
# 获取 Ingress Gateway 的地址和 HTTPS 端口exportINGRESS_HOST=$(kubectlgetpod-listio=ingressgateway-nistio-system-ojsonpath='{.items[0].status.hostIP}')exportSECURE_INGRESS_PORT=$(kubectl-nistio-systemgetserviceistio-ingressgateway-ojsonpath='{.spec.ports[?(@.name=="https")].nodePort}')# 访问 nginx 服务$curl-v--resolve"nginx.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST"--cacertexample.com.crt"https:# ......*SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384*Server certificate:*subject:O=someorganization,CN=nginx.example.com*start date:Nov 13 08:27:26 2023 GMT*expire date:Nov 12 08:27:26 2024 GMT*common name:nginx.example.com*issuer:CN=example.com,O=exampleInc.>GET / HTTP/1.1>User-Agent:curl/7.29.0>Host:nginx.example.com:30808>Accept:*/*><HTTP/1.1 200 OK<Server:nginx/1.21.5<Date:Mon,13 Nov 2023 08:50:27 GMT<Content-Type:text/html<Content-Length:615<Last-Modified:Tue,28 Dec 2021 15:28:38 GMT<Connection:keep-alive<ETag:"61cb2d26-267"<Accept-Ranges:bytes<<!DOCTYPEhtml><html><head><title>Welcome to nginx!</title><style>html{color-scheme:lightdark;}body{width:35em;margin:0auto;font-family:Tahoma,Verdana,Arial,sans-serif;}</style></head><body><h1>Welcome to nginx!</h1><p>If you see this page,the nginx web server is successfully installed andworking.Furtherconfigurationisrequired.</p><p>For online documentation and support please refer to<a href="http:Commercialsupportisavailableat<a href="http:<p><em>Thank you forusing nginx.</em></p></body></html>

image-20231116073853838

可以看到,我们成功访问了 NGINX 服务。

测试结束。😘

最近更新