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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more