How to Expose Pods in Kubernetes

This is a more a note to myself than a proper blog posting. Altogether I have spent way too much time to discover how to talk to containers running in Kubernetes from outside the Kubernetes cluster (Kubernetes for Docker, Oracle, GKE).

Run Pod via kubectl run then use kubectl expose deployment to expose via NodePort

  • deploy the container with kubectl run microg --image=fmunz/microg --port 5555
  • you will not see it as a service, e.g kubectl get services
  • expose the pod with kubectl expose deployment microg --type=NodePort. Note that other types are possible, see section below for deployment with a YAML file.
  • get the NodePort with kubectl describe service microg | grep NodePort
  • you will see the new service exposed as kubectl describe service microg

Deploy with Service YAML

For “kind” in the service YAML specify either (partly taken from K8s doc):

  • ClusterIP: Exposes the service on a cluster-internal IP. Choosing this value makes the service only reachable from within the cluster. This is the default ServiceType. To be able to talk to your service, start a Proxy server: kubectl proxy --port=8080

  • NodePort: Exposes the service on each Node’s IP at a static port (the NodePort). A ClusterIP service, to which the NodePort service will route, is automatically created. You’ll be able to contact the NodePort service, from outside the cluster, by requesting <nodeip>:<nodeport>. To find the NodePort, use the following command: kubectl describe service microg | grep NodePort

  • LoadBalancer: Exposes the service externally using a cloud provider’s load balancer. NodePort and ClusterIP services, to which the external load balancer will route, are automatically created.

You can find a good posting related to accessing services on GKE here.

Speak Your Mind