How to use LoadBalancer IP as the outgoing / outbound / egress IP of the pods

3/6/2019

I have 1 deployment on ManagedKubernetes Cluster on Alibaba Cloud with service spec.type=LoadBalancer I can successfuly use that IP for incoming traffic

But the outbound ip seems still use the NAT Gateway IP for the deployment that have service spec.type=LoadBalancer

So how can I use the same IP for outgoing and incoming traffic for deployment that use LoadBalancer ?

-- Fauzan
alibaba-cloud
flannel
kubernetes
project-calico

1 Answer

3/6/2019

You might find your solution here:

https://kubernetes.io/docs/tutorials/services/source-ip/#source-ip-for-services-with-type-loadbalancer

As of Kubernetes 1.5, packets sent to Services with Type=LoadBalancer are source NAT’d by default because all schedulable Kubernetes nodes in the Ready state are eligible for load-balanced traffic. So if packets arrive at a node without an endpoint, the system proxies it to a node with an endpoint, replacing the source IP on the packet with the IP of the node (as described in the previous section). You can test this by exposing the source-ip-app through a loadbalancer

$ kubectl expose deployment source-ip-app --name=loadbalancer --port=80 --target-port=8080 --type=LoadBalancer
service/loadbalancer exposed

$ kubectl get svc loadbalancer
NAME           TYPE           CLUSTER-IP    EXTERNAL-IP       PORT(S)   AGE
loadbalancer   LoadBalancer   10.0.65.118   104.198.149.140   80/TCP    5m

$ curl 104.198.149.140
CLIENT VALUES:
client_address=10.240.0.5
...
-- Hard Coder
Source: StackOverflow