To expose a port in a Minikube Kubernetes cluster, we can create a
Kubernetes Service. This service will target the pods we want and set
the port we need to expose. We can do this using the
kubectl expose command. This command helps us create a
service type that fits our needs like NodePort or LoadBalancer. This
way, we can allow outside access to our application’s services.
In this article, we will look at different ways to expose ports in a
Minikube Kubernetes environment. We will talk about using the
kubectl expose command. We will also cover how to create
NodePort services, access services with Minikube Tunnel, use Ingress for
application exposure, set up LoadBalancer services, and answer common
questions. Here are the topics we will discuss:
- Expose Port in Minikube Kubernetes Cluster
- Using kubectl expose to Open Ports in Minikube
- Creating a NodePort Service in Minikube
- Accessing Services via Minikube Tunnel
- Using Ingress to Expose Applications in Minikube
- Configuring LoadBalancer Services in Minikube
- Frequently Asked Questions
Using kubectl expose to Open Ports in Minikube
To expose a port in Minikube with kubectl, we can use
the kubectl expose command. This command creates a service
for our app. This service lets us show our app to the outside world.
Here are the steps to expose a deployment as a service.
Example of Exposing a Deployment
Create a Deployment (if we do not have one yet):
kubectl create deployment myapp --image=myapp:latestExpose the Deployment: To expose the deployment on a certain port, we can run:
kubectl expose deployment myapp --type=NodePort --port=8080--type=NodePort: This tells that the service will be available outside.--port=8080: This sets the port that the service will show.
Get the Service Information: To find the port that Kubernetes gave, we run:
kubectl get servicesThis command will show us the service details, and we can see the external port assigned.
Accessing the Application
After we expose the service, we can access our app using the Minikube IP and the NodePort. To get the Minikube IP, we run:
minikube ipThen we can access our app using:
http://<minikube-ip>:<node-port>
This method helps developers to quickly expose apps running in Minikube. It makes it easier to test and work with their services. For more details on Kubernetes services, check this article.
Creating a NodePort Service in Minikube
We can expose a service in our Minikube Kubernetes cluster by using a NodePort service. Here are the steps we should follow:
Make sure Minikube is running:
minikube startCreate a deployment: We can deploy a simple Nginx app like this:
kubectl create deployment nginx --image=nginxExpose the deployment as a NodePort service: We run this command to expose the Nginx deployment on a NodePort:
kubectl expose deployment nginx --type=NodePort --port=80Get the service details: We can get the service information, like the assigned NodePort:
kubectl get servicesThe output should look like this:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx NodePort 10.96.0.1 <none> 80:XXXXX/TCP 1mWe need to note the
XXXXXin80:XXXXX/TCP. This is the NodePort we will use to access the service.Access the service: To access the Nginx service from our host machine, we use this command to get the Minikube IP:
minikube ipThen we access it using the Minikube IP and the NodePort:
http://<minikube-ip>:<NodePort>
This way we can expose our applications running in Minikube with NodePort services. For more details on managing services, we can look at Kubernetes Services.
Accessing Services via Minikube Tunnel
We can access services running in a Minikube Kubernetes cluster by
using the minikube tunnel command. This command helps us
expose services of type LoadBalancer and
NodePort to our localhost. It gives us a way to reach
applications running inside our Minikube cluster from our local
machine.
Steps to Access Services via Minikube Tunnel
Start Minikube: We need to make sure Minikube is running. If it is not running, we can start it with:
bash minikube startCreate a Service: If we have not done it yet, we should create a service of type
LoadBalancerorNodePort. For example, here is how we can create a simple Node.js application and expose it:yaml apiVersion: v1 kind: Service metadata: name: my-node-app spec: type: NodePort ports: - port: 8080 targetPort: 3000 nodePort: 30001 selector: app: my-node-appApply the Service Configuration:
bash kubectl apply -f my-node-app-service.yamlRun Minikube Tunnel: In a new terminal, we run the following command to expose our service via a tunnel:
bash minikube tunnelThis command may need administrative privileges. So, we might have to run it withsudoon Unix-like systems.Access Your Application: After the tunnel is running, we can access our application using the URL:
- For a
LoadBalancerservice, we can reach the service at the IP address given by the tunnel. - For a
NodePortservice, we use:
http://localhost:<NodePort>For the example above, it will be:
http://localhost:30001- For a
Additional Notes
- The
minikube tunnelcommand will keep running until we stop it. This lets traffic flow to our services. - This method is very useful for local development and testing. It makes it easier to access services without needing to deploy to a cloud provider.
- We should check that our firewall settings allow access to the ports used by our services.
For more information on Kubernetes services, we can look at what are Kubernetes services and how do they expose applications.
Using Ingress to Expose Applications in Minikube
In Minikube, we can use Ingress to control how outside users access our services. Ingress works as a door for HTTP and HTTPS traffic to our applications in the Kubernetes cluster. Here are the steps to set up and use Ingress in Minikube.
Prerequisites
First, we need to make sure Minikube is running:
minikube startThen, we enable the Ingress addon:
minikube addons enable ingress
Deploying an Application
Now, let’s deploy a sample application. We will use a simple Nginx deployment as an example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80We apply the deployment with this command:
kubectl apply -f nginx-deployment.yamlCreating a Service
Next, we create a Service to expose our Nginx deployment:
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIPNow we apply the service:
kubectl apply -f nginx-service.yamlConfiguring Ingress Resource
Now, we will create an Ingress resource so we can route traffic to the Nginx service:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-ingress
spec:
rules:
- host: nginx.local
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx-service
port:
number: 80We apply the ingress resource now:
kubectl apply -f nginx-ingress.yamlAccessing the Application
To access our application, we need to change our
/etc/hosts file. This will link the hostname to the
Minikube IP. First, we get the Minikube IP:
minikube ipThen, we add this line to our /etc/hosts file (make sure
to replace <minikube-ip> with the IP we got):
<minikube-ip> nginx.local
Now, we can access our Nginx application at
http://nginx.local.
Summary
Using Ingress in Minikube helps us manage how outside users access our applications. We can set rules to send traffic to different services based on hostnames and paths. This makes it a great tool for handling HTTP traffic in Kubernetes.
For more details about Kubernetes services and how to expose applications, check out this article.
Configuring LoadBalancer Services in Minikube
Minikube gives us a simple way to set up a local Kubernetes cluster.
But it does not support LoadBalancer services like cloud providers do.
We can still make LoadBalancer work by using Minikube and the
minikube tunnel command. Here is how we can configure
LoadBalancer services in Minikube.
Start Minikube:
First, we need to make sure that Minikube is running. We can choose the driver if we want:
minikube start --driver=virtualboxDeploy an Application:
Next, we will create a deployment for our application. For example, we can create a simple nginx deployment with this command:
kubectl create deployment nginx --image=nginxExpose the Deployment as a LoadBalancer Service:
Now, we expose the deployment as a LoadBalancer service using this command:
kubectl expose deployment nginx --type=LoadBalancer --port=80Start Minikube Tunnel:
To set up the LoadBalancer service, we need to run the Minikube tunnel in a new terminal. This command makes a network route to our service:
minikube tunnelWe need to have the right permissions. Sometimes we may need to use sudo.
Get the Service Information:
After we expose the service, we can check the service details to see the external IP:
kubectl get servicesWe will see an external IP assigned to our LoadBalancer service after a short time.
Access the Application:
We can access our application using the external IP we got. For example, if the external IP is
192.168.99.100, we can reach Nginx by going to:http://192.168.99.100
This way works good for local development and testing of LoadBalancer services in Minikube. For more details about Kubernetes services, we can check this article.
Frequently Asked Questions
1. How do we expose a port in Minikube?
To expose a port in Minikube, we can use the
kubectl expose command. This command creates a service that
connects the internal port of our application to an external port. For
example, we can run the command
kubectl expose deployment <your-deployment-name> --type=NodePort --port=<your-app-port>.
This makes our application available outside the Minikube Kubernetes
cluster.
2. What is the difference between NodePort and LoadBalancer in Minikube?
In Minikube, a NodePort service shows our
application on a fixed port on each node’s IP address. This lets us
access the service from outside the cluster using
http://<minikube-ip>:<node-port>. On the other
hand, a LoadBalancer service is for cloud systems. It
creates an external load balancer. But this does not work in Minikube
because it runs locally. For testing, we usually use NodePort.
3. How can we use Minikube tunnel to access services?
Minikube tunnel is a command that makes a network tunnel to our
Minikube cluster. It helps us access services of type LoadBalancer and
NodePort. To use it, we just run minikube tunnel in our
terminal. This needs admin rights and will keep running until we stop
it. It lets us access services like they are in a cloud system.
4. Can we use Ingress to expose applications in Minikube?
Yes, we can use Ingress to expose applications in our Minikube
Kubernetes cluster. Ingress helps us manage external access to services,
usually HTTP/S, by setting rules for routing traffic. To set this up, we
need to enable the Ingress add-on in Minikube. We can do this with
minikube addons enable ingress, and then we create an
Ingress resource that points to our application services.
5. How do we check the exposed ports for services in Minikube?
To check the exposed ports for services in our Minikube cluster, we
can run the command kubectl get services. This command
shows all services with their details. It includes the type of service,
cluster IP, external IP (if there is one), and the ports they expose.
This is very important to make sure our applications are available as we
expect.
For more insights on Kubernetes and Minikube, we can explore related topics like how to install Minikube for local Kubernetes development or understanding Kubernetes services.