深入解析容器化技术与容器编排核心组件的关系
这是最容易困惑的地方,Service 不是通过 IP 找 Pod 的,而是通过 Label Selector(标签选择器)!
app=web version=v1selector: app=webapp=web 标签的 Pod 都会被该 Service 发现👉 类比:Service 是个"招聘负责人",Label 是"技能标签",有对应技能的 Pod 都会被"录用"(加入负载均衡)。
Pod 是 K8S 的调度单元,Container 是 Docker 的运行实例。
外部请求进入 K8S 集群的完整链路
app: web
# Pod 的定义(注意 metadata.labels)
apiVersion: v1
kind: Pod
metadata:
labels:
app: web # ← 这个标签会被 Service 匹配
version: v1
spec:
containers:
- name: nginx
image: nginx:1.25
---
# Service 的定义(注意 spec.selector)
apiVersion: v1
kind: Service
metadata:
name: service-a
spec:
selector:
app: web # ← 匹配所有带 "app: web" 标签的 Pod
ports:
- port: 80
targetPort: 8080
type: ClusterIP
K8S 会自动发现所有 labels: app=web 的 Pod,并将它们加入 Service A 的负载均衡池。
注意:③→④→⑤ 可类比为:容器 → 管理容器的副本集 → 暴露服务的入口