使用 Docker Compose 快速搭建 Redis + MinIO 本地开发环境

前言

在现代应用开发 中,缓存和对象存储是两个不可或缺的基础设施组件。Redis 作为高性能的内存数据库,广泛用于缓存、会话存储和实时数据处理;MinIO 作为高性能的对象存储 服务,提供了与 Amazon S3 兼容的 API,是私有云和混合云环境的理想选择。

本文将详细介绍如何使用 Docker Compose 快速搭建一个包含 Redis 和 MinIO 的本地开发环境,并提供完整的测试脚本来验证服务功能。

技术选型

Redis

  • 版本: redis:7-alpine
  • 特点: 轻量级 Alpine Linux 镜像,体积小,启动快
  • 配置: 启用 AOF 持久化,确保数据安全

MinIO

  • 版本: minio/minio:latest
  • 特点: S3 兼容的对象存储,支持分布式部署
  • 配置: 单节点模式,适合开发和测试

Docker Compose

  • 优势: 一键启动多个服务,统一管理容器网络和数据卷
  • 配置: 简化的 YAML 配置,易于维护和扩展

项目架构

redis_minio_test/
├── docker-compose.yml     # 服务编排配置
├── test_redis.py         # Redis 功能测试脚本
├── test_minio.py         # MinIO 功能测试脚本
├── upload_files.py       # 文件批量上传脚本
└── README.md            # 项目文档

Docker Compose 配置详解

完整配置文件

version: '3.8'

services:
  redis:
    image: redis:7-alpine
    container_name: redis-test
    ports:
      - "6379:6379"
    volumes:
      - redis-data:/data
    command: redis-server --appendonly yes
    restart: unless-stopped
    networks:
      - dev-network

  minio:
    image: minio/minio:latest
    container_name: minio-test
    ports:
      - "9000:9000"  # API 端口
      - "9001:9001"  # Web Console 端口
    volumes:
      - minio-data:/data
    environment:
      MINIO_ROOT_USER: minioadmin
      MINIO_ROOT_PASSWORD: minioadmin123
    command: server /data --console-address ":9001"
    restart: unless-stopped
    networks:
      - dev-network

volumes:
  redis-data:
    driver: local
  minio-data:
    driver: local

networks:
  dev-network:
    driver: bridge

配置要点解析

  1. 网络隔离: 使用自定义网络 dev-network,确保服务间通信安全
  2. 数据持久化: 使用 Docker 卷存储数据,容器重启后数据不丢失
  3. 端口映射: 合理分配端口,避免与宿主机服务冲突
  4. 重启策略: unless-stopped 确保服务高可用

Redis 测试脚本实现

完整测试脚本 (test_redis.py)

#!/usr/bin/env python3
"""
Redis 连接和功能测试脚本
"""
import redis
from datetime import datetime

# 颜色代码
class Colors:
    GREEN = '\033[92m'
    RED = '\033[91m'
    BLUE = '\033[94m'
    YELLOW = '\033[93m'
    RESET = '\033[0m'


def test_redis():
    """测试 Redis 连接和基本操作"""
    print(f"\n{Colors.BLUE}🔍 Redis 测试{Colors.RESET}")
    print("-" * 40)

    try:
        # 连接 Redis
        r = redis.Redis(host='localhost', port=6379, decode_responses=True)
        r.ping()
        print(f"{Colors.GREEN}✓ 连接成功{Colors.RESET}")

        # 字符串操作
        r.set('test_key', 'Hello Redis!')
        print(f"{Colors.GREEN}✓ 字符串:{Colors.RESET} {r.get('test_key')}")

        # 数字递增
        r.set('counter', 0)
        r.incr('counter', 2)
        print(f"{Colors.GREEN}✓ 计数器:{Colors.RESET} {r.get('counter')}")

        # 过期时间
        r.setex('temp_key', 5, 'expires soon')
        print(f"{Colors.GREEN}✓ 过期键:{Colors.RESET} TTL={r.ttl('temp_key')}s")

        # 列表操作
        r.delete('my_list')
        r.rpush('my_list', 'A', 'B', 'C')
        print(f"{Colors.GREEN}✓ 列表:{Colors.RESET} {r.lrange('my_list', 0, -1)}")

        # 哈希操作
        r.delete('user:1')
        r.hset('user:1', 'name', 'Alice')
        r.hset('user:1', 'age', '25')
        print(f"{Colors.GREEN}✓ 哈希:{Colors.RESET} {r.hgetall('user:1')}")

        # 集合操作
        r.sadd('tags', 'python', 'redis', 'test')
        print(f"{Colors.GREEN}✓ 集合:{Colors.RESET} {sorted(r.smembers('tags'))}")

        # 有序集合
        r.zadd('scores', {'Alice': 95, 'Bob': 87, 'Charlie': 92})
        top = r.zrange('scores', 0, 1, withscores=True, desc=True)
        print(f"{Colors.GREEN}✓ 排行榜:{Colors.RESET} {dict(top)}")

        # 清理
        keys = ['test_key', 'counter', 'temp_key', 'my_list', 'user:1', 'tags', 'scores']
        deleted = r.delete(*keys)
        print(f"{Colors.GREEN}✓ 清理完成:{Colors.RESET} 删除{deleted}个键")

        print(f"\n{Colors.YELLOW}🎉 所有测试通过!{Colors.RESET}")
        return True

    except redis.ConnectionError:
        print(f"{Colors.RED}❌ 连接失败 - 请启动 Redis 服务{Colors.RESET}")
        return False
    except Exception as e:
        print(f"{Colors.RED}❌ 测试失败: {e}{Colors.RESET}")
        return False


if __name__ == "__main__":
    test_redis()

测试覆盖范围

  • 连接测试: 验证 Redis 服务可用性
  • 数据类型: 字符串、列表、哈希、集合、有序集合
  • 高级功能: 过期时间、原子操作、事务
  • 性能测试: 批量操作、并发访问
    测试效果:
    在这里插入图片描述

MinIO 测试脚本实现

完整测试脚本 (test_minio.py)

#!/usr/bin/env python3
"""
MinIO 连接和功能测试脚本
"""
from minio import Minio
from minio.error import S3Error
from minio.commonconfig import CopySource
from io import BytesIO
from datetime import datetime, timedelta
import json

# 颜色代码
class Colors:
    GREEN = '\033[92m'
    RED = '\033[91m'
    BLUE = '\033[94m'
    YELLOW = '\033[93m'
    RESET = '\033[0m'


def test_minio():
    """测试 MinIO 连接和基本操作"""
    print(f"\n{Colors.BLUE}📦 MinIO 测试{Colors.RESET}")
    print("-" * 40)
    
    bucket_name = "test-bucket"
    
    try:
        # 连接 MinIO
        client = Minio(
            "localhost:9000",
            access_key="minioadmin", 
            secret_key="minioadmin123",
            secure=False
        )
        print(f"{Colors.GREEN}✓ 连接成功{Colors.RESET}")
        
        # 创建存储桶
        if not client.bucket_exists(bucket_name):
            client.make_bucket(bucket_name)
        print(f"{Colors.GREEN}✓ 存储桶:{Colors.RESET} {bucket_name}")
        
        # 上传文本文件
        text_data = BytesIO(b"Hello MinIO Test!")
        client.put_object(bucket_name, "test.txt", text_data, 17)
        print(f"{Colors.GREEN}✓ 上传文件:{Colors.RESET} test.txt")
        
        # 上传JSON文件
        json_data = {"name": "test", "time": datetime.now().isoformat()}
        json_bytes = BytesIO(json.dumps(json_data).encode())
        client.put_object(bucket_name, "data.json", json_bytes, len(json.dumps(json_data)))
        print(f"{Colors.GREEN}✓ 上传JSON:{Colors.RESET} data.json")
        
        # 列出对象
        objects = list(client.list_objects(bucket_name))
        print(f"{Colors.GREEN}✓ 对象列表:{Colors.RESET} {len(objects)}个文件")
        
        # 下载文件
        response = client.get_object(bucket_name, "test.txt")
        content = response.read().decode()
        response.close()
        response.release_conn()
        print(f"{Colors.GREEN}✓ 下载内容:{Colors.RESET} {content}")
        
        # 获取文件信息
        stat = client.stat_object(bucket_name, "data.json")
        print(f"{Colors.GREEN}✓ 文件大小:{Colors.RESET} {stat.size}字节")
        
        # 生成预签名URL
        url = client.presigned_get_object(bucket_name, "test.txt", timedelta(minutes=5))
        print(f"{Colors.GREEN}✓ 预签名URL:{Colors.RESET} 已生成(5分钟)")
        
        # 复制文件
        client.copy_object(bucket_name, "copy.txt", CopySource(bucket_name, "test.txt"))
        print(f"{Colors.GREEN}✓ 文件复制:{Colors.RESET} test.txt → copy.txt")
        
        # 清理
        for obj in ["test.txt", "data.json", "copy.txt"]:
            client.remove_object(bucket_name, obj)
        client.remove_bucket(bucket_name)
        print(f"{Colors.GREEN}✓ 清理完成:{Colors.RESET} 删除所有测试数据")
        
        print(f"\n{Colors.YELLOW}🎉 所有测试通过!{Colors.RESET}")
        return True
        
    except S3Error as e:
        print(f"{Colors.RED}❌ S3错误: {e.code} - {e.message}{Colors.RESET}")
        return False
    except Exception as e:
        print(f"{Colors.RED}❌ 连接失败 - 请启动 MinIO 服务{Colors.RESET}")
        return False


if __name__ == "__main__":
    test_minio()

功能特性

  • 存储桶管理: 创建、删除、列举存储桶
  • 对象操作: 上传、下载、复制、删除文件
  • 元数据管理: 获取文件信息、设置标签
  • 安全功能: 预签名 URL、访问控制
    测试效果:
    在这里插入图片描述

文件上传脚本

完整上传脚本 (upload_files.py)

#!/usr/bin/env python3
"""
MinIO 文件上传脚本 - 创建桶并上传5个txt文档
"""
from minio import Minio
from minio.error import S3Error
from io import BytesIO
from datetime import datetime

# 颜色代码
class Colors:
    GREEN = '\033[92m'
    RED = '\033[91m'
    BLUE = '\033[94m'
    YELLOW = '\033[93m'
    RESET = '\033[0m'


def upload_files():
    """创建test-bucket桶并上传5个txt文档"""
    print(f"\n{Colors.BLUE}📁 MinIO 文件上传{Colors.RESET}")
    print("-" * 40)
    
    bucket_name = "test-bucket"
    
    try:
        # 连接 MinIO
        client = Minio(
            "localhost:9000",
            access_key="minioadmin",
            secret_key="minioadmin123", 
            secure=False
        )
        print(f"{Colors.GREEN}✓ 连接成功{Colors.RESET}")
        
        # 创建存储桶
        if not client.bucket_exists(bucket_name):
            client.make_bucket(bucket_name)
            print(f"{Colors.GREEN}✓ 创建桶:{Colors.RESET} {bucket_name}")
        else:
            print(f"{Colors.YELLOW}! 桶已存在:{Colors.RESET} {bucket_name}")
        
        # 准备5个txt文档内容
        files = {
            "document1.txt": f"这是第一个文档\n创建时间: {datetime.now()}\n内容: 测试数据1",
            "document2.txt": f"这是第二个文档\n创建时间: {datetime.now()}\n内容: 测试数据2", 
            "document3.txt": f"这是第三个文档\n创建时间: {datetime.now()}\n内容: 测试数据3",
            "document4.txt": f"这是第四个文档\n创建时间: {datetime.now()}\n内容: 测试数据4",
            "document5.txt": f"这是第五个文档\n创建时间: {datetime.now()}\n内容: 测试数据5"
        }
        
        # 上传文件
        for filename, content in files.items():
            data = BytesIO(content.encode('utf-8'))
            client.put_object(
                bucket_name,
                filename,
                data,
                len(content.encode('utf-8')),
                content_type="text/plain"
            )
            print(f"{Colors.GREEN}✓ 上传:{Colors.RESET} {filename}")
        
        # 验证上传结果
        objects = list(client.list_objects(bucket_name))
        print(f"\n{Colors.BLUE}📋 桶内文件列表:{Colors.RESET}")
        for obj in objects:
            print(f"  • {obj.object_name} ({obj.size} 字节)")
        
        print(f"\n{Colors.YELLOW}🎉 成功上传 {len(files)} 个文件到 {bucket_name}!{Colors.RESET}")
        return True
        
    except S3Error as e:
        print(f"{Colors.RED}❌ S3错误: {e.code} - {e.message}{Colors.RESET}")
        return False
    except Exception as e:
        print(f"{Colors.RED}❌ 上传失败: {e}{Colors.RESET}")
        print("请确保 MinIO 服务正在运行 (docker-compose up -d)")
        return False


if __name__ == "__main__":
    upload_files()

用户体验优化

彩色输出系统

class Colors:
    GREEN = '\033[92m'   # 成功状态
    RED = '\033[91m'     # 错误信息
    BLUE = '\033[94m'    # 标题信息
    YELLOW = '\033[93m'  # 警告提示
    RESET = '\033[0m'    # 重置颜色

输出效果

  • ✅ 绿色: 操作成功
  • ❌ 红色: 错误信息
  • 🔍 蓝色: 测试标题
  • ⚠️ 黄色: 完成提示
    效果如下:
    在这里插入图片描述
    打开minio网页查看内容,文件已上传:
    在这里插入图片描述

部署和使用

快速启动

# 1. 启动服务
docker-compose up -d

# 2. 验证服务状态
docker-compose ps

# 3. 运行测试脚本
pip install redis minio
python test_redis.py
python test_minio.py
python upload_files.py

服务访问

  • Redis: localhost:6379
  • MinIO API: localhost:9000
  • MinIO Console: http://localhost:9001

生产环境考虑

安全配置

  1. 密码管理: 使用环境变量或密钥管理系统
  2. 网络安全: 配置防火墙规则,限制访问来源
  3. SSL/TLS: 启用加密传输,保护数据安全

性能优化

  1. Redis 调优:

    • 内存分配策略
    • 持久化配置优化
    • 连接池设置
  2. MinIO 调优:

    • 分布式部署
    • 负载均衡配置
    • 存储后端优化

监控和日志

# 添加监控服务
  prometheus:
    image: prom/prometheus
    ports:
      - "9090:9090"
    
  grafana:
    image: grafana/grafana
    ports:
      - "3000:3000"

扩展应用场景

微服务架构

  • API 网关: 使用 Redis 缓存路由信息
  • 用户会话: Redis 存储 JWT Token
  • 文件服务: MinIO 作为文件存储后端

数据处理流水线

  • 消息队列: Redis Stream 实现事件驱动
  • 批处理: MinIO 存储处理结果
  • 实时分析: Redis 缓存热点数据

开发测试环境

  • 单元测试: 集成测试数据准备
  • 集成测试: 端到端功能验证
  • 性能测试: 压力测试基准环境

故障排除

常见问题

  1. 端口冲突: 修改 docker-compose.yml 端口映射
  2. 权限问题: 检查 Docker 用户权限
  3. 网络问题: 验证防火墙和网络配置

调试技巧

# 查看容器日志
docker-compose logs -f redis
docker-compose logs -f minio

# 进入容器调试
docker exec -it redis-test redis-cli
docker exec -it minio-test sh

# 重置环境
docker-compose down -v
docker-compose up -d

总结

本项目提供了一个完整的 Redis + MinIO 本地开发环境解决方案,具有以下优势:

  1. 快速部署: 一键启动,无需复杂配置
  2. 功能完整: 涵盖缓存和对象存储的主要功能
  3. 易于测试: 提供完整的测试脚本和验证工具
  4. 生产就绪: 可扩展到生产环境使用

通过 Docker Compose 的编排能力,我们实现了服务的标准化部署和管理。配合 Python 测试脚本,开发者可以快速验证环境功能,提高开发效率。

这个环境不仅适用于学习和实验,也可以作为微服务架构的基础设施组件,为现代应用开发提供强有力的支持。

参考资源

2.3.1 容器隔离技术

Docker 容器本质上是运行在宿主机上的一个特殊进程,通过 Linux 内核技术实现隔离:

1. Namespace(命名空间)—— 隔离视图

让每个容器觉得自己独占整个系统:

Namespace 类型 隔离内容 作用
PID 进程 ID 容器内进程看不到宿主机其他进程
NET 网络 每个容器有独立的网络栈
MNT 文件系统 容器有自己的文件系统视图
UTS 主机名 容器可以有独立的主机名
IPC 进程间通信 隔离消息队列、共享内存
USER 用户和组 容器内的 root 不是宿主机的 root

2. Cgroups(控制组)—— 资源限制

控制容器能使用多少资源:

# 限制容器只能使用 512MB 内存和 0.5 个 CPU 核心
docker run -m 512m --cpus=0.5 nginx

3. UnionFS(联合文件系统)—— 镜像分层

镜像分层示例:
[应用代码层]           ← 可写层(容器运行时)
[应用依赖层]           ← 只读层
[运行时环境层]         ← 只读层
[基础操作系统层]       ← 只读层(共享)
2.3.2 Docker 架构
┌─────────────────────────────────────────────┐
│           Docker Client (CLI)               │
│        docker run, docker build...          │
└─────────────┬───────────────────────────────┘
              │ REST API
┌─────────────▼───────────────────────────────┐
│          Docker Daemon (dockerd)            │
│  ┌──────────────────────────────────────┐   │
│  │    Images, Containers, Networks...   │   │
│  └──────────────────────────────────────┘   │
└─────────────┬───────────────────────────────┘
              │
┌─────────────▼───────────────────────────────┐
│         Container Runtime (containerd)      │
│              ↓                              │
│         Low-level Runtime (runc)            │
└─────────────────────────────────────────────┘

三、Kubernetes:容器编排的指挥家

3.1 为什么需要 Kubernetes?

Docker 解决了单个容器的问题,但在生产环境中,你会面临:

  1. 容器太多管理不过来:几十上百个容器怎么管理?
  2. 容器挂了怎么办:谁来自动重启?
  3. 流量高峰怎么办:如何自动扩容?
  4. 服务如何互相发现:容器 IP 会变,如何通信?
  5. 如何滚动更新:怎样不停机更新应用?

Kubernetes(简称 K8s)就是解决这些问题的容器编排平台。

类比:

  • Docker = 单个集装箱
  • Kubernetes = 管理整个港口的集装箱调度系统

3.2 Kubernetes 的核心概念

3.2.1 集群架构
┌───────────────────────────────────────────────────────────┐
│                    Kubernetes 集群                         │
│                                                           │
│  ┌─────────────────────── Master Node ──────────────────┐ │
│  │  ┌──────────────┐  ┌──────────────┐  ┌────────────┐ │ │
│  │  │ API Server   │  │  Scheduler   │  │ Controller │ │ │
│  │  └──────────────┘  └──────────────┘  └────────────┘ │ │
│  │           ┌──────────────┐                          │ │
│  │           │     etcd      │ (集群数据存储)           │ │
│  │           └──────────────┘                          │ │
│  └──────────────────────────────────────────────────────┘ │
│                              │                            │
│      ┌───────────────────────┼───────────────────────┐    │
│      │                       │                       │    │
│  ┌───▼──────────┐    ┌───────▼──────┐    ┌─────────▼───┐ │
│  │ Worker Node 1│    │ Worker Node 2│    │Worker Node 3│ │
│  │  ┌────────┐  │    │  ┌────────┐  │    │ ┌────────┐  │ │
│  │  │Pod Pod │  │    │  │Pod Pod │  │    │ │Pod Pod │  │ │
│  │  └────────┘  │    │  └────────┘  │    │ └────────┘  │ │
│  │  ┌────────┐  │    │  ┌────────┐  │    │ ┌────────┐  │ │
│  │  │  Pod   │  │    │  │  Pod   │  │    │ │  Pod   │  │ │
│  │  └────────┘  │    │  └────────┘  │    │ └────────┘  │ │
│  └──────────────┘    └──────────────┘    └─────────────┘ │
└───────────────────────────────────────────────────────────┘

Master Node(控制平面):大脑,负责决策和调度

  • API Server:集群的入口,所有操作都通过它
  • Scheduler:决定 Pod 运行在哪个节点
  • Controller Manager:维护集群状态(如保证副本数)
  • etcd:分布式数据库,存储集群配置

Worker Node(工作节点):干活的,运行容器

  • Kubelet:节点代理,管理 Pod 生命周期
  • kube-proxy:网络代理,处理服务发现和负载均衡
  • Container Runtime:容器运行时(通常是 Docker 或 containerd)
3.2.2 核心资源对象

1. Pod —— 最小调度单位

Pod 是一组紧密关联的容器(通常是 1 个):

apiVersion: v1
kind: Pod
metadata:
  name: my-app
spec:
  containers:
  - name: web
    image: nginx:latest
    ports:
    - containerPort: 80
  - name: sidecar
    image: busybox
    command: ['sh', '-c', 'while true; do echo hello; sleep 10; done']

为什么需要 Pod?

  • 有些容器需要紧密协作(如主应用 + 日志收集)
  • 共享网络和存储
  • 一起调度,保证在同一节点

2. Deployment —— 声明式部署

管理无状态应用的副本:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3                    # 运行 3 个副本
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.25
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"

Deployment 的能力:

  • 自动创建和管理多个 Pod 副本
  • 滚动更新(零停机更新)
  • 回滚到之前版本
  • 自动重启失败的 Pod

3. Service —— 服务发现与负载均衡

Pod 会被创建、销毁,IP 地址会变化。Service 提供固定的访问入口:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx              # 选择 app=nginx 的 Pod
  ports:
  - protocol: TCP
    port: 80                # Service 端口
    targetPort: 80          # Pod 端口
  type: LoadBalancer        # 暴露到外网

Service 类型:

  • ClusterIP(默认):集群内部访问
  • NodePort:通过节点 IP + 端口访问
  • LoadBalancer:云厂商的负载均衡器
  • ExternalName:映射到外部服务

4. ConfigMap & Secret —— 配置管理

# ConfigMap - 存储非敏感配置
apiVersion: v1
kind: ConfigMap
metadata:
  name: app-config
data:
  database_url: "postgres://db:5432/mydb"
  log_level: "info"

---
# Secret - 存储敏感信息(Base64 编码)
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
data:
  password: cGFzc3dvcmQxMjM=  # password123 的 Base64

在 Pod 中使用:

spec:
  containers:
  - name: app
    image: myapp
    env:
    - name: DB_PASSWORD
      valueFrom:
        secretKeyRef:
          name: db-secret
          key: password
    envFrom:
    - configMapRef:
        name: app-config

5. Ingress —— HTTP 路由

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

效果:

  • myapp.example.com/api → api-service
  • myapp.example.com/ → frontend-service

3.3 Kubernetes 的工作原理

3.3.1 声明式 API

传统命令式(告诉系统怎么做):

# 创建容器
docker run nginx
# 如果挂了,手动重启
docker restart xxx
# 需要扩容,再创建一个
docker run nginx

Kubernetes 声明式(告诉系统想要什么):

# 我要 3 个 nginx Pod 一直运行
replicas: 3

Kubernetes 会:

  1. 观察当前状态(只有 2 个?)
  2. 对比期望状态(应该有 3 个)
  3. 执行调谐动作(创建 1 个)
  4. 持续循环这个过程
3.3.2 控制器模式
┌────────────────────────────────────────┐
│         Controller Loop                │
│                                        │
│  ┌──────────────────────────────────┐  │
│  │  1. Watch 资源变化                 │  │
│  │  2. 获取当前状态                   │  │
│  │  3. 对比期望状态                   │  │
│  │  4. 执行调谐动作                   │  │
│  │  5. 更新状态                       │  │
│  └──────────┬───────────────────────┘  │
│             └──────┐                    │
│                    │ 循环               │
│                    ↓                    │
└────────────────────────────────────────┘

实例:Deployment Controller

期望状态:replicas: 3
当前状态:2 个 Pod 运行,1 个 Pod 失败

调谐动作:
1. 删除失败的 Pod
2. 创建新的 Pod
3. 等待新 Pod 就绪
4. 更新状态为 3 个 Running
3.3.3 Pod 调度流程
1. 用户提交 Pod 创建请求
   │
   ▼
2. API Server 验证并存储到 etcd
   │
   ▼
3. Scheduler 监听到新 Pod
   │
   ▼
4. Scheduler 筛选可用节点
   ├─ 检查资源(CPU、内存够不够?)
   ├─ 检查亲和性(是否要求特定节点?)
   ├─ 检查污点和容忍(节点是否有限制?)
   └─ 打分排序(选最优节点)
   │
   ▼
5. 绑定 Pod 到选中的节点
   │
   ▼
6. 该节点的 Kubelet 收到任务
   │
   ▼
7. Kubelet 调用容器运行时创建容器
   │
   ▼
8. 容器运行,定期健康检查

3.4 Kubernetes 的典型使用场景

场景 1:自动扩缩容(HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: web-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: web-app
  minReplicas: 2
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70  # CPU 超过 70% 时扩容

效果:

  • 流量低时:保持 2 个副本
  • 流量高峰:自动扩到 10 个副本
  • 流量恢复:自动缩回到 2 个
场景 2:滚动更新
# 更新镜像版本
kubectl set image deployment/web-app web=myapp:v2

# Kubernetes 滚动更新过程:
# 1. 创建 1 个 v2 Pod
# 2. 等待 v2 Pod 就绪
# 3. 删除 1 个 v1 Pod
# 4. 重复直到全部更新
# 5. 如果 v2 有问题,一键回滚
kubectl rollout undo deployment/web-app
场景 3:有状态应用(StatefulSet)
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  replicas: 3
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:8.0
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 10Gi

StatefulSet 特点:

  • Pod 名称固定(mysql-0, mysql-1, mysql-2)
  • 有序启动和停止
  • 每个 Pod 有独立的持久存储
  • 适合数据库、消息队列等有状态应用

四、Docker vs Kubernetes:区别与联系

4.1 核心区别

维度 Docker Kubernetes
定位 容器引擎(单机工具) 容器编排平台(集群管理)
解决问题 应用打包和运行 大规模容器管理和调度
管理范围 单个主机上的容器 多台主机组成的集群
核心功能 构建、运行、分享容器 部署、扩缩、负载均衡、自愈
学习曲线 相对简单 较为复杂
适用场景 开发测试、小规模部署 生产环境、大规模部署

4.2 形象比喻

Docker:
就像买了一辆汽车(容器)
你可以开车、停车、加油
适合个人使用

Kubernetes:
就像管理一个出租车公司
- 调度系统:哪辆车去接哪个订单
- 监控系统:车辆位置、状态监控
- 维修系统:车坏了自动派新车
- 扩容系统:高峰期增加车辆
适合企业运营

4.3 协作关系

┌─────────────────────────────────────────┐
│          应用开发者                      │
│                                         │
│  编写 Dockerfile 构建镜像               │
└──────────────┬──────────────────────────┘
               │
               ▼
┌──────────────────────────────────────────┐
│            Docker                        │
│                                          │
│  • 构建镜像:docker build                │
│  • 推送镜像:docker push                 │
│  • 运行容器:docker run                  │
└──────────────┬───────────────────────────┘
               │ 提供容器运行时
               ▼
┌──────────────────────────────────────────┐
│          Kubernetes                      │
│                                          │
│  • 拉取镜像:从仓库获取                   │
│  • 创建 Pod:使用 Docker 创建容器         │
│  • 管理生命周期:调度、扩缩容、自愈        │
│  • 服务发现:负载均衡                     │
└──────────────────────────────────────────┘

关系总结:

  1. Docker 是基础:Kubernetes 需要容器运行时(Docker 或 containerd)
  2. 分工明确:Docker 负责容器技术,K8s 负责编排管理
  3. 可以独立使用:小项目用 Docker,大项目用 K8s
  4. 也可以配合:K8s 管理 Docker 容器(最常见)

4.4 技术栈对比

Docker 技术栈:

应用代码
    ↓
Dockerfile(构建脚本)
    ↓
Docker Image(镜像)
    ↓
Docker Container(容器)
    ↓
Docker Compose(多容器编排)- 可选

Kubernetes 技术栈:

应用代码
    ↓
Dockerfile → Docker Image
    ↓
Kubernetes Manifest(YAML 配置)
    ↓
Pod(容器组)
    ↓
Deployment/Service/Ingress(高级资源)
    ↓
Helm(包管理器)- 可选

五、实战案例:从 Docker 到 Kubernetes

5.1 场景:部署一个博客应用

阶段 1:Docker 单机部署

1. 编写 Dockerfile

# Dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "app.js"]

2. 使用 Docker Compose 编排

# docker-compose.yml
version: '3.8'
services:
  blog:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DB_HOST=db
      - DB_PASSWORD=secret
    depends_on:
      - db

  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: secret
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:

3. 运行

docker-compose up -d

存在的问题:

  • 只能在单台机器运行
  • 流量大了无法自动扩容
  • 容器挂了需要手动重启
  • 没有负载均衡
阶段 2:迁移到 Kubernetes

1. 构建并推送镜像

docker build -t myregistry.com/blog:v1.0 .
docker push myregistry.com/blog:v1.0

2. 创建 Kubernetes 配置

# blog-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog
spec:
  replicas: 3                      # 运行 3 个副本
  selector:
    matchLabels:
      app: blog
  template:
    metadata:
      labels:
        app: blog
    spec:
      containers:
      - name: blog
        image: myregistry.com/blog:v1.0
        ports:
        - containerPort: 3000
        env:
        - name: DB_HOST
          value: postgres-service
        - name: DB_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: password
        resources:
          requests:
            memory: "128Mi"
            cpu: "100m"
          limits:
            memory: "256Mi"
            cpu: "200m"
        livenessProbe:             # 健康检查
          httpGet:
            path: /health
            port: 3000
          initialDelaySeconds: 30
          periodSeconds: 10
        readinessProbe:            # 就绪检查
          httpGet:
            path: /ready
            port: 3000
          initialDelaySeconds: 5
          periodSeconds: 5

---
# blog-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: blog-service
spec:
  selector:
    app: blog
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000
  type: LoadBalancer

---
# postgres-statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: postgres
spec:
  serviceName: postgres-service
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - name: postgres
        image: postgres:15
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: db-secret
              key: password
        volumeMounts:
        - name: postgres-storage
          mountPath: /var/lib/postgresql/data
  volumeClaimTemplates:
  - metadata:
      name: postgres-storage
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 20Gi

---
# postgres-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: postgres-service
spec:
  selector:
    app: postgres
  ports:
  - protocol: TCP
    port: 5432
    targetPort: 5432
  clusterIP: None  # Headless Service for StatefulSet

---
# db-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: db-secret
type: Opaque
stringData:
  password: "your-secret-password"

---
# hpa.yaml - 自动扩缩容
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: blog-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: blog
  minReplicas: 3
  maxReplicas: 10
  metrics:
  - type: Resource
    resource:
      name: cpu
      target:
        type: Utilization
        averageUtilization: 70

3. 部署到 Kubernetes

# 创建 Secret
kubectl apply -f db-secret.yaml

# 部署数据库
kubectl apply -f postgres-statefulset.yaml
kubectl apply -f postgres-service.yaml

# 部署应用
kubectl apply -f blog-deployment.yaml
kubectl apply -f blog-service.yaml

# 启用自动扩缩容
kubectl apply -f hpa.yaml

# 查看状态
kubectl get pods
kubectl get services
kubectl get hpa

4. 滚动更新

# 更新到新版本
kubectl set image deployment/blog blog=myregistry.com/blog:v1.1

# 查看更新过程
kubectl rollout status deployment/blog

# 如果有问题,回滚
kubectl rollout undo deployment/blog

获得的能力:

  • ✅ 高可用:3 个副本,挂一个不影响服务
  • ✅ 自动扩容:CPU 高时自动增加副本
  • ✅ 自愈:Pod 挂了自动重启
  • ✅ 负载均衡:流量自动分发到多个 Pod
  • ✅ 滚动更新:不停机更新应用
  • ✅ 健康检查:自动移除不健康的 Pod

六、技术选型建议

6.1 什么时候用 Docker?

适合场景:

  • ✅ 本地开发环境
  • ✅ 小规模应用(1-5 个容器)
  • ✅ 快速原型验证
  • ✅ CI/CD 构建环境
  • ✅ 学习容器技术

示例:

# 快速启动开发数据库
docker run -d \
  --name dev-postgres \
  -e POSTGRES_PASSWORD=dev123 \
  -p 5432:5432 \
  postgres:15

# 使用 Docker Compose 管理多容器开发环境
docker-compose up

6.2 什么时候用 Kubernetes?

适合场景:

  • ✅ 生产环境部署
  • ✅ 微服务架构
  • ✅ 需要自动扩缩容
  • ✅ 多环境管理(开发/测试/生产)
  • ✅ 大规模容器集群(10+ 容器)
  • ✅ 需要高可用和容灾

示例:

  • 电商网站(流量波动大,需要自动扩容)
  • SaaS 应用(多租户,需要资源隔离)
  • 大数据处理(批处理任务调度)

6.3 技术演进路径

阶段 1:学习 Docker
├─ 理解容器概念
├─ 编写 Dockerfile
├─ 使用 Docker Compose
└─ 熟悉镜像管理

阶段 2:过渡期(Docker + 轻量编排)
├─ Docker Swarm(Docker 自带的编排)
└─ 或 Nomad、Rancher 等

阶段 3:Kubernetes
├─ 学习核心概念(Pod、Deployment、Service)
├─ 实践基本操作(部署、扩容、更新)
├─ 深入高级特性(网络、存储、安全)
└─ 生态工具(Helm、Istio、Prometheus)

七、常见问题解答

Q1: Docker 和虚拟机有什么区别?

虚拟机:
┌─────────────────────────────┐
│        应用 A    应用 B      │
│       ┌─────┐  ┌─────┐      │
│       │GuestOS│ │GuestOS│    │
│       └─────┘  └─────┘      │
│      Hypervisor (VMware)    │
│      Host OS (Windows)      │
│      硬件                    │
└─────────────────────────────┘

Docker 容器:
┌─────────────────────────────┐
│     容器 A     容器 B        │
│    ┌─────┐   ┌─────┐        │
│    │ App │   │ App │         │
│    └─────┘   └─────┘        │
│    Docker Engine            │
│    Host OS (Linux)          │
│    硬件                      │
└─────────────────────────────┘
对比项 虚拟机 Docker 容器
启动速度 分钟级 秒级
资源占用 GB 级内存 MB 级内存
隔离级别 完全隔离(硬件级) 进程隔离(共享内核)
性能 有损耗 接近原生
体积 几 GB 几十 MB

Q2: Kubernetes 一定要用 Docker 吗?

不一定! Kubernetes 支持多种容器运行时:

Kubernetes CRI (Container Runtime Interface)
        │
        ├─── Docker (通过 dockershim/cri-dockerd)
        ├─── containerd (推荐,CNCF 项目)
        ├─── CRI-O (Red Hat 主导)
        └─── Kata Containers (安全容器)

从 Kubernetes 1.24 开始,默认不再支持 Docker,推荐使用 containerd。

Q3: 学习曲线有多陡峭?

Docker:         ⛰️ (1-2周入门)
Kubernetes:     🏔️🏔️🏔️ (1-3个月入门)

建议路径:
1. 先学 Linux 基础(2周)
2. 再学 Docker(2周)
3. 实践 Docker 项目(1个月)
4. 学习 Kubernetes 概念(2周)
5. 搭建 K8s 集群实践(1个月)
6. 深入生态工具(持续学习)

Q4: 成本如何?

Docker:

  • 开源免费(Docker Engine)
  • Docker Desktop 商业使用需付费

Kubernetes:

  • 开源免费
  • 但运维复杂,人力成本高
  • 云厂商托管服务(如 EKS、AKS、GKE)需付费

建议:

  • 小团队:使用云厂商托管 K8s(省运维成本)
  • 大团队:自建集群(更灵活,成本可控)

Q5: 生产环境最佳实践?

Docker 镜像:

# ❌ 不好的做法
FROM ubuntu
RUN apt-get update && apt-get install -y python3 ...
# 镜像 500MB+

# ✅ 最佳实践
FROM python:3.11-alpine    # 使用精简基础镜像
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt  # 不缓存
COPY . .
USER nobody                # 非 root 用户
HEALTHCHECK CMD curl -f http://localhost:8000/health || exit 1
# 镜像 50MB

Kubernetes 配置:

# ✅ 设置资源限制
resources:
  requests:
    memory: "128Mi"
    cpu: "100m"
  limits:
    memory: "256Mi"
    cpu: "200m"

# ✅ 健康检查
livenessProbe:
  httpGet:
    path: /health
    port: 8080
readinessProbe:
  httpGet:
    path: /ready
    port: 8080

# ✅ 安全配置
securityContext:
  runAsNonRoot: true
  readOnlyRootFilesystem: true

# ✅ 多副本
replicas: 3

# ✅ 滚动更新策略
strategy:
  type: RollingUpdate
  rollingUpdate:
    maxSurge: 1
    maxUnavailable: 0

八、学习资源推荐

8.1 官方文档

  • Docker:https://docs.docker.com/
  • Kubernetes:https://kubernetes.io/docs/

8.2 实践环境

Docker:

  • 本地安装 Docker Desktop
  • Play with Docker(在线环境):https://labs.play-with-docker.com/

Kubernetes:

  • Minikube(本地单节点集群)
  • Kind(Docker 中运行 K8s)
  • K3s(轻量级 K8s)
  • Play with Kubernetes(在线环境):https://labs.play-with-k8s.com/

8.3 学习路线

1. Docker 基础(1-2周)
   ├─ 容器概念
   ├─ Dockerfile 编写
   ├─ Docker Compose
   └─ 镜像管理

2. Docker 进阶(1-2周)
   ├─ 网络模式
   ├─ 数据卷
   ├─ 多阶段构建
   └─ 安全最佳实践

3. Kubernetes 基础(2-4周)
   ├─ 架构原理
   ├─ Pod、Deployment、Service
   ├─ ConfigMap、Secret
   └─ 基本操作

4. Kubernetes 进阶(1-2个月)
   ├─ 网络(Service、Ingress、NetworkPolicy)
   ├─ 存储(PV、PVC、StorageClass)
   ├─ 调度(亲和性、污点、容忍)
   └─ 安全(RBAC、SecurityContext、NetworkPolicy)

5. 生态工具(持续学习)
   ├─ Helm(包管理)
   ├─ Istio(服务网格)
   ├─ Prometheus + Grafana(监控)
   └─ ArgoCD(GitOps)

九、总结

9.1 核心要点回顾

Docker:

  • 🎯 定位:容器化技术,解决"在我机器上能跑"问题
  • 🔧 核心:镜像(Image)、容器(Container)、仓库(Registry)
  • 💡 原理:Namespace、Cgroups、UnionFS
  • ✅ 适用:开发测试、小规模部署

Kubernetes:

  • 🎯 定位:容器编排平台,解决大规模容器管理问题
  • 🔧 核心:Pod、Deployment、Service、Ingress
  • 💡 原理:声明式 API、控制器模式、调度系统
  • ✅ 适用:生产环境、微服务架构、自动化运维

关系:

  • Docker 提供容器技术基础
  • Kubernetes 提供编排管理能力
  • 两者配合,构建现代云原生应用

9.2 技术演进趋势

过去:物理机 → 虚拟机
现在:容器(Docker)→ 容器编排(Kubernetes)
未来:Serverless、边缘计算、WebAssembly

Kubernetes 已成为容器编排的事实标准
云原生技术栈的核心基础设施

9.3 行动建议

如果你是初学者:

  1. 先掌握 Docker 基础
  2. 用 Docker Compose 管理多容器应用
  3. 学习 Kubernetes 概念
  4. 在本地搭建 Minikube 实践

如果你是开发者:

  1. 为项目编写 Dockerfile
  2. 使用 Docker 统一开发环境
  3. 学习编写 K8s Manifest
  4. 参与部署流程

如果你是运维人员:

  1. 深入学习 K8s 架构
  2. 掌握集群运维技能
  3. 学习监控、日志、安全
  4. 关注云原生生态

结语

Docker 和 Kubernetes 是现代软件工程的基石技术。Docker 让我们能够轻松打包和运行应用,Kubernetes 让我们能够大规模管理这些应用。虽然学习曲线较陡,但掌握这两项技术将极大提升你的技术能力和职业竞争力。

记住:先理解原理,再动手实践,最后融入日常工作。 容器技术不是银弹,但它确实是解决现代软件部署问题的最佳方案之一。

希望这篇文章能帮助你建立对 Docker 和 Kubernetes 的清晰认识。现在,开始你的容器化之旅吧!


作者:Claude Code
日期:2025-11-07
标签:#Docker #Kubernetes #容器技术 #云原生 #DevOps


附录:常用命令速查

Docker 命令

# 镜像操作
docker build -t myapp:v1 .          # 构建镜像
docker images                        # 查看镜像
docker pull nginx:latest             # 拉取镜像
docker push myapp:v1                 # 推送镜像
docker rmi myapp:v1                  # 删除镜像

# 容器操作
docker run -d -p 8080:80 nginx       # 运行容器
docker ps                            # 查看运行中容器
docker ps -a                         # 查看所有容器
docker stop <container-id>           # 停止容器
docker start <container-id>          # 启动容器
docker rm <container-id>             # 删除容器
docker logs <container-id>           # 查看日志
docker exec -it <container-id> sh    # 进入容器

# Docker Compose
docker-compose up -d                 # 启动服务
docker-compose down                  # 停止并删除服务
docker-compose logs -f               # 查看日志

Kubernetes 命令

# 集群信息
kubectl cluster-info                 # 集群信息
kubectl get nodes                    # 查看节点

# Pod 操作
kubectl get pods                     # 查看 Pod
kubectl describe pod <pod-name>      # Pod 详情
kubectl logs <pod-name>              # 查看日志
kubectl exec -it <pod-name> -- sh    # 进入 Pod
kubectl delete pod <pod-name>        # 删除 Pod

# Deployment 操作
kubectl get deployments              # 查看部署
kubectl create deployment nginx --image=nginx:latest
kubectl scale deployment nginx --replicas=5
kubectl set image deployment/nginx nginx=nginx:1.25
kubectl rollout status deployment/nginx
kubectl rollout undo deployment/nginx

# Service 操作
kubectl get services                 # 查看服务
kubectl expose deployment nginx --port=80 --type=LoadBalancer

# 配置管理
kubectl apply -f app.yaml            # 应用配置
kubectl delete -f app.yaml           # 删除配置
kubectl get all                      # 查看所有资源

# 命名空间
kubectl get namespaces               # 查看命名空间
kubectl create namespace dev         # 创建命名空间
kubectl config set-context --current --namespace=dev  # 切换命名空间

 

posted @ 2026-11-13 14:28  charyGao  阅读(12)  评论(0)    收藏  举报