← 返回文章列表

Kubernetes 核心概念完全指南:Pod、Deployment、Service、Ingress

深入理解 Kubernetes 核心资源:Pod、Deployment、Service、Ingress 的概念和使用

6 分钟阅读
字号

Kubernetes 核心概念完全指南

本文基于 Kubernetes 1.28


概述

Kubernetes(简称 K8s)是一个容器编排平台,用于自动化容器化应用的部署、扩缩容和管理。

本文介绍 K8s 最核心的四个资源类型:PodDeploymentServiceIngress


一、Pod

什么是 Pod

Pod 是 Kubernetes 的最小调度单位。

一个 Pod 可以包含一个或多个容器,这些容器共享网络和存储。

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: nginx
    image: nginx:1.21
    ports:
    - containerPort: 80

Pod 的特点

  • 共享 network namespace(容器间可用 localhost 通信)
  • 共享 UTS namespace( hostname 相同)
  • 共享 IPC namespace(进程间通信)
  • 通常只包含一个容器(单容器 Pod)
  • 多个容器时称为 Sidecar 模式

常用命令

# 查看 Pod
kubectl get pods
 
# 查看 Pod 详情
kubectl describe pod my-pod
 
# 查看 Pod 日志
kubectl logs my-pod
 
# 进入 Pod 容器
kubectl exec -it my-pod -- /bin/sh
 
# 删除 Pod
kubectl delete pod my-pod

二、Deployment

什么是 Deployment

Deployment 用于管理无状态应用,提供 Pod 的副本管理、滚动更新和回滚能力。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app
        image: my-app:v1.0
        ports:
        - containerPort: 8080
        resources:
          requests:
            memory: "128Mi"
            cpu: "250m"
          limits:
            memory: "256Mi"
            cpu: "500m"
        livenessProbe:
          httpGet:
            path: /health
            port: 8080
          initialDelaySeconds: 10
          periodSeconds: 5
        readinessProbe:
          httpGet:
            path: /ready
            port: 8080
          initialDelaySeconds: 5
          periodSeconds: 3

核心功能

功能说明
副本管理维持指定数量的 Pod 副本
滚动更新平滑升级应用版本
回滚回到之前的版本
扩缩容调整副本数量

滚动更新策略

spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1        # 最多额外 1 个 Pod
      maxUnavailable: 0  # 0 个不可用

常用命令

# 创建 Deployment
kubectl apply -f deployment.yaml
 
# 扩缩容
kubectl scale deployment my-app --replicas=5
 
# 更新镜像
kubectl set image deployment/my-app my-app=my-app:v2.0
 
# 查看更新状态
kubectl rollout status deployment/my-app
 
# 回滚
kubectl rollout undo deployment/my-app
 
# 查看历史版本
kubectl rollout history deployment/my-app

三、Service

什么是 Service

Service 为一组 Pod 提供稳定的访问入口,解决 Pod IP 不稳定的问题。

四种 Service 类型

类型说明外部访问
ClusterIP集群内部访问
NodePort节点端口映射
LoadBalancer云厂商负载均衡
ExternalNameDNS 别名

ClusterIP(默认)

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ClusterIP
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 8080

NodePort

spec:
  type: NodePort
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 8080
    nodePort: 30080

LoadBalancer

spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
  - port: 80
    targetPort: 8080

Service 工作原理

Pod A 访问 Service "my-service:80"

CoreDNS 解析 Service 名为 ClusterIP

kube-proxy 负载均衡到某个 Pod

请求转发到 Pod IP:8080

健康检查与流量

  • livenessProbe 失败 → 重启容器
  • readinessProbe 失败 → 从 Service Endpoints 摘除,不再接收流量

常用命令

# 查看 Service
kubectl get svc
 
# 查看 Service 详情
kubectl describe service my-service
 
# 本地端口转发测试
kubectl port-forward svc/my-service 8080:80

四、Ingress

什么是 Ingress

Ingress 是 HTTP/HTTPS 路由规则,提供七层负载均衡。

Ingress YAML

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

Ingress Controller

Ingress 只是一组规则,Ingress Controller 负责执行这些规则。

常见 Ingress Controller:

  • Nginx Ingress Controller
  • Traefik
  • APISIX
  • 云厂商 ALB

常用命令

# 查看 Ingress
kubectl get ingress
 
# 查看 Ingress 详情
kubectl describe ingress my-ingress

五、资源关系

┌─────────────────────────────────────────────┐
│              Ingress                        │
│         (七层路由:域名/路径)                 │
└──────────────────┬──────────────────────────┘


┌─────────────────────────────────────────────┐
│              Service                       │
│         (四层负载均衡)                       │
└──────────────────┬──────────────────────────┘


┌─────────────────────────────────────────────┐
│              Deployment                    │
│         (管理 Pod 副本)                      │
└──────────────────┬──────────────────────────┘


┌─────────────────────────────────────────────┐
│              Pod                           │
│         (最小调度单位)                       │
└─────────────────────────────────────────────┘

六、总结

资源作用
Pod最小调度单位,包含容器
Deployment管理无状态应用副本
Service提供稳定的访问入口
IngressHTTP/HTTPS 路由

下一步

分享

// RELATED_POSTS

0%