第 14 章:Docker 部署
第 14 章:Docker 部署
14.1 快速启动
# 最简启动
docker run -d --name memcached -p 11211:11211 memcached:1.6-alpine
# 带参数启动
docker run -d --name memcached \
-p 11211:11211 \
memcached:1.6-alpine \
memcached -m 256 -c 2048 -t 4 -o lru_maintainer
# 验证
docker exec memcached sh -c 'echo "version" | nc localhost 11211'
# VERSION 1.6.x
14.2 镜像选择
| 标签 | 大小 | 基础系统 | 推荐 |
|---|
1.6-alpine | ~10MB | Alpine Linux | ✅ 生产推荐 |
1.6 | ~90MB | Debian | 兼容性需要 |
latest | ~90MB | 等同 1.6 | 不推荐(不明确) |
# 检查镜像信息
docker inspect memcached:1.6-alpine | jq '.[0].Size'
docker run --rm memcached:1.6-alpine memcached --version
14.3 生产级 Docker 部署
资源限制
docker run -d \
--name mc-prod \
--restart=unless-stopped \
--memory=2g \
--memory-swap=2g \
--cpus=2 \
--ulimit nofile=65535:65535 \
--ulimit memlock=-1:-1 \
-p 11211:11211 \
-v /etc/localtime:/etc/localtime:ro \
memcached:1.6-alpine \
memcached \
-m 1536 \
-c 10000 \
-t 4 \
-f 1.25 \
-o lru_maintainer,slab_automove,slab_reassign,maxconns_fast,hash_algorithm=murmur3,lru_crawler \
-R 20 \
-k
参数说明
| Docker 参数 | 说明 |
|---|
--memory=2g | 容器内存限制(含 swap) |
--memory-swap=2g | 限制 swap = 内存(禁用 swap) |
--cpus=2 | CPU 限制 |
--ulimit nofile=65535:65535 | 文件描述符限制 |
--ulimit memlock=-1:-1 | 内存锁定(mlockall) |
--restart=unless-stopped | 自动重启 |
-v /etc/localtime:/etc/localtime:ro | 同步时区 |
14.4 Docker Compose 部署
单实例
# docker-compose.yml
version: '3.8'
services:
memcached:
image: memcached:1.6-alpine
container_name: memcached
restart: unless-stopped
ports:
- "11211:11211"
command: >
memcached
-m 512
-c 4096
-t 4
-o lru_maintainer,slab_automove,maxconns_fast
deploy:
resources:
limits:
cpus: '2'
memory: 768M
reservations:
memory: 256M
ulimits:
nofile:
soft: 65535
hard: 65535
healthcheck:
test: ["CMD-SHELL", "echo stats | nc localhost 11211 | grep -q uptime"]
interval: 30s
timeout: 5s
retries: 3
start_period: 10s
# 监控 Exporter
memcached-exporter:
image: prom/memcached-exporter:latest
container_name: mc-exporter
restart: unless-stopped
ports:
- "9150:9150"
command:
- "--memcached.address=memcached:11211"
depends_on:
memcached:
condition: service_healthy
# 启动
docker-compose up -d
# 查看状态
docker-compose ps
# 查看日志
docker-compose logs -f memcached
多实例集群
# docker-compose-cluster.yml
version: '3.8'
services:
mc-1:
image: memcached:1.6-alpine
container_name: mc-1
restart: unless-stopped
ports:
- "11211:11211"
command: >
memcached
-m 256
-c 4096
-t 2
-o lru_maintainer,slab_automove,maxconns_fast
deploy:
resources:
limits:
cpus: '1'
memory: 384M
mc-2:
image: memcached:1.6-alpine
container_name: mc-2
restart: unless-stopped
ports:
- "11212:11211"
command: >
memcached
-m 256
-c 4096
-t 2
-o lru_maintainer,slab_automove,maxconns_fast
deploy:
resources:
limits:
cpus: '1'
memory: 384M
mc-3:
image: memcached:1.6-alpine
container_name: mc-3
restart: unless-stopped
ports:
- "11213:11211"
command: >
memcached
-m 256
-c 4096
-t 2
-o lru_maintainer,slab_automove,maxconns_fast
deploy:
resources:
limits:
cpus: '1'
memory: 384M
# mc-router 代理
mc-router:
image: grumpycoders/mc-router:latest
container_name: mc-router
restart: unless-stopped
ports:
- "11210:11211"
command: >
--servers mc-1:11211,mc-2:11211,mc-3:11211
depends_on:
- mc-1
- mc-2
- mc-3
# Prometheus 监控
prometheus:
image: prom/prometheus:latest
container_name: prometheus
restart: unless-stopped
ports:
- "9090:9090"
volumes:
- ./prometheus.yml:/etc/prometheus/prometheus.yml
depends_on:
- mc-router
# Grafana 仪表盘
grafana:
image: grafana/grafana:latest
container_name: grafana
restart: unless-stopped
ports:
- "3000:3000"
environment:
- GF_SECURITY_ADMIN_PASSWORD=admin
depends_on:
- prometheus
Prometheus 配置
# prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'memcached'
static_configs:
- targets:
- 'mc-1:9150'
- 'mc-2:9150'
- 'mc-3:9150'
# 启动集群
docker-compose -f docker-compose-cluster.yml up -d
# 验证集群
for port in 11211 11212 11213; do
echo "Node :$port"
echo "version" | nc localhost $port
done
14.5 自定义镜像
# Dockerfile
FROM memcached:1.6-alpine
# 安装工具
RUN apk add --no-cache bash curl netcat-openbsd
# 复制配置脚本
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# 健康检查
HEALTHCHECK --interval=30s --timeout=5s --retries=3 \
CMD echo stats | nc localhost 11211 | grep -q uptime
ENTRYPOINT ["/entrypoint.sh"]
#!/bin/bash
# entrypoint.sh
# 默认参数
MEMCACHED_OPTS="${MEMCACHED_OPTS:--m 256 -c 2048 -t 4}"
# 从环境变量生成参数
MAX_MEMORY=${MAX_MEMORY:-256}
MAX_CONNECTIONS=${MAX_CONNECTIONS:-2048}
THREADS=${THREADS:-4}
exec memcached \
-m $MAX_MEMORY \
-c $MAX_CONNECTIONS \
-t $THREADS \
-o lru_maintainer,slab_automove,maxconns_fast \
-f 1.25 \
"$@"
# 构建
docker build -t my-memcached .
# 使用环境变量
docker run -d \
-e MAX_MEMORY=512 \
-e MAX_CONNECTIONS=4096 \
-e THREADS=4 \
-p 11211:11211 \
my-memcached
14.6 Docker Swarm 部署
# docker-stack.yml
version: '3.8'
services:
memcached:
image: memcached:1.6-alpine
deploy:
replicas: 3
restart_policy:
condition: on-failure
delay: 5s
max_attempts: 3
resources:
limits:
cpus: '1'
memory: 512M
reservations:
memory: 256M
update_config:
parallelism: 1
delay: 30s
failure_action: rollback
command: >
memcached
-m 256
-c 4096
-t 2
-o lru_maintainer,slab_automove,maxconns_fast
networks:
- cache-net
healthcheck:
test: ["CMD-SHELL", "echo stats | nc localhost 11211 | grep -q uptime"]
interval: 30s
timeout: 5s
retries: 3
networks:
cache-net:
driver: overlay
# 部署到 Swarm
docker stack deploy -c docker-stack.yml memcache
# 查看服务
docker service ls
docker service ps memcache_memcached
14.7 Kubernetes 部署
# memcached-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: memcached
labels:
app: memcached
spec:
replicas: 3
selector:
matchLabels:
app: memcached
template:
metadata:
labels:
app: memcached
spec:
containers:
- name: memcached
image: memcached:1.6-alpine
args:
- "memcached"
- "-m"
- "256"
- "-c"
- "4096"
- "-t"
- "2"
- "-o"
- "lru_maintainer,slab_automove,maxconns_fast"
ports:
- containerPort: 11211
name: memcache
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "384Mi"
cpu: "1000m"
livenessProbe:
exec:
command:
- sh
- -c
- "echo stats | nc localhost 11211 | grep -q uptime"
initialDelaySeconds: 10
periodSeconds: 30
readinessProbe:
exec:
command:
- sh
- -c
- "echo stats | nc localhost 11211 | grep -q uptime"
initialDelaySeconds: 5
periodSeconds: 10
---
apiVersion: v1
kind: Service
metadata:
name: memcached
spec:
selector:
app: memcached
ports:
- port: 11211
targetPort: 11211
clusterIP: None # Headless Service(供客户端发现所有 Pod)
# 部署
kubectl apply -f memcached-deployment.yaml
# 查看
kubectl get pods -l app=memcached
kubectl get svc memcached
# 测试
kubectl run mc-test --rm -it --image=busybox -- sh
# 在 Pod 中: echo "version" | nc memcached 11211
14.8 Docker 网络模式
| 网络模式 | 适用场景 | 说明 |
|---|
bridge(默认) | 单机开发 | Docker 默认网络 |
host | 高性能 | 共享宿主机网络栈,减少开销 |
overlay | Swarm 跨主机 | 跨主机通信 |
macvlan | 需要独立 IP | 每个容器一个独立 IP |
# 使用 host 网络(性能最佳)
docker run -d --name memcached \
--network host \
memcached:1.6-alpine \
memcached -m 512 -l 127.0.0.1 -t 4
扩展阅读
小结
| 要点 | 内容 |
|---|
| 镜像推荐 | memcached:1.6-alpine(~10MB) |
| 资源限制 | --memory + --cpus + --ulimit |
| 禁用 Swap | --memory-swap 等于 --memory |
| 集群方案 | mc-router 代理 + 多实例 |
| K8s | Deployment + Headless Service |