日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

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

一、Redis 的多種高可用方案

常見的Redis的高可用方案有以下幾種:

  1. Redis Replication(主從復(fù)制):Redis的主從復(fù)制可以實(shí)現(xiàn)數(shù)據(jù)的備份和讀寫分離。通過配置主節(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ù)制適用于對讀操作較多、對可用性要求較高的場景。
  2. Redis Sentinel(哨兵模式):哨兵模式是Redis官方推薦的實(shí)現(xiàn)高可用的方案之一。通過運(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ù)。哨兵模式適用于對高可用性要求不是特別高的場景。
  3. Redis Cluster(集群模式):Redis Cluster是Redis官方提供的高可用和分布式解決方案。通過將多個(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適用于對可用性和擴(kuò)展性要求較高的場景。集群模式只能存儲在db0。
  4. 第三方中間件/解決方案:除了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試和監(jiān)控,確保Redis集群的穩(wěn)定性和高可用性。

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

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

  集群的架構(gòu)一般服務(wù)器為奇數(shù)臺,所以,如果是采用集群模式,那么至少準(zhǔn)備3臺linux服務(wù)器,受生產(chǎn)環(huán)境所限,我們只有兩臺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存儲目錄,在兩臺主從服務(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

4. 準(zhǔn)備Redis哨兵的配置文件,每個(gè)Redis服務(wù)對應(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)沒有應(yīng)答哨兵sentinel 此時(shí) 哨兵主觀上認(rèn)為主節(jié)點(diǎn)下線 默認(rèn)30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在發(fā)生failover主備切換時(shí)最多可以有多少個(gè)slave同時(shí)對新的master進(jìn)行同步,這個(gè)數(shù)字越小,完成failover所需的時(shí)間就越長
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)沒有應(yīng)答哨兵sentinel 此時(shí) 哨兵主觀上認(rèn)為主節(jié)點(diǎn)下線 默認(rèn)30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在發(fā)生failover主備切換時(shí)最多可以有多少個(gè)slave同時(shí)對新的master進(jìn)行同步,這個(gè)數(shù)字越小,完成failover所需的時(shí)間就越長
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

5. 在兩臺服務(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

通過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

6. 通過命令查看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容器,通過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

7. 測試redis哨兵主備切換

  • 手動(dòng)關(guān)閉主Redis服務(wù)
docker stop 5541698b65a1
  • 進(jìn)入到Sentinel容器,通過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

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

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

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

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

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

  • 準(zhǔn)備Redis存儲目錄,在兩臺主從服務(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上傳至對應(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訪問密碼)
requirepass 密碼
 #(設(shè)置集群節(jié)點(diǎ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. 在兩臺服務(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

通過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. 在兩臺服務(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集群端口。

分享到:
標(biāo)簽:Redis
用戶無頭像

網(wǎng)友整理

注冊時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定