How can you publish a Kubernetes Service without using the type LoadBalancer (on GCP)

后端 未结 3 1081
無奈伤痛
無奈伤痛 2021-02-04 10:43

I would like to avoid using type: \"LoadBalancer\" for a certain Kubernetes Service, but still to be able to publish it on the Internet. I am using Google Cloud Pla

相关标签:
3条回答
  • 2021-02-04 10:43

    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.).

    0 讨论(0)
  • 2021-02-04 11:02

    There are a few idiomatic ways to expose a service externally in Kubernetes (see note#1):

    1. Service.Type=LoadBalancer, as OP pointed out.
    2. Service.Type=NodePort, this would exposing node's IP.
    3. Service.Type=ExternalName, Maps the Service to the contents of the externalName field by returning a CNAME record (You need CoreDNS version 1.7 or higher to use the ExternalName type.)
    4. Ingress. This is a new concept that expose eternal HTTP and/or HTTPS routes to services within the Kubernetes cluster, you can even map a route to multiple services. However, this only maps HTTP and/or HTTPS routes only. (See note#2)
    0 讨论(0)
  • 2021-02-04 11:05

    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.

    0 讨论(0)
提交回复
热议问题