justmeandopensource/kubernetes

Preserving Source IP in NGINX Ingress Controller

salimbuet09 opened this issue · 2 comments

Hi Guys,

I am new to Kubernetes :) I am developing a simple flusk app, that stores client real IP address.
I have made docker image and deployed it in Kubernetes. Then created service. And then Installed kubernetes ingress-nginx.
Everything working fine, except I cant print client real IP address, it always returns pod. I have spent almost 5/6 days and tried all possible solutions from google.

deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: monsy-dep
  namespace: ingress-nginx
spec:
  selector:
    matchLabels:
      app: monsy-dep
  replicas: 3
  template:
    metadata:
      labels:
        app: monsy-dep
    spec:
      containers:
      - name: monsy-dep
        image: monsy-image-v1
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8000

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: monsy-service
  namespace: ingress-nginx
spec:
  selector:
    app: monsy-dep
  ports:
    - port: 80
      protocol: TCP
      targetPort: 8000
  type: NodePort
  externalTrafficPolicy: Local

#ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: monsy-ingress
  namespace: ingress-nginx
  annotations:
    kubernetes.io/ingress.class: "nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: monsy-service
                port:
                  number: 80

##ingress-nginx config map

apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    helm.sh/chart: ingress-nginx-4.0.1
    app.kubernetes.io/name: ingress-nginx
    app.kubernetes.io/instance: ingress-nginx
    app.kubernetes.io/version: 1.0.0
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/component: controller
  name: ingress-nginx-controller
  namespace: ingress-nginx
data:
  use-proxy-protocol: "true"

[here I tried so many properties, none worked]

note : main manifst file https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.0/deploy/static/provider/cloud/deploy.yaml

kubectl describe ingress monsy-ingress

Name:             monsy-ingress
Namespace:        ingress-nginx
Address:          MY_SERVER_PIBLIC_IP
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host        Path  Backends
  ----        ----  --------
  *           
              /   monsy-service:80 (10.42.0.44:8000,10.42.0.45:8000,10.42.0.46:8000)
Annotations:  kubernetes.io/ingress.class: nginx
              nginx.ingress.kubernetes.io/rewrite-target: /
Events:
  Type    Reason  Age                From                      Message
  ----    ------  ----               ----                      -------
  Normal  Sync    14m (x2 over 14m)  nginx-ingress-controller  Scheduled for sync

kubectl describe service monsy-service

Name:                     monsy-service
Namespace:                ingress-nginx
Labels:                   <none>
Annotations:              <none>
Selector:                 app=monsy-dep
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.43.215.245
IPs:                      10.43.215.245
Port:                     <unset>  80/TCP
TargetPort:               8000/TCP
NodePort:                 <unset>  32501/TCP
Endpoints:                10.42.0.44:8000,10.42.0.45:8000,10.42.0.46:8000
Session Affinity:         None
External Traffic Policy:  Local
Events:                   <none>

Did you ever manage to resolve this? If not, take a look at this:
https://hub.docker.com/r/mendhak/http-https-echo

Forgot to mention that when you deploy ingress.nginx, you need to set externalTrafficPolicy to Local so that external client IP is preserved - see this.