Comment on page
Exposing Services in Kubernetes
There are different ways to expose services in Kubernetes so both internal endpoints and external endpoints can access them. This Kubernetes configuration is pretty critical as the administrator could give access to attackers to services they shouldn't be able to access.
Before starting enumerating the ways K8s offers to expose services to the public, know that if you can list namespaces, services and ingresses, you can find everything exposed to the public with:
kubectl get namespace -o custom-columns='NAME:.metadata.name' | grep -v NAME | while IFS='' read -r ns; do
echo "Namespace: $ns"
kubectl get service -n "$ns"
kubectl get ingress -n "$ns"
done | grep -v "ClusterIP"
# Remove the last '| grep -v "ClusterIP"' to see also type ClusterIP
A ClusterIP service is the default Kubernetes service. It gives you a service inside your cluster that other apps inside your cluster can access. There is no external access.
However, this can be accessed using the Kubernetes Proxy:
kubectl proxy --port=8080
Now, you can navigate through the Kubernetes API to access services using this scheme:
For example you could use the following URL:
to access this service:
- name: http
This method requires you to run
kubectlas an authenticated user.
NodePort opens a specific port on all the Nodes (the VMs), and any traffic that is sent to this port is forwarded to the service. This is a really bad option usually.
An example of NodePort specification:
- name: http
If you don't specify the nodePort in the yaml (it's the port that will be opened) a port in the range 30000–32767 will be used.
Exposes the Service externally using a cloud provider's load balancer. On GKE, this will spin up a Network Load Balancer that will give you a single IP address that will forward all traffic to your service.
You have to pay for a LoadBalancer per exposed service, which can get expensive.
Services of type ExternalName map a Service to a DNS name, not to a typical selector such as
cassandra. You specify these Services with the
This Service definition, for example, maps the
my-serviceService in the
When looking up the host
my-service.prod.svc.cluster.local, the cluster DNS Service returns a
CNAMErecord with the value
my-serviceworks in the same way as other Services but with the crucial difference that redirection happens at the DNS level rather than via proxying or forwarding.
Traffic that ingresses into the cluster with the external IP (as destination IP), on the Service port, will be routed to one of the Service endpoints.
externalIPsare not managed by Kubernetes and are the responsibility of the cluster administrator.
In the Service spec,
externalIPscan be specified along with any of the
ServiceTypes. In the example below, "
my-service" can be accessed by clients on "
- name: http
Unlike all the above examples, Ingress is NOT a type of service. Instead, it sits in front of multiple services and act as a “smart router” or entrypoint into your cluster.
You can do a lot of different things with an Ingress, and there are many types of Ingress controllers that have different capabilities.
The default GKE ingress controller will spin up a HTTP(S) Load Balancer for you. This will let you do both path based and subdomain based routing to backend services. For example, you can send everything on foo.yourdomain.com to the foo service, and everything under the yourdomain.com/bar/ path to the bar service.
- host: foo.mydomain.com
- host: mydomain.com
- path: /bar/*