如何在没有入口的情况下在 AKS 上获取 HTTPS

How to get HTTPS on AKS without ingress

我的问题很简单。我有一个带有 LoadBalancer 服务的 AKS 部署,需要使用带证书的 HTTPS。

我该怎么做?

我在网上看到的所有内容都特别涉及到 Ingress 和 nginx-ingress。

但我的部署不是网站,它是一个 Dropwizard 服务,一个端口上有 REST API,另一个端口上有管理服务。我不想将端口映射到端口 80 上的路径,我想保持端口不变。为什么 HTTPS 与入口绑定?

我只想要带有证书的 HTTPS,没有其他更改,有没有简单的解决方案?

是的,在撰写本文时是正确的 Ingress 当前将在端口 80 或端口 443 上工作,它可能会扩展为使用任何端口,因为 nginx, Traefik, haproxy 等都可以在不同的端口上侦听。

因此,出于安全原因,您只能选择 LoadBalancer or a NodePort type of service. Type LoadBalancer will not work directly with TLS since the Azure load balancers are layer 4. So you will have to use Application Gateway and it's preferred to use an internal load balancer

由于您使用的是 Azure,因此您可以 运行 这样的事情(假设您的 K8s 集群配置正确,可以使用 Azure 云提供商,--cloud-provider 选项或云-控制器经理):

$ cat <<EOF
apiVersion: v1
kind: Service
metadata:
  name: your-app
  annotations:
    service.beta.kubernetes.io/azure-load-balancer-internal: "true"
spec:
  type: LoadBalancer
  ports:
  - port: <your-port>
  selector:
    app: your-app
EOF | kubectl apply -f -

这将在您喜欢的服务端口上创建一个 Azure 负载均衡器。在幕后,负载均衡器将指向节点上的一个端口,在节点内,将有防火墙规则路由到您的容器。然后你可以配置 Application Gateway. Here's a good article describing it but using port 80, you will have to change it use port 443 and configuring the TLS certs, and the Application Gateway also supports end to end TLS 以防你也想直接在你的应用程序上终止 TLS。

另一个选项是 NodePort,您可以 运行 像这样:

$ kubectl expose deployment <deployment-name> --type=NodePort

然后 Kubernetes 将在您的所有节点上选择一个随机端口,您可以在其中将流量发送到侦听 <your-port> 的服务。因此,在这种情况下,您将不得不手动创建一个带有 TLS 的负载均衡器或一个监听 TLS <your-port> 的流量源并将其转发到所有节点上的 NodePort,这个负载均衡器可以是任何类似 haproxy 的东西, nginx、Traefik 或其他支持终止 TLS 的东西。你也可以使用 Application Gateway 直接转发到你的节点端口,换句话说,定义一个监听器来监听你集群的 NodePort。

带有正确证书的 nginx 的 sidecar 容器(可能从 Secret 或 ConfigMap 加载)将在没有入口的情况下完成这项工作。 This seems to be a good example, using nginx-ssl-proxy container.