自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

如何使用Docker搭建Redis Cluster集群?

數(shù)據(jù)庫 Redis
本文主要講了如何在一臺Linux服務器上使用Docker搭建一個Cluster模式的Redis集群,這種方式搭建的集群主要用于測試用途,不建議在生產(chǎn)環(huán)境使用。

要搭建的集群情況說明

在一臺Linux服務器上使用docker搭建一個cluster模式的redis集群。三個master節(jié)點,三個slave節(jié)點,六個節(jié)點因為在同一臺服務器上,所以每個節(jié)點使用不同的端口,端口范圍是6380到6385。

redis cluster集群具有如下幾個特點:

  • 去中心化,采用多主多從模式。所有節(jié)點彼此互聯(lián)(PING-PONG機制),內(nèi)部使用二進制協(xié)議傳輸。
  • 客戶端不需要連接集群所有節(jié)點,連接集群中任何一個可用節(jié)點即可。
  • 每一個分區(qū)都是由一個主節(jié)點和多個從節(jié)點組成,分片和分片之間平行。
  • 每一個master節(jié)點負責維護一部分槽,以及槽所映射的鍵值數(shù)據(jù);集群中每個節(jié)點都有全量的槽信息,通過槽每個node都知道具體數(shù)據(jù)存儲到哪個node上。

拉取redis鏡像

可以選擇指定版本的redis,本文為了方便演示,使用最新版本

docker pull redis

創(chuàng)建使用固定的network

docker network create rediscluster

創(chuàng)建redis配置文件

因為六個節(jié)點監(jiān)聽端口不同,所以配置文件也有區(qū)別,可以使用如下shell腳本生成對應配置文件(假如命名為createRedisConf.sh):

#!/bin/sh

for port in $(seq 6380 6385);
do
mkdir -p ~/redisCluster/node-${port}/conf
touch ~/redisCluster/node-${port}/conf/redis.conf
cat << EOF > ~/redisCluster/node-${port}/conf/redis.conf
#節(jié)點端口
port ${port}
#添加訪問認證
requirepass luduoxin
#如果主節(jié)點開啟了訪問認證,從節(jié)點訪問主節(jié)點需要認證
masterauth luduoxin
#保護模式,默認值 yes,即開啟。開啟保護模式以后,需配置 bind ip 或者設置訪問密碼;關閉保護模式,外部網(wǎng)絡可以直接訪問
protected-mode no
#bind 0.0.0.0

#是否以守護線程的方式啟動(后臺啟動),默認 no
daemonize no
#是否開啟 AOF 持久化模式,默認 no
appendonly yes
#是否開啟集群模式,默認 no
cluster-enabled yes
#集群節(jié)點信息文件
cluster-config-file nodes.conf
#群節(jié)點連接超時時間
cluster-node-timeout 5000
#集群節(jié)點 IP,我使用的服務的ip為172.16.3.110,替換為自己的服務器的即可
cluster-announce-ip 172.16.3.110
#集群節(jié)點映射端口
cluster-announce-port ${port}
#集群節(jié)點總線端口
cluster-announce-bus-port 1${port}
EOF
done

創(chuàng)建此文件后,先賦予執(zhí)行權(quán)限,然后執(zhí)行生成配置文件

$ chmod 755 ./createRedisConf.sh
$ sh ./createRedisConf.sh

創(chuàng)建出六個Redis節(jié)點

可以使用如下shell腳本快速創(chuàng)建出來(createRedis.sh)。

#!/bin/sh

## 首次創(chuàng)建并運行redis容器
if [ "$1" = "create" ]; then
for port in $(seq 6380 6385);
do
docker run -di --restart always --name redis-${port} --net rediscluster -p ${port}:${port} -p 1${port}:1${port} -v ~/redisCluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf -v ~/redisCluster/node-${port}/data:/data redis redis-server /usr/local/etc/redis/redis.conf
done
fi

##停止redis容器
if [ "$1" = "stop" ]; then
for port in $(seq 6380 6385);
do
docker stop redis-${port}
done
fi

##啟動已有的redis容器
if [ "$1" = "start" ]; then
for port in $(seq 6380 6385);
do
docker start redis-${port}
done
fi

## 刪除redis容器
if [ "$1" = "rm" ]; then
for port in $(seq 6380 6385);
do
docker rm redis-${port}
done
fi

創(chuàng)建此文件后,先賦予執(zhí)行權(quán)限,然后執(zhí)行創(chuàng)建出redis服務

$ chmod 755 ./createRedis.sh
$ sh ./createRedis.sh create

創(chuàng)建完成后,可以使用命令 docker ps -a 查看容器。

創(chuàng)建cluster集群

選擇一個redis容器,例如選擇redis-6380容器,進入容器。

docker exec -it redis-6380 /bin/bash

在容器里面執(zhí)行如下命令。

redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110:6385 --cluster-replicas 1

然后出現(xiàn)如下提示,輸入 yes 繼續(xù)。

Can I set the above configuration ? (type 'yes' to accept)

創(chuàng)建過程如下:

# redis-cli -a luduoxin --cluster create 172.16.3.110:6380 172.16.3.110:6381 172.16.3.110:6382 172.16.3.110:6383 172.16.3.110:6384 172.16.3.110:6385 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 172.16.3.110:6384 to 172.16.3.110:6380
Adding replica 172.16.3.110:6385 to 172.16.3.110:6381
Adding replica 172.16.3.110:6383 to 172.16.3.110:6382
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
replicates 78891932599b7497c8dd921295ba19eb0f549285
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 172.16.3.110:6380)
M: 78891932599b7497c8dd921295ba19eb0f549285 172.16.3.110:6380
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 254d130ac0f88ec36be9cda27e59500e04c283cc 172.16.3.110:6382
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081 172.16.3.110:6381
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a87af69f190a86455864c5ca73fabb60290abd1e 172.16.3.110:6384
slots: (0 slots) slave
replicates 43c735f5e5bd1dfd7e4fa80aed467dc6e10a9081
S: 846fa01cecc7fa75fc558eb8fcfb2788abb2a83d 172.16.3.110:6385
slots: (0 slots) slave
replicates 254d130ac0f88ec36be9cda27e59500e04c283cc
S: e2875613c12f0754e485e5eb56d968dd78493bae 172.16.3.110:6383
slots: (0 slots) slave
replicates 78891932599b7497c8dd921295ba19eb0f549285
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

到這里集群就創(chuàng)建成功了。

檢查集群狀態(tài)。

redis-cli -a luduoxin --cluster check 172.16.3.110:6382

連接集群某個節(jié)點。

$ redis-cli -c -a luduoxin -h 172.16.3.110 -p 6382
//查看集群信息
172.16.3.110:6381> cluster info
//查看集群結(jié)點信息
172.16.3.110:6381> cluster nodes
//查看集群的slot分配區(qū)間及對應的主從節(jié)點映射關系
172.16.3.110:6381> cluster slots

小結(jié)

本文主要講了如何在一臺Linux服務器上使用docker搭建一個cluster模式的redis集群,這種方式搭建的集群主要用于測試用途,不建議在生產(chǎn)環(huán)境使用。

責任編輯:姜華 來源: 今日頭條
相關推薦

2024-03-07 16:03:56

RedisDocker

2021-04-06 06:04:36

Redis 6.X C集群搭建操作系統(tǒng)

2023-11-13 09:03:10

2017-07-11 13:30:12

RedisDockerLinux

2024-04-03 00:00:00

Redis集群代碼

2023-06-10 23:09:40

Redis場景內(nèi)存

2022-09-15 08:31:11

主從復制模式Docker

2016-01-07 09:36:20

Docker容器

2017-02-27 21:55:04

LinuxCentOS 7.0Redis

2021-06-03 18:42:26

Redis集群故障

2021-05-12 09:13:48

MySQL數(shù)據(jù)庫Docker搭建

2021-01-07 10:18:03

Redis數(shù)據(jù)庫環(huán)境搭建

2015-05-27 10:29:41

DockerHadoopHadoop集群

2015-08-03 16:15:53

Docker部署集群

2019-12-13 10:50:49

集群Redis存儲

2015-06-16 16:20:40

2022-01-27 20:15:31

集群存儲元數(shù)據(jù)

2022-01-26 00:06:08

Redis分布式客戶端

2025-01-06 13:00:00

RedisSentinel高可用模式

2019-09-16 16:05:13

Redis集群模式
點贊
收藏

51CTO技術棧公眾號