Kubernetes 服务与网络
发布: (2026年1月10日 GMT+8 05:51)
6 分钟阅读
原文: Dev.to
I’m happy to translate the article for you, but I need the full text of the post. Could you please paste the content you’d like translated (excluding the source line you already provided)? Once I have the article text, I’ll translate it into Simplified Chinese while preserving the original formatting, markdown, and any code blocks or URLs.
架构概览(思维模型)

流量路径
Browser
↓
Ingress
↓
Service
↓
Pod
↓
Container
本材料中的所有内容都围绕此流程构建。
模块 1 — Kubernetes 服务与网络
为什么需要 Service
Pods
- 拥有动态 IP
- 可以随时重新创建
- 绝不能直接访问
Service 提供
- 稳定的 IP
- 负载均衡
- Pod 发现
Service 类型
| 类型 | 用途 | 生产环境使用 |
|---|---|---|
| ClusterIP | 内部访问 | 最常用 |
| NodePort | 直接节点访问 | 调试 / 学习 |
| LoadBalancer | 云负载均衡 | 外部流量 |
项目 1 — Service 流量流向
步骤 1 — Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
spec:
replicas: 2
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: app
image: hashicorp/http-echo:0.2.3
args:
- "-listen=:8080"
- "-text=SERVICE WORKS"
ports:
- containerPort: 8080
Apply:
kubectl apply -f deployment.yaml
步骤 2 — ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: web-svc
spec:
selector:
app: web
ports:
- port: 80
targetPort: 8080
Apply:
kubectl apply -f service.yaml
Verify:
kubectl get svc
kubectl get endpoints web-svc
步骤 3 — 在集群内部访问
kubectl run tmp --rm -it --image=busybox -- sh
wget -qO- http://web-svc
学到的关键概念
- Service 使用标签选择 Pods。
- Endpoints 显示真实的流量目标。
- Service 失败通常意味着选择器不匹配。
模块 2 — Ingress(真实生产入口)


Ingress 提供
- 单入口点
- 基于路径的路由
- 基于主机的路由
- SSL 终止
项目 2 — Ingress 路由
步骤 1 — 部署两个版本
稳定部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: stable
spec:
replicas: 2
selector:
matchLabels:
app: echo
version: stable
template:
metadata:
labels:
app: echo
version: stable
spec:
containers:
- name: app
image: hashicorp/http-echo:0.2.3
args:
- "-listen=:8080"
- "-text=STABLE VERSION"
ports:
- containerPort: 8080
金丝雀部署
apiVersion: apps/v1
kind: Deployment
metadata:
name: canary
spec:
replicas: 1
selector:
matchLabels:
app: echo
version: canary
template:
metadata:
labels:
app: echo
version: canary
spec:
containers:
- name: app
image: hashicorp/http-echo:0.2.3
args:
- "-listen=:8080"
- "-text=CANARY VERSION"
ports:
- containerPort: 8080
步骤 2 — 服务
稳定服务
apiVersion: v1
kind: Service
metadata:
name: stable-svc
spec:
selector:
app: echo
version: stable
ports:
- port: 80
targetPort: 8080
金丝雀服务
apiVersion: v1
kind: Service
metadata:
name: canary-svc
spec:
selector:
app: echo
version: canary
ports:
- port: 80
targetPort: 8080
步骤 3 — Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: app-ingress
spec:
rules:
- http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: stable-svc
port:
number: 80
- path: /canary
pathType: Prefix
backend:
service:
name: canary-svc
port:
number: 80
测试
curl http:///
curl http:///canary
MODULE 3 — ConfigMaps 与 Secrets
为何将配置外部化
镜像必须:
- 保持不可变
- 在所有环境中均能运行
配置必须:
- 在不重新构建镜像的情况下进行更改
- 针对特定环境
项目 3 — ConfigMap 注入
步骤 1 — ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: app-config
data:
MESSAGE: "CONFIGMAP VALUE"
步骤 2 — 使用 ConfigMap 的部署
containers:
- name: app
image: hashicorp/http-echo:0.2.3
args:
- "-listen=:8080"
- "-text=$(MESSAGE)"
env:
- name: MESSAGE
valueFrom:
configMapKeyRef:
name: app-config
key: MESSAGE
实时更新配置
kubectl edit configmap app-config
kubectl rollout restart deployment web
MODULE 4 — 资源管理

请求 vs 限制
| 设置 | 含义 |
|---|---|
| requests | 保证的资源 |
| limits | 允许的最大值 |
项目 4 — OOM‑Kill 演示
resources:
requests:
memory: "32Mi"
cpu: "50m"
limits:
memory: "64Mi"
cpu: "100m"
观察:
kubectl describe pod
模块 5 — 自动扩缩 (HPA)
项目 5 — 基于 CPU 的扩缩
步骤 1 — 启用指标
kubectl get apiservices | grep metrics
步骤 2 — HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
生成负载
while true; do wget -qO- http://web-svc; done
观察:
kubectl get hpa
kubectl get pods
MODULE 6 — 日志与故障排除
调试顺序
- Pod 状态
- 事件
- 日志
- 资源使用情况
- 服务端点
常用命令
kubectl get pods
kubectl describe pod
kubectl logs
kubectl get events --sort-by=.metadata.creationTimestamp
事件模拟
- Pod 为 Running
- 浏览器未显示任何内容
- 端点列表为空
- 修复: 正确的 selector
模块 7 — 安全基础
最小化 securityContext
securityContext:
runAsNonRoot: true
allowPrivilegeEscalation: false
镜像最佳实践
- 切勿使用
latest - 使用固定版本
- 使用受信任的注册表
最终综合项目
生产应用包含:
- 带就绪探针的 Deployment
- ClusterIP Service
- Ingress 路由
- ConfigMap
- 资源限制
- HPA
- 日志与事件
- 安全容器设置
这反映了 Kubernetes 在真实公司中的使用方式。