🐶
Kubernetes

Expose Port in Minikube: A Step-by-Step Guide

By Jan on 02/09/2025

Learn how to expose a port in Minikube to access your application running in a Kubernetes cluster locally.

Expose Port in Minikube: A Step-by-Step Guide

Table of Contents

Introduction

This guide provides a quick and easy way to access your applications running in a Kubernetes cluster using Minikube. We'll cover two methods: exposing your application as a NodePort service and using port forwarding. Choose the method that best suits your needs.

Step-by-Step Guide

  1. Verify your application is running:

    kubectl get pods
  2. Expose your deployment as a service:

    kubectl expose deployment <deployment-name> --type=NodePort --port=<container-port> --target-port=<container-port>

    Replace <deployment-name> with the name of your deployment, and <container-port> with the port your application listens on inside the container.

  3. Get the service details:

    kubectl get services

    Note the NodePort assigned to your service.

  4. Access your application:

    http://<minikube-ip>:<node-port>
    

    Replace <minikube-ip> with the IP address of your minikube instance (obtainable with minikube ip) and <node-port> with the NodePort from the previous step.

Alternatively, use port forwarding:

  1. Forward a local port to the application pod:

    kubectl port-forward <pod-name> <local-port>:<container-port>

    Replace <pod-name> with the name of your application pod, <local-port> with the desired port on your local machine, and <container-port> with the port your application listens on inside the container.

  2. Access your application:

    http://localhost:<local-port>
    

    Use the <local-port> you specified in the previous step.

Code Example

This text describes how to expose an application deployed on a Kubernetes cluster using NodePort and Port Forwarding. It provides YAML configuration for a Deployment object and command-line instructions for deploying the application, verifying its status, exposing it through a NodePort service, and accessing it using the cluster IP address and assigned NodePort. It also explains how to use kubectl port-forward to access the application locally.

deployment.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: <your-docker-image>
        ports:
        - containerPort: 8080

Steps:

  1. Deploy your application:

    kubectl apply -f deployment.yaml
  2. Verify your application is running:

    kubectl get pods

    You should see a pod with the name my-app-<random-string> running.

  3. Expose your deployment as a service:

    kubectl expose deployment my-app --type=NodePort --port=8080 --target-port=8080
  4. Get the service details:

    kubectl get services

    Note the NodePort assigned to the my-app service.

  5. Access your application:

    minikube ip

    Let's say the output is 192.168.99.100 and the NodePort is 30000. Then you can access your application at:

    http://192.168.99.100:30000
    

Example using Port Forwarding:

Steps:

  1. Verify your application is running:

    kubectl get pods

    Note the name of your application pod. Let's say it's my-app-56c897f9bf-xyz78.

  2. Forward a local port to the application pod:

    kubectl port-forward my-app-56c897f9bf-xyz78 8081:8080

    This command forwards requests from your local port 8081 to the container port 8080.

  3. Access your application:

    http://localhost:8081
    

These examples demonstrate how to expose your application running in a Kubernetes cluster using NodePort and Port Forwarding. Remember to replace the placeholders with your actual values.

Additional Notes

General:

  • Minikube IP: The command minikube ip is crucial for accessing your services externally. Remember that this IP might change if you restart Minikube.
  • Choosing a method: NodePort is generally better for exposing your application more permanently, while port forwarding is useful for quick testing and debugging.
  • Firewalls: Ensure your firewall isn't blocking the NodePort or the port you're forwarding.

NodePort:

  • Port Range: Minikube typically uses a specific range for NodePorts (e.g., 30000-32767). Be mindful of this when choosing a port.
  • External Access: Anyone on the same network as your Minikube instance can access the application via the NodePort.
  • Multiple Services: If you expose multiple services with NodePorts, ensure you don't have port conflicts.

Port Forwarding:

  • Connection Persistence: The kubectl port-forward command needs to run in the terminal for as long as you need access. Closing the terminal or interrupting the command will stop the forwarding.
  • Localhost Only: Port forwarding makes the application accessible only from your local machine.
  • Multiple Pods: If your deployment has multiple pods, the port-forward command will forward to one of them randomly. You can specify a specific pod if needed.

Troubleshooting:

  • Application Logs: If you can't access your application, check the logs of your pod using kubectl logs <pod-name> to diagnose the issue.
  • Service Status: Use kubectl describe service <service-name> to get detailed information about your service, including its endpoints and any issues.
  • Minikube Status: Ensure Minikube is running correctly with minikube status.

This information should provide a more comprehensive understanding of how to access applications running in Minikube and help you troubleshoot potential issues.

Summary

This document summarizes two methods for accessing your application running within a Minikube cluster:

Method 1: NodePort

Step Description Command
1 Verify application is running kubectl get pods
2 Expose deployment as a NodePort service kubectl expose deployment <deployment-name> --type=NodePort --port=<container-port> --target-port=<container-port>
3 Get service details and note the assigned NodePort kubectl get services
4 Access your application http://<minikube-ip>:<node-port> (Get <minikube-ip> with minikube ip)

Method 2: Port Forwarding

Step Description Command
1 Forward a local port to the application pod kubectl port-forward <pod-name> <local-port>:<container-port>
2 Access your application http://localhost:<local-port>

Key Differences:

  • NodePort: Exposes your application on a static port across all nodes in the cluster, accessible from outside the cluster.
  • Port Forwarding: Creates a temporary tunnel between your local machine and the application pod, accessible only from your machine.

Choose the method that best suits your needs and development workflow.

Conclusion

This guide explained how to access applications deployed on a Minikube Kubernetes cluster using NodePort and Port Forwarding. Each method offers unique advantages depending on your needs, whether it's exposing your application for external access or establishing a temporary connection for testing and debugging. By understanding these approaches, you can effectively manage and interact with your applications running in a Minikube environment.

References

Were You Able to Follow the Instructions?

😍Love it!
😊Yes
😐Meh-gical
😞No
🤮Clickbait