目錄
- 環(huán)境準(zhǔn)備
- 安裝gcc-c++
- 查看版本
- 創(chuàng)建redis網(wǎng)卡
- 創(chuàng)建6個(gè)redis服務(wù)
- 在/var目錄下創(chuàng)建腳本文件create_redis_script.sh
- 編寫(xiě)腳本內(nèi)容
- 查看腳本文件
- 賦予create_redis_script.sh文件可執(zhí)行權(quán)限
- 執(zhí)行腳本,創(chuàng)建redis服務(wù)
- 查看redis節(jié)點(diǎn)
- 查看redis.conf配置文件
- 啟動(dòng)redis服務(wù)
- 啟動(dòng)方式一(不推薦):
- 啟動(dòng)方式二(推薦):
- 創(chuàng)建start_redis_script.sh文件
- 編寫(xiě)腳本內(nèi)容
- 賦予start_redis_script.sh文件可執(zhí)行權(quán)限
- 執(zhí)行腳本,啟動(dòng)redis容器服務(wù)
- 查看redis容器
- 創(chuàng)建集群
- 進(jìn)入redis-1容器
- 創(chuàng)建集群主節(jié)點(diǎn)
- 測(cè)試連接集群
- 連接集群 -c
- 查看集群信息 cluster info
- 查看節(jié)點(diǎn)信息 cluster nodes
- 測(cè)試主從節(jié)點(diǎn)替換
- set值
- get值
- 停止redis-3容器里的redis服務(wù)
- 再次取值
- 查看redis節(jié)點(diǎn)服務(wù)信息
前面已經(jīng)簡(jiǎn)單熟悉過(guò)redis
的下載安裝使用,今天接著部署redis
集群(cluster),簡(jiǎn)單體會(huì)一下redis集群的高可用特性。
環(huán)境準(zhǔn)備
Redis是C語(yǔ)言開(kāi)發(fā),安裝Redis需要先將Redis的源碼進(jìn)行編譯,編譯依賴(lài)gcc
環(huán)境
安裝gcc-c++
yum install gcc-c++
查看版本
[root@--- redis]# gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.8.5/lto-wrapper Target: x86_64-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --disable-libgcj --with-isl=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/isl-install --with-cloog=/builddir/build/BUILD/gcc-4.8.5-20150702/obj-x86_64-redhat-linux/cloog-install --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) [root@--- redis]#
Redis的四種模式
- 單機(jī)模式
- master+slave(主從)
- sentinel(哨兵)
- cluster(集群)
創(chuàng)建redis網(wǎng)卡
[root@--- ~]# docker network create redis --subnet 172.38.0.0/16 0350d84612bef09bc32b39f15ac6b608dae17cbd75d04b282c0bd37c0283bb7c [root@iZm5e23n3ueobwkjtfartxZ ~]# docker network ls NETWORK ID NAME DRIVER SCOPE feafa30d4051 bridge bridge local e8bf4fced9e2 host host local 0096a971fd2c mynet bridge local 6263db0933b9 none null local 0350d84612be redis bridge local 799426d70aa2 test-network bridge local [root@--- ~]#
創(chuàng)建6個(gè)redis服務(wù)
通過(guò)編寫(xiě)腳本創(chuàng)建6個(gè)redis服務(wù)
- cluster-enabled:是否啟動(dòng)集群,選值:yes 、no
- cluster-config-file 配置文件.conf :指定節(jié)點(diǎn)信息,自動(dòng)生成
- cluster-node-timeout 毫秒值: 配置節(jié)點(diǎn)連接超時(shí)時(shí)間
- appendonly:是否開(kāi)啟持久化,選值:yes、no
- daemonize: 守護(hù)進(jìn)程啟動(dòng)(后臺(tái)啟動(dòng))
在/var
目錄下創(chuàng)建腳本文件create_redis_script.sh
touch create_redis_script.sh
編寫(xiě)腳本內(nèi)容
vim create_redis_script.sh
,寫(xiě)入下面的內(nèi)容
for port in $(seq 1 6); \ do \ mkdir -p /var/redisNode/node-${port}/conf touch /var/redisNode/node-${port}/conf/redis.conf cat << EOF >/var/redisNode/node-${port}/conf/redis.conf port 6379 bind 0.0.0.0 cluster-enabled yes cluster-config-file nodes.conf cluster-node-timeout 5000 cluster-announce-ip 172.38.0.1${port} cluster-announce-port 6379 cluster-announce-bus-port 16379 appendonly yes #daemonize yes EOF done
查看腳本文件
cat create_redis_script.sh
賦予create_redis_script.sh
文件可執(zhí)行權(quán)限
[root@--- ~]# chmod o+x create_redis_script.sh
執(zhí)行腳本,創(chuàng)建redis服務(wù)
./create_redis_script.sh
查看redis節(jié)點(diǎn)
[root@--- redisNode]# pwd /var/redisNode [root@--- redisNode]# ls node-1 node-2 node-3 node-4 node-5 node-6 [root@--- redisNode]#
查看redis.conf配置文件
啟動(dòng)redis服務(wù)
- -v 掛載數(shù)據(jù)卷
- redis:5.0.9-alpine3.11 redis鏡像
- 6379 是redis端口: 映射云服務(wù)器端口6371 – 6376
- 16379 是redis集群TCP端口: 映射云服務(wù)器端口16371 – 16376
- –net 使用自定義redis網(wǎng)絡(luò)
- –ip 是本容器綁定的ip: 172.38.0.11 – 16
- –name 容器名字: redis-1 – 6
- redis-server /etc/redis/redis.conf 追加命令,通過(guò)redis.conf啟動(dòng)
啟動(dòng)方式一(不推薦):
一個(gè)一個(gè)啟動(dòng)
啟動(dòng)第一個(gè)redis節(jié)點(diǎn)
docker run -p 6371:6379 -p 16371:16379 --name redis-1 \ -v /var/redisNode/node-1/data:/data \ -v /var/redisNode/node-1/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.11 redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf
如上,類(lèi)似的,遞增ip、port和name,啟動(dòng)其它五個(gè)redis節(jié)點(diǎn),太麻煩了,可以通過(guò)編寫(xiě)腳本來(lái)一次啟動(dòng)6個(gè)服務(wù)
啟動(dòng)方式二(推薦):
編寫(xiě)啟動(dòng)腳本,6個(gè)全部啟動(dòng)
創(chuàng)建start_redis_script.sh
文件
touch start_redis_script.sh
編寫(xiě)腳本內(nèi)容
vim start_redis_script.sh
,寫(xiě)入下面的內(nèi)容
for port in $(seq 1 6); \ do \ echo ${port} docker run -p 637${port}:6379 -p 1637${port}:16379 --name redis-${port} \ -v /var/redisNode/node-${port}/data:/data \ -v /var/redisNode/node-${port}/conf/redis.conf:/etc/redis/redis.conf \ -d --net redis --ip 172.38.0.1${port} redis:5.0.9-alpine3.11 redis-server /etc/redis/redis.conf; \ done
賦予start_redis_script.sh
文件可執(zhí)行權(quán)限
[root@--- ~]# chmod o+x start_redis_script.sh
執(zhí)行腳本,啟動(dòng)redis容器服務(wù)
./start_redis_script.sh
查看redis容器
[root@--- var]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0d3c18b61098 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 2 seconds ago Up 1 second 0.0.0.0:6376->6379/tcp, 0.0.0.0:16376->16379/tcp redis-6 ee896fb093da redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 13 seconds ago Up 12 seconds 0.0.0.0:6375->6379/tcp, 0.0.0.0:16375->16379/tcp redis-5 a83bb7b16dff redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 48 seconds ago Up 47 seconds 0.0.0.0:6374->6379/tcp, 0.0.0.0:16374->16379/tcp redis-4 ffe4c9619bed redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 58 seconds ago Up 57 seconds 0.0.0.0:6373->6379/tcp, 0.0.0.0:16373->16379/tcp redis-3 a316a31e273f redis:5.0.9-alpine3.11 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6372->6379/tcp, 0.0.0.0:16372->16379/tcp redis-2 71c072f57c29 redis:5.0.9-alpine3.11 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 0.0.0.0:6371->6379/tcp, 0.0.0.0:16371->16379/tcp redis-1 [root@--- var]#
創(chuàng)建集群
進(jìn)入任意一個(gè) Redis
實(shí)例
進(jìn)入redis-1容器
docker exec -it redis-1 /bin/sh
創(chuàng)建集群主節(jié)點(diǎn)
- Redis Cluster最低要求是3個(gè)主節(jié)點(diǎn)
- –cluster-replicas 參數(shù)為數(shù)字,1表示每個(gè)主節(jié)點(diǎn)需要1個(gè)從節(jié)點(diǎn)。
# redis-1 容器內(nèi) /data # redis-cli --cluster create 172.38.0.11:6379 172.38.0.12:6379 172.38.0.13:6379 172.38.0.14:6379 172.38.0.15:6379 172.38.0.16:6379 --cluster-replicas 1
測(cè)試連接集群
連接集群 -c
# redis-1 容器內(nèi) /data # redis-cli -c 127.0.0.1:6379>
查看集群信息 cluster info
127.0.0.1:6379> 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:1 cluster_stats_messages_ping_sent:292 cluster_stats_messages_pong_sent:307 cluster_stats_messages_sent:599 cluster_stats_messages_ping_received:302 cluster_stats_messages_pong_received:292 cluster_stats_messages_meet_received:5 cluster_stats_messages_received:599 127.0.0.1:6379>
查看節(jié)點(diǎn)信息 cluster nodes
127.0.0.1:6379> cluster nodes b3f737d0d77b823794087011f1501fd43a2ea316 172.38.0.12:6379@16379 master - 0 1633332452560 2 connected 5461-10922 506ff2bf1020d468e7d7a81ddbfb616a7e9b7d96 172.38.0.13:6379@16379 master - 0 1633332453000 3 connected 10923-16383 7f57f8a01493fa9e46e24ad8a9e46995634f7442 172.38.0.15:6379@16379 slave 70ebdca0264c079bb2c33452e6388d37706da282 0 1633332453000 5 connected 70ebdca0264c079bb2c33452e6388d37706da282 172.38.0.11:6379@16379 myself,master - 0 1633332452000 1 connected 0-5460 f839b6924977e99d3b74324f2ef70b3678e1a257 172.38.0.14:6379@16379 slave 506ff2bf1020d468e7d7a81ddbfb616a7e9b7d96 0 1633332453762 4 connected bbc4dd0639b90e9ddfb5cef278be04a24276cbdb 172.38.0.16:6379@16379 slave b3f737d0d77b823794087011f1501fd43a2ea316 0 1633332453000 6 connected 127.0.0.1:6379>
測(cè)試主從節(jié)點(diǎn)替換
驗(yàn)證高可用
set一個(gè)值,然后停掉對(duì)應(yīng)的服務(wù),再次get,驗(yàn)證是否可以得到值
set值
127.0.0.1:6379> set a b -> Redirected to slot [15495] located at 172.38.0.13:6379 OK 172.38.0.13:6379>
發(fā)現(xiàn)a值設(shè)置在了172.38.0.13容器上,然后我們?cè)囍ネ5魊edis-3容器的服務(wù),然后再次get,看一下結(jié)果
get值
172.38.0.13:6379> get a "b" 172.38.0.13:6379>
停止redis-3
容器里的redis服務(wù)
docker stop redis-3
再次取值
172.38.0.13:6379> get a Could not connect to Redis at 172.38.0.13:6379: Host is unreachable (34.04s) not connected> exit # 重連集群 /data # redis-cli -c 127.0.0.1:6379> get a -> Redirected to slot [15495] located at 172.38.0.14:6379 "b" 172.38.0.14:6379>
發(fā)現(xiàn)a
值被存儲(chǔ)到了172.38.0.14
機(jī)器上,172.38.0.14
是172.38.0.13
的從節(jié)點(diǎn),當(dāng)主節(jié)點(diǎn)172.38.0.13
發(fā)生故障時(shí),自動(dòng)替換到172.38.0.13
,這就是高可用
查看redis節(jié)點(diǎn)服務(wù)信息
發(fā)現(xiàn)172.38.0.13
節(jié)點(diǎn)fail
, 172.38.0.14
變成master
docker搭建redis集群完成