<p>I am setting up a kubernetes cluster in the On-Premise servers. Now for setting up external traffic, I can run Nginx Ingress behind Nodeport or I can run Nginx Deployment(Pods) with NodePort service exposed.</p>

<p>The only difference I got to know is with Ingress, I will get the sticky sessions which I anyhow do not need. So which one I should prefer and why?</p>

<p>Apart from this, I also have one requirement on Nginx Caching of htmls(with purging logic). So I have Nginx Deplpyment, then I can use PVC and PV. But what if I use Nginx Ingress. How will it work then.</p>

Ankit Bansal
<p>When you expose a <strong>Nginx Deployment</strong> you essentially create a <strong>L4 load balancer</strong> with <strong>Ingress</strong> you are creating a <strong>L7 load balancer</strong>.</p>

<p>If you want to host multiple domains like example1.com, example2.com so on the having a L7 load balancer makes sense also you can have defaulted backend defined if you want the request to endup somewhere special like some special service or endpoint.</p>

<p>Coming to 2nd part of enabling cache you can do it in ingress controller as follows:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mywebsite
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/proxy-buffering: "on"  # Important!
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 1m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;
</code></pre>

<p>say you want to enable it for 1 path not for others, like you want to enable it for /static/ path and not for / path then you can have:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mysite
  annotations:
    ingress.kubernetes.io/force-ssl-redirect: "true"
    nginx.ingress.kubernetes.io/proxy-body-size: 10m
spec:
  tls:
    - secretName: mysite-ssl
      hosts:
        - mysite.example.com
  rules:
    - host: mysite.example.com
      http:
        paths:
          - path: /
            backend:
              serviceName: mysite
              servicePort: http
---
# Leverage nginx-ingress cache for /static/
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: mysite-static
  annotations:
    nginx.ingress.kubernetes.io/proxy-buffering: "on"
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_cache static-cache;
      proxy_cache_valid 404 10m;
      proxy_cache_use_stale error timeout updating http_404 http_500 http_502 http_503 http_504;
      proxy_cache_bypass $http_x_purge;
      add_header X-Cache-Status $upstream_cache_status;
spec:
  rules:
    - host: mysite.example.com
      http:
        paths:
          - path: /static/
            backend:
              serviceName: mysite
              servicePort: http
</code></pre>

<p>Ultimately the design decision is yours, honestly its better to use ingress controller as it gives way more flexibility.
I hope this clears this up for you.</p>
