Learn how to expose a port in Minikube to access your application running in a Kubernetes cluster locally.
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.
Verify your application is running:
kubectl get podsExpose 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.
Get the service details:
kubectl get servicesNote the NodePort assigned to your service.
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:
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.
Access your application:
http://localhost:<local-port>
Use the <local-port> you specified in the previous step.
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: 8080Steps:
Deploy your application:
kubectl apply -f deployment.yamlVerify your application is running:
kubectl get podsYou should see a pod with the name my-app-<random-string> running.
Expose your deployment as a service:
kubectl expose deployment my-app --type=NodePort --port=8080 --target-port=8080Get the service details:
kubectl get servicesNote the NodePort assigned to the my-app service.
Access your application:
minikube ipLet'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
Steps:
Verify your application is running:
kubectl get podsNote the name of your application pod. Let's say it's my-app-56c897f9bf-xyz78.
Forward a local port to the application pod:
kubectl port-forward my-app-56c897f9bf-xyz78 8081:8080This command forwards requests from your local port 8081 to the container port 8080.
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.
General:
minikube ip is crucial for accessing your services externally. Remember that this IP might change if you restart Minikube.NodePort:
Port Forwarding:
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.Troubleshooting:
kubectl logs <pod-name> to diagnose the issue.kubectl describe service <service-name> to get detailed information about your service, including its endpoints and any issues.minikube status.This information should provide a more comprehensive understanding of how to access applications running in Minikube and help you troubleshoot potential issues.
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:
Choose the method that best suits your needs and development workflow.
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.
Module 4 - Expose your app publicly | minikube | Difficulty: Beginner Estimated Time: 10 minutes
In this scenario you will learn how to expose Kubernetes applications outside the cluster using the kubectl expose command. You will also learn how to view and apply labels to objects with the kubectl label command.
Step 1 - Create a new service Let’s verify that our application is running. We’ll use the kubectl get command and look for existing Pods:
kubectl get pods Next, let’s list the current Services from our cluster:
How to Expose a Port in Minikube | Baeldung on Ops | A quick and practical guide to exposing a port in Minikube.