I’ve been giving a talk and demo about Kubernetes for a few months now, and during my demo, I have to wait for an ephemeral, external IP address from a load balancer to show off that Kubernetes does in fact work. Consequently, I get asked “Is there any way to have a static address so that you can actually point a hostname at it?” The answer is: of course you can.
Start up your Kubernetes environment, making sure to configure a service with a load balancer.
apiVersion: v1 kind: Service metadata: labels: name: frontend name: frontend spec: type: LoadBalancer ports: # The port that this service should serve on. - port: 80 targetPort: 8080 protocol: TCP # Label keys and values that must match in order to receive traffic for this service. selector: app: "todotodo-fe"
Once your app is up, make note of the External IP using
kubectl get services.
Now go to the Google Cloud Platform Console -> Networking -> External IP Addresses.
Find the IP you were assigned earlier. Switch it from “Ephemeral” to “Static.” You will have to give it a name and it would be good to give it a description so you know why it is static.
Then modify your service (or service yaml file) to point to this static address. I’m going to modify the yaml.
Once your yaml is modified you just need to run it; use
kubectl apply -f service.yaml.
To prove that the IP address works, you should
kubectl delete the service and then
kubectl apply, but you don’t have to do that. If you do that though, please be aware that although your IP address is locked in, your load balancer still needs a little bit of time to fire up.
Instead of this method, you can create a static IP address ahead of time and create the forwarding rules manually. I think that’s its own blog post, and I think it is just easier to let Container Engine do it.
I got lots of help for this post from wernight’s answer on StackOverflow, and the documentation on Kubernetes Services.
I can confirm this works with Google Container Engine. It should work with a Kubernetes cluster installed by hand on Google Cloud Platform. I couldn’t ascertain if it works on other cloud providers.
4 thoughts on “Making Kubernetes IP addresses static on Google Container Engine”
Thanks for the post, Terrance. I’ve been trying to make this work on GKE by first creating a new reserved IP address in the same region as my cluster, and then creating the service with the loadBalancerIP property set to the new address. So far it hasn’t worked. The services external IP remains ” ” and it appears that the backend is trying to forward the external port to the instance group rather than the k8s service nodeport. I’ve seen a couple of people reference the workflow you suggest, where the service is allowed to create the lb first and the external IP is then manually promoted. We’d really like to avoid the console and any timing issues as our stuff is supposed to be deployable in an automated way. Have you ever seen it work creating the address first?
My brackets got subbed out. The word between quotes was “pending.”
Nice tip – cheers!