不要在 Kubernetes 的入口级别终止 SSL
Don not terminate SSL at ingress level for Kubernetes
我有一个 Java 应用程序 运行 在 tomcat 服务器内(在 pod 内),配置为使用 https。
我正在使用 nginx 入口。问题是,nginx ingress 正在终止 SSL 并仅将纯 http 转发到 tomcat 服务器(实际上是转发到 pod)。由于 tomcat 服务器配置为仅使用 HTTPS,因此它不接受流量。
以下无效:
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
终于找到答案了:
我必须添加以下两行:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
所以入口是这样的(我还添加了一些评论来描述和显示我尝试过但没有用的选项,这样你就不会浪费你的时间):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-resource-staging
namespace: staging-space
annotations:
kubernetes.io/ingress.class: nginx #You may deploy any number of ingress controllers within a cluster. When you create an ingress, you should annotate each ingress with the appropriate ingress.class to indicate which ingress controller should be used if more than one exists within your cluster.
#If you do not define a class, your cloud provider may use a default ingress controller.
#nginx.ingress.kubernetes.io/ssl-passthrough: "true"
##Following 2 lines are important, otherwise the SSL is terminated at the ingress level and the
## traffic sent to the service is plain http and then tomcat complains that the host and port combination
## needs https connection (in the tomcat server we have enabled the HTTPS internally)
## We want to forward the HTTPS traffic to the pods
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
#tls:
# - hosts:
# - yourhost.com
rules:
- host: yourhost.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: my-app-service
port:
#number: 8080
number: 8443
默认情况下禁用 SSL 直通,需要使用 --enable-ssl-passthrough 标志启动控制器。
因此,如果您想使用注释 nginx.ingress.kubernetes.io/ssl-passthrough
,则需要使用 --enable-ssl-passthrough 标志启动 Nginx Ingress Controller
此外,由于 SSL 直通适用于 OSI 模型 (TCP) 的第 4 层而不是第 7 层 (HTTP),因此使用 SSL 直通会使 Ingress 对象上设置的所有其他注释无效。
编辑:
如果您将入口注释 nginx.ingress.kubernetes.io/ssl-passthrough 与入口控制器的 --enable-ssl-passthrough=true 标志一起使用,则 SSL 终止将在您的 Tomcat 服务器上发生荚。因此,您的客户端浏览器收到的 SSL 服务器证书就是您的 Tomcat SSL 服务器证书。在这种情况下,您的客户端浏览器必须信任 Tomcat SSL 服务器证书。此 SSL 直通发生在第 4 层 TCP,因此 NGINX 入口控制器不会解密来自客户端浏览器的 SSL 流量,它只是将其传递到 Tomcat 服务器 Pod。
如果您只使用注解 nginx.ingress.kubernetes.io/backend-protocol: "HTTPS",那么第一个 SSL 终止发生在您的入口控制器上。因此,您的客户端浏览器收到的 SSL 服务器证书就是您的 Nginx Ingress Controller SSL 服务器证书,您的客户端浏览器必须信任它。然后从 Nginx Ingress Controller 到 Tomcat Pod 的通信使用另一个 SSL 加密。在这种情况下,您的 Nginx Ingress Controller 将必须信任 Tomcat SSL 服务器证书,并且您拥有双重 SSL 加密和解密。
如果您使用注解 nginx.ingress.kubernetes.io/force-ssl-redirect: "true" 那么您的所有 http 请求都将使用 308 重定向 http 代码重定向到 https。你调用的是 http:// 还是 https:// ?
下面是代码和文档链接
https://github.com/kubernetes/ingress-nginx/blob/master/rootfs/etc/nginx/lua/lua_ingress.lua
https://github.com/openresty/lua-nginx-module
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
当您在入口资源中进行更改时,检查 /etc/nginx/nginx.conf 如何在 nginx 控制器 pod 内更改
我有一个 Java 应用程序 运行 在 tomcat 服务器内(在 pod 内),配置为使用 https。 我正在使用 nginx 入口。问题是,nginx ingress 正在终止 SSL 并仅将纯 http 转发到 tomcat 服务器(实际上是转发到 pod)。由于 tomcat 服务器配置为仅使用 HTTPS,因此它不接受流量。
以下无效:
nginx.ingress.kubernetes.io/ssl-passthrough: "true"
终于找到答案了:
我必须添加以下两行:
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
所以入口是这样的(我还添加了一些评论来描述和显示我尝试过但没有用的选项,这样你就不会浪费你的时间):
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-resource-staging
namespace: staging-space
annotations:
kubernetes.io/ingress.class: nginx #You may deploy any number of ingress controllers within a cluster. When you create an ingress, you should annotate each ingress with the appropriate ingress.class to indicate which ingress controller should be used if more than one exists within your cluster.
#If you do not define a class, your cloud provider may use a default ingress controller.
#nginx.ingress.kubernetes.io/ssl-passthrough: "true"
##Following 2 lines are important, otherwise the SSL is terminated at the ingress level and the
## traffic sent to the service is plain http and then tomcat complains that the host and port combination
## needs https connection (in the tomcat server we have enabled the HTTPS internally)
## We want to forward the HTTPS traffic to the pods
nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"
nginx.ingress.kubernetes.io/force-ssl-redirect: "true"
spec:
#tls:
# - hosts:
# - yourhost.com
rules:
- host: yourhost.com
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: my-app-service
port:
#number: 8080
number: 8443
默认情况下禁用 SSL 直通,需要使用 --enable-ssl-passthrough 标志启动控制器。
因此,如果您想使用注释 nginx.ingress.kubernetes.io/ssl-passthrough
,则需要使用 --enable-ssl-passthrough 标志启动 Nginx Ingress Controller此外,由于 SSL 直通适用于 OSI 模型 (TCP) 的第 4 层而不是第 7 层 (HTTP),因此使用 SSL 直通会使 Ingress 对象上设置的所有其他注释无效。
编辑:
如果您将入口注释 nginx.ingress.kubernetes.io/ssl-passthrough 与入口控制器的 --enable-ssl-passthrough=true 标志一起使用,则 SSL 终止将在您的 Tomcat 服务器上发生荚。因此,您的客户端浏览器收到的 SSL 服务器证书就是您的 Tomcat SSL 服务器证书。在这种情况下,您的客户端浏览器必须信任 Tomcat SSL 服务器证书。此 SSL 直通发生在第 4 层 TCP,因此 NGINX 入口控制器不会解密来自客户端浏览器的 SSL 流量,它只是将其传递到 Tomcat 服务器 Pod。
如果您只使用注解 nginx.ingress.kubernetes.io/backend-protocol: "HTTPS",那么第一个 SSL 终止发生在您的入口控制器上。因此,您的客户端浏览器收到的 SSL 服务器证书就是您的 Nginx Ingress Controller SSL 服务器证书,您的客户端浏览器必须信任它。然后从 Nginx Ingress Controller 到 Tomcat Pod 的通信使用另一个 SSL 加密。在这种情况下,您的 Nginx Ingress Controller 将必须信任 Tomcat SSL 服务器证书,并且您拥有双重 SSL 加密和解密。
如果您使用注解 nginx.ingress.kubernetes.io/force-ssl-redirect: "true" 那么您的所有 http 请求都将使用 308 重定向 http 代码重定向到 https。你调用的是 http:// 还是 https:// ?
下面是代码和文档链接
https://github.com/kubernetes/ingress-nginx/blob/master/rootfs/etc/nginx/lua/lua_ingress.lua
https://github.com/openresty/lua-nginx-module
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
当您在入口资源中进行更改时,检查 /etc/nginx/nginx.conf 如何在 nginx 控制器 pod 内更改