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
http://192.168.39.25:30062

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":["192.168.39.25"]}}'
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: https://github.com/mreferre/yelb/tree/master/deployments/platformdeployment/Kubernetes/yaml using exact step above:
    NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
    service/hello-minikube NodePort 10.96.112.211 8080:30817/TCP 56d
    service/kubernetes ClusterIP 10.96.0.1 443/TCP 56d
    service/redis-server ClusterIP 10.96.206.167 6379/TCP 13h
    service/yelb-appserver ClusterIP 10.96.181.112 4567/TCP 13h
    service/yelb-db ClusterIP 10.96.121.111 5432/TCP 13h
    service/yelb-ui LoadBalancer 10.96.175.15 80:32442/TCP 13h

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

    Will try running the patch of EXTERNAL_IP as well. Thanks1

    ReplyDelete

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