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

Time:09-12

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

CodePudding user response:

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

Use either ingress or nodeport methods.

CodePudding user response:

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.

  • Related