How can you publish a Kubernetes Service without u

2020-05-23 16:33发布

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 Platform (GCP) to run a Kubernetes cluster currently running on a single node.

I tried to us the externalIPs Service configuration and to give at turns, the IPs of:

  • the instance hosting the Kubernetes cluster (External IP; which also conincides with the IP address of the Kubernetes node as reported by kubernetes describe node)
  • the Kubernetes cluster endpoint (as reported by the Google Cloud Console in the details of the cluster)
  • the public/external IP of another Kubernetes Service of type LoadBalancer running on the same node.

None of the above helped me reach my application using the Kubernetes Service with an externalIPs configuration.

So, how can I publish a service on the Internet without using a LoadBalancer-type Kubernetes Service.

3条回答
相关推荐>>
2楼-- · 2020-05-23 17:11

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

查看更多
够拽才男人
3楼-- · 2020-05-23 17:14

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)
查看更多
▲ chillily
4楼-- · 2020-05-23 17:28

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.

查看更多
登录 后发表回答