DEV Community

Bernardo
Bernardo

Posted on

Mini guide, access your k8s service local development

In a kind/minikube/etc. cluster, start a deployment (a workload resource that manages a pod replicaset)

kubectl create deployment home --image=b4bz/homer
Enter fullscreen mode Exit fullscreen mode

expose the deployment, i.e. make it receive comms from other containers or the outside.

kubectl expose deploy/home --port 8080
Enter fullscreen mode Exit fullscreen mode

-> make sure the port exposed is actually the one the container exposes, e.g. for postgres this would 5432, for b4bz/homer it is 8080. There is no default here.

The command above creates a service of the type ClusterIP, which makes it accessible to other services only within cluster.

To make it accessible outside the cluster, the service has to be of type NodePort, like so

kubectl expose deploy/home --type=NodePort --port 8080
Enter fullscreen mode Exit fullscreen mode

Now, how to you open it in a browser using kind, k8s? Well, you can port-forward from the localhost to the container port using kubectl port-forward - checking out kubectl port-forward --help clarifies a lot.

kubectl port-forward services/home :8080
# assigns a random localhost port to the port 8080 of the container
Enter fullscreen mode Exit fullscreen mode

Top comments (0)