首页 > 解决方案 > 如何在 Kubernetes 集群中运行 https 安全应用程序

问题描述

我想在 https 上的 kubernetes 集群内运行我的应用程序,并通过 https 在集群外公开它。我创建pod并暴露了端口443。之后,我创建了一个ClusterIP服务,它连接到 port 上的 pod443并公开 port 443。最后,我创建了一个ingress连接到 port 上的服务443。我在 GKE 上使用 helm chart 部署了所有这些资源。我使用NGINX Ingress控制器。你可以在这里找到图表。

当我通过 https 在集群内部访问应用程序时,它可以工作。

curl https://my-nginx.https-app-64-production --cacert /etc/nginx/ssl/tls.crt
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        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 and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

但是当我使用外部 url 访问它时,我得到以下错误。

curl https://staging.vs-creator.iotcrawler.eu/
<html>
<head><title>400 The plain HTTP request was sent to HTTPS port</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<center>The plain HTTP request was sent to HTTPS port</center>
<hr><center>nginx/1.9.1</center>
</body>
</html>

我无法弄清楚出了什么问题。我怀疑这与入口控制器配置有关。请帮助我。

标签: kuberneteskubernetes-ingressnginx-ingress

解决方案


在入口资源中使用以下注释

nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"

文档

使用 backend-protocol annotations 可以指示 NGINX 应该如何与后端服务通信。(替换旧版本中的安全后端)有效值:HTTP、HTTPS、GRPC、GRPCS、AJP 和 FCGI

默认情况下,NGINXHTTP在将请求转发到后端 pod 时使用,400 The plain HTTP request was sent to HTTPS port因为后端 pod 正在等待HTTPS请求。


推荐阅读