简体   繁体   中英

How do I expose running applications on local k8s cluster to my local machine?

I'm deploying this project ( GitHub ) locally on k3d Kubernetes Cluster. It includes a Helm chart. There is also a documentation for this example which can be found here .

What I have done so far is what's below. It works just fine. The problem is the ClusterIPs it gives are internal for k8s and I can't access them outside of the cluster. What I want is to be able to run them on my machine's browser. I was told that I need a nodeport or a loadbalancer to do that. How can I do that?

// Build Docker Images
// Navigate to root directory -> ./ProtoClusterTutorial
docker build . -t proto-cluster-tutorial:1.0.0

// Navigate to root directory
docker build -f ./SmartBulbSimulatorApp/Dockerfile . -t smart-bulb-simulator:1.0.0

// Push Docker Image to Docker Hub
docker tag proto-cluster-tutorial:1.0.0 hulkstance/proto-cluster-tutorial:1.0.0
docker push hulkstance/proto-cluster-tutorial:1.0.0

docker tag smart-bulb-simulator:1.0.0 hulkstance/smart-bulb-simulator:1.0.0
docker push hulkstance/smart-bulb-simulator:1.0.0

// List Docker Images
docker images

// Deployment to Kubernetes cluster
helm install proto-cluster-tutorial chart-tutorial
helm install simulator chart-tutorial --values .\simulator-values.yaml

// It might fail with the following message:
// Error: INSTALLATION FAILED: Kubernetes cluster unreachable: Get "https://host.docker.internal:64285/version": dial tcp 172.16.1.131:64285: connectex: No connection could be made because the target machine actively refused it.
// which means we don't have a running Kubernetes cluster. We need to create one:
k3d cluster create two-node-cluster --agents 2

// If we want to switch between clusters:
kubectl config use-context k3d-two-node-cluster

// Confirm everything is okay
kubectl get pods
kubectl logs proto-cluster-tutorial-78b5db564c-jrz26

You can use kubectl port-forward command.
Syntax:

kubectl port-forward TYPE/NAME [options] LOCAL_PORT:REMOTE_PORT

In your case:

kubectl port-forward pod/proto-cluster-tutorial-78b5db564c-jrz26 8181:PORT_OF_POD

Now, you can access localhost:8181 to use.

I suggest you follow the official docs of k3d for exposing services.

Use either ingress or nodeport methods.

This totally depends on your use case. If you are testing from you local machine you might need to use portfowrd command kubectl port-forward <pod-name> <localport>:<remoteport>

If you are using Minikube then use minikube service <service-name> --url

If you are using a Cloud provider like AKS,GKE OR EKS then you might need to think of using some other way of accessing application this could be done by using NodePort,LoadBalancer or Ingress.

If you use service type of Nodeport the same could be achieved. But in the case of Nodeport service all the port ranges which it supports is 30000-32767 and this is a very hard number to memorise the port number.Another disadvantage of Nodeport service is that Node's IP address(as it changes if node restart) hence this is not used for project purposes.

Create a NodePort service: Node port service

LoadBalancer service exposes an external IP and then you can access the service using: but if you have 100 services you will be charged for 100 external IPs and this hampers the budget.

Create a load balancer service on Kubernetes: Load Balancer service

Another way of exposing an application is using ingress-controller to achieve the same thing.Using ingress you can expose 100 applications with one external IP.You will need to install the ingress controller and then using an ingress file to configure the rules.

Setup ingress controller on Kubernetes: Ingress controller

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM