1. 程式人生 > 其它 >docker 多機分散式安裝redis叢集

docker 多機分散式安裝redis叢集

分散式安裝redis叢集


序號 主機
1 192.168.1.10
2 192.168.1.11
3 192.168.1.12

共搭建六個節點,每臺主機一主一從

分別在每臺主機上建立主從配置檔案
mkdir -p /data/redis-cluster
# 建立主redis配置
touch /data/redis-cluster/redis01.conf
cat << EOF > /data/redis-cluster/redis01.conf
port 6381 
bind 192.168.1.10 # bind 為宿主機IP
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
masterauth test123456
requirepass test123456
cluster-announce-ip 192.168.1.10 # 同是宿主機IP
cluster-announce-port 6381
cluster-announce-bus-port 16381
EOF

# 建立從redis配置
touch /data/redis-cluster/redis02.conf
cat << EOF > /data/redis-cluster/redis02.conf
port 6382
bind 192.168.1.10
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
masterauth test123456
requirepass test123456
cluster-announce-ip 192.168.1.10 
cluster-announce-port 6381
cluster-announce-bus-port 16381
EOF

在不同主機建立redis容器
# 192.168.1.10 node01 node02
docker run -p 6381:6381 --name redis-node01 \
-v /data/redis-cluster/redis01.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf

docker run -p 6382:6382 --name redis-node02 \
-v /data/redis-cluster/redis02.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf

# 192.168.1.11 node01 node02
docker run -p 6381:6381 --name redis-node01 \
-v /data/redis-cluster/redis01.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf


docker run -p 6382:6382 --name redis-node02 \
-v /data/redis-cluster/redis02.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf

# 192.168.1.12 node01 node02
docker run -p 6381:6381 --name redis-node01 \
-v /data/redis-cluster/redis01.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf

docker run -p 6382:6382 --name redis-node02 \
-v /data/redis-cluster/redis02.conf:/etc/redis/redis.conf \
--net host --restart=always \
-d redis:6.2 redis-server /etc/redis/redis.conf

Could not connect to Redis at 192.168.1.10:6381: Connection refused
--net host 在不同主機搭建redis叢集的話,就需要新增該配置。不然無法找到redis其他主機上的叢集

搭建redis叢集

在任一主機redis容器內使用命令建立叢集

docker exec -it redis-node01 /bin/bash
#  -a test123456 redis設定密碼之後需要指定密碼
redis-cli --cluster create 192.168.1.18:6381 192.168.1.18:6382 192.168.1.83:6381 192.168.1.83:6382 192.168.1.182:6381 192.168.1.182:6382 --cluster-replicas 1 -a test123456
# 選項 填yes 
Can I set the above configuration? (type 'yes' to accept): yes
# 檢視叢集狀態
redis-cli -c -h 192.168.1.10 -p 6381 -a test123456
cluster nodes