<p>I have a Kubernetes cluster running on Google Compute Engine and I would like to assign static IP addresses to my external services (<code>type: LoadBalancer</code>). I am unsure about whether this is possible at the moment or not. I found the following sources on that topic:</p>

<ul>
<li><a href="https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/user-guide/services.md#type-loadbalancer" rel="noreferrer">Kubernetes Service Documentation</a> lets you define an external IP address, but it fails with <em>cannot unmarshal object into Go value of type []v1.LoadBalancerIngress</em></li>
<li>The <a href="https://stackoverflow.com/questions/29770679/how-to-expose-kubernetes-service-to-public-without-hardcoding-to-minion-ip">publicIPs field</a> seems to let me specify external IPs, but it doesn't seem to work either</li>
<li><a href="https://github.com/kubernetes/kubernetes/issues/10323" rel="noreferrer">This Github issue</a> states that what I'm trying to do is not supported yet, but will be in Kubernetes v1.1</li>
<li>The <a href="http://kubernetes.io/v1.0/docs/user-guide/services.html#choosing-your-own-ip-address" rel="noreferrer">clusterIP field</a> also lets me specify an IP address, but fails with "<em>provided IP is not in the valid range</em>"</li>
</ul>

<p>I feel like the usage of static IPs is quite important when setting up web services. Am I missing something here? I'd be very grateful if somebody could enlighten me here!</p>

<p>EDIT: For clarification: I am not using Container Engine, I set up a cluster myself using the official installation instructions for Compute Engine. All IP addresses associated with my k8s services are marked as "ephemeral", which means recreating a kubernetes service may lead to a different external IP address (which is why I need them to be static).</p>

<p><strong>TL;DR</strong> Google Container Engine running Kubernetes <strong>v1.1</strong> supports <code>loadBalancerIP</code> just mark the auto-assigned IP as <strong>static</strong> first.</p>

<p>Kubernetes v1.1 supports <a href="https://kubernetes.io/docs/api-reference/v1.7/#servicespec-v1-core" rel="noreferrer">externalIPs</a>:</p>

<pre><code>apiVersion: v1
kind: Service
spec:
  type: LoadBalancer
  loadBalancerIP: 10.10.10.10
  ...
</code></pre>

<p>So far there isn't a really good consistent documentation on how to use it on GCE. What is sure is that this IP must first be one of your pre-allocated <strong>static</strong> IPs.</p>

<p>The <a href="https://cloud.google.com/compute/docs/load-balancing/http/cross-region-example" rel="noreferrer">cross-region load balancing</a> documentation is mostly for Compute Engine and not Kubernetes/Container Engine, but it's still useful especially the part "Configure the load balancing service".</p>

<p>If you just create a Kubernetes LoadBalancer on GCE, it will create a network Compute Engine > Network > Network load balancing > Forwarding Rule pointing to a target pool made of your machines on your cluster (normally only those running the Pods matching the service selector). It looks like deleting a namespace doesn't nicely clean-up the those created rules.</p>

<hr>

<h2>Update</h2>

<p>It is actually now supported (even though under documented):</p>

<ol>
<li>Check that you're running Kubernetes 1.1 or later (under <a href="https://console.cloud.google.com/project/_/kubernetes/list" rel="noreferrer">GKE</a> edit your cluster and check "Node version")</li>
<li>Allocate static IPs under <a href="https://console.cloud.google.com/project/_/networking/addresses/list" rel="noreferrer">Networking > External IP addresses</a>, either:

<ul>
<li>Deploy once without <code>loadBalancerIP</code>, wait until you've an external IP allocated when you run <code>kubectl get svc</code>, and look up that IP in the list on that page and change those from <em>Ephemeral</em> to <em>Static</em>.</li>
<li>Click "Reserver a static address" regional in the region of your cluster, attached to None.</li>
</ul></li>
<li>Edit your <em>LoadBalancer</em> to have <code>loadBalancerIP=10.10.10.10</code> as above (adapt to the IP that was given to you by Google).</li>
</ol>

<p>Now if you delete your LoadBalancer or even your namespace, it'll preserve that IP address upon re-reploying on that cluster.</p>

<hr>

<h2>Update 2016-11-14</h2>

<p>See also <a href="https://beroux.com/english/articles/kubernetes/?part=2" rel="noreferrer">Kubernetes article</a> describing how to set up a static IP for single or multiple domains on Kubernetes.</p>
