ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

istio网关安全-文件挂载

2021-08-27 18:32:08  阅读:198  来源: 互联网

标签:网关 httpbin istio secret 挂载 com example


生成服务器证书和私钥

创建一个根证书和私钥以为您的服务所用的证书签名:

$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt

httpbin.example.com 创建一个证书和私钥:

$ openssl req -out httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization" $ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin.example.com.csr -out httpbin.example.com.crt 

基于文件挂载的方式配置 TLS ingress 网关

本节中,您将配置一个使用 443 端口的 ingress 网关,以处理 HTTPS 流量。 首先使用证书和私钥创建一个 secret。该 secret 将被挂载为 /etc/istio/ingressgateway-certs 路径下的一个文件。 然后您可以创建一个网关定义,它将配置一个运行于端口 443 的服务。

1、创建一个 Kubernetes secret 以保存服务器的证书和私钥。使用 kubectl 在命名空间 istio-system 下创建 secret istio-ingressgateway-certs。Istio 网关将会自动加载该 secret。

该 secret 必须在 istio-system 命名空间下,且名为 istio-ingressgateway-certs,以与此任务中使用的 Istio 默认 ingress 网关的配置保持一致。
$ kubectl create -n istio-system secret tls istio-ingressgateway-certs --key httpbin.example.com.key --cert httpbin.example.com.crt
secret "istio-ingressgateway-certs" created

请注意,默认情况下,istio-system 命名空间下的所有 pod 都能挂载这个 secret 并访问该私钥。您可以将 ingress 网关部署到一个单独的命名空间中,并在那创建 secret,这样就只有这个 ingress 网关 pod 才能挂载它。

验证 tls.crttls.key 是否都已经挂载到 ingress 网关 pod 中:

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

2、为 443 端口定义 Gateway 并设置 server

证书和私钥必须位于 /etc/istio/ingressgateway-certs,否则网关将无法加载它们。
 kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
      privateKey: /etc/istio/ingressgateway-certs/tls.key
    hosts:
    - "httpbin.example.com"
EOF

3、配置路由以让流量从 Gateway 进入。定义与控制 Ingress 流量任务中相同的 VirtualService

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  hosts:
  - "httpbin.example.com"
  gateways:
  - httpbin-gateway
  http:
  - match:
    - uri:
        prefix: /status
    - uri:
        prefix: /delay
    route:
    - destination:
        port:
          number: 8000
        host: httpbin
EOF

4、使用 curl 发送一个 https 请求到 SECURE_INGRESS_PORT 以通过 HTTPS 访问 httpbin 服务。

--resolve 标志让 curl 在通过 TLS 访问网关 IP 时支持 SNIhttpbin.example.com--cacert 选项则让 curl 使用您创建的证书来验证服务器。

-HHost:httpbin.example.com 标志也包含了,但只有当 SECURE_INGRESS_PORT 与实际网关端口(443)不同(例如,您通过映射的 NodePort 来访问服务)时才真正需要。

通过发送请求到 /status/418 URL 路径,您可以很好地看到您的 httpbin 服务确实已被访问。 httpbin 服务将返回 418 I’m a Teapot 代码。

curl -v -HHost:httpbin.example.com --resolve httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST --cacert example.com.crt https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418
...
Server certificate:
  subject: CN=httpbin.example.com; O=httpbin organization
  start date: Oct 27 19:32:48 2019 GMT
  expire date: Oct 26 19:32:48 2020 GMT
  common name: httpbin.example.com (matched)
  issuer: O=example Inc.; CN=example.com
  SSL certificate verify ok.
SSL certificate verify ok.
...
HTTP/2 418
...
-=[ teapot ]=-

   _...._
 .'  _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
  |       ;/
  \_     _/
    `"""`
网关定义传播需要时间,因此您可能会得到以下报错: Failed to connect to httpbin.example.com port <your secure port>: Connection refused。请稍后重新执行 curl 命令。

curl 的输出中寻找 Server certificate 部分,尤其是找到与 common name 匹配的行:common name: httpbin.example.com (matched)。 输出中的 SSL certificate verify ok 这一行表示服务端的证书验证成功。 如果一切顺利,您还应该看到返回的状态 418,以及精美的茶壶图。

配置双向 TLS ingress 网关

 

标签:网关,httpbin,istio,secret,挂载,com,example
来源: https://www.cnblogs.com/fat-girl-spring/p/15194914.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有