Service是Kubernetes中的抽象层,定义了一组逻辑上的Pod访问策略。它提供了一个稳定的网络入口,屏蔽了后端Pod的动态变化。
Pod是Kubernetes的最小部署单元,包含一个或多个容器。Pod有独立的IP地址,但这个IP会随着Pod的重启、迁移而变化。
Label是附加到K8S对象(如Pod)上的键值对,用于组织和选择对象子集。格式:key: value
Selector是Service连接Pod的桥梁!
Service通过selector字段定义标签选择器,用于匹配拥有对应标签的Pod。只有标签完全匹配的Pod才会被该Service代理。
修改Selector规则,观察哪些Pod会被匹配到Service
等待更新...
spec.selector中定义标签选择器environment = production 或 environment != qaenvironment in (production, qa) 或 environment notin (dev)key: value映射(等值匹配)apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx # 👈 这就是Selector!
env: prod # 必须同时匹配这两个标签
ports:
- protocol: TCP
port: 80
targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx # Pod的Label
env: prod # Pod的Label
template:
metadata:
labels:
app: nginx # 👈 Pod模板中的Label定义
env: prod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 8080
app, version, env, tier等