Access Minikube Loadbalancer Service From Host Machine

ghz 7months ago ⋅ 82 views

I am trying to learn how to use Kibernetes with Minikube and have the following deployment and service:

---
kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app: myapp
  ports:
  - protocol: "TCP"
    # Port accessible inside cluster
    port: 8081
    # Port to forward to inside the pod
    targetPort: 8080
    # Port accessible outside cluster
    nodePort: 30002
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myappdeployment
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: tutum/hello-world
        ports:
        - containerPort: 8080

I expect to be able to hit this service from my local machine at

http://192.168.64.2:30002

As per the command: minikube service exampleservice --url but when I try to access this from the browser I get a site cannot be reached error.

Some information that may help debugging:

kubectl get services --all-namespaces:

NAMESPACE     NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default       exampleservice         LoadBalancer   10.104.248.158   <pending>     8081:30002/TCP           26m
default       kubernetes             ClusterIP      10.96.0.1        <none>        443/TCP                  2h
default       user-service-service   LoadBalancer   10.110.181.202   <pending>     8080:30001/TCP           42m
kube-system   kube-dns               ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   2h
kube-system   kubernetes-dashboard   ClusterIP      10.110.65.24     <none>        80/TCP                   2h

I am running minikube on OSX.

Answers

The issue you're facing might be due to the configuration of your Minikube environment and the type of service you're using. There are a few points to consider:

  1. LoadBalancer Type Service in Minikube: Minikube does not support the LoadBalancer type out-of-the-box like managed Kubernetes services (e.g., GKE, EKS, AKS) because it does not provision cloud load balancers. Instead, you should use the NodePort type or use minikube tunnel to get LoadBalancer-like behavior.

  2. NodePort Service: If you want to expose a service externally in Minikube, you should use NodePort. Minikube assigns a port on the node to the service, which you can access using the Minikube IP.

Here’s how you can modify your service configuration:

kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app: myapp
  ports:
  - protocol: "TCP"
    port: 8081
    targetPort: 8080
    nodePort: 30002
  type: NodePort

With this configuration, Minikube will expose the service on a node port (30002).

  1. Minikube IP: Ensure you are using the correct Minikube IP. You can find this IP by running:

    minikube ip
    

You should then be able to access the service at:

http://<minikube-ip>:30002
  1. Using minikube service command: Another method to access your service is using the minikube service command, which opens the service in your default web browser.
minikube service exampleservice

Troubleshooting Steps

  1. Check Pods and Service Status:

    Ensure your pods are running and the service is correctly set up.

    kubectl get pods
    kubectl get svc
    
  2. Check Service Logs:

    If there's an issue, check the logs for your application to ensure it's running correctly.

    kubectl logs <pod-name>
    
  3. Port Forwarding:

    As an alternative to exposing services, you can use kubectl port-forward to forward a port from your local machine to a port on a pod.

    kubectl port-forward <pod-name> 8081:8080
    

    Access your service at http://localhost:8081.

By following these steps and ensuring your Minikube configuration is correct, you should be able to access your service from your local machine.