关键性能指标:
┌─────────────────────────────────────────┐
│ Docker性能指标 │
├─────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────┐ │
│ │ CPU性能 │ │
│ │ ├─ CPU使用率 │ │
│ │ ├─ CPU负载 │ │
│ │ ├─ 上下文切换 │ │
│ │ └─ CPU就绪时间 │ │
│ └─────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────┐ │
│ │ 内存性能 │ │
│ │ ├─ 内存使用率 │ │
│ │ ├─ 缓存命中率 │ │
│ │ ├─ 页面错误 │ │
│ │ └─ OOM事件 │ │
│ └─────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────┐ │
│ │ IO性能 │ │
│ │ ├─ 读写延迟 │ │
│ │ ├─ IOPS │ │
│ │ ├─ 吞吐量 │ │
│ │ └─ 队列长度 │ │
│ └─────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────┐ │
│ │ 网络性能 │ │
│ │ ├─ 网络延迟 │ │
│ │ ├─ 吞吐量 │ │
│ │ ├─ 丢包率 │ │
│ │ └─ 连接数 │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────┘
东巴文理解:
性能指标对比:
┌──────────────────┬──────────────┬──────────────┐
│ 指标类型 │ 正常范围 │ 告警阈值 │
├──────────────────┼──────────────┼──────────────┤
│ CPU使用率 │ < 70% │ > 80% │
│ 内存使用率 │ < 80% │ > 90% │
│ 磁盘IO延迟 │ < 10ms │ > 50ms │
│ 网络延迟 │ < 1ms │ > 10ms │
│ 应用响应时间 │ < 100ms │ > 500ms │
└──────────────────┴──────────────┴──────────────┘
性能调优目标:
├─ 提升吞吐量: 处理更多请求
├─ 降低延迟: 快速响应
├─ 提高资源利用率: 充分利用资源
├─ 保证稳定性: 避免性能抖动
└─ 降低成本: 资源优化配置
常见性能瓶颈:
CPU瓶颈:
├─ CPU使用率高
├─ 上下文切换频繁
├─ 进程调度延迟
└─ CPU就绪时间长
内存瓶颈:
├─ 内存使用率高
├─ 频繁GC
├─ 页面错误多
└─ OOM Killer触发
IO瓶颈:
├─ 磁盘IO延迟高
├─ IOPS达到上限
├─ 吞吐量不足
└─ IO队列过长
网络瓶颈:
├─ 网络延迟高
├─ 带宽不足
├─ 丢包率高
└─ 连接数过多
性能调优步骤:
┌─────────────────────────────────────────┐
│ 性能调优流程 │
├─────────────────────────────────────────┤
│ │
│ 1. 性能监控 │
│ ├─ 收集性能数据 │
│ ├─ 建立性能基线 │
│ └─ 识别性能问题 │
│ │
│ 2. 性能分析 │
│ ├─ 分析性能瓶颈 │
│ ├─ 定位问题根源 │
│ └─ 制定调优方案 │
│ │
│ 3. 性能优化 │
│ ├─ 实施优化方案 │
│ ├─ 验证优化效果 │
│ └─ 调整优化策略 │
│ │
│ 4. 性能测试 │
│ ├─ 压力测试 │
│ ├─ 稳定性测试 │
│ └─ 性能对比 │
│ │
│ 5. 持续优化 │
│ ├─ 监控性能变化 │
│ ├─ 发现新问题 │
│ └─ 循环优化 │
└─────────────────────────────────────────┘
调优原则:
性能调优原则:
✅ 先监控后优化
✅ 找准瓶颈
✅ 逐步优化
✅ 量化效果
✅ 避免过度优化
✅ 权衡成本收益
✅ 保持系统稳定
✅ 文档化过程
查看CPU使用情况:
# 查看容器CPU使用
docker stats --no-stream
# 输出
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a1b2c3d4e5f6 nginx 85.50% 5.234MiB / 7.777GiB 0.07% 1.2kB / 0B 0B / 0B 2
# 查看主机CPU使用
top -p $(docker inspect --format '{{.State.Pid}}' nginx)
# 查看CPU详细信息
docker inspect --format '{{.HostConfig.NanoCpus}}' nginx
# 查看CPU配额
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.cfs_quota_us
cat /sys/fs/cgroup/cpu/docker/<container_id>/cpu.cfs_period_us
CPU性能分析工具:
# 使用perf分析CPU性能
perf top -p <pid>
# 输出
Samples: 10K of event 'cpu-clock', Event count (approx.): 2500000000
Overhead Shared Object Symbol
45.23% nginx [.] ngx_http_process_request
30.15% nginx [.] ngx_http_parse_request_line
24.62% [kernel] [k] __do_softirq
# 使用strace跟踪系统调用
strace -c -p <pid>
# 输出
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
45.23 0.001234 12 100 0 read
30.15 0.000823 15 55 0 write
24.62 0.000672 18 37 0 open
------ ----------- ----------- --------- --------- ----------------
100.00 0.002729 192 0 total
CPU瓶颈特征:
CPU瓶颈指标:
├─ CPU使用率持续 > 80%
├─ 运行队列长度 > CPU核心数
├─ 上下文切换频繁
├─ CPU就绪时间长
└─ 应用响应慢
CPU瓶颈原因:
├─ 计算密集型任务
├─ 线程数过多
├─ 锁竞争严重
├─ 系统调用频繁
└─ 算法效率低
设置CPU限制:
# 限制CPU使用核数
docker run -d \
--name nginx \
--cpus="1.5" \
nginx
# 设置CPU配额
docker run -d \
--name nginx \
--cpu-quota=50000 \
--cpu-period=100000 \
nginx
# 设置CPU权重
docker run -d \
--name nginx \
--cpu-shares=512 \
nginx
# 绑定CPU核心
docker run -d \
--name nginx \
--cpuset-cpus="0,1" \
nginx
# 实时调整CPU限制
docker update --cpus="2.0" nginx
CPU限制说明:
CPU限制方式:
┌──────────────────┬──────────────┬──────────────┐
│ 方式 │ 说明 │ 适用场景 │
├──────────────────┼──────────────┼──────────────┤
│ --cpus │ CPU核数限制 │ 精确控制 │
│ --cpu-quota │ CPU配额 │ 精确控制 │
│ --cpu-shares │ CPU权重 │ 相对分配 │
│ --cpuset-cpus │ CPU核心绑定 │ 性能优化 │
└──────────────────┴──────────────┴──────────────┘
CPU配额计算:
CPU核数 = cpu-quota / cpu-period
例如: --cpu-quota=50000 --cpu-period=100000
CPU核数 = 50000 / 100000 = 0.5核
CPU亲和性配置:
# 绑定到特定CPU核心
docker run -d \
--name nginx \
--cpuset-cpus="0,1" \
nginx
# 绑定到CPU核心范围
docker run -d \
--name nginx \
--cpuset-cpus="0-3" \
nginx
# 查看CPU亲和性
taskset -pc $(docker inspect --format '{{.State.Pid}}' nginx)
# 输出
pid 12345's current affinity list: 0,1
pid 12345's new affinity list: 0,1
CPU亲和性优势:
CPU亲和性优势:
✅ 减少CPU迁移
✅ 提高缓存命中率
✅ 降低上下文切换
✅ 提升性能稳定性
适用场景:
├─ 高性能计算
├─ 实时应用
├─ NUMA架构
└─ 低延迟应用
进程优先级调整:
# 设置进程优先级
docker run -d \
--name nginx \
--cpu-rt-runtime=950000 \
--cpu-rt-period=1000000 \
--cap-add=sys_nice \
nginx
# 调整nice值
renice -n -10 -p $(docker inspect --format '{{.State.Pid}}' nginx)
# 查看进程优先级
ps -eo pid,ni,cmd | grep nginx
查看内存使用情况:
# 查看容器内存使用
docker stats --no-stream nginx
# 输出
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a1b2c3d4e5f6 nginx 0.50% 150.5MiB / 512MiB 29.39% 1.2kB / 0B 0B / 0B 2
# 查看内存详细信息
cat /sys/fs/cgroup/memory/docker/<container_id>/memory.usage_in_bytes
cat /sys/fs/cgroup/memory/docker/<container_id>/memory.limit_in_bytes
cat /sys/fs/cgroup/memory/docker/<container_id>/memory.stat
# 查看内存映射
pmap -x $(docker inspect --format '{{.State.Pid}}' nginx)
# 查看内存页面错误
ps -o min_flt,maj_flt -p $(docker inspect --format '{{.State.Pid}}' nginx)
内存性能分析工具:
# 使用free查看内存
free -h
# 输出
total used free shared buff/cache available
Mem: 7.7Gi 2.1Gi 4.2Gi 256Mi 1.4Gi 5.1Gi
Swap: 2.0Gi 0B 2.0Gi
# 使用vmstat查看内存统计
vmstat 1 5
# 输出
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 4312000 102400 1433600 0 0 5 10 50 80 5 2 93 0 0
# 使用smem查看内存使用
smem -P nginx
# 输出
PID User Command Swap USS PSS RSS
12345 root nginx: master process 0 1024 2048 4096
内存瓶颈特征:
内存瓶颈指标:
├─ 内存使用率 > 90%
├─ 频繁页面错误
├─ Swap使用增加
├─ OOM Killer触发
└─ 应用响应慢
内存瓶颈原因:
├─ 内存泄漏
├─ 缓存配置过大
├─ 对象创建过多
├─ 数据结构不合理
└─ 内存碎片化
设置内存限制:
# 限制内存使用
docker run -d \
--name nginx \
--memory="512m" \
--memory-swap="1g" \
nginx
# 设置内存预留
docker run -d \
--name nginx \
--memory="512m" \
--memory-reservation="256m" \
nginx
# 禁用OOM Killer
docker run -d \
--name nginx \
--memory="512m" \
--oom-kill-disable=true \
nginx
# 设置OOM分数
docker run -d \
--name nginx \
--memory="512m" \
--oom-score-adj=-500 \
nginx
# 实时调整内存限制
docker update --memory="1g" nginx
内存限制说明:
内存限制方式:
┌──────────────────┬──────────────┬──────────────┐
│ 参数 │ 说明 │ 效果 │
├──────────────────┼──────────────┼──────────────┤
│ --memory │ 内存硬限制 │ 超过触发OOM │
│ --memory-reservation│ 内存软限制│ 尽量保证 │
│ --memory-swap │ 内存+Swap限制 │ 总内存限制 │
│ --oom-kill-disable│ 禁用OOM │ 不触发OOM │
│ --oom-score-adj│ OOM优先级 │ 调整优先级 │
└──────────────────┴──────────────┴──────────────┘
内存计算:
memory-swap = memory + swap
例如: --memory="512m" --memory-swap="1g"
swap = 1g - 512m = 512m
内存swappiness调整:
# 设置内存swappiness
docker run -d \
--name nginx \
--memory="512m" \
--memory-swappiness=0 \
nginx
# 说明
# 0: 尽量不使用swap
# 100: 积极使用swap
# 默认: -1 (使用主机设置)
内存锁优化:
# 锁定内存,防止swap
docker run -d \
--name redis \
--memory="512m" \
--cap-add=IPC_LOCK \
redis
# Redis配置
redis-server --maxmemory 400mb --maxmemory-policy allkeys-lru
内存大页优化:
# 启用透明大页
echo always > /sys/kernel/mm/transparent_hugepage/enabled
# 查看大页状态
cat /sys/kernel/mm/transparent_hugepage/enabled
# 使用大页运行容器
docker run -d \
--name nginx \
--memory="512m" \
--mount type=tmpfs,destination=/dev/hugepages \
nginx
查看IO使用情况:
# 查看容器IO使用
docker stats --no-stream nginx
# 输出
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a1b2c3d4e5f6 nginx 0.50% 5.234MiB / 7.777GiB 0.07% 1.2kB / 0B 15.3MB / 5.2MB 2
# 查看IO详细信息
iostat -x 1 5
# 输出
avg-cpu: %user %nice %system %iowait %steal %idle
5.00 0.00 2.00 1.00 0.00 92.00
Device: rrqm/s wrqm/s r/s w/s rkB/s wkB/s avgrq-sz avgqu-sz await r_await w_await svctm %util
sda 0.00 5.00 2.00 3.00 16.00 32.00 19.20 0.01 2.00 1.00 2.67 0.80 0.40
# 查看进程IO
iotop -p $(docker inspect --format '{{.State.Pid}}' nginx)
# 查看磁盘IO延迟
fio --name=randread --ioengine=libaio --iodepth=16 --rw=randread --bs=4k --direct=1 --size=1G --numjobs=1 --filename=/tmp/test
IO性能分析工具:
# 使用iostat查看IO统计
iostat -x 1
# 使用iotop查看进程IO
iotop
# 使用fio测试IO性能
fio --name=randread --ioengine=libaio --iodepth=16 --rw=randread --bs=4k --direct=1 --size=1G --numjobs=1
# 使用dd测试磁盘性能
dd if=/dev/zero of=/tmp/test bs=1M count=1024 oflag=direct
IO瓶颈特征:
IO瓶颈指标:
├─ IO等待时间 > 10ms
├─ IO利用率 > 80%
├─ IO队列长度 > 2
├─ 磁盘吞吐量达到上限
└─ 应用响应慢
IO瓶颈原因:
├─ 磁盘性能不足
├─ 随机IO过多
├─ IO调度算法不当
├─ 缓存配置不合理
└─ 文件系统性能差
设置IO限制:
# 限制磁盘IO带宽
docker run -d \
--name nginx \
--device-read-bps=/dev/sda:10mb \
--device-write-bps=/dev/sda:10mb \
nginx
# 限制IO操作数
docker run -d \
--name nginx \
--device-read-iops=/dev/sda:1000 \
--device-write-iops=/dev/sda:1000 \
nginx
# 设置IO权重
docker run -d \
--name nginx \
--blkio-weight=500 \
nginx
# 实时调整IO限制
docker update --blkio-weight=700 nginx
IO限制说明:
IO限制方式:
┌──────────────────┬──────────────┬──────────────┐
│ 参数 │ 说明 │ 单位 │
├──────────────────┼──────────────┼──────────────┤
│ --device-read-bps│ 读带宽限制 │ KB/MB/GB │
│ --device-write-bps│ 写带宽限制 │ KB/MB/GB │
│ --device-read-iops│ 读IOPS限制 │ 次数/秒 │
│ --device-write-iops│ 写IOPS限制│ 次数/秒 │
│ --blkio-weight │ IO权重 │ 10-1000 │
└──────────────────┴──────────────┴──────────────┘
IO权重说明:
默认权重: 500
范围: 10-1000
权重越高,获得的IO资源越多
选择合适的存储驱动:
# 查看当前存储驱动
docker info | grep "Storage Driver"
# 输出
Storage Driver: overlay2
# 配置存储驱动
# /etc/docker/daemon.json
{
"storage-driver": "overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true",
"overlay2.size=20G"
]
}
# 重启Docker
systemctl restart docker
存储驱动对比:
存储驱动性能对比:
┌──────────────────┬──────────────┬──────────────┐
│ 驱动 │ 性能 │ 适用场景 │
├──────────────────┼──────────────┼──────────────┤
│ overlay2 │ 优秀 │ 推荐使用 │
│ devicemapper │ 一般 │ RHEL/CentOS │
│ btrfs │ 好 │ 特定场景 │
│ zfs │ 好 │ 特定场景 │
│ vfs │ 差 │ 测试环境 │
└──────────────────┴──────────────┴──────────────┘
overlay2优势:
✅ 性能优秀
✅ 空间效率高
✅ 配置简单
✅ 社区支持好
使用数据卷优化IO:
# 使用数据卷
docker run -d \
--name nginx \
--mount type=volume,source=mydata,destination=/data \
nginx
# 使用绑定挂载
docker run -d \
--name nginx \
--mount type=bind,source=/host/data,destination=/data \
nginx
# 使用tmpfs(内存文件系统)
docker run -d \
--name nginx \
--mount type=tmpfs,destination=/tmp,tmpfs-size=100m \
nginx
# 使用多个数据卷
docker run -d \
--name nginx \
--mount type=volume,source=data1,destination=/data1 \
--mount type=volume,source=data2,destination=/data2 \
nginx
卷挂载性能对比:
卷挂载性能:
┌──────────────────┬──────────────┬──────────────┐
│ 类型 │ 性能 │ 适用场景 │
├──────────────────┼──────────────┼──────────────┤
│ 数据卷 │ 好 │ 持久化数据 │
│ 绑定挂载 │ 最好 │ 高性能IO │
│ tmpfs │ 极好 │ 临时数据 │
│ 容器层 │ 一般 │ 不推荐 │
└──────────────────┴──────────────┴──────────────┘
性能优化建议:
✅ 高IO应用使用绑定挂载
✅ 临时数据使用tmpfs
✅ 避免在容器层写入大量数据
✅ 使用SSD存储
查看网络使用情况:
# 查看容器网络使用
docker stats --no-stream nginx
# 输出
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
a1b2c3d4e5f6 nginx 0.50% 5.234MiB / 7.777GiB 0.07% 15.2MB / 8.5MB 0B / 0B 2
# 查看网络详细信息
docker network inspect bridge
# 查看网络接口
docker exec nginx ifconfig
# 查看网络连接
docker exec nginx netstat -an
# 查看网络统计
docker exec nginx cat /proc/net/dev
网络性能分析工具:
# 使用iperf测试网络带宽
# 服务端
docker run -d --name iperf-server --network host networkstatic/iperf3 -s
# 客户端
docker run --rm --network host networkstatic/iperf3 -c <server_ip>
# 输出
[ ID] Interval Transfer Bandwidth Retr
[ 4] 0.00-10.00 sec 1.10 GBytes 945 Mbits/sec 0 sender
[ 4] 0.00-10.00 sec 1.10 GBytes 944 Mbits/sec receiver
# 使用ping测试网络延迟
ping -c 10 <container_ip>
# 使用traceroute跟踪路由
traceroute <container_ip>
# 使用tcpdump抓包分析
tcpdump -i docker0 -nn
网络瓶颈特征:
网络瓶颈指标:
├─ 网络延迟 > 10ms
├─ 丢包率 > 0.1%
├─ 带宽利用率 > 80%
├─ 连接数过多
└─ 应用响应慢
网络瓶颈原因:
├─ 网络带宽不足
├─ 网络延迟高
├─ TCP参数配置不当
├─ 网络拥塞
└─ DNS解析慢
选择合适的网络模式:
# Bridge模式(默认)
docker run -d --name nginx --network bridge nginx
# Host模式(高性能)
docker run -d --name nginx --network host nginx
# None模式(隔离)
docker run -d --name nginx --network none nginx
# 自定义网络
docker network create --driver bridge mynetwork
docker run -d --name nginx --network mynetwork nginx
网络模式性能对比:
网络模式性能:
┌──────────────────┬──────────────┬──────────────┐
│ 模式 │ 性能 │ 适用场景 │
├──────────────────┼──────────────┼──────────────┤
│ bridge │ 一般 │ 默认选择 │
│ host │ 最好 │ 高性能需求 │
│ none │ 无网络 │ 安全隔离 │
│ 自定义网络 │ 好 │ 推荐使用 │
│ overlay │ 较差 │ 跨主机通信 │
│ macvlan │ 好 │ 直接接入网络 │
└──────────────────┴──────────────┴──────────────┘
性能优化建议:
✅ 高性能应用使用host模式
✅ 需要隔离使用自定义网络
✅ 避免使用overlay网络(除非必要)
✅ 使用macvlan获得最佳性能
TCP参数优化:
# 优化主机TCP参数
# /etc/sysctl.conf
net.core.somaxconn = 65535
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_wmem = 4096 65536 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.core.wmem_max = 16777216
net.core.rmem_max = 16777216
# 应用配置
sysctl -p
# 容器内TCP参数优化
docker run -d \
--name nginx \
--sysctl net.core.somaxconn=65535 \
--sysctl net.ipv4.tcp_max_syn_backlog=65535 \
nginx
网络缓冲区优化:
# 设置网络缓冲区大小
docker run -d \
--name nginx \
--sysctl net.core.rmem_max=16777216 \
--sysctl net.core.wmem_max=16777216 \
--sysctl net.ipv4.tcp_rmem='4096 87380 16777216' \
--sysctl net.ipv4.tcp_wmem='4096 65536 16777216' \
nginx
DNS配置优化:
# 设置DNS服务器
docker run -d \
--name nginx \
--dns 8.8.8.8 \
--dns 8.8.4.4 \
nginx
# 设置DNS搜索域
docker run -d \
--name nginx \
--dns-search example.com \
nginx
# 禁用DNS
docker run -d \
--name nginx \
--dns-opt ndots:1 \
nginx
# 使用主机DNS
docker run -d \
--name nginx \
--network host \
nginx
JVM参数优化:
# 设置JVM堆内存
docker run -d \
--name myapp \
-e JAVA_OPTS="-Xms512m -Xmx512m -XX:+UseG1GC" \
myapp:latest
# JVM参数说明
-Xms512m # 初始堆内存
-Xmx512m # 最大堆内存
-XX:+UseG1GC # 使用G1垃圾收集器
-XX:MaxGCPauseMillis=200 # 最大GC停顿时间
-XX:+HeapDumpOnOutOfMemoryError # OOM时生成堆转储
JVM性能监控:
# 查看JVM进程
docker exec myapp jps -l
# 查看JVM统计
docker exec myapp jstat -gc <pid> 1000 5
# 输出
S0C S1C S0U S1U EC EU OC OU MC MU CCSC CCSU YGC YGCT FGC FGCT GCT
10240.0 10240.0 0.0 0.0 61440.0 8192.0 409600.0 204800.0 51200.0 25600.0 6400.0 3200.0 10 0.500 0 0.000 0.500
# 生成堆转储
docker exec myapp jmap -dump:format=b,file=/tmp/heap.hprof <pid>
# 查看线程栈
docker exec myapp jstack <pid> > /tmp/thread.txt
Nginx配置优化:
# nginx.conf
worker_processes auto;
worker_rlimit_nofile 65535;
events {
worker_connections 65535;
use epoll;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip压缩
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_types text/plain text/css text/xml application/json application/javascript application/rss+xml application/atom+xml image/svg+xml;
# 缓存配置
open_file_cache max=65535 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# 连接优化
client_body_buffer_size 128k;
client_max_body_size 50m;
large_client_header_buffers 4 16k;
# FastCGI优化
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
}
Nginx性能测试:
# 使用ab测试
ab -n 10000 -c 100 http://localhost/
# 输出
Requests per second: 5432.10 [#/sec] (mean)
Time per request: 18.410 [ms] (mean)
Time per request: 0.184 [ms] (mean, across all concurrent requests)
Transfer rate: 1234.56 [Kbytes/sec] received
# 使用wrk测试
wrk -t12 -c400 -d30s http://localhost/
# 输出
Running 30s test @ http://localhost/
12 threads and 400 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 25.00ms 10.00ms 100.00ms 70.00%
Req/Sec 1.20k 200.00 2.00k 65.00%
432000 requests in 30.00s, 1.20GB read
Requests/sec: 14400.00
Transfer/sec: 40.00MB
MySQL配置优化:
# 启动MySQL容器
docker run -d \
--name mysql \
-e MYSQL_ROOT_PASSWORD=root123 \
-v mysql_data:/var/lib/mysql \
mysql:5.7 \
--innodb-buffer-pool-size=1G \
--innodb-log-file-size=256M \
--max-connections=1000 \
--query-cache-size=0 \
--query-cache-type=0 \
--innodb-flush-log-at-trx-commit=2 \
--innodb-flush-method=O_DIRECT
MySQL性能监控:
# 查看MySQL状态
docker exec mysql mysql -uroot -proot123 -e "SHOW STATUS LIKE '%Threads_connected%';"
# 查看慢查询
docker exec mysql mysql -uroot -proot123 -e "SHOW VARIABLES LIKE 'slow_query%';"
# 查看InnoDB状态
docker exec mysql mysql -uroot -proot123 -e "SHOW ENGINE INNODB STATUS\G"
Redis配置优化:
# 启动Redis容器
docker run -d \
--name redis \
-v redis_data:/data \
redis:alpine \
redis-server \
--maxmemory 1gb \
--maxmemory-policy allkeys-lru \
--save 900 1 \
--save 300 10 \
--save 60 10000 \
--appendonly yes \
--appendfsync everysec
Redis性能监控:
# 查看Redis信息
docker exec redis redis-cli INFO
# 查看内存使用
docker exec redis redis-cli INFO memory
# 查看统计信息
docker exec redis redis-cli INFO stats
# 性能测试
docker exec redis redis-benchmark -t set,get -n 100000 -c 50
CPU调优:
├─ CPU限制: --cpus, --cpu-quota
├─ CPU亲和性: --cpuset-cpus
├─ CPU权重: --cpu-shares
└─ 进程优先级: nice, renice
内存调优:
├─ 内存限制: --memory, --memory-swap
├─ 内存预留: --memory-reservation
├─ OOM控制: --oom-kill-disable
└─ Swap控制: --memory-swappiness
IO调优:
├─ IO带宽限制: --device-read-bps, --device-write-bps
├─ IOPS限制: --device-read-iops, --device-write-iops
├─ IO权重: --blkio-weight
└─ 存储驱动: overlay2
网络调优:
├─ 网络模式: host, bridge, none
├─ TCP参数: sysctl
├─ DNS优化: --dns
└─ 网络缓冲区: net.core.*
性能调优流程:
1. 性能监控: 收集数据
2. 性能分析: 找出瓶颈
3. 性能优化: 实施优化
4. 性能测试: 验证效果
5. 持续优化: 循环改进
性能调优原则:
✅ 先监控后优化
✅ 找准瓶颈
✅ 逐步优化
✅ 量化效果
✅ 权衡成本收益
CPU优化:
✅ 合理设置CPU限制
✅ 使用CPU亲和性
✅ 避免CPU竞争
✅ 监控CPU使用
内存优化:
✅ 设置合理的内存限制
✅ 避免内存泄漏
✅ 使用内存缓存
✅ 监控内存使用
IO优化:
✅ 使用高性能存储
✅ 合理配置IO限制
✅ 使用数据卷
✅ 监控IO性能
网络优化:
✅ 选择合适的网络模式
✅ 优化TCP参数
✅ 使用DNS缓存
✅ 监控网络性能
监控指标:
✅ CPU使用率、负载
✅ 内存使用率、缓存
� IO延迟、吞吐量
✅ 网络延迟、带宽
✅ 应用响应时间
监控工具:
✅ docker stats: 资源监控
� Prometheus: 指标收集
� Grafana: 可视化
� ELK: 日志分析
下一章: Docker故障排查
将学习:
CPU调优: 创建一个容器,限制CPU使用为1.5核,并绑定到CPU核心0和1。
内存调优: 创建一个容器,限制内存为512MB,Swap为1GB,并设置内存预留为256MB。
IO调优: 创建一个容器,限制磁盘读写带宽为10MB/s。
网络调优: 优化容器的TCP参数,提高网络性能。
应用优化: 对一个Nginx容器进行性能优化,并进行压力测试。
性能分析: 分析一个性能问题的容器,找出瓶颈并优化。
完整性能优化: 对一个完整的Docker应用进行性能优化,要求:
性能对比测试: 对优化前后的容器进行性能对比测试,生成性能报告。