Source IP Not available within Cluster using Nginx Ingress
What happened: Our scenario is that we have deployed an AKS cluster using advanced networking and nginx ingress and we've noticed that the SourceIP address for requests originating from the internet is not visible on the ingress controller.
We have tried setting controller.service.externalTrafficPolicy=Local which didn't resolve the issue.
The IP addresses reported to nginx vary between pods/nodes connected to the VNET in which AKS is resident and the Azure LB's internal IP.
What you expected to happen:
Upon setting controller.service.externalTrafficPolicy=Local the source IP address should have been reported in nginx.
How to reproduce it (as minimally and precisely as possible):
- Build a new AKS cluster (v1.10.5) using advanced networking
- Deploy nginx-ingress using Helm (v.0.23.0)
- Enable
controller.service.externalTrafficPolicy=Localannotation in ingress controller - Try accessing your cluster and observing the IP addresses nginx is reporting; they will all be local
Anything else we need to know?: This is a pretty significant gap as short of a fix we have no idea which public IP addresses are making requests of our AKS hosted services.
Environment:
- Kubernetes version (use
kubectl version):
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.0", GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05", GitTreeState:"clean", BuildDate:"2017-12-15T21:07:38Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.5", GitCommit:"32ac1c9073b132b8ba18aa830f46b77dcceb0723", GitTreeState:"clean", BuildDate:"2018-06-21T11:34:22Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"linux/amd64"}
- Size of cluster (how many worker nodes are in the cluster?) 3
- General description of workloads in the cluster (e.g. HTTP microservices, Java app, Ruby on Rails, machine learning, etc.) Internet facing www and api endpoints
@moomzni I believe this the same issue as this: https://github.com/Azure/AKS/issues/495 ?
I've got a similar setup, and I'm seeing the public internet addresses being reported on the logs of the nginx-ingress-controller pod. Not quite sure what might be different, though.
I've reached out to Azure support and they have confirmed that a fix is being deployed and will be available in about a week
Many thanks to @marrobi for providing a temporary fix for this issue. Please save the following contents to yaml files, and deploy the config map and daemon set to mitigate this issue.
daemonset.yaml
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: source-ip-fix
labels:
app: source-ip-fix
spec:
template:
metadata:
labels:
name: source-ip-fix
spec:
hostNetwork: true
containers:
- name: source-ip-fix
securityContext:
privileged: true
image: alpine
imagePullPolicy: Always
command:
- /bin/sh
- -c
- |
echo "copy scripts to /tmp"
cp /scripts/*.sh /tmp
echo "chmod"
chmod +x /tmp/run.sh
echo "execute"
/tmp/run.sh
resources:
requests:
cpu: 5m
memory: 50Mi
volumeMounts:
- mountPath: /scripts
name: scripts
volumes:
- name: scripts
configMap:
name: source-ip-fix
configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: source-ip-fix
data:
run.sh: |
#!/bin/sh
echo "adding iptables"
apk add iptables
while true; do
echo $(iptables -t nat -L POSTROUTING )
# check if exists
iptables -t nat -C POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d 10.0.0.0/8 -j MASQUERADE
EXISTS=$?
# delete rule if exists
if [ $EXISTS -eq 0 ]
then
echo "Bad rule exists"
iptables -t nat -D POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d 10.0.0.0/8 -j MASQUERADE
echo "Deleted Rule"
else
echo "Bad rule does not exist"
fi
# Get subnet of azure nic, sufficient as source traffic comes from same subnet in our instance
SUBNET_CIDR=$(ip -o -f inet addr show | awk '/azure0/ {print $4}')
echo "Subnet is " ${SUBNET_CIDR}
# check if exists
iptables -t nat -C POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d $SUBNET_CIDR -j MASQUERADE
EXISTS=$?
if [ $EXISTS -eq 0 ]
then
echo "Rule already exists"
else
iptables -t nat -A POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d $SUBNET_CIDR -j MASQUERADE
echo "Added rule"
fi
sleep 60
done
exit 0
@JunSun17 as per @serbrech issue need to ensure specify --wait in the iptables commands above.
It works the x-forwarded-from header gets set with the source-ip with the following configuration.
The wait needs to be set and the lock file needs to be mounted into the daemonset.
configmap:
apiVersion: v1
kind: ConfigMap
metadata:
name: source-ip-fix
data:
run.sh: |
#!/bin/sh
echo "adding iptables"
apk add iptables
while true; do
echo $(iptables -t nat -L POSTROUTING)
# check if exists
iptables -t nat -C POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d 10.0.0.0/8 -j MASQUERADE -w
EXISTS=$?
# delete rule if exists
if [ $EXISTS -eq 0 ]
then
echo "Bad rule exists"
iptables -t nat -D POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d 10.0.0.0/8 -j MASQUERADE -w
echo "Deleted Rule"
else
echo "Bad rule does not exist"
fi
# Get subnet of azure nic, sufficient as source traffic comes from same subnet in our instance
SUBNET_CIDR=$(ip -o -f inet addr show | awk '/azure0/ {print $4}')
echo "Subnet is " ${SUBNET_CIDR}
# check if exists
iptables -t nat -C POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d $SUBNET_CIDR -j MASQUERADE -w
EXISTS=$?
if [ $EXISTS -eq 0 ]
then
echo "Rule already exists"
else
iptables -t nat -A POSTROUTING -m iprange ! --dst-range 168.63.129.16 -m addrtype ! --dst-type local ! -d $SUBNET_CIDR -j MASQUERADE -w
echo "Added rule"
fi
sleep 60
done
exit 0
daemonset
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: source-ip-fix
labels:
app: source-ip-fix
spec:
template:
metadata:
labels:
name: source-ip-fix
spec:
hostNetwork: true
containers:
- name: source-ip-fix
securityContext:
privileged: true
image: alpine
imagePullPolicy: Always
command:
- /bin/sh
- -c
- |
echo "copy scripts to /tmp"
cp /scripts/*.sh /tmp
echo "chmod"
chmod +x /tmp/run.sh
echo "execute"
/tmp/run.sh
resources:
requests:
cpu: 5m
memory: 50Mi
volumeMounts:
- mountPath: /scripts
name: scripts
- mountPath: /var/run/xtables.lock
name: xtables
volumes:
- name: scripts
configMap:
name: source-ip-fix
- name: xtables
hostPath:
path: /run/xtables.lock
Any news on this, should we wait for MSFT rollout or use the fix ?
Please use this mitigation for now. The fix is pending for more testing before roll out.
Any update?
any updates on this?
Hi All, the fix is being tested and currently in the phase of rolling out. ETA for WW deployment is end of Jan.
Just a sidenote: We are running with externalTrafficPolicy=Local and when we access our services from within Azure we usually get a 127.0.0.1 as source. But when we access from the real outside (i.e not Azure) we do get the proper source IP.
Will setting the externalTrafficPolicy to Local be required when this rolls out? I've read a few GitHub issues of stating that setting this to local is not a great idea.
Yes, it will be required by nigix-ingress to preserve IP from incoming requests.
@JunSun17 we are having the issue of the X-ForwardedFor header not properly being flowed through the Azure Load Balancer and through our NGINX ingress controller to our apps. I'm a little confused, is that what this issue is about?
Would be great to see this rolled out, I thought it was fixed a while back but glad to know fix is def on the way.
We are also waiting for that thing, we are using a multi cluster environment, and while trying to route the traffic from the external cluster into our internal cluster, the source ip is omitted and replaced by the pod ip from the external cluster in the X-Forwarded-For header
Found a workaround that work well for us:
Use the externalTrafficPolicy to Local and then you will have the header name X-Original-Forwarded-For with the source IP.
The only thing that we needed to add in our code is to check if that header exists -> use this IP, else -> use X-Forwarded-For (so we will not have any issue on another non-kubernetes environment that we have our application implemented.
Hope that it works for you as well

The fix has been released WW and ready for try out :)
@JunSun17 we are having trouble just getting the X-Forwarded-For header to make it through the Azure Load Balancer and the NGINX ingress to our app with the correct header value in tact. Do you have any idea how we can get this to work?