initialize repository

This commit is contained in:
Reinaldy Rafli 2022-05-28 12:45:08 +07:00
commit c8198e1d08
No known key found for this signature in database
GPG Key ID: 748A8EC5DB8653E8
4 changed files with 2215 additions and 0 deletions

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2022 Reinaldy Rafli <aldy505@tutanota.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

57
README.md Normal file
View File

@ -0,0 +1,57 @@
# Local Redis Cluster with Docker Compose
Sets up a local Redis v6 cluster with 3 master nodes and no replica.
To add a new replica, just modify the `docker-compose.yml` file and add some more redis instance with different `ipv4_address` subnet. Then, add that new subnet into the `command` field on `redis-cluster-init`, and add the service on the `depends_on` field so the initialization service will only execute if everything's running.
To run it:
```sh
docker compose up -d
```
There is no user. The password is `foobared`. Change the password to anything in `redis.conf` file.
Don't use this configuration in production.
To connect with Go:
```go
package main
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
func main() {
cache := redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{"localhost:6379", "localhost:6380", "localhost:6381"},
ReadOnly: true,
Username: "",
Password: "foobared",
DialTimeout: time.Second * 30,
WriteTimeout: time.Second * 5,
ReadTimeout: time.Second * 5,
IdleTimeout: time.Second * 30,
})
defer cache.Close()
ctx, cancel := context.WithTimeout(context.Background(), time.Second * 60)
defer cancel()
err := cache.SetEX(ctx, "hello", "world").Err()
if err != nil {
log.Printf(err)
return
}
res, err := cache.Get(ctx, "hello").Result()
if err != nil {
log.Printf(err)
return
}
log.Println(res)
}
```

84
docker-compose.yml Normal file
View File

@ -0,0 +1,84 @@
services:
redis-cluster-init:
image: redis:6-alpine
command: redis-cli -h redis1 -a foobared --cluster create 172.20.0.31:6379 172.20.0.32:6379 172.20.0.33:6379 --cluster-replicas 0 --cluster-yes
depends_on:
redis1:
condition: service_healthy
redis2:
condition: service_healthy
redis3:
condition: service_healthy
networks:
app_subnet:
ipv4_address: 172.20.0.30
redis1:
image: redis:6-alpine
ports:
- 6379:6379
restart: unless-stopped
environment:
TZ: UTC
TZDATA: UTC
command: redis-server /home/redis/redis.conf --cluster-announce-ip 172.20.0.31
volumes:
- ./redis.conf:/home/redis/redis.conf
networks:
app_subnet:
ipv4_address: 172.20.0.31
healthcheck:
test: "redis-cli -a foobared ping | grep PONG"
interval: 10s
timeout: 10s
retries: 10
start_period: 10s
redis2:
image: redis:6-alpine
ports:
- 6380:6379
restart: unless-stopped
environment:
TZ: UTC
TZDATA: UTC
command: redis-server /home/redis/redis.conf --cluster-announce-ip 172.20.0.32
volumes:
- ./redis.conf:/home/redis/redis.conf
networks:
app_subnet:
ipv4_address: 172.20.0.32
healthcheck:
test: "redis-cli -a foobared ping | grep PONG"
interval: 10s
timeout: 10s
retries: 10
start_period: 10s
redis3:
image: redis:6-alpine
ports:
- 6381:6379
restart: unless-stopped
environment:
TZ: UTC
TZDATA: UTC
command: redis-server /home/redis/redis.conf --cluster-announce-ip 172.20.0.33
volumes:
- ./redis.conf:/home/redis/redis.conf
networks:
app_subnet:
ipv4_address: 172.20.0.33
healthcheck:
test: "redis-cli -a foobared ping | grep PONG"
interval: 10s
timeout: 10s
retries: 10
start_period: 10s
networks:
app_subnet:
driver: bridge
ipam:
config:
- subnet: 172.20.0.0/24

2053
redis.conf Normal file

File diff suppressed because it is too large Load Diff