In Kubernetes, a Service makes an application running on a set of pods network-accessible. It provides a consistent DNS name for these pods and distributes traffic across them for load balancing. This section describes the basic concepts of Kubernetes Services and provides a comparison of various Service types.
You can create a specified type of Service in a cluster. The description and application scenarios of different types of Services are listed in the table below.
Service Type | Description | Application |
|---|---|---|
ClusterIP Services are the default type of Kubernetes Services. They assign a virtual IP address, accessible only within the cluster, from the cluster's Service CIDR block. | Services that only need to communicate with each other within a cluster and do not need to be exposed outside the cluster. For example, if a frontend application pod in a cluster needs to access a backend database in the same cluster, you can create a ClusterIP Service for the backend database to ensure that the backend database can be accessed only within the cluster. | |
A NodePort Service opens a port on each node in a cluster to allow external traffic to access the Service through <node-IP-address>:<node-port>. | Scenarios where temporary or low-volume access is required. For example, in a testing environment, you can use a NodePort Service when deploying and debugging a web application. | |
A LoadBalancer Service adds an external load balancer on the top of a NodePort Service and distributes external traffic to multiple pods within a cluster. It automatically assigns an external IP address to allow access from the clients. LoadBalancer Services process TCP and UDP traffic at Layer 4 (transport layer) of the OSI model. They can be extended to support Layer 7 (application layer) capabilities to manage HTTP and HTTPS traffic. | Cloud applications that require a stable, easy-to-manage entry for external access. For example, in a production environment, you can use LoadBalancer Services to expose public-facing services such as web applications and API services to the Internet. These services often need to handle heavy traffic while maintaining high availability. | |
A DNAT Service provides Network Address Translation (NAT) for all nodes in a cluster so that multiple nodes can share an EIP. | Services that require temporary or low-volume access from the Internet. DNAT Services provide higher reliability than NodePort Services. With a DNAT Service, there is no need to bind an EIP to a single node, and requests can still be distributed to the workload even if any of the nodes inside is down. | |
For headless Services, no cluster IP address is allocated. When a client attempts to access a backend pod and performs a DNS query for the Service, it receives a list of the IP addresses of the backend pods. This allows the client to communicate directly with individual pods. | Applications that require direct communication with specific backend pods instead of using proxies or load balancers. For example, when you deploy a stateful application (such as a ClickHouse database), you can use a headless Service. It allows application pods to directly access each ClickHouse pod and enables targeted read and write operations. This enhances overall data processing efficiency. |
If the service affinity of a Service is set to the node level, that is, the value of externalTrafficPolicy is Local, the Service may fail to be accessed from within the cluster (specifically, nodes or containers). Information similar to the following is displayed:
It is common that a load balancer in a cluster cannot be accessed. The reason is as follows: When Kubernetes creates a Service, kube-proxy adds the access address of the load balancer as an external IP address (External-IP, as shown in the following command output) to iptables or IPVS. If a client inside the cluster initiates a request to access the load balancer, the address is considered as the external IP address of the Service, and the request is directly forwarded by kube-proxy without passing through the load balancer outside the cluster.
# kubectl get svc nginxNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEnginx LoadBalancer 10.247.76.156 123.**.**.**,192.168.0.133 80:32146/TCP 37s
When the value of externalTrafficPolicy is Local, the access failures in different container network models and service forwarding modes are as follows: Server Service Type Access Type Client Request Source Tunnel Network Cluster VPC Network Cluster (DataPlane V2 disabled. If enabled, see Clusters with DataPlane V2 Enabled.) CCE Turbo Cluster (DataPlane V2 disabled. If enabled, see Clusters with DataPlane V2 Enabled.) NodePort Service Public/Private network Same node as service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Accessible Different node from service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on another node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node EIP + NodePort on the client node: Failure Access via node IP + NodePort on another node: Failure Accessible Other containers on the same node as the service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Access via node EIP + NodePort on the server node: Success Access via node private EIP + NodePort on the server node: Failure Access via node IP + NodePort on a non-server node: Failure Accessible Other containers on a different node from the service pod Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node IP + NodePort on another node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node IP + NodePort on another node: Failure Accessible LoadBalancer Service using a dedicated load balancer Private network Same node as service pod Inaccessible Inaccessible Accessible Other containers on the same node as the service pod Inaccessible Inaccessible Accessible Server Service Type Access Type Client Request Source Tunnel Network Cluster VPC Network Cluster (DataPlane V2 disabled. If enabled, see Clusters with DataPlane V2 Enabled.) CCE Turbo Cluster (DataPlane V2 disabled. If enabled, see Clusters with DataPlane V2 Enabled.) NodePort Service Public/Private network Same node as service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Accessible Different node from service pod Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node EIP + NodePort on the client node: Failure Access via node IP + NodePort on another node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node EIP + NodePort on the client node: Failure Access via node IP + NodePort on another node: Failure Accessible Other containers on the same node as the service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Access via node EIP + NodePort on the server node: Success Access via node private EIP + NodePort on the server node: Failure Access via node IP + NodePort on a non-server node: Failure Accessible Other containers on a different node from the service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on a non-server node: Failure Accessible LoadBalancer Service using a dedicated load balancer Private network Same node as service pod Inaccessible Inaccessible Accessible Other containers on the same node as the service pod Inaccessible Inaccessible Accessible DataPlane V2 is supported only in CCE VPC-network clusters and Turbo clusters. Once enabled, it replaces kube-proxy entirely, eliminating the need to configure a service forwarding mode. Server Service Type Access Type Client Request Source VPC Network Cluster (DataPlane V2 Enabled to Replace kube-proxy) CCE Turbo Cluster (DataPlane V2 Enabled to Replace kube-proxy) NodePort Service Public/Private network Same node as service pod Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on a non-server node: Success Access via node EIP + NodePort on a non-server node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on a non-server node: Success Access via node EIP + NodePort on a non-server node: Failure Different node from service pod Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on a non-server node: Success Access via node EIP + NodePort on a non-server node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on a non-server node: Success Access via node EIP + NodePort on a non-server node: Failure Other containers on the same node as the service pod Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on another node: Failure Access via node IP + NodePort on the server node: Success Access via node IP + NodePort on another node: Failure Other containers on a different node from the service pod Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node IP + NodePort on another node: Failure Access via node IP + NodePort on the server node: Success Access via node private IP + NodePort on the client node: Success Access via node IP + NodePort on another node: Failure LoadBalancer Service using a dedicated load balancer Private network Same node as service pod Inaccessible Inaccessible Other containers on the same node as the service pod Inaccessible Accessible
The following methods can be used to solve this problem:
Assume that the Service name is my-service and it is in the default namespace. The kubectl command example is as follows:
kubectl patch service my-service -n default --type='merge' -p '{"spec":{"externalTrafficPolicy":"Cluster"}}'