Hello Im learning kubernetes with the minikube. I can access a service via minikubeip:NodePort on the machine where the minikube is running and now I want to access the Service via LAN from other machine. I tried ingress but it didn't work for me.
Deployment file:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: aspnetapp-deployment
labels:
app: aspnetapp
spec:
replicas: 2
selector:
matchLabels:
app: aspnetapp
template:
metadata:
labels:
app: aspnetapp
spec:
containers:
- name: aspnetapp-cn
image: localhost:5000/aspnetapp
ports:
- containerPort: 80
Service file:
---
apiVersion: v1
kind: Service
metadata:
name: aspnetapp-service
spec:
type: NodePort
ports:
- name: http
targetport: 80
port: 80
protocol: TCP
selector:
app: aspnetapp
Ingress file:
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: aspnetapp-ingress
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
rules:
- host:
http:
paths:
- path: /aspnetapp
backend:
serviceName: aspnetapp-service
servicePort: 80
To expose your application to LAN with Ubuntu with a
--docker
driver you can use:$ kubectl port-forward ...
Example:
Let's assume that you have an Ubuntu machine with IP:
192.168.0.115
.I've created an example using
nginx
image:Deployment.yaml
As for the service exposing your
Deployment
you can either:$ kubectl expose deployment nginx --port=80 --type=NodePort
You can expose your
nginx
in two ways:$ kubectl port-forward
.Ingress
controller.Direct access
You can expose your
Service
directly without usingIngress
by:$ kubectl port-forward --address=0.0.0.0 deployment/nginx 10000:80
Dissecting above command:
--address=0.0.0.0
- expose outside of localhostdeployment/nginx
- resource/resource_name10000:80
- port on host machine/port on pod to send the traffic toAfter running above command you should be able to run:
curl 192.168.1.115:10000
Command
$ kubectl port-forward
will generate:Directing the traffic to the
Ingress
controllerIn your example you used
Ingress
resource. In this situation you should:Ingress
resource (as you did).Ingress
controller!Ingress
controller after receiving the traffic will forward it further (to yourService
and then toPod
)To forward the traffic to your
Ingress
controller run this command:kubectl port-forward --address=0.0.0.0 --namespace=kube-system deployment/ingress-nginx-controller 80:80
Dissecting above command once more:
--address=0.0.0.0
- expose outside of localhost--namespace=kube-system
- namespace that theDeployment
ofIngress
controller resides indeployment/ingress-nginx-controller
- resource/resource-name80:80
- port on host machine/port on pod to send the traffic toCommand
$ kubectl port-forward
will generate:I also encourage you to use different
--driver
like for example Virtualbox. You will be able to expose your application without$ kubectl port-forward
(NAT).Additional resources: