目錄
- 1. 集群配置
- 2.編寫redis.conf
- 3. 在每個(gè)redis-*文件夾下創(chuàng)建redis.conf文件,并寫入如下內(nèi)容:
- 4.編寫docker-compose.yml文件
- 5.開啟集群
- 6.測試
- 6.1 查看節(jié)點(diǎn)屬性
- 6.2 查看節(jié)點(diǎn)信息
- 6.3 插入一個(gè)值
- 7.添加密碼:
- 7.1 編輯redis.conf文件
- 7.2 編輯docker-compose.yml文件
- 7.3 連接集群
1. 集群配置
- 3主+3從
- 由于僅用于測試,故我這里只用1臺(tái)服務(wù)器進(jìn)行模擬
redis列表
2.編寫redis.conf
在server上創(chuàng)建一個(gè)目錄用于存放redis集群部署文件。這里我放的路徑為/root/redis-cluster
在/opt/docker/redis-cluster目錄下創(chuàng)建redis-1,redis-2,redis-3,redis-4,redis-5,redis-6文件夾
mkdir -p /opt/docker/redis-cluster/{redis-1,redis-2,redis-3,redis-4,redis-5,redis-6} #創(chuàng)建持久化目錄 mkdir -p /opt/docker/redis-cluster/redis-1/data mkdir -p /opt/docker/redis-cluster/redis-2/data mkdir -p /opt/docker/redis-cluster/redis-3/data mkdir -p /opt/docker/redis-cluster/redis-4/data mkdir -p /opt/docker/redis-cluster/redis-5/data mkdir -p /opt/docker/redis-cluster/redis-6/data
3. 在每個(gè)redis-*文件夾下創(chuàng)建redis.conf文件,并寫入如下內(nèi)容:
cluster-enabled yes # 開啟集群 cluster-config-file nodes.conf # 集群配置文件 cluster-node-timeout 5000 # 集群節(jié)點(diǎn)多少時(shí)間未響應(yīng)視為該節(jié)點(diǎn)丟失 appendonly yes port 6379 # redis監(jiān)聽端口 masterauth passwd123 #設(shè)置master節(jié)點(diǎn)密碼 requirepass passwd123 #設(shè)置密碼
注意:port值不能都為6379,根據(jù)上面redis列表設(shè)置的端口號(hào),依次給redis-1 ~ redis-6設(shè)置6379~6384端口號(hào)
4.編寫docker-compose.yml文件
這里直接使用daocloud提供的redis鏡像地址daocloud.io/library/redis:6.0.4
在/root/redis-cluster文件夾下創(chuàng)建docker-compose.yml文件。
docker-compose.yml文件內(nèi)容如下:
version: '3.1' services: # redis1配置 redis1: image: daocloud.io/library/redis:6.0.4 container_name: redis-1 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-1/data:/data - /opt/docker/redis-cluster/redis-1/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis2配置 redis2: image: daocloud.io/library/redis:6.0.4 container_name: redis-2 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-2/data:/data - /opt/docker/redis-cluster/redis-2/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis3配置 redis3: image: daocloud.io/library/redis:6.0.4 container_name: redis-3 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-3/data:/data - /opt/docker/redis-cluster/redis-3/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis4配置 redis4: image: daocloud.io/library/redis:6.0.4 container_name: redis-4 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-4/data:/data - /opt/docker/redis-cluster/redis-4/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis5配置 redis5: image: daocloud.io/library/redis:6.0.4 container_name: redis-5 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-5/data:/data - /opt/docker/redis-cluster/redis-5/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"] # redis6配置 redis6: image: daocloud.io/library/redis:6.0.4 container_name: redis-6 restart: always network_mode: "host" volumes: - /opt/docker/redis-cluster/redis-6/data:/data - /opt/docker/redis-cluster/redis-6/redis.conf:/usr/local/etc/redis/redis.conf command: ["redis-server", "/usr/local/etc/redis/redis.conf"]
啟動(dòng)容器,執(zhí)行命令:
#啟動(dòng)容器 docker-compose -f xxx.yaml up -d
5.開啟集群
隨便找一個(gè)容器進(jìn)入,這里我選擇redis-1進(jìn)入。 在進(jìn)入容器后,輸入如下命令開啟集群:
redis-cli --cluster create 192.168.66.180:6379 \ 192.168.66.180:6380 \ 192.168.66.180:6381 \ 192.168.66.180:6382 \ 192.168.66.180:6383 \ 192.168.66.180:6384 \ --cluster-replicas 1
出現(xiàn)下圖即為成功。
6.測試
使用redis-cli命令,連接集群中任意節(jié)點(diǎn)。(隨便找一臺(tái)能ping通集群所在IP的電腦,這里我用的本機(jī)進(jìn)行測試)
6.1 查看節(jié)點(diǎn)屬性
redis-cli -c -h 192.168.66.180 -p 6379 192.168.66.180:6379> cluster info
顯示為如下圖,即為集群健康狀態(tài)
6.2 查看節(jié)點(diǎn)信息
cluster nodes
注意看圖中的slave,master,myself等關(guān)鍵字。
6.3 插入一個(gè)值
192.168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380>
注意:這里根據(jù)切片自動(dòng)切換到了該數(shù)據(jù)分片所在的節(jié)點(diǎn)上,所以下面可以看到連接的節(jié)點(diǎn)變?yōu)榱?92.168.1.4:6380
get test
168.66.180:6379> set test 'hello world' -> Redirected to slot [6918] located at 192.168.66.180:6380 OK 192.168.66.180:6380> get test "hello world" 192.168.66.180:6380>
這里因?yàn)榫褪窃谠摂?shù)據(jù)所在分片上進(jìn)行的查詢,所以連接節(jié)點(diǎn)沒有改變。
7.添加密碼:
7.1 編輯redis.conf文件
#為redis.conf文件添加如下配置。這里設(shè)置密碼為123456 masterauth 123456 requirepass 123456
7.2 編輯docker-compose.yml文件
#為docker-compose.yml中每個(gè)容器添加如下配置: environment: - REDISCLI_AUTH=123456
7.3 連接集群
redis-cli -c -h 192.168.66.180 -p 6379 -a 123456