Minikube: How to expose a service externally to the outside world (external IP)

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Minikube is a popular tool to run Kubernetes locally. It’s a valuable resource for developers looking to test their applications before deploying to a production environment. In this tutorial, we will go over how to expose a service running on Minikube to the outside world using an external IP. This is crucial for the times when you want your local services to be accessible from an external network.

Setting Up Minikube

First and foremost, ensure that Minikube is installed on your machine. You can install it by following the official documentation here. Once installed, you can start a local Minikube cluster by executing:

minikube start

Once the cluster is up, you’ll want to deploy a sample application to expose it externally. Let’s deploy a simple Hello World application using Kubectl:

kubectl create deployment hello-world --image=k8s.gcr.io/echoserver:1.10

Now, confirm that your deployment is running using:

kubectl get deployments

Simple Service Exposure

To expose your application outside of the Kubernetes virtual network, you can create a Service of type NodePort or LoadBalancer. NodePort exposes the service on each Node’s IP at a static port, whereas the LoadBalancer enables you to use an external load balancer.

For Minikube, you’ll primarily use NodePort. Here is how you expose your deployment using NodePort:

kubectl expose deployment hello-world --type=NodePort --port=8080

Then, you can find the allocated port and IP by running:

minikube service hello-world --url

This command outputs the URL that you can use to access your service from the browser or postman:

http://192.168.99.100:32345

You’ve just exposed your first service. However, you might now need a stable external IP address rather than being bound to the one minikube assigns.

Using Minikube’s ‘tunnel’ Command

Minikube provides the tunnel command, which can help in allocating an external IP address that will route to a LoadBalancer service in your cluster. First, create a Service of type LoadBalancer:

kubectl expose deployment hello-world --type=LoadBalancer --port=8080

Now run:

minikube tunnel

Note: You may need to run the tunnel command with administrative privileges.

Running this command will give you an external IP to your service. Find it by calling:

kubectl get services

Use the given EXTERNAL-IP to access your service through a web browser.

Advanced Exposing: Using Ingress

In a production-like environment, you might want a bit more control over how requests are routed to your services. Ingress is an API object that manages external access to services in a cluster, typically HTTP. Ingress can provide load balancing, SSL termination and name-based virtual hosting.

To make use of Ingress in Minikube, you must first enable the Ingress addon by running:

minikube addons enable ingress

Then deploy an Ingress resource file that defines routing rules. Below is a basic example:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: example-ingress
spec:
  rules:
  - host: hello-world.info
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: hello-world
            port:
              number: 8080

Note: Replace hello-world.info with the domain pointed to your Minikube IP.

To get the Minikube IP address, execute:

minikube ip

Once the Ingress resource is created, you can access your application through the domain specified in your Ingress configuration if you configure it in your /etc/hosts file or DNS.

Conclusion

Through this guide, you’ve learned how you can expose a service externally in Minikube. Whether you’re using a simple NodePort, LoadBalancer with the tunnel command or setting up an Ingress for more complex routing, Minikube is flexible to suit different development needs.