Search
⌃K

Expose localhost Kubernetes

This tutorial will guide you on how you can expose services running on your local kubernetes setup.

1. Getting Kubernetes (minikube)

For this tutorial we are using minikube.
Minikube will allow you to setup local kubernetes cluster on Windows, macOS or Linux machine very easily. You can follow this guide on how to setup minikube in your local machine.
Once you have installed minikube, start your cluster.
$minikube start
😄 minikube v1.10.1 on Debian Parrot
✨ Using the virtualbox driver based on existing profile
👍 Starting control plane node minikube in cluster minikube
🔄 Restarting existing virtualbox VM for "minikube" ...
🐳 Preparing Kubernetes v1.18.2 on Docker 19.03.8 ...
🌟 Enabled addons: dashboard, default-storageclass, storage-provisioner
🏄 Done! kubectl is now configured to use "minikube"

2. Getting Kubernetes CLI

To interact with your new kubernetes cluster you will need kubectl. Download the latest binary of kubectl for Linux with the commands below.
#Downloading the kubectl binary
$curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
#Making the binary executeable
$chmod +x ./kubectl
#Moving kubectl binary to $PATH
$sudo mv ./kubectl /usr/bin/kubectl
You can find further instructions for Windows & macOS here.
Once you finished setting up kubectl,check it's version. Try using it to check your new local cluster status.
$kubectl version --client
Client Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.3", GitCommit:"2e7996e3e2712684bc73f0dec0200d64eec7fe40", GitTreeState:"clean", BuildDate:"2020-05-20T12:52:00Z", GoVersion:"go1.13.9", Compiler:"gc", Platform:"linux/amd64"}
$kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready master 3d3h v1.18.2
$kubectl get po -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-66bff467f8-k96mv 1/1 Running 6 3d3h
kube-system coredns-66bff467f8-txkn2 1/1 Running 6 3d3h
kube-system etcd-minikube 1/1 Running 4 3d3h
kube-system kube-apiserver-minikube 1/1 Running 4 3d3h
kube-system kube-controller-manager-minikube 1/1 Running 4 3d3h
kube-system kube-proxy-rzzlq 1/1 Running 4 3d3h
kube-system kube-scheduler-minikube 1/1 Running 4 3d3h
kube-system storage-provisioner 1/1 Running 6 3d3h
kubernetes-dashboard dashboard-metrics-scraper-84bfdf55ff-sjs97 1/1 Running 4 2d6h
kubernetes-dashboard kubernetes-dashboard-696dbcc666-lvzjl 1/1 Running 6 2d6h

3. Create & Run your Service (nginx)

Let's deploy simple a nginx web-server on our local kubernetes cluster.
#Running a nginx image on our cluster
$kubectl create deployment nginx-server --image=nginx
deployment.apps/nginx-server created
#See the status of running pod
$kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-server-69886f95bc-l5zf2 1/1 Running 0 103s

4. Expose Nginx Pod with NodePort

In order to access the service in our newly created nginx pod, we need to expose port 80 using NodePort service like following.
$kubectl expose deployment nginx-server --type=NodePort --port=80
service/nginx-server exposed
Find the the NodePort assigned to our nginx pod.
$kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d4h
nginx-server NodePort 10.107.60.216 <none> 80:32428/TCP 9m38s
Alternatively, you can get it withkubectl get --namespace default -o jsonpath="{.spec.ports[0].nodePort}" services nginx-server
Verify if you can access nginx default welcome page on http://192.168.99.100:32428.

5. Expose Port assigned by NodePort

Since we can access our nginx service on port 32428, let's go ahead and tunnel it with LocalXpose.
$loclx tunnel http --to 192.168.99.100:32428 --subdomain mypod --region ap
✓ Creating HTTP tunnel...
Tunneling http://mypod.ap.loclx.io --> 192.168.99.100:32428
Tunneling https://mypod.ap.loclx.io --> 192.168.99.100:32428
You should be able to access nginx from the domain provided by LocalXpose.

6. Kubernetes Dashboard

Similarly, if you want to expose your kubernetes dashboard,
$kubectl proxy --address='0.0.0.0' --disable-filter=true
W0529 01:25:13.905234 23151 proxy.go:167] Request filter disabled, your proxy is vulnerable to XSRF attacks, please be cautious
Starting to serve on [::]:8001
now, expose port 8001 with LocalXpose.
$loclx tunnel http --to 0.0.0.0:8001 --subdomain dashboard --region ap
✓ Creating HTTP tunnel...
Tunneling http://dashboard.ap.loclx.io --> 0.0.0.0:8001
Tunneling https://dashboard.ap.loclx.io --> 0.0.0.0:8001
Alternatively you can use minikube cli to proxy dashboard & expose those port.
$minikube dashboard --url=True
🤔 Verifying dashboard health ...
🚀 Launching proxy ...
🤔 Verifying proxy health ...
http://127.0.0.1:36537/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
Browse, https://<your_domain>/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/#/overview
From here you can manage your Kubernetes Cluster.
If you want to use your custom domain or SSL certificate check (creating custom domain name) & (using custom SSL certificate).