feat: add more

This commit is contained in:
Sun-ZhenXing
2025-10-06 21:48:39 +08:00
parent f330e00fa0
commit 3c609b5989
120 changed files with 7698 additions and 59 deletions

View File

@@ -0,0 +1,2 @@
# Redis version
REDIS_VERSION="8.2.1-alpine"

120
src/redis-cluster/README.md Normal file
View File

@@ -0,0 +1,120 @@
# Redis Cluster
[English](./README.md) | [中文](./README.zh.md)
This service deploys a Redis Cluster with 6 nodes (3 masters + 3 replicas).
## Services
- `redis-1` to `redis-6`: Redis cluster nodes
- `redis-cluster-init`: Initialization container (one-time setup)
## Environment Variables
| Variable Name | Description | Default Value |
| ------------- | ------------------- | -------------- |
| REDIS_VERSION | Redis image version | `8.2.1-alpine` |
Please modify the `.env` file as needed for your use case.
## Volumes
- `redis_1_data` to `redis_6_data`: Data persistence for each Redis node
## Usage
### Start Redis Cluster
```bash
# Start all Redis nodes
docker compose up -d
# Initialize the cluster (first time only)
docker compose --profile init up redis-cluster-init
# Verify cluster status
docker exec redis-1 redis-cli --cluster check redis-1:6379
```
### Connect to Cluster
```bash
# Connect using redis-cli
docker exec -it redis-1 redis-cli -c
# Test cluster
127.0.0.1:6379> CLUSTER INFO
127.0.0.1:6379> SET mykey "Hello"
127.0.0.1:6379> GET mykey
```
### Access from Application
Use cluster mode connection from your application:
```python
# Python example
from redis.cluster import RedisCluster
startup_nodes = [
{"host": "localhost", "port": "7000"},
{"host": "localhost", "port": "7001"},
{"host": "localhost", "port": "7002"},
]
rc = RedisCluster(startup_nodes=startup_nodes, decode_responses=True)
rc.set("foo", "bar")
print(rc.get("foo"))
```
## Ports
- 7000-7005: Mapped to each Redis node's 6379 port
## Cluster Information
- **Masters**: 3 nodes (redis-1, redis-2, redis-3)
- **Replicas**: 3 nodes (redis-4, redis-5, redis-6)
- **Total slots**: 16384 (distributed across masters)
## Adding New Nodes
To add more nodes to the cluster:
1. Add new service in `docker-compose.yaml`
2. Start the new node
3. Add to cluster:
```bash
docker exec redis-1 redis-cli --cluster add-node new-node-ip:6379 redis-1:6379
docker exec redis-1 redis-cli --cluster reshard redis-1:6379
```
## Removing Nodes
```bash
# Remove a node
docker exec redis-1 redis-cli --cluster del-node redis-1:6379 <node-id>
```
## Notes
- Cluster initialization only needs to be done once
- Each node stores a subset of the data
- Automatic failover is handled by Redis Cluster
- Minimum 3 master nodes required for production
- Data is automatically replicated to replica nodes
## Security
- Add password authentication for production:
```bash
command: redis-server --requirepass yourpassword --cluster-enabled yes ...
```
- Use firewall rules to restrict access
- Consider using TLS for inter-node communication in production
## License
Redis is available under the Redis Source Available License 2.0 (RSALv2). See [Redis GitHub](https://github.com/redis/redis) for more information.

View File

@@ -0,0 +1,142 @@
x-default: &default
restart: unless-stopped
volumes:
- &localtime /etc/localtime:/etc/localtime:ro
- &timezone /etc/timezone:/etc/timezone:ro
logging:
driver: json-file
options:
max-size: 100m
services:
redis-cluster-init:
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-cluster-init
command: >
sh -c "
echo 'Waiting for all Redis instances to start...' &&
sleep 5 &&
redis-cli --cluster create
redis-1:6379 redis-2:6379 redis-3:6379
redis-4:6379 redis-5:6379 redis-6:6379
--cluster-replicas 1 --cluster-yes
"
depends_on:
- redis-1
- redis-2
- redis-3
- redis-4
- redis-5
- redis-6
profiles:
- init
redis-1:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-1
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7000:6379"
volumes:
- *localtime
- *timezone
- redis_1_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
redis-2:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-2
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7001:6379"
volumes:
- *localtime
- *timezone
- redis_2_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
redis-3:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-3
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7002:6379"
volumes:
- *localtime
- *timezone
- redis_3_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
redis-4:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-4
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7003:6379"
volumes:
- *localtime
- *timezone
- redis_4_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
redis-5:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-5
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7004:6379"
volumes:
- *localtime
- *timezone
- redis_5_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
redis-6:
<<: *default
image: redis:${REDIS_VERSION:-8.2.1-alpine}
container_name: redis-6
command: redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes
ports:
- "7005:6379"
volumes:
- *localtime
- *timezone
- redis_6_data:/data
deploy:
resources:
limits:
cpus: '0.5'
memory: 512M
volumes:
redis_1_data:
redis_2_data:
redis_3_data:
redis_4_data:
redis_5_data:
redis_6_data: