Files
upkg/example/redis-cache/deploy.sh
2026-01-28 10:28:13 +08:00

73 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
echo "=== Redis Cache Demo K8s 部署脚本 ==="
# 检查 kubectl 是否可用
if ! command -v kubectl &> /dev/null; then
echo "Error: kubectl not found. Please install kubectl first."
exit 1
fi
echo "1. 创建 Redis StatefulSet (3个副本支持读写分离)"
kubectl apply -f k8s/redis.yaml
echo "2. 等待 Redis Pod 启动..."
kubectl wait --for=condition=ready pod -l app=redis -n redis-cache-demo --timeout=120s
echo "3. 检查 Redis Pod 状态"
kubectl get pods -n redis-cache-demo -l app=redis -o wide
echo "4. 检查 Redis Service"
kubectl get svc -n redis-cache-demo
echo ""
echo "=== Redis 集群信息 ==="
echo "Master Pod: redis-0.redis-headless.redis-cache-demo.svc.cluster.local:6379"
echo "Replica Pods: redis-1.redis-headless.redis-cache-demo.svc.cluster.local:6379, redis-2.redis-headless.redis-cache-demo.svc.cluster.local:6379"
echo "Headless Service: redis-headless.redis-cache-demo.svc.cluster.local:6379"
echo "LoadBalancer Service: redis.redis-cache-demo.svc.cluster.local:6379"
echo ""
echo "=== 部署应用 ==="
echo "请先构建镜像并部署应用:"
echo "1. docker build -t redis-cache-demo:latest ."
echo "2. kubectl apply -f k8s/app.yaml"
echo "3. kubectl wait --for=condition=ready pod -l app=redis-cache-demo -n redis-cache-demo --timeout=120s"
echo "4. kubectl get svc -n redis-cache-demo redis-cache-demo"
echo ""
echo "=== 测试应用 ==="
echo "获取应用服务地址:"
echo "kubectl get svc redis-cache-demo -n redis-cache-demo"
echo ""
echo "API 测试示例:"
echo "# 健康检查"
echo "curl http://<SERVICE_IP>/health"
echo ""
echo "# 设置缓存"
echo "curl -X POST http://<SERVICE_IP>/api/cache/test -H 'Content-Type: application/json' -d '{\"value\":\"hello world\",\"expires_in\":300}'"
echo ""
echo "# 获取缓存"
echo "curl http://<SERVICE_IP>/api/cache/test"
echo ""
echo "# Hash 操作"
echo "curl -X POST http://<SERVICE_IP>/api/hash/user:1/name -H 'Content-Type: application/json' -d '{\"value\":\"张三\"}'"
echo "curl http://<SERVICE_IP>/api/hash/user:1/name"
echo ""
echo "# 计数器"
echo "curl -X POST http://<SERVICE_IP>/api/counter/visits/inc"
echo ""
echo "# 测试重连"
echo "curl -X POST http://<SERVICE_IP>/api/test/reconnect"
echo ""
echo "=== 测试读写分离 ==="
echo "监控 Redis 查看读写分离效果:"
echo "kubectl exec -it redis-0 -n redis-cache-demo -- redis-cli monitor"
echo "kubectl exec -it redis-1 -n redis-cache-demo -- redis-cli monitor"
echo "kubectl exec -it redis-2 -n redis-cache-demo -- redis-cli monitor"
echo ""
echo "执行读写操作,观察请求分布"