问题1:我正在阅读文档,我对其中的措辞有点困惑。它说:

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

NodePort服务类型是否仍然使用ClusterIP,但只是在一个不同的端口上,该端口对外部客户端开放?所以在这种情况下,<NodeIP>:<NodePort>与<ClusterIP>:<NodePort>?

或者NodeIP实际上是运行kubectl get节点时找到的IP,而不是用于ClusterIP服务类型的虚拟IP ?

问题2 -图表中也有下面的链接:

客户端在节点内部有什么特别的原因吗?我假设它需要在ClusterIP服务类型的情况下,ClusterIP服务类型?

如果为NodePort绘制相同的图,那么将客户端完全画在节点和集群之外是否有效?还是我完全错过了重点?


当前回答

Summary: There are five types of Services: ClusterIP (default): Internal clients send requests to a stable internal IP address. NodePort: Clients send requests to the IP address of a node on one or more nodePort values that are specified by the Service. LoadBalancer: Clients send requests to the IP address of a network load balancer. ExternalName: Internal clients use the DNS name of a Service as an alias for an external DNS name. Headless: You can use a headless service when you want a Pod grouping, but don't need a stable IP address. The NodePort type is an extension of the ClusterIP type. So a Service of type NodePort has a cluster IP address. The LoadBalancer type is an extension of the NodePort type. So a Service of type LoadBalancer has a cluster IP address and one or more nodePort values.


通过图像说明


Details ClusterIP ClusterIP is the default and most common service type. Kubernetes will assign a cluster-internal IP address to ClusterIP service. This makes the service only reachable within the cluster. You cannot make requests to service (pods) from outside the cluster. You can optionally set cluster IP in the service definition file. Use Cases Inter-service communication within the cluster. For example, communication between the front-end and back-end components of your app. NodePort NodePort service is an extension of ClusterIP service. A ClusterIP Service, to which the NodePort Service routes, is automatically created. It exposes the service outside of the cluster by adding a cluster-wide port on top of ClusterIP. NodePort exposes the service on each Node’s IP at a static port (the NodePort). Each node proxies that port into your Service. So, external traffic has access to fixed port on each Node. It means any request to your cluster on that port gets forwarded to the service. You can contact the NodePort Service, from outside the cluster, by requesting :. Node port must be in the range of 30000–32767. Manually allocating a port to the service is optional. If it is undefined, Kubernetes will automatically assign one. If you are going to choose node port explicitly, ensure that the port was not already used by another service. Use Cases When you want to enable external connectivity to your service. Using a NodePort gives you the freedom to set up your own load balancing solution, to configure environments that are not fully supported by Kubernetes, or even to expose one or more nodes’ IPs directly. Prefer to place a load balancer above your nodes to avoid node failure. LoadBalancer LoadBalancer service is an extension of NodePort service. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created. It integrates NodePort with cloud-based load balancers. It exposes the Service externally using a cloud provider’s load balancer. Each cloud provider (AWS, Azure, GCP, etc) has its own native load balancer implementation. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service. Traffic from the external load balancer is directed at the backend Pods. The cloud provider decides how it is load balanced. The actual creation of the load balancer happens asynchronously. Every time you want to expose a service to the outside world, you have to create a new LoadBalancer and get an IP address. Use Cases When you are using a cloud provider to host your Kubernetes cluster. ExternalName Services of type ExternalName map a Service to a DNS name, not to a typical selector such as my-service. You specify these Services with the spec.externalName parameter. It maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is established. Use Cases This is commonly used to create a service within Kubernetes to represent an external datastore like a database that runs externally to Kubernetes. You can use that ExternalName service (as a local service) when Pods from one namespace talk to a service in another namespace.

其他回答

Feature ClusterIP NodePort LoadBalancer
Exposition Exposes the Service on an internal IP in the cluster. Exposing services to external clients Exposing services to external clients
Cluster This type makes the Service only reachable from within the cluster A NodePort service, each cluster node opens a port on the node itself (hence the name) and redirects traffic received on that port to the underlying service. A LoadBalancer service accessible through a dedicated load balancer, provisioned from the cloud infrastructure Kubernetes is running on
Accessibility It is default service and Internal clients send requests to a stable internal IP address. The service is accessible at the internal cluster IP-port, and also through a dedicated port on all nodes. Clients connect to the service through the load balancer’s IP.
Yaml Config type: ClusterIP type: NodePort type: LoadBalancer
Port Range Any public ip form Cluster 30000 - 32767 Any public ip form Cluster
User Cases For internal communication Best for testing public or private access or providing access for a small amount of time. widely used For External communication

来源:

Kubernetes在行动 Kubernetes。io服务 Kubernetes Services简单直观地解释了

实际的理解。

我已经为NodePort和ClusterIP创建了2个服务

如果我想访问集群内的服务(从主节点或任何工作节点),那么两者都是可访问的。

现在,如果我想从集群外部访问服务,那么只能访问Nodeport,不能访问ClusterIP。

在这里你可以看到localhost不会监听端口80,即使我的nginx容器监听端口80。


是的,这是唯一的区别。

ClusterIP。公开只能从集群内部访问的服务。 NodePort。通过每个节点IP上的静态端口公开服务。 loadbalance。通过云提供商的负载平衡器公开服务。 ExternalName。通过返回CNAME记录的值,将服务映射到预定义的externalName字段。


实际使用案例

假设您必须在集群中创建以下架构。我想这很常见。

现在,用户只会在某个端口上与前端通信。后端和DB服务始终对外部世界隐藏。

下面是关于图表的问题2的答案,因为它似乎仍然没有直接回答:

客户端在节点内部有什么特别的原因吗?我 假设它需要在ClusterIP的情况下在ClusterIP中 服务类型?

At the diagram the Client is placed inside the Node to highlight the fact that ClusterIP is only accessible on a machine which has a running kube-proxy daemon. Kube-proxy is responsible for configuring iptables according to the data provided by apiserver (which is also visible at the diagram). So if you create a virtual machine and put it into the network where the Nodes of your cluster are and also properly configure networking on that machine so that individual cluster pods are accessible from there, even with that ClusterIP services will not be accessible from that VM, unless the VM has it's iptables configured properly (which doesn't happen without kubeproxy running on that VM).

如果为NodePort绘制了相同的图,那么绘制是否有效 客户端完全外部的节点和集群或我 完全没抓住重点?

在节点和集群外部绘制客户端是有效的,因为NodePort可以被任何能够访问集群节点和相应端口的机器访问,包括集群外部的机器。

Summary: There are five types of Services: ClusterIP (default): Internal clients send requests to a stable internal IP address. NodePort: Clients send requests to the IP address of a node on one or more nodePort values that are specified by the Service. LoadBalancer: Clients send requests to the IP address of a network load balancer. ExternalName: Internal clients use the DNS name of a Service as an alias for an external DNS name. Headless: You can use a headless service when you want a Pod grouping, but don't need a stable IP address. The NodePort type is an extension of the ClusterIP type. So a Service of type NodePort has a cluster IP address. The LoadBalancer type is an extension of the NodePort type. So a Service of type LoadBalancer has a cluster IP address and one or more nodePort values.


通过图像说明


Details ClusterIP ClusterIP is the default and most common service type. Kubernetes will assign a cluster-internal IP address to ClusterIP service. This makes the service only reachable within the cluster. You cannot make requests to service (pods) from outside the cluster. You can optionally set cluster IP in the service definition file. Use Cases Inter-service communication within the cluster. For example, communication between the front-end and back-end components of your app. NodePort NodePort service is an extension of ClusterIP service. A ClusterIP Service, to which the NodePort Service routes, is automatically created. It exposes the service outside of the cluster by adding a cluster-wide port on top of ClusterIP. NodePort exposes the service on each Node’s IP at a static port (the NodePort). Each node proxies that port into your Service. So, external traffic has access to fixed port on each Node. It means any request to your cluster on that port gets forwarded to the service. You can contact the NodePort Service, from outside the cluster, by requesting :. Node port must be in the range of 30000–32767. Manually allocating a port to the service is optional. If it is undefined, Kubernetes will automatically assign one. If you are going to choose node port explicitly, ensure that the port was not already used by another service. Use Cases When you want to enable external connectivity to your service. Using a NodePort gives you the freedom to set up your own load balancing solution, to configure environments that are not fully supported by Kubernetes, or even to expose one or more nodes’ IPs directly. Prefer to place a load balancer above your nodes to avoid node failure. LoadBalancer LoadBalancer service is an extension of NodePort service. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created. It integrates NodePort with cloud-based load balancers. It exposes the Service externally using a cloud provider’s load balancer. Each cloud provider (AWS, Azure, GCP, etc) has its own native load balancer implementation. The cloud provider will create a load balancer, which then automatically routes requests to your Kubernetes Service. Traffic from the external load balancer is directed at the backend Pods. The cloud provider decides how it is load balanced. The actual creation of the load balancer happens asynchronously. Every time you want to expose a service to the outside world, you have to create a new LoadBalancer and get an IP address. Use Cases When you are using a cloud provider to host your Kubernetes cluster. ExternalName Services of type ExternalName map a Service to a DNS name, not to a typical selector such as my-service. You specify these Services with the spec.externalName parameter. It maps the Service to the contents of the externalName field (e.g. foo.bar.example.com), by returning a CNAME record with its value. No proxying of any kind is established. Use Cases This is commonly used to create a service within Kubernetes to represent an external datastore like a database that runs externally to Kubernetes. You can use that ExternalName service (as a local service) when Pods from one namespace talk to a service in another namespace.

不要忘记“new”服务类型(来自k8s文档):

ExternalName:通过返回带有值的CNAME记录,将服务映射到ExternalName字段的内容(例如,foo.bar.example.com)。没有设置任何类型的代理。

注意:使用ExternalName类型需要kube-dns版本1.7或CoreDNS版本0.0.8或更高版本。