Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Minikube Loadbalancer Service From Host Machine

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.

like image 918
Jack Zavarella Avatar asked Apr 01 '19 19:04

Jack Zavarella


People also ask

How do you use a load balancer on minikube?

Using minikube tunnel Services of type LoadBalancer can be exposed via the minikube tunnel command. It must be run in a separate terminal window to keep the LoadBalancer running. Ctrl-C in the terminal can be used to terminate the process at which time the network routes will be cleaned up.

Does minikube support LoadBalancer?

On cloud providers that support load balancers, an external IP address would be provisioned to access the Service. On minikube, the LoadBalancer type makes the Service accessible through the minikube service command.

How do I access minikube from another computer?

You can't access minikube remotely because it's only accessible locally. For this reason, you need to deploy an Nginx reverse proxy next to minikube that will allow receiving requests from remote clients then forward them to kube-apiserver.

How do I access application deployed on minikube?

In this case, 192.168. 64.7 is the IP address of minikube which you can also retrieve using the command `minikube ip`. Open a browser and check your nginx application. That's it; you can try different applications and you will realize how easy it is to use minikube for your Kubernetes learning and development.


2 Answers

This is expected. Do note that LoadBalancer is for cloud to create external load balancer like ALP/NLP in AWS and something similar in GCP/Azure etc.

Update the service as shown here. here i assume 192.168.64.2 is your minikube ip. if not, update it with minikube ip to make it work.

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: 80
    # Port accessible outside cluster
    nodePort: 30002
  type: LoadBalancer
  externalIPs:
  - 192.168.64.2

Now you can access your application at http://192.168.64.2:8081/


If you need to access the application at 30002, you can use it like this

    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: 80
        # Port accessible outside cluster
        nodePort: 30002
      type: NodePort

Your deployment file does not look correct to me.

delete it kubectl delete deploy/myappdeployment

use this to create again.

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: myappdeployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
  strategy: {}
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - image: tutum/hello-world
        name: myapp
        ports:
        - containerPort: 80
like image 91
KitKarson Avatar answered Nov 16 '22 00:11

KitKarson


NOTE: Minikube support LoadBalancer services (via minikube tunnel)

you can get the IP and port through which you can access the service by running

minikube service kubia-http #=> To open a browser with an IP and port

OR

minikube service kubia --url #=> To get the IP and port in the terminal

like image 28
Gupta Avatar answered Nov 15 '22 23:11

Gupta