How can you publish a Kubernetes Service without using the type LoadBalancer (on GCP)
If you don't want to use a LoadBalancer
service, other options for exposing your service publicly are:
Type NodePort
Create your service with type
set to NodePort
, and Kubernetes will allocate a port on all of your node VMs on which your service will be exposed (docs). E.g. if you have 2 nodes, w/ public IPs 12.34.56.78
and 23.45.67.89
, and Kubernetes assigns your service port 31234, then the service will be available publicly on both 12.34.56.78:31234
& 23.45.67.89:31234
Specify externalIPs
If you have the ability to route public IPs to your nodes, you can specify externalIPs
in your service to tell Kubernetes "If you see something come in destined for that IP w/ my service port, route it to me." (docs)
The cluster endpoint won't work for this because that is only the IP of your Kubernetes master. The public IP of another LoadBalancer
service won't work because the LoadBalancer is only configured to route the port of that original service. I'd expect the node IP to work, but it may conflict if your service port is a privileged port.
Use the /proxy/
endpoint
The Kubernetes API includes a /proxy/
endpoint that allows you to access services on the cluster endpoint IP. E.g. if your cluster endpoint is 1.2.3.4
, you could reach my-service
in namespace my-ns
by accessing https://1.2.3.4/api/v1/proxy/namespaces/my-ns/services/my-service
with your cluster credentials. This should really only be used for testing/debugging, as it takes all traffic through your Kubernetes master on the way to the service (extra hops, SPOF, etc.).
There's another option: set the hostNetwork
flag on your pod.
For example, you can use helm3 to install nginx this way:
helm install --set controller.hostNetwork=true nginx-ingress nginx-stable/nginx-ingress
The nginx is then available at port 80 & 443 on the IP address of the node that runs the pod. You can use node selectors or affinity or other tools to influence this choice.