在 Kubernetes 中,Ingress、Service、Deployment 和 Pod 共同构成了应用的完整部署架构。让我详细解释这些核心组件及其关系:

核心组件架构

  1. Pod(容器组)

· 最小部署单元,包含一个或多个容器
· 共享网络命名空间和存储卷
· 临时性资源,可随时被调度和替换

apiVersion: v1
kind: Pod
metadata:
  name: myapp-pod
spec:
  containers:
  - name: nginx
    image: nginx:latest
    ports:
    - containerPort: 80
  1. Deployment(部署)

· 管理 Pod 副本的控制器
· 提供滚动更新、回滚、扩缩容能力
· 确保指定数量的 Pod 始终运行

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: nginx
        image: nginx:1.19
        ports:
        - containerPort: 80
  1. Service(服务)

· 提供稳定的网络端点
· 负载均衡到后端 Pod
· 四种类型:
· ClusterIP(默认):集群内部访问
· NodePort:节点端口暴露
· LoadBalancer:云提供商负载均衡器
· ExternalName:外部服务别名

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: ClusterIP
  1. Ingress(入口)

· 管理外部 HTTP/HTTPS 访问
· 提供基于主机名和路径的路由
· 支持 TLS 终止
· 需要 Ingress Controller(如 Nginx, Traefik)

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: myapp-ingress
spec:
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: myapp-service
            port:
              number: 80

完整工作流程

外部用户
    ↓
[ Ingress ] ← 路由规则、TLS终止
    ↓
[ Service ] ← 负载均衡、服务发现
    ↓
[ Deployment ] ← Pod副本管理、滚动更新
    ↓
[ Pod1, Pod2, Pod3 ] ← 实际运行容器

部署架构最佳实践

  1. 多环境配置
# deployment-prod.yaml
replicas: 5
resources:
  requests:
    memory: "256Mi"
    cpu: "250m"
  limits:
    memory: "512Mi"
    cpu: "500m"

# deployment-dev.yaml
replicas: 2
resources:
  requests:
    memory: "128Mi"
    cpu: "100m"
  1. 健康检查配置
livenessProbe:
  httpGet:
    path: /healthz
    port: 8080
  initialDelaySeconds: 15
  periodSeconds: 20

readinessProbe:
  httpGet:
    path: /ready
    port: 8080
  initialDelaySeconds: 5
  periodSeconds: 10
  1. 完整的应用示例
# 1. 部署应用
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - name: webapp
        image: myapp:v1.2.3
        ports:
        - containerPort: 8080
        env:
        - name: ENVIRONMENT
          value: "production"

# 2. 创建服务
apiVersion: v1
kind: Service
metadata:
  name: webapp-service
spec:
  selector:
    app: webapp
  ports:
  - port: 80
    targetPort: 8080
  type: ClusterIP

# 3. 配置入口
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: webapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  tls:
  - hosts:
    - webapp.example.com
    secretName: tls-secret
  rules:
  - host: webapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: webapp-service
            port:
              number: 80

关键考虑因素

  1. 资源管理
    · 设置合理的 requests/limits
    · 配置 HPA(Horizontal Pod Autoscaler)
  2. 网络策略
    · 使用 NetworkPolicy 控制 Pod 间通信
    · 考虑服务网格(如 Istio)
  3. 存储
    · 使用 PersistentVolumeClaim 持久化数据
    · 配置 ConfigMap 和 Secret
  4. 监控
    · 集成 Prometheus 监控
    · 配置日志收集

这种分层架构提供了:

· 弹性:自动恢复和扩缩容
· 可靠性:负载均衡和健康检查
· 可维护性:清晰的分离关注点
· 安全性:网络隔离和 TLS 支持

Logo

这里是“一人公司”的成长家园。我们提供从产品曝光、技术变现到法律财税的全栈内容,并连接云服务、办公空间等稀缺资源,助你专注创造,无忧运营。

更多推荐