Troubleshooting Kubernetes Service Exposure with Minikube

Introduction

In the world of Kubernetes, managing and exposing services can sometimes be a challenging task, especially when dealing with deprecated commands and network configurations. Recently, I encountered an issue while following instructions from the book Kubernetes in Action. The command kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1 did not work due to its obsolescence. This blog post will walk you through the troubleshooting steps I took to resolve the issue and successfully expose my service using Minikube.

Step 1: Updating Deprecated Commands

The original command from the book was:

kubectl run kubia --image=luksa/kubia --port=8080 --generator=run/v1

This command is deprecated and no longer works in recent versions of kubectl. Instead, I updated it using the deploy and expose commands:

kubectl create deployment kubia --image=luksa/kubia
kubectl expose deployment kubia --type=LoadBalancer --port=8080

Step 2: Handling LoadBalancer Issues

After exposing the deployment, I noticed that the LoadBalancer service was stuck in a pending state:

kubectl get services

Output:

NAME         TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
kubia        LoadBalancer   10.103.233.110   <pending>     8080:31041/TCP   64s

This issue occurs because the default LoadBalancer does not work in Minikube or local Kubernetes clusters. To resolve this, I changed the service type to NodePort:

Delete the Existing Service: kubectl delete service kubia

Expose the Deployment Using NodePort:

kubectl expose deployment kubia --type=NodePort --port=8080

Step 3: Debugging Connectivity Issues

Even after changing to NodePort, I was unable to access the service via web browser or command line. To debug this, I logged into Minikube using SSH:

minikube ssh

From within the Minikube VM, I tested the service connectivity:

curl http://10.97.147.167:8080

Then I use minikube ip and ipconfig to check the ip address of minikube and my host computer, they are different because minikube is running in a virtual machine.

Step 4: Using Port Forwarding

To access the service locally, I set up port forwarding:

kubectl port-forward service/kubia 8080:8080

This allowed me to access the service at http://localhost:8080:

curl http://localhost:8080

Step 5: Using Minikube Tunnel

For a more seamless access method, I tried using minikube tunnel:

sudo minikube tunnel

However, I encountered issues accessing the service using the external IP provided by the tunnel. To resolve this, I used the minikube service command to find the correct URL:

minikube service kubia --url

This command provided the correct URL to access the service.

You can also run minikube service kubia to quickly get the service ip and visit it.

Conclusion

Troubleshooting Kubernetes service exposure can be a complex process, especially when dealing with deprecated commands and network configurations. By updating commands, changing service types, and using tools like port forwarding and Minikube tunnel, I was able to successfully expose and access my service. If you encounter similar issues, I hope this guide helps you navigate through the troubleshooting steps.