<p>Question: Within Kubernetes, how do I configure the nginx ingress to treat traffic from an elastic load balancer as HTTPS, when it is defined as TCP?</p>

<hr>

<p>I am working with a Kubernetes cluster in an AWS environment. I want to use an nginx ingress to do path-based routing of the HTTPS traffic; however, I do <em>not</em> want to do SSL termination or reencryption on the AWS elastic load balancer.</p>

<p>The desired setup is:</p>

<p>client -> elastic load balancer -> nginx ingress -> pod</p>

<p>Requirements:<br>
1. The traffic be end-to-end encrypted.<br>
2. An AWS ELB must be used (the traffic cannot go directly into Kubernetes from the outside world).   </p>

<p>The problem that I have is that to do SSL passthrough on the ELB, I must configure the ELB as TCP traffic. However, when the ELB is defined as TCP, all traffic bypasses nginx.</p>

<p>As far as I can tell, I can set up a <a href="https://github.com/kubernetes/ingress-nginx/blob/e8d81034b8f6b3308caac85280e4cf3d93baee1c/docs/user-guide/exposing-tcp-udp-services.md" rel="nofollow noreferrer">TCP passthrough via a ConfigMap</a>, but that is merely another passthrough; it does not allow me to do path-based routing within nginx.</p>

<p>I am looking for a way to define the ELB as TCP (for passthrough) while still having the ingress treat the traffic as HTTPS.</p>

<p>I can define the ELB as HTTPS, but then there is a second, unnecessary negotiate/break/reencrypt step in the process that I want to avoid if at all possible.</p>

Magua
<p>To make it more clear I'll start from <a href="https://en.wikipedia.org/wiki/OSI_model" rel="nofollow noreferrer">OSI model</a>, which tells us that <strong>TCP</strong> <strong>is level 4</strong> protocol and <strong>HTTP/HTTPS is level 7</strong> protocol. So, frankly speaking <code>HTTP/HTTP</code> data is <a href="http://www.omnisecu.com/tcpip/tcpip-encapsulation-decapsulation.php" rel="nofollow noreferrer">encapsulated</a> to <code>TCP</code> data before doing rest levels encapsulations to transfer packet to another network device.</p>

<p>If you setup <a href="https://www.sumologic.com/insight/aws-elastic-load-balancers-classic-vs-application/" rel="nofollow noreferrer">Classic (TCP) LoadBalancer</a> it stops reading packet data after reading TCP part, which is enough to decide (according to LB configuration) to which <code>IP address</code> and to which <code>IP port</code> this data packet should be delivered. After that LB takes the TCP payload data and wrap it around with another TCP layer data and send it to the destination point (which in turn cause all other OSI layers applied).</p>

<p>To make your configuration works as expected, it's required to expose <a href="https://kubernetes.github.io/ingress-nginx/deploy/" rel="nofollow noreferrer">nginx-ingress-controller</a> Pod using <a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer">NodePort service</a>. Then Classic ELB can be configured to deliver traffic to any cluster node to port selected for that NodePort service. Usually it is in between <code>30000</code> and <code>32767</code>. Sou your LB pool will look like the following:</p>

<p>Let's imagine cluster nodes have IP addresses <code>10.132.10.1...10</code> and NodePort port is <code>30276</code>. </p>

<pre><code>ELB Endpoint 1:  10.132.10.1:30276
ELB Endpoint 2:  10.132.10.2:30276
...
ELB Endpoint 10: 10.132.10.10:30276
</code></pre>

<p>Note: In case of AWS ELB, I guess, nodes DNS names should be used instead of IP addresses. </p>

<p>So it should cause the following sequence of traffic distribution from a client to Kubernetes application Pod:</p>

<ol>
<li>Client sends TCP packet with HTTP/HTTPS request in payload to <strong>ELB_IP:ELB_port</strong> (<code>a.b.c.d:80</code>).</li>
<li>ELB receives IP packet, analyze its TCP data, finds the appropriate endpoint from backend pool (whole list of Kubernetes cluster nodes), and creates another TCP packet with the same HTTP/HTTPS data inside, and also replaces destination IP and destination TCP port to cluster <strong>node IP</strong> and <strong>Service NodePort TCP</strong> port (<code>l.m.n.k:30xxx</code>) and then send it to the selected destination.</li>
<li>Kubernetes node receives TCP packet and using the iptables rules changes the destination IP and destination port of the TCP packet again, and forward the packet (according to the Nodeport Service configuration) to destination pod. In this case it would be nginx-ingress-controller pod. </li>
<li>Nginx-ingress-controller pod receives the TCP packet, and because according to TCP data it have to be delivered locally, extracts HTTP/HTTP data out of it and send the data (HTTP/HTTPS request) to Nginx process inside the Nginx container in the Pod, </li>
<li>Nginx process in the container receives HTTP/HTTPS request, decrypt it (in case of HTTPS) and analyze all HTTP headers. </li>
<li>According to <code>nginx.conf</code> settings, Nginx process change HTTP request and deliver it to the cluster service, specified for the configured host and URL path.</li>
<li>Nginx process sends changed HTTP request to the backend application.</li>
<li>Then TCP header is added to the HTTP request and send it to the backend service <code>IP_address:TCP_port</code>.</li>
<li>iptables rules defined for the backend service, deliver packet to one of the service endpoints (application Pods).</li>
</ol>

<p><strong>Note</strong>: To terminate SSL on ingress controller you have to create SSL certificates that includes ELB IP and ELB FQDN in the SAN section.</p>

<p><strong>Note</strong>: If you want to terminate SSL on the application Pod to have end to end SSL encryption, you may want to configure nginx to bypass SSL traffic.</p>

<p><strong>Bottom line</strong>: ELB configured for delivering TCP traffic to Kubernetes cluster works perfectly with nginx-ingress controller if you configure it in the correct way.  </p>

<p>In GKE (Google Kubernetes Engine) if you create a Service with type:LoadBalancer it creates you exactly TCP LB which forward traffic to a Service NodePort and then Kubernetes is responsible to deliver it to the Pod. EKS (Elastic Kubernetes Service) from AWS works in pretty much similar way.</p>
