Search This Blog

February 4, 2019

LoadBalancer support with Minikube for Kubernetes

Kubernetes (aka K8s) is an open source system (from Google) for managing containerized applications across multiple hosts; providing basic mechanisms for deployment, maintenance, and scaling of applications.

Minikube is a tool that makes it easy to run Kubernetes locally on your laptop/desktop. Minikube runs a single-node K8s cluster inside a VM on your laptop for users looking to try out K8s or develop with it day-to-day.

Minikube for K8s is a popular method to get your hands dirty learning K8s. And a lot of K8s examples use LoadBalancer to teach how you can expose a service to the external world with k8s.
Exposing an External IP Address to Access an Application in a Cluster is one such tutorial. This tutorial however clearly states that you need a cloud environment to work on this tutorial.

For the purpose of convenience I tried running it all on my local machine that leverages Minikube for k8s. The only trouble is that when you run the command to list services, you will observe that the LoadBalancer service will have its External-IP status as "pending" as shown below:

Minikube doesn't come bundled with a LoadBalancer. So LoadBalancer services run fine with Minikube but with no real external-load balancer being created.

Should you want to access your application services via this load balancer service, you are kind of lost without an external-ip isn't? After all, the application services are internal services having only cluster level visibility.

Thanks to minikube, we do have a work-around. The LoadBalancer service get a node port assigned too so you can access services via $ minikube service my-loadbalancer-service-name to open browser or add --url flag to output service URL to terminal. You should see something like below:
$ minikube service nginx-ils-service --url

Not happy with the work around and want real meat in your dev-box as well? Then go about trying LoadBalancer emulation using the $ minikube tunnel command; see k8s docs for more on this.

Or a simpler approach is to patch up the EXTERNAL-IP using a kubectl command like -- 
$ kubectl patch service my-loadbalancer-service-name \
-n lb-service-namespace \
-p '{"spec": {"type": "LoadBalancer", "externalIPs":[""]}}'
Note: This external-ip that was patched was derived from the output of running $ minikube ip command.

Or the cleanest approach possibly is to install a LoadBalancer in your local cluster - you may try MetalLB for this.

1 comment:

  1. Thanks for the helpful tip and clarification that external LB IP are not supported by minikube (which is understandable b/c it doesn't have much control on external IP allocation). Got it working with multi-tier Yelb app example: using exact step above:
    service/hello-minikube NodePort 8080:30817/TCP 56d
    service/kubernetes ClusterIP 443/TCP 56d
    service/redis-server ClusterIP 6379/TCP 13h
    service/yelb-appserver ClusterIP 4567/TCP 13h
    service/yelb-db ClusterIP 5432/TCP 13h
    service/yelb-ui LoadBalancer 80:32442/TCP 13h

    C:\TOOLS\Kubernetes\practice>minikube service yelb-ui --url <== that URL still works.

    Will try running the patch of EXTERNAL_IP as well. Thanks1


Like it or hate it, feel free to share your feedback. Cheers!