Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static outgoing IP in Kubernetes

I run a k8s cluster in google cloud (GKE) and a MySQL server in aws (RDS). Pods need to connect to RDS which only allows connections from certain IP. How can I configure outgoing traffic to have a static IP?

like image 689
Shouichi Avatar asked Dec 14 '16 02:12

Shouichi


People also ask

How do I set a static IP address for Kubernetes?

Allocate static IPs under Networking > External IP addresses, either: Deploy once without loadBalancerIP , wait until you've an external IP allocated when you run kubectl get svc , and look up that IP in the list on that page and change those from Ephemeral to Static.

How does Kubernetes assign IPs?

Kubernetes assigns an IP address (the Pod IP) to the virtual network interface in the Pod's network namespace from a range of addresses reserved for Pods on the node. This address range is a subset of the IP address range assigned to the cluster for Pods, which you can configure when you create a cluster.

Can a service have its own IP address in Kubernetes?

Kubernetes gives Pods their own IP addresses and a single DNS name for a set of Pods, and can load-balance across them.

Can we give same IP to two Kubernetes pods?

One way or another you'll have to consolidate onto the same pod. You can create a deployment that proxies each of the ports to the appropriate service.


2 Answers

I had the same problem to connect to a sftp server from a Pod. To solve this, first you need to create an external IP address:

gcloud compute addresses create {{ EXT_ADDRESS_NAME }} --region {{ REGION }} 

Then, I suppose that your pod is assigned to your default-pool node cluster. Extract your default-pool node name:

gcloud compute instances list | awk '{ print $1 }' | grep default-pool 

Erase default external ip of the vm instance:

gcloud compute instances delete-access-config {{ VM_DEFAULT-POOL_INSTANCE }} --access-config-name external-nat 

Add your external static ip created before:

gcloud compute instances add-access-config {{ VM_DEFAULT-POOL_INSTANCE }} --access-config-name external-nat --address {{ EXT_ADDRESS_IP }} 

If your Pod is not attached to the default-pool node, don't forget to select it with a nodeSelector:

nodeSelector:     cloud.google.com/gke-nodepool: {{ NODE_NAME }}  
like image 125
Luc Charpentier Avatar answered Oct 08 '22 11:10

Luc Charpentier


I made some research and I found a couple of things.

The thing we are looking for is called "egress IPs" or NAT-as-a-Service and they are both not yet available in GKE.

In any case we have two different options:

  1. create a NAT Gateway VM which acts as an egress proxy. Here is a nice article talking about that (google cloud NAT gateway)
  2. assign static IPs to container cluster VM instances

Hope it helps!

like image 23
Michele Orsi Avatar answered Oct 08 '22 13:10

Michele Orsi