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 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.