kubernetes - 哪个服务在 Kubernetes 节点之间做负载均衡?
问题描述
k8s集群的入口点是什么?请求如何从入口点路由到某个容器?
解决方案
这个问题很笼统,您有多种类型的负载均衡器(内部、第 3 方、Ingresses ...)
但最好的答案是 Kubernetes服务,因为上述所有服务都依赖于它们。
kubernetes 中的服务是一组 Linux iptables(或 IPVS)规则,它们将对发送到特定 IP 地址的数据包执行目标网络地址转换(DNAT)。简而言之:
1- 服务将有一个称为 ServiceIP 或 ClusterIP 的虚拟 IP 地址。
2- 用户使用 clusterIP 与 kubernetes pod(单个容器或一组相关容器)通信。
3- 节点中的 Iptables 将使用CNI将目标为 ClusterIP 的数据包转发到关联 Pod 的 IP 地址。
负载均衡是通过 iptables 完成的,每个服务都有一个 iptables 规则:
# iptables -t nat -L KUBE-SERVICES
Chain KUBE-SERVICES (2 references)
target prot opt source destination
KUBE-MARK-MASQ tcp -- !10.244.0.0/16 10.104.192.249 /* default/hypriot: cluster IP */ tcp dpt:http
KUBE-SVC-IKNY2FZN6EXMQQCV tcp -- anywhere 10.104.192.249 /* default/hypriot: cluster IP */ tcp dpt:http
# kubectl get svc hypriot
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hypriot ClusterIP 10.104.192.249 <none> 80/TCP 11d
对于这个例子,部署 hypriot 有一个 ClusterIP 10.104.192.249
,第二个 iptables 规则将所有具有这个目标 IP 的数据包转发到 CHAIN KUBE-SVC-IKNY2FZN6EXMQQCV
。
看看这个 CHAIN 会做什么:
# iptables -t nat -L KUBE-SVC-IKNY2FZN6EXMQQCV
Chain KUBE-SVC-IKNY2FZN6EXMQQCV (1 references)
target prot opt source destination
KUBE-SEP-JEK5XLX6ULDDGJAZ all -- anywhere anywhere /* default/hypriot: */ statistic mode random probability 0.33332999982
KUBE-SEP-WTXTLPWDUQWUHKOF all -- anywhere anywhere /* default/hypriot: */ statistic mode random probability 0.50000000000
KUBE-SEP-OQ7KPRR3BI2AFITK all -- anywhere anywhere /* default/hypriot: */
每个 KUBE-SEP 都是一个服务端点,它代表一个 pod 的地址,对于这个部署,hypriot 有 3 个副本。
# kubectl get endpoints hypriot
NAME ENDPOINTS AGE
hypriot 10.244.1.14:80,10.244.2.21:80,10.244.3.153:80 11d
# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE
hypriot-587768b4f5-9dq2k 1/1 Running 0 11d 10.244.2.21 node03
hypriot-587768b4f5-czd86 1/1 Running 0 11d 10.244.3.153 node04
hypriot-587768b4f5-j22sh 1/1 Running 0 11d 10.244.1.14 node02
将选择这些端点之一,并将数据包转发到关联的 KUBE-SEP CHAIN:
# iptables -t nat -L KUBE-SEP-JEK5XLX6ULDDGJAZ
Chain KUBE-SEP-JEK5XLX6ULDDGJAZ (1 references)
target prot opt source destination
KUBE-MARK-MASQ all -- 10.244.1.14 anywhere /* default/hypriot: */
DNAT tcp -- anywhere anywhere /* default/hypriot: */ tcp to:10.244.1.14:80
This is the last piece of the puzzle where the DNAT will occur and the new destination will be the selected pod IP ( 10.244.1.14
for the pod hypriot-587768b4f5-j22sh
in this example), when another service Endpoint is selected, the packet will be DNATed to another pod.
您可以使用 Iptables-v
标志来检查使用的规则,这将有助于您理解过程。
一些不错的读物:https ://kubernetes.io/docs/concepts/extend-kubernetes/compute-storage-net/network-plugins/ https://kubernetes.io/docs/concepts/cluster-administration/networking/ https: //kubernetes.io/docs/concepts/services-networking/service/#proxy-mode-iptables
推荐阅读
- octave - 如何将 RGB 颜色分配给 Octave 中的散点图?
- java - 如何使用 HazelcastJsonValue 聚合?
- c++ - GCOV - 在不同文件夹中生成的 gcda 文件
- cron - 为什么我在 cron 中启动时 WebKit 不工作,但在终端中启动时工作?
- informatica - 如何将高级过滤表达式添加到 Informatica Data Integration Hub
- swift - 如何在 UIImage 上描边?
- android - 在 MaterialCardView 中更改拐角半径的问题
- python - 基于索引的数据集外推
- css - Flex-grow 在 Angular 中不起作用
- dart - Dart:如何获取列表中的倒数第二个项目