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

使用Docker Compose搭建高可用Redis集群

數(shù)據(jù)庫(kù) Redis
在選擇高可用方案時(shí),需要考慮系統(tǒng)的可用性需求、數(shù)據(jù)一致性要求、網(wǎng)絡(luò)拓?fù)涞纫蛩?。同時(shí),也要注意進(jìn)行適當(dāng)?shù)臏y(cè)試和監(jiān)控,確保Redis集群的穩(wěn)定性和高可用性。

如今業(yè)務(wù)系統(tǒng)對(duì)于緩存Redis的依賴似乎是必不可少的,我們可以在各種各樣的系統(tǒng)中看到Redis的身影??紤]到系統(tǒng)運(yùn)行的穩(wěn)定性,Redis的應(yīng)用和MySQL數(shù)據(jù)庫(kù)一樣需要做到高可用部署。

一、Redis 的多種高可用方案

常見(jiàn)的Redis的高可用方案有以下幾種:

  • Redis Replication(主從復(fù)制):Redis的主從復(fù)制可以實(shí)現(xiàn)數(shù)據(jù)的備份和讀寫分離。通過(guò)配置主節(jié)點(diǎn)和從節(jié)點(diǎn),主節(jié)點(diǎn)將數(shù)據(jù)異步復(fù)制到從節(jié)點(diǎn)上。當(dāng)主節(jié)點(diǎn)發(fā)生故障時(shí),一個(gè)從節(jié)點(diǎn)可以被提升為新的主節(jié)點(diǎn),實(shí)現(xiàn)故障轉(zhuǎn)移。主從復(fù)制適用于對(duì)讀操作較多、對(duì)可用性要求較高的場(chǎng)景。
  • Redis Sentinel(哨兵模式):哨兵模式是Redis官方推薦的實(shí)現(xiàn)高可用的方案之一。通過(guò)運(yùn)行一個(gè)或多個(gè)Sentinel進(jìn)程,監(jiān)控Redis主節(jié)點(diǎn)的狀態(tài)。當(dāng)主節(jié)點(diǎn)故障時(shí),Sentinel會(huì)自動(dòng)進(jìn)行故障轉(zhuǎn)移,將其中一個(gè)從節(jié)點(diǎn)提升為新的主節(jié)點(diǎn)。哨兵還可以監(jiān)控從節(jié)點(diǎn)并進(jìn)行故障恢復(fù)。哨兵模式適用于對(duì)高可用性要求不是特別高的場(chǎng)景。
  • Redis Cluster(集群模式):Redis Cluster是Redis官方提供的高可用和分布式解決方案。通過(guò)將多個(gè)Redis實(shí)例組成一個(gè)集群,Redis Cluster提供了自動(dòng)的數(shù)據(jù)分片和高可用性。數(shù)據(jù)被分配到不同的節(jié)點(diǎn)上,并使用Gossip協(xié)議進(jìn)行節(jié)點(diǎn)之間的通信。當(dāng)有節(jié)點(diǎn)發(fā)生故障時(shí),Redis Cluster可以自動(dòng)將數(shù)據(jù)遷移到其他正常的節(jié)點(diǎn)上。Redis Cluster適用于對(duì)可用性和擴(kuò)展性要求較高的場(chǎng)景。集群模式只能存儲(chǔ)在db0。
  • 第三方中間件/解決方案:除了Redis官方提供的高可用方案,還有一些第三方中間件或解決方案可以用于實(shí)現(xiàn)Redis的高可用,如Codis、Twemproxy等。這些中間件提供了更多的功能和擴(kuò)展性,如代理、負(fù)載均衡、故障恢復(fù)等。

?在選擇高可用方案時(shí),需要考慮系統(tǒng)的可用性需求、數(shù)據(jù)一致性要求、網(wǎng)絡(luò)拓?fù)涞纫蛩亍M瑫r(shí),也要注意進(jìn)行適當(dāng)?shù)臏y(cè)試和監(jiān)控,確保Redis集群的穩(wěn)定性和高可用性。

二、使用Docker Compose安裝Redis并配置哨兵模式(Redis Sentinel)

1、環(huán)境準(zhǔn)備

?集群的架構(gòu)一般服務(wù)器為奇數(shù)臺(tái),所以,如果是采用集群模式,那么至少準(zhǔn)備3臺(tái)Linux服務(wù)器,受生產(chǎn)環(huán)境所限,我們只有兩臺(tái)Linux服務(wù)器,但是我們可以使用Docker搭建多個(gè)Redis服務(wù)(Redis主服務(wù)1、Redis從服務(wù)2、Redis從服務(wù)3):

  • 192.168.0.210 (Redis主服務(wù)1、Redis從服務(wù)2)。
  • 192.168.0.195 (Redis從服務(wù)3)。

2、準(zhǔn)備Redis文件存放目錄

  • 準(zhǔn)備Redis存儲(chǔ)目錄,在兩臺(tái)主從服務(wù)器上分別執(zhí)行一下命令。
    在192.168.0.210服務(wù)器上執(zhí)行Redis主服務(wù)1所需目錄及權(quán)限命令。
mkdir -p /opt/container/redis/master/data /opt/container/redis/master/conf /opt/container/redis/master/logs /opt/container/redis/sentinel/data /opt/container/redis/sentinel/conf /opt/container/redis/sentinel/logs

chmod -R 777 /opt/container/redis/master/data /opt/container/redis/master/conf /opt/container/redis/master/logs /opt/container/redis/sentinel/data /opt/container/redis/sentinel/conf /opt/container/redis/sentinel/logs

在192.168.0.210服務(wù)器上執(zhí)行Redis從服務(wù)2所需目錄及權(quán)限命令。

mkdir -p /opt/container/redis/slave1/data /opt/container/redis/slave1/conf /opt/container/redis/slave1/logs /opt/container/redis/sentinel1/data /opt/container/redis/sentinel1/conf /opt/container/redis/sentinel1/logs

chmod -R 777 /opt/container/redis/slave1/data /opt/container/redis/slave1/conf /opt/container/redis/slave1/logs /opt/container/redis/sentinel1/data /opt/container/redis/sentinel1/conf /opt/container/redis/sentinel1/logs

在192.168.0.195服務(wù)器上執(zhí)行Redis從服務(wù)3所需目錄及權(quán)限命令。

mkdir -p /opt/container/redis/slave2/data /opt/container/redis/slave2/conf /opt/container/redis/slave2/logs /opt/container/redis/sentinel2/data /opt/container/redis/sentinel2/conf /opt/container/redis/sentinel2/logs

chmod -R 777 /opt/container/redis/slave2/data /opt/container/redis/slave2/conf /opt/container/redis/slave2/logs /opt/container/redis/sentinel2/data /opt/container/redis/sentinel2/conf /opt/container/redis/sentinel2/logs

/opt/container/redis/ ** /data 用于存放Redis數(shù)據(jù)文件。
/opt/container/redis/ ** /conf 用于存放Redis配置文件。
/opt/container/redis/ ** /logs 用于存放Redis日志文件。

/opt/container/sentinel/ ** /data 用于存放Redis哨兵數(shù)據(jù)文件。
/opt/container/sentinel/ ** /conf 用于存放Redis哨兵配置文件。
/opt/container/sentinel/ ** /logs 用于存放Redis哨兵日志文件。

3、Redis服務(wù)器docker-compose.yml文件

  • 192.168.0.210服務(wù)器上部署兩個(gè)Redis服務(wù)(Redis主服務(wù)1、Redis從服務(wù)2)的docker-compose-redis.yml文件
version: '3'
services:
    ##redis主配置
    redisMaster:
      image: redis:latest
      restart: always
      container_name: redis-master
      command: redis-server /usr/local/etc/redis/redis.conf
      ##將26381映射到26381上
      ports:
        - "26381:26381"
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/master/data:/data
        - /opt/container/redis/master/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/master/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis從配置
    redisSlave1:
      image: redis:latest
      restart: always
      container_name: redis-slave-1
      command: redis-server /usr/local/etc/redis/redis.conf
      ##將26382映射到26382上
      ports:
        - "26382:26382"
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/slave1/data:/data
        - /opt/container/redis/slave1/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/slave1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel:
      image: redis:latest
      restart: always
      container_name: redis-sentinel
      ports:
        - 36381:36381
      command: redis-sentinel /opt/redis/sentinel/sentinel.conf
      volumes:
        - /opt/container/redis/sentinel/data:/data
        - /opt/container/redis/sentinel/conf/sentinel.conf:/opt/redis/sentinel/sentinel.conf
        - /opt/container/redis/sentinel/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel1:
      image: redis:latest
      restart: always
      container_name: redis-sentinel-1
      ports:
        - 36382:36382
      command: redis-sentinel /opt/redis/sentinel/sentinel1.conf
      volumes:
        - /opt/container/redis/sentinel1/data:/data
        - /opt/container/redis/sentinel1/conf/sentinel1.conf:/opt/redis/sentinel/sentinel1.conf
        - /opt/container/redis/sentinel1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
  • 192.168.0.195服務(wù)器上部署Redis服務(wù)(Redis從服務(wù)3)的docker-compose-redis-slave.yml文件。
version: '3'
services:
    ##redis從2配置
    redisSlave2:
      image: redis:latest
      restart: always
      container_name: redis-slave-2
      command: redis-server /usr/local/etc/redis/redis.conf
      ##將26383映射到26383上
      ports:
        - "26383:26383"
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/slave2/data:/data
        - /opt/container/redis/slave2/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/slave2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel2:
      image: redis:latest
      restart: always
      container_name: redis-sentinel-2
      ports:
        - 36383:36383
      command: redis-sentinel /opt/redis/sentinel/sentinel2.conf
      volumes:
        - /opt/container/redis/sentinel2/data:/data
        - /opt/container/redis/sentinel2/conf/sentinel2.conf:/opt/redis/sentinel/sentinel2.conf
        - /opt/container/redis/sentinel2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"

4、準(zhǔn)備Redis服務(wù)的配置文件

  • 192.168.0.210主Redis配置文件,配置端口26381,Redis密碼,并將配置文件redis.conf上傳至/opt/container/redis/master/conf目錄。
appendonly yes
port 26381
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "設(shè)置密碼"
 
masterauth "設(shè)置密碼" 
replica-read-only no
  • 192.168.0.210從Redis配置文件,配置端口26382,密碼,并將配置文件redis.conf上傳至/opt/container/redis/slave1/conf目錄。
appendonly yes
port 26382
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "設(shè)置密碼"
 
replicaof 192.168.0.210 26381
masterauth "設(shè)置密碼" 
replica-read-only no
  • 192.168.0.195從Redis配置文件,配置端口26383,密碼,并將配置文件redis.conf上傳至/opt/container/redis/slave2/conf目錄。
appendonly yes
port 26383
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "設(shè)置密碼"
 
replicaof 192.168.0.210 26381
masterauth "設(shè)置密碼" 
replica-read-only no

5、準(zhǔn)備Redis哨兵的配置文件,每個(gè)Redis服務(wù)對(duì)應(yīng)一個(gè)哨兵配置

  • 192.168.0.210主Redis哨兵配置文件,配置哨兵sentinel實(shí)例運(yùn)行的端口36381,Redis密碼,并將配置文件sentinel.conf上傳至/opt/container/redis/sentinel/conf目錄。
# 哨兵sentinel實(shí)例運(yùn)行的端口
port 36381
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 192.168.0.210 26381 2
sentinel auth-pass mymaster "Redis密碼"
# 指定多少毫秒之后 主節(jié)點(diǎn)沒(méi)有應(yīng)答哨兵sentinel 此時(shí) 哨兵主觀上認(rèn)為主節(jié)點(diǎn)下線 默認(rèn)30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在發(fā)生failover主備切換時(shí)最多可以有多少個(gè)slave同時(shí)對(duì)新的master進(jìn)行同步,這個(gè)數(shù)字越小,完成failover所需的時(shí)間就越長(zhǎng)
sentinel parallel-syncs mymaster 1
# 故障轉(zhuǎn)移的超時(shí)時(shí)間
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
  • 192.168.0.210從Redis哨兵配置文件,配置哨兵sentinel1實(shí)例運(yùn)行的端口36382,Redis密碼,并將配置文件sentinel1.conf上傳至/opt/container/redis/sentinel1/conf目錄.
# 哨兵sentinel實(shí)例運(yùn)行的端口
port 36382
daemonize no
pidfile /var/run/redis-sentinel1.pid
dir /tmp
sentinel monitor mymaster 192.168.0.210 26381 2
sentinel auth-pass mymaster "Redis密碼"
# 指定多少毫秒之后 主節(jié)點(diǎn)沒(méi)有應(yīng)答哨兵sentinel 此時(shí) 哨兵主觀上認(rèn)為主節(jié)點(diǎn)下線 默認(rèn)30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在發(fā)生failover主備切換時(shí)最多可以有多少個(gè)slave同時(shí)對(duì)新的master進(jìn)行同步,這個(gè)數(shù)字越小,完成failover所需的時(shí)間就越長(zhǎng)
sentinel parallel-syncs mymaster 1
# 故障轉(zhuǎn)移的超時(shí)時(shí)間
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
  • 192.168.0.195從Redis哨兵配置文件,配置哨兵sentinel2實(shí)例運(yùn)行的端口36383,Redis密碼,并將配置文件sentinel2.conf上傳至/opt/container/redis/sentinel2/conf目錄。
appendonly yes
port 26383
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "設(shè)置密碼"
 
replicaof 192.168.0.210 26381
masterauth "設(shè)置密碼" 
replica-read-only no

6、在兩臺(tái)服務(wù)器上分別執(zhí)行docker-compose安裝啟動(dòng)命令

將docker-compose-redis.yml上傳至/opt/software目錄,這個(gè)目錄可以自己選擇,然后到目錄下執(zhí)行安裝啟動(dòng)命令。

docker-compose -f docker-compose-redis.yml up -d
[root@localhost software]# docker-compose -f docker-compose-redis.yml up -d
[+] Running 10/10
 ? sentinel1 Pulled                                                                                                                                                                                                                        15.7s
 ? redisSlave1 Pulled                                                                                                                                                                                                                      15.7s
 ? sentinel Pulled                                                                                                                                                                                                                          3.4s
   ? a2abf6c4d29d Already exists                                                                                                                                                                                                            0.0s
   ? c7a4e4382001 Pull complete                                                                                                                                                                                                             0.4s
   ? 4044b9ba67c9 Pull complete                                                                                                                                                                                                             1.0s
   ? c8388a79482f Pull complete                                                                                                                                                                                                             2.3s
   ? 413c8bb60be2 Pull complete                                                                                                                                                                                                             2.3s
   ? 1abfd3011519 Pull complete                                                                                                                                                                                                             2.4s
 ? redisMaster Pulled                                                                                                                                                                                                                      15.7s
WARN[0015] Found orphan containers ([mysql nginx]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 
[+] Running 4/4
 ? Container redis-sentinel-1  Started                                                                                                                                                                                                      0.5s
 ? Container redis-slave-1     Started                                                                                                                                                                                                      0.6s
 ? Container redis-master      Started                                                                                                                                                                                                      0.6s
 ? Container redis-sentinel    Started                                                                                                                                                                                                      0.5s

通過(guò)docker ps命令可以看到redis和redis哨兵已經(jīng)安裝并啟動(dòng)成功。

[root@localhost software]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                            NAMES
f609322cabaa   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:26381->26381/tcp, :::26381->26381/tcp                          redis-master
18b75828b5b7   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:36381->36381/tcp, :::36381->36381/tcp                          redis-sentinel
f0f9a037c7ae   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:26382->26382/tcp, :::26382->26382/tcp                          redis-slave-1
e51d3b0bc696   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:36382->36382/tcp, :::36382->36382/tcp                          redis-sentinel-1

7、通過(guò)命令查看redis哨兵狀態(tài)

  • 進(jìn)入docker容器
docker exec -it f609322cabaa bash
  • 進(jìn)入Redis目錄
cd /usr/local/bin
  • 運(yùn)行info Replication命令查看主節(jié)點(diǎn)信息,我們可以看到connected_slaves有2個(gè),其中slave0的ip為172.18.0.1,這是因?yàn)槠浍@取的是docker的ip地址。
./redis-cli  -h 127.0.0.1 -p 26381 -a "密碼" info Replication

# Replication
role:master
connected_slaves:2
slave0:ip=172.18.0.1,port=26382,state=online,offset=700123,lag=0
slave1:ip=192.168.0.195,port=26383,state=online,offset=700123,lag=0
master_failover_state:no-failover
master_replid:9ee56f68d25b71158544f6cfafc677822c401ec3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:700123
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:700123
  • 同樣的操作方式進(jìn)入到Sentinel容器,通過(guò)INFO Sentinel命令查看哨兵信息,我們可以看到有兩個(gè)redis從服務(wù),三個(gè)哨兵。
root@fba6d91e10f6:/usr/local/bin# ./redis-cli -h 127.0.0.1 -p 36381 INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.0.210:26381,slaves=2,sentinels=3

8、測(cè)試redis哨兵主備切換

  • 手動(dòng)關(guān)閉主Redis服務(wù)。
docker stop 5541698b65a1
  • 進(jìn)入到Sentinel容器,通過(guò)INFO Sentinel命令查看哨兵信息,可以看到主Redis服務(wù)地址已經(jīng)切換到192.168.0.195:26383。
root@fba6d91e10f6:/usr/local/bin# ./redis-cli -h 127.0.0.1 -p 36381 INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.0.195:26383,slaves=3,sentinels=3
  • 進(jìn)入到192.168.0.195的Redis容器,info Replication命令查看節(jié)點(diǎn)信息,我們可以看到此Redis節(jié)點(diǎn)已變?yōu)橹鞣?wù),有一個(gè)從服務(wù)slave0:ip=192.168.0.210,port=26382。
./redis-cli  -h 127.0.0.1 -p 26383 -a "密碼" info Replication

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.0.210,port=26382,state=online,offset=894509,lag=0
master_failover_state:no-failover
master_replid:e9ebebdff0a5f7b7622c6c5fbfed7b1e44d84ae2
master_replid2:9ee56f68d25b71158544f6cfafc677822c401ec3
master_repl_offset:894509
second_repl_offset:884279
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:846
repl_backlog_histlen:893664

三、配置并測(cè)試Redis集群模式(Redis Cluster)

Redis哨兵模式其實(shí)質(zhì)還是主從復(fù)制,只不過(guò)加了哨兵進(jìn)行自動(dòng)主備切換,Redis集群模式(Redis Cluster)才是真正意義上的集群部署,它可以將數(shù)據(jù)進(jìn)行分布式分片存儲(chǔ),但是其只能存儲(chǔ)到db0。

1、環(huán)境準(zhǔn)備

我們?nèi)匀皇褂蒙厦娴腖inux服務(wù)器環(huán)境進(jìn)行安裝redis集群:在192.168.0.210上安裝redisCluster1、redisCluster2、redisCluster3三臺(tái)Reids服務(wù);在在192.168.0.195上安裝redisCluster4、redisCluster5、redisCluster6三臺(tái)Reids服務(wù)。

2、準(zhǔn)備Redis文件存放目錄

  • 準(zhǔn)備Redis存儲(chǔ)目錄,在兩臺(tái)主從服務(wù)器上分別執(zhí)行一下命令。
    在192.168.0.210服務(wù)器上執(zhí)行Redis集群所需目錄及權(quán)限命令。
mkdir -p /opt/container/redis/cluster1/data /opt/container/redis/cluster1/conf /opt/container/redis/cluster1/logs /opt/container/redis/cluster2/data /opt/container/redis/cluster2/conf /opt/container/redis/cluster2/logs /opt/container/redis/cluster3/data /opt/container/redis/cluster3/conf /opt/container/redis/cluster3/logs

chmod -R 777 /opt/container/redis/cluster1/data /opt/container/redis/cluster1/conf /opt/container/redis/cluster1/logs /opt/container/redis/cluster2/data /opt/container/redis/cluster2/conf /opt/container/redis/cluster2/logs /opt/container/redis/cluster3/data /opt/container/redis/cluster3/conf /opt/container/redis/cluster3/logs

在192.168.0.195服務(wù)器上執(zhí)行Redis集群所需目錄及權(quán)限命令。

mkdir -p /opt/container/redis/cluster4/data /opt/container/redis/cluster4/conf /opt/container/redis/cluster4/logs /opt/container/redis/cluster5/data /opt/container/redis/cluster5/conf /opt/container/redis/cluster5/logs  /opt/container/redis/cluster6/data /opt/container/redis/cluster6/conf /opt/container/redis/cluster6/logs 

chmod -R 777 /opt/container/redis/cluster4/data /opt/container/redis/cluster4/conf /opt/container/redis/cluster4/logs /opt/container/redis/cluster5/data /opt/container/redis/cluster5/conf /opt/container/redis/cluster5/logs /opt/container/redis/cluster6/data /opt/container/redis/cluster6/conf /opt/container/redis/cluster6/logs
  • 192.168.0.210 /192.168.0.195Redis配置文件、端口(12381、12382、12383、12384、12385、12386)、密碼,并將配置文件redis.conf上傳至對(duì)應(yīng)目錄,只需要端口不同。
port 12381
cluster-enabled yes #啟動(dòng)集群模式
cluster-config-file nodes-1.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.0.210
cluster-announce-port 12381
cluster-announce-bus-port 22381
bind 0.0.0.0
protected-mode no
appendonly yes
#如果要設(shè)置密碼需要增加如下配置:
 #(設(shè)置redis訪問(wèn)密碼)
requirepass 密碼
 #(設(shè)置集群節(jié)點(diǎn)間訪問(wèn)密碼,跟上面一致)
masterauth 密碼

3、Redis服務(wù)器docker-compose.yml文件

  • 192.168.0.210服務(wù)器上部署兩個(gè)Redis服務(wù)的docker-compose-redis-cluster.yml文件。
version: '3'
services:
    ##redis節(jié)點(diǎn)1配置
    redisCluster1:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster1
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster1/data:/data
        - /opt/container/redis/cluster1/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis節(jié)點(diǎn)2配置
    redisCluster2:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster2
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster2/data:/data
        - /opt/container/redis/cluster2/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis節(jié)點(diǎn)3配置
    redisCluster3:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster3
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster3/data:/data
        - /opt/container/redis/cluster3/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster3/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
  • 192.168.0.195服務(wù)器上部署三個(gè)Redis服務(wù)的docker-compose-redis-cluster.yml文件。
version: '3'
services:
    ##redis節(jié)點(diǎn)4配置
    redisCluster4:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster4
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster4/data:/data
        - /opt/container/redis/cluster4/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster4/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis節(jié)點(diǎn)5配置
    redisCluster5:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster5
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster5/data:/data
        - /opt/container/redis/cluster5/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster5/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis節(jié)點(diǎn)6配置
    redisCluster6:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster6
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##數(shù)據(jù)目錄,要確保先創(chuàng)建好
        - /opt/container/redis/cluster6/data:/data
        - /opt/container/redis/cluster6/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster6/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"

4、在兩臺(tái)服務(wù)器上分別執(zhí)行docker-compose安裝啟動(dòng)命令


docker-compose-redis-cluster.yml上傳至/opt/software目錄,這個(gè)目錄可以自己選擇,然后到目錄下執(zhí)行安裝啟動(dòng)命令。

docker-compose -f docker-compose-redis-cluster.yml up -d
[+] Running 3/3
 ? Container redis-cluster3  Started                                                                                                                                                                                                        0.5s
 ? Container redis-cluster2  Started                                                                                                                                                                                                        0.4s
 ? Container redis-cluster1  Started                                                                                                                                                                                                        0.5s

通過(guò)docker ps命令可以看到redis和redis哨兵已經(jīng)安裝并啟動(dòng)成功。

[root@localhost software]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS             PORTS                                                                            NAMES
67a48962160d   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52381->52381/tcp, :::52381->52381/tcp                          redis-cluster1
b10f669691b3   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52383->52383/tcp, :::52383->52383/tcp                          redis-cluster3
d3899c9c01f6   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52382->52382/tcp, :::52382->52382/tcp                          redis-cluster2

5、在兩臺(tái)服務(wù)器上分別執(zhí)行docker-compose安裝啟動(dòng)命令

  • 登錄到docker容器中。
docker exec -it 67a48962160d bash
  • 用redis-cli創(chuàng)建整個(gè)redis集群。
cd /usr/local/bin

root@localhost:/usr/local/bin# ./redis-cli --cluster create 192.168.0.210:12381 192.168.0.210:12382 192.168.0.210:12383 192.168.0.195:12384 192.168.0.195:12385 192.168.0.195:12386 --cluster-replicas 1 -a "密碼"
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 192.168.0.195:12386 to 192.168.0.210:12381
Adding replica 192.168.0.210:12383 to 192.168.0.195:12384
Adding replica 192.168.0.195:12385 to 192.168.0.210:12382
M: 061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381
   slots:[0-5460] (5461 slots) master
M: 6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382
   slots:[10923-16383] (5461 slots) master
S: 5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383
   replicates d54562615048044b43e368db71789829d76fa263
M: d54562615048044b43e368db71789829d76fa263 192.168.0.195:12384
   slots:[5461-10922] (5462 slots) master
S: 9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385
   replicates 6cfbf9677ab802483ddc7cbb715fe770c8de884a
S: de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386
   replicates 061d7b1bf2f93df7cbf261e47a7981800d636e63
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 192.168.0.210:12381)
M: 061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383
   slots: (0 slots) slave
   replicates d54562615048044b43e368db71789829d76fa263
S: 9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385
   slots: (0 slots) slave
   replicates 6cfbf9677ab802483ddc7cbb715fe770c8de884a
M: d54562615048044b43e368db71789829d76fa263 192.168.0.195:12384
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386
   slots: (0 slots) slave
   replicates 061d7b1bf2f93df7cbf261e47a7981800d636e63
M: 6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 登錄redis,并查看集群信息。
# 查看集群信息
cluster info
# 查看節(jié)點(diǎn)列表
cluster nodes
[root@localhost software]# docker exec -it 407e3847371a bash
root@localhost:/data# cd /usr/local/bin
root@localhost:/usr/local/bin# redis-cli -c -h 127.0.0.1 -p 12383 -a "密碼"
127.0.0.1:12383> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:4
cluster_stats_messages_ping_sent:173
cluster_stats_messages_pong_sent:163
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:337
cluster_stats_messages_ping_received:163
cluster_stats_messages_pong_received:174
cluster_stats_messages_received:337
127.0.0.1:12383> cluster nodes
061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381@22381 master - 0 1696952366541 1 connected 0-5460
de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386@22386 slave 061d7b1bf2f93df7cbf261e47a7981800d636e63 0 1696952366541 1 connected
5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383@22383 slave d54562615048044b43e368db71789829d76fa263 0 1696952366039 4 connected
9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385@22385 slave 6cfbf9677ab802483ddc7cbb715fe770c8de884a 0 1696952367043 2 connected
6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382@22382 master - 0 1696952365537 2 connected 10923-16383
d54562615048044b43e368db71789829d76fa263 192.168.0.195:12383@22384 myself,master - 0 1696952366000 4 connected 5461-10922

搭建Redis集群模式一定要注意,docker-compose配置一定要將network_mode設(shè)置為"host"模式,且不需要端口映射。如果創(chuàng)建集群時(shí),一直顯示等待連接,那么需要配置防火墻,放開Redis集群端口。

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2023-04-11 08:30:52

2017-11-13 11:07:32

Nginx搭建高可用

2020-10-28 07:10:07

Nginx高可用高并發(fā)

2021-06-17 06:29:16

kube-vip Kubernetes開源項(xiàng)目

2023-04-07 08:28:14

2014-10-09 10:04:23

CentOS集群

2024-03-07 16:03:56

RedisDocker

2022-05-31 08:04:03

Redis高可用集群

2024-02-27 09:48:25

Redis集群數(shù)據(jù)庫(kù)

2019-10-09 16:02:16

NginxKeepalivedLvs

2020-07-24 08:50:17

Redis數(shù)據(jù)庫(kù)

2023-09-27 06:26:07

2023-09-26 01:07:34

2023-10-17 14:29:35

2021-09-09 07:45:25

kube-vip Kuberneteshostname

2015-07-29 13:21:58

DockerRails 集群高可用架構(gòu)

2020-10-28 11:20:18

RabbitMQHAProxy運(yùn)維

2021-03-17 10:05:42

KubernetesRedis數(shù)據(jù)庫(kù)

2017-07-11 13:30:12

RedisDockerLinux

2012-02-15 22:40:23

heartbeat高可用
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)