3
votes

I want to expose my kubernetes cluster with minikube.

consider my tree

.                                                                                                                                                                            
├── deployment.yaml                                                                                                                                                          
├── Dockerfile                                                                                                                                                               
├── server.js                                                                                                                                                                
└── service.yaml    

I build my docker image locally and am able to run all pods via

kubectl create -f deployment.yaml 
kubectl create -f service.yaml 

. However when I run

 $ kubectl get services                                                                                                                                        
NAME                      TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE                                                                                    
kubernetes                ClusterIP      10.96.0.1       <none>        443/TCP        2h
nodeapp                   LoadBalancer   10.110.106.83   <pending>     80:32711/TCP   9m

There is no external ip to be able to connect to the cluster. Tried to expose one pod but the the external Ip stays none. Why Is there no external ip?

 $ cat deployment.yaml                                                                                                                                         
apiVersion: apps/v1                                                                                                                                                          
kind: Deployment
metadata:
  name: nodeapp
  labels:
    app: nodeapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nodeapp
  template:
    metadata:
      labels:
        app: nodeapp
    spec:
      containers:
      - name: hello-node
        image: hello-node:v2
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 3000

and

 cat service.yaml                                                                                                                                            
kind: Service                                                                                                                                                                
apiVersion: v1
metadata:
  name: nodeapp
spec:
  selector:
    app: nodeapp
  ports:
  - name: http 
    port: 80
    targetPort: 3000
    protocol: TCP
  type: LoadBalancer


$ cat server.js                                                                                                                                               
var http = require('http');                                                                                                                                                  

var handleRequest = function(request, response) {
  console.log('Received request for URL: ' + request.url);
  response.writeHead(200);
  response.end('Hello User');
};
var www = http.createServer(handleRequest);
3

3 Answers

5
votes

According to the K8S documentation here. So, type=LoadBalancer can be used on AWS, GCP and other supported Clouds, not on Minikube.

On cloud providers which support external load balancers, setting the type field to LoadBalancer will provision a load balancer for your Service.

Specify type as NodePort as mentioned here and the service will be exposed on a port on the Minikube. Then the service can be accessed by using url from the host OS.

minikube service nodeapp --url

3
votes

A load balancer type service can be achieved in minikube using the metallb project https://github.com/google/metallb

This allows you to use external ip offline and in minikube and not only with a cloud provider.

Good luck!

0
votes

If you run the following command:

kubectl expose deployment nodeapp --type=NodePort

Then run:

kubectl get services

It should show you the service and what port it's exposed on.

You can get your Minikube IP using:

curl $(minikube service nodeapp --url)